diff --git a/src/tests/mockloadsave.c b/src/tests/mockloadsave.c index 35f08a68..b4caf6e6 100644 --- a/src/tests/mockloadsave.c +++ b/src/tests/mockloadsave.c @@ -22,7 +22,10 @@ */ #include +#include #include "../loadsave.h" +#include "../global.h" +#include "../gfx2log.h" void Pre_load(T_IO_Context *context, short width, short height, long file_size, int format, enum PIXEL_RATIO ratio, byte bpp) { @@ -30,13 +33,24 @@ void Pre_load(T_IO_Context *context, short width, short height, long file_size, context, width, height, file_size, format, ratio, bpp); context->Width = width; context->Height = height; + if (bpp > 8) { + fprintf(stderr, "Truecolor not supported yet\n"); + File_error = 1; + } + if (context->Type == CONTEXT_SURFACE) + { + if (context->Surface) + Free_GFX2_Surface(context->Surface); + context->Surface = New_GFX2_Surface(width, height); + if (context->Surface == NULL) + File_error = 1; + } } byte Get_pixel(T_IO_Context *context, short x, short y) { - (void)context; - (void)x; - (void)y; + if (context->Type == CONTEXT_SURFACE) + return Get_GFX2_Surface_pixel(context->Surface, x, y); return 0; } @@ -50,10 +64,20 @@ void Pixel_in_layer(int layer, word x, word y, byte color) void Set_pixel(T_IO_Context *context, short x, short y, byte c) { - (void)context; - (void)x; - (void)y; - (void)c; + if (context->Type == CONTEXT_SURFACE) + { + if (context->Surface == NULL) + { + GFX2_Log(GFX2_ERROR, "Set_pixel() : no Surface allocated\n"); + File_error = 1; + } + if ((x < 0) || (x >= context->Surface->w) || (y < 0) || (y >= context->Surface->h)) + { + GFX2_Log(GFX2_WARNING, "Set_pixel() : position %(%hd,%hd) is outside of the image\n", x, y); + return; + } + Set_GFX2_Surface_pixel(context->Surface, x, y, c); + } } void Set_pixel_24b(T_IO_Context *context, short x, short y, byte r, byte g, byte b) diff --git a/src/tests/testformats.c b/src/tests/testformats.c index 221693cb..c8cd71d6 100644 --- a/src/tests/testformats.c +++ b/src/tests/testformats.c @@ -29,49 +29,54 @@ #include #include #include +#include #include "../global.h" #include "../fileformats.h" #include "../gfx2log.h" #include "../gfx2mem.h" -#define TESTFMT(fmt, sample) { # fmt, Test_ ## fmt, Load_ ## fmt, sample }, +// Load/Save +#define TESTFMT(fmt, sample) { # fmt, Test_ ## fmt, Load_ ## fmt, Save_ ## fmt, sample }, +// Load only +#define TESTFMTL(fmt, sample) { # fmt, Test_ ## fmt, Load_ ## fmt, NULL, sample }, static const struct { const char * name; Func_IO_Test Test; Func_IO Load; + Func_IO Save; const char * sample; } formats[] = { TESTFMT(PKM, "pkm/EVILNUN.PKM") TESTFMT(GIF, "gif/2b_horse.gif") - TESTFMT(PCX, "pcx/lena.pcx") - TESTFMT(NEO, "atari_st/ATARIART.NEO") - TESTFMT(PC1, "atari_st/eunmiisa.pc1") - TESTFMT(PI1, "atari_st/evolutn.pi1") - TESTFMT(FLI, "autodesk_FLI_FLC/2noppaa.fli") - TESTFMT(BMP, "bmp/test16bf565.bmp") - TESTFMT(ICO, "ico/gitlab_favicon.ico") - TESTFMT(C64, "c64/multicolor/ARKANOID.KOA") - TESTFMT(PRG, "c64/multicolor/speedball2_loading_jonegg.prg") - TESTFMT(GPX, "c64/pixcen/Cyberbird.gpx") - TESTFMT(SCR, "cpc/scr/DANCEOFF.SCR") - TESTFMT(CM5, "cpc/mode5/spidey.cm5") - TESTFMT(PPH, "cpc/pph/BF.PPH") - TESTFMT(GOS, "cpc/iMPdraw_GFX/SONIC.GO1") - TESTFMT(MOTO,"thomson/exocet-alientis.map") - TESTFMT(HGR, "apple2/hgr/pop-swordfight.hgr") - {"ACBM",Test_ACBM,Load_IFF, "iff/ACBM/Jupiter_alt.pic"}, - {"LBM", Test_LBM, Load_IFF, "iff/Danny_SkyTravellers_ANNO.iff"}, - {"PBM", Test_PBM, Load_IFF, "iff/pbm/FC.LBM"}, - TESTFMT(INFO,"amiga_icons/4colors/Utilities/Calculator.info") + TESTFMT(PCX, "pcx/lena2.pcx") + TESTFMTL(NEO, "atari_st/ATARIART.NEO") // Format with limitations + TESTFMTL(PC1, "atari_st/eunmiisa.pc1") // Format with limitations + TESTFMTL(PI1, "atari_st/evolutn.pi1") + TESTFMTL(FLI, "autodesk_FLI_FLC/2noppaa.fli") + TESTFMT(BMP, "bmp/test8.bmp") + TESTFMTL(ICO, "ico/punzip.ico") // Format with limitations + TESTFMTL(C64, "c64/multicolor/ARKANOID.KOA") // Format with limitations + TESTFMTL(PRG, "c64/multicolor/speedball2_loading_jonegg.prg") + TESTFMTL(GPX, "c64/pixcen/Cyberbird.gpx") + TESTFMTL(SCR, "cpc/scr/DANCEOFF.SCR") // Format with limitations + TESTFMTL(CM5, "cpc/mode5/spidey.cm5") // Format with limitations + TESTFMTL(PPH, "cpc/pph/BF.PPH") // Format with limitations + TESTFMTL(GOS, "cpc/iMPdraw_GFX/SONIC.GO1") + TESTFMTL(MOTO,"thomson/exocet-alientis.map") // Format with limitations + TESTFMTL(HGR, "apple2/hgr/pop-swordfight.hgr") // Format with limitations + {"ACBM",Test_ACBM,Load_IFF, NULL, "iff/ACBM/Jupiter_alt.pic"}, + {"LBM", Test_LBM, Load_IFF, Save_IFF, "iff/Danny_SkyTravellers_ANNO.iff"}, + {"PBM", Test_PBM, Load_IFF, Save_IFF, "iff/pbm/FC.LBM"}, + TESTFMTL(INFO,"amiga_icons/4colors/Utilities/Calculator.info") #ifndef __no_pnglib__ TESTFMT(PNG, "png/happy-birthday-guys.png") #endif #ifndef __no_tifflib__ TESTFMT(TIFF,"tiff/grafx2_banner.tif") #endif - TESTFMT(GPL, "palette-mariage_115.gpl") - TESTFMT(PAL, "pal/dp4_256.pal") - { NULL, NULL, NULL, NULL} + TESTFMTL(GPL, "palette-mariage_115.gpl") //PALETTE + TESTFMTL(PAL, "pal/dp4_256.pal") // PALETTE + { NULL, NULL, NULL, NULL, NULL} }; /** @@ -174,6 +179,7 @@ int Test_Load(void) int i; memset(&context, 0, sizeof(context)); + context.Type = CONTEXT_SURFACE; for (i = 0; formats[i].name != NULL; i++) { GFX2_Log(GFX2_DEBUG, "Testing format %s (Load)\n", formats[i].name); @@ -187,9 +193,111 @@ int Test_Load(void) GFX2_Log(GFX2_ERROR, "Load_%s failed for file %s\n", formats[i].name, formats[i].sample); return 0; } + if (context.Surface) + { + printf(" %hux%hu\n", context.Surface->w, context.Surface->h); + Free_GFX2_Surface(context.Surface); + context.Surface = NULL; + } } //Destroy_context(&context); free(context.File_name); free(context.File_directory); return 1; // OK } + +/** + * Test the Save_* functions + */ +int Test_Save(void) +{ + T_IO_Context context; + char path[256]; + char tmpdir[256]; + int i; + int ok = 0; + T_GFX2_Surface * testpic256 = 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/pkm/EVILNUN.PKM"); + File_error = 0; + Load_PKM(&context); + if (File_error != 0) + { + fprintf(stderr, "Failed to load reference picture\n"); + goto ret; + } + testpic256 = context.Surface; + context.Surface = NULL; + snprintf(tmpdir, sizeof(tmpdir), "/tmp/grafx2-test.XXXXXX"); + if (mkdtemp(tmpdir) == NULL) + { + perror("mkdtemp"); + goto ret; + } + printf("temp dir : %s\n", tmpdir); + ok = 1; + for (i = 0; ok && formats[i].name != NULL; i++) + { + if (formats[i].Save == NULL) + continue; + GFX2_Log(GFX2_DEBUG, "Testing format %s (Save)\n", formats[i].name); + snprintf(path, sizeof(path), "%s/%s.%s", tmpdir, "test", formats[i].name); + context_set_file_path(&context, path); + // save the reference picture + context.Surface = testpic256; + context.Target_address = testpic256->pixels; + context.Pitch = testpic256->w; + 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 + { + // 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 + { + // compare with the reference picture + if (context.Surface->w != testpic256->w || context.Surface->h != testpic256->h) + { + GFX2_Log(GFX2_ERROR, "Saved %hux%hu, reloaded %hux%hu from %s\n", + testpic256->w, testpic256->h, context.Surface->w, context.Surface->h, path); + ok = 0; + } + else if (0 != memcmp(context.Surface->pixels, testpic256->pixels, testpic256->w * testpic256->h)) + { + GFX2_Log(GFX2_ERROR, "Save%s/Load_%s: Pixels mismatch\n", formats[i].name, formats[i].name); + ok = 0; + } + else + { + if (unlink(path) < 0) + perror("unlink"); + } + Free_GFX2_Surface(context.Surface); + context.Surface = NULL; + } + } + } + if (rmdir(tmpdir) < 0) + perror("rmdir"); +ret: + if (testpic256) + Free_GFX2_Surface(testpic256); + free(context.File_name); + free(context.File_directory); + return ok; +} diff --git a/src/tests/testlist.h b/src/tests/testlist.h index 4d1e6277..faf37d61 100644 --- a/src/tests/testlist.h +++ b/src/tests/testlist.h @@ -7,3 +7,4 @@ TEST(Packbits) TEST(Convert_24b_bitmap_to_256) TEST(Formats) TEST(Load) +TEST(Save)