test Save_xxx functions

This commit is contained in:
Thomas Bernard 2019-12-07 13:07:25 +01:00
parent 43b8707fc4
commit f026c3f04b
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
3 changed files with 164 additions and 31 deletions

View File

@ -22,7 +22,10 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "../loadsave.h" #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) 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, height, file_size, format, ratio, bpp);
context->Width = width; context->Width = width;
context->Height = height; 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) byte Get_pixel(T_IO_Context *context, short x, short y)
{ {
(void)context; if (context->Type == CONTEXT_SURFACE)
(void)x; return Get_GFX2_Surface_pixel(context->Surface, x, y);
(void)y;
return 0; 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 Set_pixel(T_IO_Context *context, short x, short y, byte c)
{ {
(void)context; if (context->Type == CONTEXT_SURFACE)
(void)x; {
(void)y; if (context->Surface == NULL)
(void)c; {
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) void Set_pixel_24b(T_IO_Context *context, short x, short y, byte r, byte g, byte b)

View File

@ -29,49 +29,54 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include "../global.h" #include "../global.h"
#include "../fileformats.h" #include "../fileformats.h"
#include "../gfx2log.h" #include "../gfx2log.h"
#include "../gfx2mem.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 { static const struct {
const char * name; const char * name;
Func_IO_Test Test; Func_IO_Test Test;
Func_IO Load; Func_IO Load;
Func_IO Save;
const char * sample; const char * sample;
} formats[] = { } formats[] = {
TESTFMT(PKM, "pkm/EVILNUN.PKM") TESTFMT(PKM, "pkm/EVILNUN.PKM")
TESTFMT(GIF, "gif/2b_horse.gif") TESTFMT(GIF, "gif/2b_horse.gif")
TESTFMT(PCX, "pcx/lena.pcx") TESTFMT(PCX, "pcx/lena2.pcx")
TESTFMT(NEO, "atari_st/ATARIART.NEO") TESTFMTL(NEO, "atari_st/ATARIART.NEO") // Format with limitations
TESTFMT(PC1, "atari_st/eunmiisa.pc1") TESTFMTL(PC1, "atari_st/eunmiisa.pc1") // Format with limitations
TESTFMT(PI1, "atari_st/evolutn.pi1") TESTFMTL(PI1, "atari_st/evolutn.pi1")
TESTFMT(FLI, "autodesk_FLI_FLC/2noppaa.fli") TESTFMTL(FLI, "autodesk_FLI_FLC/2noppaa.fli")
TESTFMT(BMP, "bmp/test16bf565.bmp") TESTFMT(BMP, "bmp/test8.bmp")
TESTFMT(ICO, "ico/gitlab_favicon.ico") TESTFMTL(ICO, "ico/punzip.ico") // Format with limitations
TESTFMT(C64, "c64/multicolor/ARKANOID.KOA") TESTFMTL(C64, "c64/multicolor/ARKANOID.KOA") // Format with limitations
TESTFMT(PRG, "c64/multicolor/speedball2_loading_jonegg.prg") TESTFMTL(PRG, "c64/multicolor/speedball2_loading_jonegg.prg")
TESTFMT(GPX, "c64/pixcen/Cyberbird.gpx") TESTFMTL(GPX, "c64/pixcen/Cyberbird.gpx")
TESTFMT(SCR, "cpc/scr/DANCEOFF.SCR") TESTFMTL(SCR, "cpc/scr/DANCEOFF.SCR") // Format with limitations
TESTFMT(CM5, "cpc/mode5/spidey.cm5") TESTFMTL(CM5, "cpc/mode5/spidey.cm5") // Format with limitations
TESTFMT(PPH, "cpc/pph/BF.PPH") TESTFMTL(PPH, "cpc/pph/BF.PPH") // Format with limitations
TESTFMT(GOS, "cpc/iMPdraw_GFX/SONIC.GO1") TESTFMTL(GOS, "cpc/iMPdraw_GFX/SONIC.GO1")
TESTFMT(MOTO,"thomson/exocet-alientis.map") TESTFMTL(MOTO,"thomson/exocet-alientis.map") // Format with limitations
TESTFMT(HGR, "apple2/hgr/pop-swordfight.hgr") TESTFMTL(HGR, "apple2/hgr/pop-swordfight.hgr") // Format with limitations
{"ACBM",Test_ACBM,Load_IFF, "iff/ACBM/Jupiter_alt.pic"}, {"ACBM",Test_ACBM,Load_IFF, NULL, "iff/ACBM/Jupiter_alt.pic"},
{"LBM", Test_LBM, Load_IFF, "iff/Danny_SkyTravellers_ANNO.iff"}, {"LBM", Test_LBM, Load_IFF, Save_IFF, "iff/Danny_SkyTravellers_ANNO.iff"},
{"PBM", Test_PBM, Load_IFF, "iff/pbm/FC.LBM"}, {"PBM", Test_PBM, Load_IFF, Save_IFF, "iff/pbm/FC.LBM"},
TESTFMT(INFO,"amiga_icons/4colors/Utilities/Calculator.info") TESTFMTL(INFO,"amiga_icons/4colors/Utilities/Calculator.info")
#ifndef __no_pnglib__ #ifndef __no_pnglib__
TESTFMT(PNG, "png/happy-birthday-guys.png") TESTFMT(PNG, "png/happy-birthday-guys.png")
#endif #endif
#ifndef __no_tifflib__ #ifndef __no_tifflib__
TESTFMT(TIFF,"tiff/grafx2_banner.tif") TESTFMT(TIFF,"tiff/grafx2_banner.tif")
#endif #endif
TESTFMT(GPL, "palette-mariage_115.gpl") TESTFMTL(GPL, "palette-mariage_115.gpl") //PALETTE
TESTFMT(PAL, "pal/dp4_256.pal") TESTFMTL(PAL, "pal/dp4_256.pal") // PALETTE
{ NULL, NULL, NULL, NULL} { NULL, NULL, NULL, NULL, NULL}
}; };
/** /**
@ -174,6 +179,7 @@ int Test_Load(void)
int i; int i;
memset(&context, 0, sizeof(context)); memset(&context, 0, sizeof(context));
context.Type = CONTEXT_SURFACE;
for (i = 0; formats[i].name != NULL; i++) for (i = 0; formats[i].name != NULL; i++)
{ {
GFX2_Log(GFX2_DEBUG, "Testing format %s (Load)\n", formats[i].name); 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); GFX2_Log(GFX2_ERROR, "Load_%s failed for file %s\n", formats[i].name, formats[i].sample);
return 0; 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); //Destroy_context(&context);
free(context.File_name); free(context.File_name);
free(context.File_directory); free(context.File_directory);
return 1; // OK 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;
}

View File

@ -7,3 +7,4 @@ TEST(Packbits)
TEST(Convert_24b_bitmap_to_256) TEST(Convert_24b_bitmap_to_256)
TEST(Formats) TEST(Formats)
TEST(Load) TEST(Load)
TEST(Save)