From 3ba447262693397b6de060712bdaa0803267047c Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Fri, 15 Nov 2019 22:57:55 +0100 Subject: [PATCH] add Test_Packbits() --- src/Makefile | 2 +- src/fileformats.c | 6 ++-- src/tests/mockloadsave.c | 21 ++++++++++++ src/tests/testlist.h | 2 +- src/tests/testmain.c | 3 ++ src/tests/tests.c | 74 +++++++++++++++++++++++++++++++++++++++- 6 files changed, 102 insertions(+), 6 deletions(-) diff --git a/src/Makefile b/src/Makefile index d489d122..8d8a25a6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -818,7 +818,7 @@ OBJS += loadrecoil.o recoil.o endif TESTSOBJS = $(patsubst %.c,%.o,$(wildcard tests/*.c)) \ - miscfileformats.o oldies.o libraw2crtc.o \ + miscfileformats.o fileformats.o oldies.o libraw2crtc.o \ loadsavefuncs.o \ unicode.o \ io.o realpath.o version.o pversion.o \ diff --git a/src/fileformats.c b/src/fileformats.c index e0b0c3ea..b4eda9e0 100644 --- a/src/fileformats.c +++ b/src/fileformats.c @@ -692,7 +692,7 @@ static void LBM_Decode(T_IO_Context * context, FILE * file, byte compression, by File_error=22; break; } - // temp_byte > 127 => repeat (256-temp_byte) the next byte + // temp_byte > 127 => repeat (257-temp_byte) the next byte // temp_byte <= 127 => copy (temp_byte + 1) bytes if(temp_byte == 128) // 128 = NOP ! { @@ -2202,7 +2202,7 @@ void Load_IFF(T_IO_Context * context) byte IFF_repetition_mode; // ------------- Ecrire les couleurs que l'on vient de traiter ------------ - static void Transfer_colors(FILE * file) + void Transfer_colors(FILE * file) { byte index; @@ -4467,7 +4467,7 @@ void Load_GIF(T_IO_Context * context) // on jette les autres. if (context->Comment[0]=='\0') { - int nb_char_to_keep=Min(size_to_read,COMMENT_SIZE); + int nb_char_to_keep = MIN(size_to_read, COMMENT_SIZE); Read_bytes(GIF_file,context->Comment,nb_char_to_keep); context->Comment[nb_char_to_keep+1]='\0'; diff --git a/src/tests/mockloadsave.c b/src/tests/mockloadsave.c index dc084f8a..4f5eb335 100644 --- a/src/tests/mockloadsave.c +++ b/src/tests/mockloadsave.c @@ -54,6 +54,21 @@ void Set_pixel(T_IO_Context *context, short x, short y, byte c) (void)c; } +void Set_pixel_24b(T_IO_Context *context, short x, short y, byte r, byte g, byte b) +{ + (void)context; + (void)x; + (void)y; + (void)r; + (void)g; + (void)b; +} + +void Fill_canvas(T_IO_Context *context, byte color) +{ + printf("Fill_canvas(%p, %hhu)\n", context, color); +} + void Set_saving_layer(T_IO_Context *context, int layer) { printf("Set_saving_layer(%p, %d)\n", context, layer); @@ -73,3 +88,9 @@ void Set_frame_duration(T_IO_Context *context, int duration) { printf("Set_frame_duration(%p, %d)\n", context, duration); } + +int Get_frame_duration(T_IO_Context *context) +{ + (void)context; + return 0; +} diff --git a/src/tests/testlist.h b/src/tests/testlist.h index 2e3bcc7f..3278f1ef 100644 --- a/src/tests/testlist.h +++ b/src/tests/testlist.h @@ -3,4 +3,4 @@ TEST(MOTO_MAP_pack) TEST(CPC_compare_colors) - +TEST(Packbits) diff --git a/src/tests/testmain.c b/src/tests/testmain.c index 05b16f5d..db3d5d95 100644 --- a/src/tests/testmain.c +++ b/src/tests/testmain.c @@ -27,7 +27,9 @@ /// Unit tests. #include +#include #include +#include #include "../struct.h" #include "../global.h" #include "../gfx2log.h" @@ -100,6 +102,7 @@ int main(int argc, char * * argv) int i, r; int fail = 0; + srandom(time(NULL)); GFX2_verbosity_level = GFX2_DEBUG; for (i = 0; tests[i].test_func != 0; i++) diff --git a/src/tests/tests.c b/src/tests/tests.c index e7c04306..d2b4dc01 100644 --- a/src/tests/tests.c +++ b/src/tests/tests.c @@ -2,7 +2,7 @@ */ /* Grafx2 - The Ultimate 256-color bitmap paint program - Copyright 2018 Thomas Bernard + Copyright 2018-2019 Thomas Bernard Copyright 2011 Pawel Góralski Copyright 2009 Petter Lindquist Copyright 2008 Yves Rizoud @@ -28,11 +28,15 @@ /// #include +#include #include +#include #include "../struct.h" #include "../oldies.h" #include "../gfx2log.h" +unsigned int MOTO_MAP_pack(byte * packed, const byte * unpacked, unsigned int unpacked_len); + /** * Tests for MOTO_MAP_pack() */ @@ -84,6 +88,9 @@ int Test_MOTO_MAP_pack(void) return 1; // test OK } +/** + * Test for Test_CPC_compare_colors() + */ int Test_CPC_compare_colors(void) { unsigned int r, g, b; @@ -129,3 +136,68 @@ int Test_CPC_compare_colors(void) } return 1; // test OK } + +extern word IFF_list_size; +void New_color(FILE * f, byte color); +void Transfer_colors(FILE * f); + +/** + * Tests for the packbits compression used in IFF ILBM, etc. + * see http://fileformats.archiveteam.org/wiki/PackBits + */ +int Test_Packbits(void) +{ + char tempfilename[64]; + FILE * f; + int i, j; + long unpacked; + long packed; + static const char * tests[] = { + "1234AAAAAAAAAAAAAAAAAAAAA", // best : 03 "1234" -20(ec) 'A' => 7 bytes + "AABBCCDDDDDDD12345@@@54321", // best : -1(ff) 'A' -1(ff) 'B' -1(ff) 'C' -6(fa) 'D' 12(0c) "12345@@@54321" => 22 bytes + // or 04 "12345" -2(fe) '@' 04 "54321" + "123AA123BBB123CCCC123DDDDD", // best : 07 "123AA123" -2 'B' 02 "123" -3(fd) 'C' 02 "123" -4(fc) 'D' => 23 bytes + NULL + }; + const long best_packed = 7 + 22 + 23; + + snprintf(tempfilename, sizeof(tempfilename), "/tmp/gfx2test-packbits-%lx", random()); + GFX2_Log(GFX2_DEBUG, "tempfile %s\n", tempfilename); + f = fopen(tempfilename, "wb"); + if (f == NULL) + { + GFX2_Log(GFX2_ERROR, "Failed to open %s for writing\n", tempfilename); + return 0; + } + + // Start encoding + IFF_list_size = 0; + for (i = 0, unpacked = 0; tests[i]; i++) + { + for (j = 0; tests[i][j]; j++) + { + New_color(f, (byte)tests[i][j]); + unpacked++; + } + Transfer_colors(f); + } + packed = ftell(f); + fclose(f); + GFX2_Log(GFX2_DEBUG, "Compressed %ld bytes to %ld\n", unpacked, packed); + if (packed > best_packed) { + GFX2_Log(GFX2_ERROR, "*** Packbits less efficient as expected (%ld > %ld bytes) ***\n", + packed, best_packed); + return 0; + } + + // TODO : test unpacking + f = fopen(tempfilename, "rb"); + if (f == NULL) + { + GFX2_Log(GFX2_ERROR, "Failed to open %s for reading\n", tempfilename); + return 0; + } + fclose(f); + unlink(tempfilename); + return 1; // test OK +}