Skip to content

Instantly share code, notes, and snippets.

@jsfaint
Created May 27, 2014 03:31
Show Gist options
  • Save jsfaint/4ea993506d843c78d62d to your computer and use it in GitHub Desktop.
Save jsfaint/4ea993506d843c78d62d to your computer and use it in GitHub Desktop.
Convert gif to C file
/*
* gif2c.c
* convert gif file to c file.
* support multi file one time.
*
* author: jason([email protected])
* date: 2009.03.30
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define COMMENT "\n//the following content is for .h file.\n"
int gif2c(char* szFullName);
int getFileName(const char *szFullName, char *szFileName, char *szFilePath);
char* szFileNameUppder(const char *pszFileName, char* pszUpper);
int main(int argc, char** argv)
{
int iIndex = 0;
puts("Gif2C\n");
if (argc < 2) //argc < 2, means there is no input gif file.
{
printf("Please execute with params as follow:\ngif2c 1.gif 2.gif\n");
system("pause");
return -1;
}
for (iIndex = 1; iIndex <argc; iIndex++)
{
if (strlen(argv[iIndex])> 1024)
{
printf("file path too long...\n");
return 0;
}
printf("convert %s\tplease wait...\n", argv[iIndex]);
gif2c(argv[iIndex]);
}
system("pause");
return 0;
}
int gif2c(char* szFullName)
{
FILE *fpIn;
FILE *fpOut;
char szFileName[256];
char szUpper[256];
char szFilePath[1024];
char szOutPut[1024];
unsigned char c;
unsigned long uFileSize = 0;
unsigned char count = 0;
if (getFileName(szFullName, szFileName, szFilePath) == -1)
{
printf("get filename failed.\n");
return -1;
}
sprintf(szOutPut, "%s\\%s.c", szFilePath, szFileName);
fpIn = fopen(szFullName, "rb");
fpOut = fopen(szOutPut, "w+");
if (!fpIn || !fpOut)
{
printf("open file failed.\n");
return -1;
}
fprintf(fpOut, "unsigned char const far webgif_%s[WEBGIF_%s_LEN] = {\n", szFileName, szFileNameUppder(szFileName, szUpper));
c = fgetc(fpIn);
while(!feof(fpIn))
{
uFileSize++;
fprintf(fpOut, "0x%02X, ", c);
c = fgetc(fpIn);
if(count<10)
count++;
else
{
count = 0;
fputc('\n', fpOut);
}
}
fputs("}\n", fpOut);
fputs(COMMENT, fpOut);
fprintf(fpOut, "#define WEBGIF_%s_LEN %lu\n", szFileNameUppder(szFileName, szUpper), uFileSize);
fprintf(fpOut, "extern unsigned char const far webgif_%s[WEBGIF_%s_LEN];\n", szFileName, szFileNameUppder(szFileName, szUpper));
fclose(fpOut);
fclose(fpIn);
return 0;
}
/*
* get filename(without path and extension) and file path.
*/
int getFileName(const char *szFullName, char *szFileName, char *szFilePath)
{
char *result = NULL;
result = strrchr(szFullName, 0x5C); // '\' = 0x5C
if(result)
sprintf(szFileName, "%s", result+1);
else
return -1;
strcpy(szFilePath, szFullName);
result = strrchr(szFilePath, 0x5C); // '\' = 0x5C
if(result)
*result = 0x00;
else
return -1;
//get filename without extension. e.g.: 1.gif -> 1
result = strchr(szFileName, '.');
if(result)
*result = 0x00;
else
return -1;
return 0;
}
/*
* covert the filename to upper size.
*/
char* szFileNameUppder(const char *pszFileName, char* pszUpper)
{
int ii;
strcpy(pszUpper, pszFileName);
for (ii=0; ii<strlen(pszUpper); ii++)
{
pszUpper[ii] = toupper(pszUpper[ii]);
}
return pszUpper;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment