Skip to content

Instantly share code, notes, and snippets.

@hanoba
Created January 6, 2019 07:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanoba/41e7ba45bb048e8f4d09a69b2814f8aa to your computer and use it in GitHub Desktop.
Save hanoba/41e7ba45bb048e8f4d09a69b2814f8aa to your computer and use it in GitHub Desktop.
GIF to C conversion program
#include <stdio.h> // standard input / output functions
// This program converts a GIF file into a C header file for the
// POV Cylinder (https://www.instructables.com/id/POV-Cylinder-With-Arduino-Due/)
//
// Author: Harald Bauer
// Date: 2015-04-17
//-----------------------------------------------------------------------------
void convert_gif_file(const char *inFileName, const char *outFileName, const char *arrayName)
//-----------------------------------------------------------------------------
{
#define MAXFILESIZE 200001
static unsigned char fileData[MAXFILESIZE];
size_t fileSize;
size_t i;
FILE *fpIn, *fpOut;
char s1[64], s2[64], s3[64];
fpIn = fopen(inFileName, "rb");
if (fpIn==NULL) {
fprintf(stderr, "Command aborted - File '%s' not found\n", inFileName);
return;
}
fpOut = fopen(outFileName, "wb");
if (fpOut==NULL) {
fprintf(stderr, "Error opening file %s\n", outFileName);
return;
}
fileSize = fread(fileData, 1, MAXFILESIZE, fpIn);
fclose(fpIn);
if (fileSize>=MAXFILESIZE) {
fprintf(stderr, "Command aborted - Files size is greater than %d bytes\n", MAXFILESIZE-1);
return;
}
if (fileSize==0) {
fprintf(stderr, "Command aborted - Error reading file\n");
return;
}
fprintf(stderr, "Converting file %s - %lu bytes\n", inFileName, fileSize);
// { "Dog", sizeof(gif_dog), gif_dog, 0, 0 },
sprintf(s1,"\"%s\",", arrayName);
sprintf(s2,"sizeof(%s),", arrayName);
sprintf(s3,"%s,", arrayName);
printf(" { %-20s %-25s %-18s 0, 0 },\n", s1, s2, s3);
fprintf(fpOut, "// GIF File: %s\n", inFileName);
fprintf(fpOut, "static const unsigned char %s[%lu] PROGMEM = {\n", arrayName, fileSize);
fprintf(fpOut, " 0x%02X", fileData[0]);
for (i=1; i<fileSize; i++) {
if (i&15) fprintf(fpOut, ",0x%02X", fileData[i]);
else fprintf(fpOut, ",\n 0x%02X", fileData[i]);
}
fprintf(fpOut, "\n};\n");
fclose(fpOut);
}
//-----------------------------------------------------------------------------
int main (int argc, char *argv[])
//-----------------------------------------------------------------------------
{
fprintf(stderr, "GIF to C conversion program\n");
if (argc != 4) fprintf(stderr, "Usage: gif2c <infile> <outfile> <arrayname>\n");
else convert_gif_file(argv[1], argv[2], argv[3]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment