Test C64 format + reorganize Save_C64_multi()

This commit is contained in:
Thomas Bernard 2019-12-31 02:00:22 +01:00
parent 796db2003d
commit a5f2989577
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
4 changed files with 248 additions and 148 deletions

View File

@ -1333,49 +1333,38 @@ int Save_C64_fli_monolayer(T_IO_Context *context, byte saveWhat, word loadAddr)
} }
/** /**
* Save a C64 multicolor picture * Encode a picture in the C64 Multicolor bitmap format.
* *
* @param context the IO context * 8000 bytes bitmap, 1000 bytes screen RAM and 1000 bytes color RAM :
* @param saveWhat what part of the data to save *
* @param loadAddr The load address * BITS COLOR INFORMATION COMES FROM
* 00 Background color #0 (screen color)
* 01 Upper 4 bits of Screen RAM
* 10 Lower 4 bits of Screen RAM
* 11 Color RAM nybble (nybble = 1/2 byte = 4 bits)
*/ */
int Save_C64_multi(T_IO_Context *context, byte saveWhat, word loadAddr) static int Encode_C64_multicolor(T_IO_Context * context, byte * bitmap, byte * screen_ram, byte * color_ram, byte * background)
{ {
/* int cx, cy, x, y;
BITS COLOR INFORMATION COMES FROM int c[4] = {0,0,0,0};
00 Background color #0 (screen color) int color, lut[16], bits, pixel, pos=0;
01 Upper 4 bits of Screen RAM
10 Lower 4 bits of Screen RAM
11 Color RAM nybble (nybble = 1/2 byte = 4 bits)
*/
int cx,cy,x,y,c[4]={0,0,0,0},color,lut[16],bits,pixel,pos=0;
int cand,n,used; int cand,n,used;
word cols, candidates = 0, invalids = 0; word cols, candidates = 0, invalids = 0;
// FIXME allocating this on the stack is not a good idea. On some platforms
// the stack has a rather small size...
byte bitmap[8000],screen_ram[1000],color_ram[1000];
word numcolors;
dword cusage[256];
byte i,background=0;
FILE *file;
// Detect the background color the image should be using. It's the one that's // Detect the background color the image should be using. It's the one that's
// used on all tiles having 4 colors. // used on all tiles having 4 colors.
for(y=0;y<200;y=y+8) for(y = 0; y < 200; y += 8)
{ {
for (x = 0; x<160; x=x+4) for (x = 0; x < 160; x += 4)
{ {
cols = 0; cols = 0;
// Compute the usage count of each color in the tile // Compute the usage count of each color in the tile
for (cy=0;cy<8;cy++) for (cy = 0; cy < 8; cy++)
for (cx=0;cx<4;cx++) for (cx = 0; cx < 4; cx++)
{ {
pixel=Get_pixel(context, x+cx,y+cy); pixel = Get_pixel(context, x+cx,y+cy);
if(pixel>15) if(pixel > 15)
{ {
Warning_message("Color above 15 used"); Warning_message("Color above 15 used");
// TODO hilite as in hires, you should stay to // TODO hilite as in hires, you should stay to
@ -1394,7 +1383,7 @@ int Save_C64_multi(T_IO_Context *context, byte saveWhat, word loadAddr)
used++; used++;
} }
if (used>3) if (used > 3)
{ {
GFX2_Log(GFX2_DEBUG, "(%3d,%3d) used=%d cols=%04x\n", x, y, used,(unsigned)cols); GFX2_Log(GFX2_DEBUG, "(%3d,%3d) used=%d cols=%04x\n", x, y, used,(unsigned)cols);
// This is a tile that uses the background color (and 3 others) // This is a tile that uses the background color (and 3 others)
@ -1437,12 +1426,12 @@ int Save_C64_multi(T_IO_Context *context, byte saveWhat, word loadAddr)
for (n = 0; n<16; n++) for (n = 0; n<16; n++)
{ {
if (candidates & (1 << n)) { if (candidates & (1 << n)) {
background = n; *background = n;
break; break;
} }
} }
GFX2_Log(GFX2_DEBUG, "Save_C64_multi() background=%d ($%x) candidates=%x invalid=%x\n", GFX2_Log(GFX2_DEBUG, "Save_C64_multi() background=%d ($%x) candidates=%x invalid=%x\n",
(int)background, (int)background, (unsigned)candidates, (unsigned)invalids); (int)*background, (int)*background, (unsigned)candidates, (unsigned)invalids);
// Now that we know which color is the background, we can encode the cells // Now that we know which color is the background, we can encode the cells
@ -1450,53 +1439,72 @@ int Save_C64_multi(T_IO_Context *context, byte saveWhat, word loadAddr)
{ {
for(cx=0; cx<40; cx++) for(cx=0; cx<40; cx++)
{ {
numcolors=Count_used_colors_area(cusage,cx*4,cy*8,4,8); memset(lut, 0xff, sizeof(lut));
if(numcolors>4) c[0] = *background;
{ lut[*background] = 0;
Warning_with_format("More than 4 colors\nin 4x8 pixel cell: (%d, %d)\nRect: (%d, %d, %d, %d)", cx, cy, cx * 4, cy * 8, cx * 4 + 3, cy * 8 + 7); color = 1;
// TODO hilite offending block
return 1;
}
color=1;
c[0]=background;
for(i=0; i<16; i++)
{
lut[i]=0;
if(cusage[i] && (i!=background))
{
lut[i]=color;
c[color]=i;
color++;
}
}
// add to screen_ram and color_ram
screen_ram[cx+cy*40]=c[1]<<4|c[2];
color_ram[cx+cy*40]=c[3];
for(y=0;y<8;y++) for(y=0;y<8;y++)
{ {
bits=0; bits=0;
for(x=0;x<4;x++) for(x=0;x<4;x++)
{ {
pixel = Get_pixel(context, cx*4+x,cy*8+y); pixel = Get_pixel(context, cx*4+x, cy*8+y);
if (lut[pixel] < 0)
{
if (color < 4)
{
c[color] = pixel;
lut[pixel] = color;
color++;
}
else
{
Warning_with_format("More than 4 colors\nin 4x8 pixel cell: (%d, %d)\nRect: (%d, %d, %d, %d)", cx, cy, cx * 4, cy * 8, cx * 4 + 3, cy * 8 + 7);
// TODO hilite offending block
return 1;
}
}
bits = (bits << 2) | lut[pixel]; bits = (bits << 2) | lut[pixel];
} }
bitmap[pos++]=bits; bitmap[pos++]=bits;
} }
// add to screen_ram and color_ram
screen_ram[cx+cy*40] = (c[1] << 4) | c[2];
color_ram[cx+cy*40] = c[3];
} }
} }
return 0;
}
/**
* Save a C64 multicolor picture
*
* @param context the IO context
* @param saveWhat what part of the data to save
* @param loadAddr The load address
*/
int Save_C64_multi(T_IO_Context *context, byte saveWhat, word loadAddr)
{
FILE *file;
byte background = 0;
// FIXME allocating this on the stack is not a good idea. On some platforms
// the stack has a rather small size...
byte bitmap[8000], screen_ram[1000], color_ram[1000];
File_error = Encode_C64_multicolor(context, bitmap, screen_ram, color_ram, &background);
if (File_error != 0)
return File_error;
file = Open_file_write(context); file = Open_file_write(context);
if (file == NULL)
if(!file)
{ {
Warning_message("File open failed"); Warning_message("File open failed");
File_error = 2; File_error = 2;
return 2; return 2;
} }
setvbuf(file, NULL, _IOFBF, 64*1024);
if (loadAddr) if (loadAddr)
Write_word_le(file,loadAddr); Write_word_le(file,loadAddr);

View File

@ -37,6 +37,7 @@ void Warning_with_format(const char *template, ...)
va_start(arg_ptr, template); va_start(arg_ptr, template);
vprintf(template, arg_ptr); vprintf(template, arg_ptr);
va_end(arg_ptr); va_end(arg_ptr);
putchar('\n');
} }
void Error_function(int error_code, const char *filename, int line_number, const char *function_name) void Error_function(int error_code, const char *filename, int line_number, const char *function_name)
@ -93,7 +94,8 @@ T_Normal_button * Window_set_normal_button(word x_pos, word y_pos,
short Window_clicked_button(void) short Window_clicked_button(void)
{ {
return 0; // return 1, which is often the OK button
return 1;
} }
int Is_shortcut(word key, word function) int Is_shortcut(word key, word function)

View File

@ -397,3 +397,92 @@ ret:
free(context.File_directory); free(context.File_directory);
return ok; return ok;
} }
int Test_C64_Formats(void)
{
int ok = 0;
T_IO_Context context;
char path[256];
T_GFX2_Surface * testpicmulti = NULL;
memset(&context, 0, sizeof(context));
context.Type = CONTEXT_SURFACE;
context.Nb_layers = 1;
// Load EVILNUN.PKM
context_set_file_path(&context, "../tests/pic-samples/c64/multicolor/STILLIFE.rpm");
File_error = 0;
Load_C64(&context);
if (File_error != 0)
{
fprintf(stderr, "Failed to load reference multicolors picture\n");
goto ret;
}
testpicmulti = context.Surface;
context.Surface = NULL;
snprintf(path, sizeof(path), "/tmp/%s", "test.c64");
context_set_file_path(&context, path);
// save the reference picture
context.Surface = testpicmulti;
context.Target_address = context.Surface->pixels;
context.Pitch = context.Surface->w;
context.Width = context.Surface->w;
context.Height = context.Surface->h;
context.Ratio = PIXEL_WIDE;
memcpy(context.Palette, context.Surface->palette, sizeof(T_Palette));
context.Format = FORMAT_C64;
File_error = 0;
Save_C64(&context);
context.Surface = NULL;
if (File_error != 0)
{
GFX2_Log(GFX2_ERROR, "Save_C64 failed.\n");
ok = 0;
}
else
{
FILE * f;
// Test the saved file
f = fopen("/tmp/test.c64", "rb");
if (f == NULL)
{
GFX2_Log(GFX2_ERROR, "error opening %s\n", path);
ok = 0;
}
else
{
File_error = 1;
Test_C64(&context, f);
fclose(f);
if (File_error != 0)
{
GFX2_Log(GFX2_ERROR, "Test_C64 failed for file %s\n", path);
ok = 0;
}
}
memset(context.Palette, -1, sizeof(T_Palette));
// load the saved file
Load_C64(&context);
if (File_error != 0 || context.Surface == NULL)
{
GFX2_Log(GFX2_ERROR, "Load_C64 failed for file %s\n", path);
ok = 0;
}
else
{
ok = 1;
if (memcmp(testpicmulti->pixels, context.Surface->pixels, 160*200) != 0)
{
GFX2_Log(GFX2_ERROR, "Save_C64/Load_C64: Pixels mismatch\n");
ok = 0;
}
}
}
ret:
if (testpicmulti)
Free_GFX2_Surface(testpicmulti);
free(context.File_name);
free(context.File_directory);
return ok;
}

View File

@ -8,3 +8,4 @@ TEST(Convert_24b_bitmap_to_256)
TEST(Formats) TEST(Formats)
TEST(Load) TEST(Load)
TEST(Save) TEST(Save)
TEST(C64_Formats)