Skip to content

Commit c8c6a9e

Browse files
authored
Merge pull request #85 from MCLoebl/test_zero_compression
add test for zero compression
2 parents 9ba30df + a344eee commit c8c6a9e

4 files changed

Lines changed: 77 additions & 0 deletions

File tree

fuzz/seeds.sha256

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
5496eb57d6e50a60657ed36c5413a1a7483a42057fd40212ee916d0d05bcb08b all_optim.seed
22
123c8933f13b7e18f88e1b260af6fc98c8cb7d5a5000529f274e099b58111227 alpha.seed
3+
04ea5d3ed210097cef6f1973eabb11fdb17a2f1bbc529ee86d9e09dc6542099f avoid_compression.seed
34
cbdd22c6777494464ca6139ce5f9444ee7928fed7f69c8c56db6397036f80840 animated_color_gradient.seed
45
6b85a73c6c98ff22b9ab122024c9c05056bf3e63d4b16ab6c34655a4e65c9812 animated_interlaced.seed
56
855f496df19ddcc070ae9fbf4ad40b43fff47150c5a86ac3b61066adbfda9be1 animated_single_pixel.seed

tests/avoid_compression.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <stdlib.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
#include "cgif.h"
7+
8+
#define WIDTH 3840 // 4096 - 2 - 256 = 3838 (free lzw codes) +1 (to force dict reset: k pixels make adding k-1 codes) +1 to overflow if too litte is allocated in pLZWData
9+
#define HEIGHT 1
10+
11+
static uint64_t seed;
12+
13+
int psdrand(void) {
14+
// simple pseudo random function from musl libc
15+
seed = 6364136223846793005ULL * seed + 1;
16+
return seed >> 33;
17+
}
18+
19+
int main(void) {
20+
CGIF* pGIF;
21+
CGIF_Config gConfig;
22+
CGIF_FrameConfig fConfig;
23+
uint8_t* pImageData;
24+
cgif_result r;
25+
int palette_size = 256;
26+
uint8_t aPalette[palette_size * 3];
27+
28+
seed = 42;
29+
for(int i = 0; i < palette_size; ++i) {
30+
aPalette[i * 3] = psdrand() % 256;
31+
aPalette[i * 3 + 1] = psdrand() % 256;
32+
aPalette[i * 3 + 2] = psdrand() % 256;
33+
}
34+
memset(&gConfig, 0, sizeof(CGIF_Config));
35+
memset(&fConfig, 0, sizeof(CGIF_FrameConfig));
36+
gConfig.width = WIDTH;
37+
gConfig.height = HEIGHT;
38+
gConfig.pGlobalPalette = aPalette;
39+
gConfig.numGlobalPaletteEntries = palette_size;
40+
gConfig.path = "avoid_compression.gif";
41+
//
42+
// create new GIF
43+
pGIF = cgif_newgif(&gConfig);
44+
if(pGIF == NULL) {
45+
fputs("failed to create new GIF via cgif_newgif()\n", stderr);
46+
return 1;
47+
}
48+
//
49+
// add frames to GIF
50+
uint8_t* code_dict = malloc(sizeof(uint8_t)*palette_size*palette_size); // to check if codes are used already, then avoid them
51+
memset(code_dict, 0, sizeof(uint8_t)*palette_size*palette_size);
52+
pImageData = malloc(WIDTH * HEIGHT);
53+
pImageData[0] = psdrand() % palette_size;
54+
for(int i = 1; i < WIDTH * HEIGHT; ++i){
55+
uint8_t idx = 0;
56+
while (idx == 0 || code_dict[palette_size*idx + pImageData[i-1]]) idx = psdrand() % palette_size;
57+
pImageData[i] = idx;
58+
code_dict[palette_size*idx + pImageData[i-1]] = 1; //mark code as used
59+
}
60+
fConfig.pImageData = pImageData;
61+
r = cgif_addframe(pGIF, &fConfig);
62+
free(code_dict);
63+
free(pImageData);
64+
//
65+
// write GIF to file
66+
r = cgif_close(pGIF); // free allocated space at the end of the session
67+
68+
// check for errors
69+
if(r != CGIF_OK) {
70+
fprintf(stderr, "failed to create GIF. error code: %d\n", r);
71+
return 2;
72+
}
73+
return 0;
74+
}

tests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ tests_index = [
1212
{ 'name' : 'animated_snake', 'seed_should_fail' : false},
1313
{ 'name' : 'animated_stripes_horizontal', 'seed_should_fail' : false},
1414
{ 'name' : 'animated_stripe_pattern', 'seed_should_fail' : false},
15+
{ 'name' : 'avoid_compression', 'seed_should_fail' : false},
1516
{ 'name' : 'animated_stripe_pattern_2', 'seed_should_fail' : false},
1617
{ 'name' : 'duplicate_frames', 'seed_should_fail' : false},
1718
{ 'name' : 'earlyclose', 'seed_should_fail' : true },

tests/tests.sha256

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
150d5d8e3aedd105bd7b3609547e375ce5432c435509b9a88842119ec6afe8e6 all_optim.gif
22
1c45ad2d19b1435a7ded09b4d98817f57d4157eabf04f0817bc479b139edaf0b alpha.gif
3+
aecc2b3022aa789181029430ee270206b38df94e92581ce9bef31d8f2ff266e6 avoid_compression.gif
34
fe2c3217ac41089bbf95c30e558f9d00fd4e003e76b4c81531f4dec4abe22e8e animated_color_gradient.gif
45
bed405ac125e51b30503922d29a0e66f604458ad769194397319b5c692b671e0 animated_interlaced.gif
56
88665c8e961a0915c8982b8adac5dcf715eaaec2655b8be455e967a0b4d5b350 animated_single_pixel.gif

0 commit comments

Comments
 (0)