Skip to content

Instantly share code, notes, and snippets.

@lecram
Last active September 20, 2020 11:48
Show Gist options
  • Save lecram/450f024d0ca9a77d7e09b576f65f5d89 to your computer and use it in GitHub Desktop.
Save lecram/450f024d0ca9a77d7e09b576f65f5d89 to your computer and use it in GitHub Desktop.
/* Compiling:
* $ gcc -O2 -o explode gifdec.c explode.c
* Comparing gifdec with ImageMagick's GIF decoder:
* $ ./explode foo.gif # generates gd_%03d.ppm files
* $ convert -coalesce foo.gif im_%03d.ppm
* $ # comparing entire animation:
* $ cat gd_*.ppm | md5sum
* $ cat im_*.ppm | md5sum
* $ # comparing each frame:
* $ md5sum gd_*.ppm
* $ md5sum im_*.ppm
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "gifdec.h"
int
main(int argc, char *argv[])
{
gd_GIF *gif;
int nframes = 0;
char fname[0x10] = {0};
uint8_t *image;
FILE *fp;
if (argc != 2) {
fprintf(stderr, "usage:\n %s input.gif\n", argv[0]);
return 1;
}
gif = gd_open_gif(argv[1]);
if (!gif) {
fprintf(stderr, "could not read '%s'\n", argv[1]);
return 1;
}
image = malloc(gif->width * gif->height * 3);
if (!image) {
fprintf(stderr, "could not allocate image buffer\n");
return 1;
}
while (gd_get_frame(gif) == 1) {
snprintf(fname, sizeof(fname) - 1, "gd_%03d.ppm", nframes);
fp = fopen(fname, "w");
fprintf(fp, "P6\n%d %d\n255\n", gif->width, gif->height);
gd_render_frame(gif, image);
fwrite(image, gif->width * gif->height, 3, fp);
fclose(fp);
nframes++;
}
gd_close_gif(gif);
free(image);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment