add Test_Packbits()

This commit is contained in:
Thomas Bernard 2019-11-15 22:57:55 +01:00
parent cd1349fdab
commit 3ba4472626
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
6 changed files with 102 additions and 6 deletions

View File

@ -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 \

View File

@ -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';

View File

@ -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;
}

View File

@ -3,4 +3,4 @@
TEST(MOTO_MAP_pack)
TEST(CPC_compare_colors)
TEST(Packbits)

View File

@ -27,7 +27,9 @@
/// Unit tests.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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++)

View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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
}