From 19dec01d4fdc28f294e8d028ba2bfee6466d8ac6 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 2 Jan 2020 01:55:50 +0100 Subject: [PATCH] Save_PRG() supports C64 hires bitmap mode --- src/c64formats.c | 64 +++++++++++++++------ src/tests/testformats.c | 121 ++++++++++++++++++++++++---------------- 2 files changed, 121 insertions(+), 64 deletions(-) diff --git a/src/c64formats.c b/src/c64formats.c index 3461c744..027b63ca 100644 --- a/src/c64formats.c +++ b/src/c64formats.c @@ -1161,15 +1161,17 @@ static int Save_C64_window(enum c64_format *saveFormat, byte *saveWhat, word *lo return button==1; } -/// Save a C64 hires picture -/// -/// c64 hires is 320x200 with only 2 colors per 8x8 block. -static int Save_C64_hires(T_IO_Context *context, byte saveWhat, word loadAddr) + +/** + * Encode a C64 HiRes Bitmap picture. + * 320x200 pixels, with only 2 different colors per 8x8 block. + * + * 8000 bytes bitmap, 1000 bytes screen RAM + */ +static int Encode_C64_hires(T_IO_Context * context, byte * bitmap, byte * screen_ram) { int i, pos = 0; word cx, cy, x, y; - byte screen_ram[1000],bitmap[8000]; - FILE *file; for(cy=0; cy<25; cy++) // Character line, 25 lines { @@ -1246,7 +1248,21 @@ static int Save_C64_hires(T_IO_Context *context, byte saveWhat, word loadAddr) } } } + return 0; +} +/// Save a C64 hires picture +/// +/// c64 hires is 320x200 with only 2 colors per 8x8 block. +static int Save_C64_hires(T_IO_Context *context, byte saveWhat, word loadAddr) +{ + byte screen_ram[1000],bitmap[8000]; + FILE *file; + int ret; + + ret = Encode_C64_hires(context, bitmap, screen_ram); + if (ret != 0) + return ret; file = Open_file_write(context); if(!file) @@ -1722,7 +1738,17 @@ void Save_PRG(T_IO_Context * context) saveFormat = (context->Width == 320) ? F_hires : F_multi; File_error = 1; Set_saving_layer(context, 0); - File_error = Encode_C64_multicolor(context, bitmap, screen_ram, color_ram, &background); + switch (saveFormat) + { + case F_hires: + File_error = Encode_C64_hires(context, bitmap, screen_ram); + break; + case F_multi: + File_error = Encode_C64_multicolor(context, bitmap, screen_ram, color_ram, &background); + break; + default: + GFX2_Log(GFX2_ERROR, "Save_PRG(): format %d not handled (yet?)\n", saveFormat); + } if (File_error == 0) { FILE *file; @@ -1736,7 +1762,7 @@ void Save_PRG(T_IO_Context * context) } if (!Write_bytes(file, picview_prg, sizeof(picview_prg))) File_error = 2; - Write_byte(file, 0x30); // Mode : Bitmap + Multicolor + Write_byte(file, saveFormat == F_multi ? 0x30 : 0x20); // Mode : 0x40 Extended bg, 0x20 Bitmap, 0x10 Multicolor n = PackBits_pack_buffer(NULL, bitmap, 8000); GFX2_Log(GFX2_DEBUG, "PackBits of bitmap : 8000 => %d bytes\n", n + 1); if (n >= 0 && n < 7999) @@ -1764,15 +1790,19 @@ void Save_PRG(T_IO_Context * context) Write_byte(file, 0x20); // screen RAM / no packing Write_bytes(file, screen_ram, 1000); } - //Write_byte(file, 0x30); // color RAM / no packing - //Write_bytes(file, color_ram, 1000); - Write_byte(file, 0x32); // color RAM / special color RAM packing - n = C64_color_ram_pack(file, color_ram, 1000); - if (n < 0) - File_error = 1; - GFX2_Log(GFX2_DEBUG, "custom packing of color RAM : 1000 => %d bytes\n", n); - Write_byte(file, 0x42); // border/background/etc. / color ram RLE packing - Write_byte(file, background | 0x10); + if (saveFormat == F_multi) + { + //Write_byte(file, 0x30); // color RAM / no packing + //Write_bytes(file, color_ram, 1000); + Write_byte(file, 0x32); // color RAM / special color RAM packing + n = C64_color_ram_pack(file, color_ram, 1000); + if (n < 0) + File_error = 1; + GFX2_Log(GFX2_DEBUG, "custom packing of color RAM : 1000 => %d bytes\n", n); + + Write_byte(file, 0x42); // border/background/etc. / color ram RLE packing + Write_byte(file, background | 0x10); + } Write_byte(file, 0); // end of file fclose(file); } diff --git a/src/tests/testformats.c b/src/tests/testformats.c index 52f0a278..4d7b8cfb 100644 --- a/src/tests/testformats.c +++ b/src/tests/testformats.c @@ -404,11 +404,12 @@ ret: int Test_C64_Formats(void) { - int i; + int i, j; int ok = 0; T_IO_Context context; char path[256]; T_GFX2_Surface * testpicmulti = NULL; + T_GFX2_Surface * testpichires = NULL; memset(&context, 0, sizeof(context)); context.Type = CONTEXT_SURFACE; @@ -424,6 +425,16 @@ int Test_C64_Formats(void) } testpicmulti = context.Surface; context.Surface = NULL; + // Load a hires picture + context_set_file_path(&context, "../tests/pic-samples/c64/hires/midear.dd"); + Load_C64(&context); + if (File_error != 0) + { + fprintf(stderr, "Failed to load reference hires picture\n"); + goto ret; + } + testpichires = context.Surface; + context.Surface = NULL; ok = 1; for (i = 0; ok && formats[i].name != NULL; i++) @@ -433,70 +444,86 @@ int Test_C64_Formats(void) if (!(formats[i].flags & FLAG_C64)) continue; GFX2_Log(GFX2_DEBUG, "Testing format %s (Save)\n", formats[i].name); - snprintf(path, sizeof(path), "/tmp/%s.%s", "test", formats[i].name); - context_set_file_path(&context, path); + for (j = 0; j < 2; j++) + { + T_GFX2_Surface * ref; - // 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 = formats[i].format; - File_error = 0; - formats[i].Save(&context); - context.Surface = NULL; - if (File_error != 0) - { - GFX2_Log(GFX2_ERROR, "Save_%s failed.\n", formats[i].name); - ok = 0; - } - else - { - FILE * f; - // Test the saved file - f = fopen(path, "rb"); - if (f == NULL) + if (j == 0) { - GFX2_Log(GFX2_ERROR, "error opening %s\n", path); + ref = testpicmulti; + context.Ratio = PIXEL_WIDE; + } + else + { + ref = testpichires; + context.Ratio = PIXEL_SIMPLE; + } + snprintf(path, sizeof(path), "/tmp/%s-%d.%s", "test", j, formats[i].name); + context_set_file_path(&context, path); + + // save the reference picture + context.Surface = ref; + context.Target_address = context.Surface->pixels; + context.Pitch = context.Surface->w; + context.Width = context.Surface->w; + context.Height = context.Surface->h; + memcpy(context.Palette, context.Surface->palette, sizeof(T_Palette)); + context.Format = formats[i].format; + File_error = 0; + formats[i].Save(&context); + context.Surface = NULL; + if (File_error != 0) + { + GFX2_Log(GFX2_ERROR, "Save_%s failed.\n", formats[i].name); ok = 0; } else { - File_error = 1; - formats[i].Test(&context, f); - fclose(f); - if (File_error != 0) + FILE * f; + // Test the saved file + f = fopen(path, "rb"); + if (f == NULL) { - GFX2_Log(GFX2_ERROR, "Test_%s failed for file %s\n", formats[i].name, path); + GFX2_Log(GFX2_ERROR, "error opening %s\n", path); ok = 0; } - } - memset(context.Palette, -1, sizeof(T_Palette)); - // load the saved file - formats[i].Load(&context); - if (File_error != 0 || context.Surface == NULL) - { - GFX2_Log(GFX2_ERROR, "Load_%s failed for file %s\n", formats[i].name, path); - ok = 0; - } - else - { - if (memcmp(testpicmulti->pixels, context.Surface->pixels, 160*200) != 0) + else { - GFX2_Log(GFX2_ERROR, "Save_%s/Load_%s: Pixels mismatch\n", formats[i].name, formats[i].name); + File_error = 1; + formats[i].Test(&context, f); + fclose(f); + if (File_error != 0) + { + GFX2_Log(GFX2_ERROR, "Test_%s failed for file %s\n", formats[i].name, path); + ok = 0; + } + } + memset(context.Palette, -1, sizeof(T_Palette)); + // load the saved file + formats[i].Load(&context); + if (File_error != 0 || context.Surface == NULL) + { + GFX2_Log(GFX2_ERROR, "Load_%s failed for file %s\n", formats[i].name, path); ok = 0; } - Free_GFX2_Surface(context.Surface); - context.Surface = NULL; + else + { + if (memcmp(ref->pixels, context.Surface->pixels, ref->w * ref->h) != 0) + { + GFX2_Log(GFX2_ERROR, "Save_%s/Load_%s: Pixels mismatch\n", formats[i].name, formats[i].name); + ok = 0; + } + Free_GFX2_Surface(context.Surface); + context.Surface = NULL; + } } } } ret: if (testpicmulti) Free_GFX2_Surface(testpicmulti); + if (testpichires) + Free_GFX2_Surface(testpichires); free(context.File_name); free(context.File_directory); return ok;