Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/seeds.sha256
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
5496eb57d6e50a60657ed36c5413a1a7483a42057fd40212ee916d0d05bcb08b all_optim.seed
123c8933f13b7e18f88e1b260af6fc98c8cb7d5a5000529f274e099b58111227 alpha.seed
04ea5d3ed210097cef6f1973eabb11fdb17a2f1bbc529ee86d9e09dc6542099f avoid_compression.seed
cbdd22c6777494464ca6139ce5f9444ee7928fed7f69c8c56db6397036f80840 animated_color_gradient.seed
6b85a73c6c98ff22b9ab122024c9c05056bf3e63d4b16ab6c34655a4e65c9812 animated_interlaced.seed
855f496df19ddcc070ae9fbf4ad40b43fff47150c5a86ac3b61066adbfda9be1 animated_single_pixel.seed
Expand Down
74 changes: 74 additions & 0 deletions tests/avoid_compression.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#include "cgif.h"

#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
#define HEIGHT 1

static uint64_t seed;

int psdrand(void) {
// simple pseudo random function from musl libc
seed = 6364136223846793005ULL * seed + 1;
return seed >> 33;
}

int main(void) {
CGIF* pGIF;
CGIF_Config gConfig;
CGIF_FrameConfig fConfig;
uint8_t* pImageData;
cgif_result r;
int palette_size = 256;
uint8_t aPalette[palette_size * 3];

seed = 42;
for(int i = 0; i < palette_size; ++i) {
aPalette[i * 3] = psdrand() % 256;
aPalette[i * 3 + 1] = psdrand() % 256;
aPalette[i * 3 + 2] = psdrand() % 256;
}
memset(&gConfig, 0, sizeof(CGIF_Config));
memset(&fConfig, 0, sizeof(CGIF_FrameConfig));
gConfig.width = WIDTH;
gConfig.height = HEIGHT;
gConfig.pGlobalPalette = aPalette;
gConfig.numGlobalPaletteEntries = palette_size;
gConfig.path = "avoid_compression.gif";
//
// create new GIF
pGIF = cgif_newgif(&gConfig);
if(pGIF == NULL) {
fputs("failed to create new GIF via cgif_newgif()\n", stderr);
return 1;
}
//
// add frames to GIF
uint8_t* code_dict = malloc(sizeof(uint8_t)*palette_size*palette_size); // to check if codes are used already, then avoid them
memset(code_dict, 0, sizeof(uint8_t)*palette_size*palette_size);
pImageData = malloc(WIDTH * HEIGHT);
pImageData[0] = psdrand() % palette_size;
for(int i = 1; i < WIDTH * HEIGHT; ++i){
uint8_t idx = 0;
while (idx == 0 || code_dict[palette_size*idx + pImageData[i-1]]) idx = psdrand() % palette_size;
pImageData[i] = idx;
code_dict[palette_size*idx + pImageData[i-1]] = 1; //mark code as used
}
fConfig.pImageData = pImageData;
r = cgif_addframe(pGIF, &fConfig);
free(code_dict);
free(pImageData);
//
// write GIF to file
r = cgif_close(pGIF); // free allocated space at the end of the session

// check for errors
if(r != CGIF_OK) {
fprintf(stderr, "failed to create GIF. error code: %d\n", r);
return 2;
}
return 0;
}
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tests_index = [
{ 'name' : 'animated_snake', 'seed_should_fail' : false},
{ 'name' : 'animated_stripes_horizontal', 'seed_should_fail' : false},
{ 'name' : 'animated_stripe_pattern', 'seed_should_fail' : false},
{ 'name' : 'avoid_compression', 'seed_should_fail' : false},
{ 'name' : 'animated_stripe_pattern_2', 'seed_should_fail' : false},
{ 'name' : 'duplicate_frames', 'seed_should_fail' : false},
{ 'name' : 'earlyclose', 'seed_should_fail' : true },
Expand Down
1 change: 1 addition & 0 deletions tests/tests.sha256
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
150d5d8e3aedd105bd7b3609547e375ce5432c435509b9a88842119ec6afe8e6 all_optim.gif
1c45ad2d19b1435a7ded09b4d98817f57d4157eabf04f0817bc479b139edaf0b alpha.gif
aecc2b3022aa789181029430ee270206b38df94e92581ce9bef31d8f2ff266e6 avoid_compression.gif
fe2c3217ac41089bbf95c30e558f9d00fd4e003e76b4c81531f4dec4abe22e8e animated_color_gradient.gif
bed405ac125e51b30503922d29a0e66f604458ad769194397319b5c692b671e0 animated_interlaced.gif
88665c8e961a0915c8982b8adac5dcf715eaaec2655b8be455e967a0b4d5b350 animated_single_pixel.gif
Expand Down
Loading