Skip to content

Instantly share code, notes, and snippets.

@jsfaint
Created May 27, 2014 03:10
Show Gist options
  • Save jsfaint/ef36cd6cddba5e6ef1cf to your computer and use it in GitHub Desktop.
Save jsfaint/ef36cd6cddba5e6ef1cf to your computer and use it in GitHub Desktop.
Convert gif to binary file.
/*
v0.3 implement getFileName(), optimize output file.
v0.2 add multi file support
v0.1 first demo
*/
#include <string.h>
#include <stdio.h>
void getFileName(const char *szFullName, char *szFileName);
int main(int argc, char **argv)
{
FILE *fpSrc = NULL;
FILE *fpDst = NULL;
char szOutputFileName[1024];
char szFileName[512];
char szUpperSizeFileName[512];
unsigned char c;
unsigned long size = 0, dwIndex; // file size
int n = 0, ii;
if (argv[1] == NULL)
{
printf("please use this tool with args, e.g.:image.exe a.gif\n");
getch();
return 1;
}
for (dwIndex = 1; dwIndex<argc; dwIndex++)
{
fpSrc = fopen(argv[dwIndex], "rb");
if (fpSrc == NULL)
{
printf("open dst file failed.\n");
getch();
return 1;
}
sprintf(szOutputFileName, "%s.c", argv[dwIndex]);
fpDst = fopen(szOutputFileName, "w+");
//getFileName without path and extension name.
getFileName(argv[dwIndex], szFileName);
memset(szUpperSizeFileName, 0, 512);
for (ii=0; ii<strlen(szFileName); ii++)
szUpperSizeFileName[ii] = toupper(szFileName[ii]);
fprintf(fpDst, "/*%s*/\n", argv[dwIndex]);
fprintf(fpDst, "unsigned char const far webgif_%s[WEBGIF_%s_LEN] = {\n", szFileName, szUpperSizeFileName);
c = fgetc(fpSrc);
while (!feof(fpSrc))
{
fprintf(fpDst, "%02X, ", c);
c = fgetc(fpSrc);
n++;
size++;
if (n>=16)
{
fprintf(fpDst, "\n");
n = 0;
}
}
fprintf(fpDst, "}\n");
fprintf(fpDst, "\n#define WEBGIF_%s_LEN %lu\n", szUpperSizeFileName, size);
fclose(fpSrc);
fclose(fpDst);
}
system("pause");
return 0;
}
void getFileName(const char *szFullName, char *szFileName)
{
char *result = NULL;
result = strrchr(szFullName, 0x5C); // '\' = 0x5C
//printf("%s\n", result);
result = strtok(result, ".");
//printf("%s\n", result);
sprintf(szFileName, "%s", result+1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment