GIF文件转换为头文件工具

时间:2023-03-09 03:00:23
GIF文件转换为头文件工具
  • 目的:

    GIF文件转为头文件

  • 举例:

    用UE打开GIF文件,如下图所示:

    GIF文件转换为头文件工具
    图1 test.gif文件

    将上面文件内容转化为头文件,放到一个数组里面,内容如下:

    GIF文件转换为头文件工具
    图2 test.h文件

  • 思路:

    从上面可知,将二进制文件转换为文本文件,十六进制 47  转为 0x47,其他类推。

  • 代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> static char pre_compiler_str_begin[] = "#ifdef PIP_PNG_GIF";
    static char pre_compiler_str_end[] = "#endif";
    static char const_char[] = "const char ";
    static char sign_begin[] = "[] = {";
    static char sign_end[] = "};"; int match_file(unsigned char *buf, int file_size, char *src_file_name)
    {
    unsigned char *p_start = NULL, *p_end = NULL;
    char *head_data = NULL;
    char str[64], filename[256], *p;
    int i,ret = 0; if((buf == NULL) || (src_file_name==NULL))
    {
    return -1;
    } p_end = buf; if ((strlen(src_file_name) > 4)
    && (strcmp(src_file_name + strlen(src_file_name) - 4, ".gif") == 0))
    {
    strncpy(filename, src_file_name, strlen(src_file_name) - 4);
    strcpy(filename + strlen(src_file_name) - 4, ".h");
    }
    else
    {
    sprintf(filename, "%s.h", src_file_name);
    } do
    {
    FILE *head_file = NULL;
    head_file = fopen(filename, "w");
    if (head_file == NULL)
    {
    ret = -1;
    fprintf(stderr,"[%s] Open file %s error!\n", src_file_name, filename);
    break;
    } // write str:
    // #ifdef PIP_PNG_GIF
    // const char test0_gif[] = {
    fwrite(pre_compiler_str_begin, 1, strlen(pre_compiler_str_begin), head_file);
    fputc('\n',head_file); fwrite(const_char, 1, strlen(const_char), head_file);
    p = strrchr(filename, '\\')+1;
    fwrite(p, 1, strlen(p)-2, head_file);
    fwrite(sign_begin, 1, strlen(sign_begin), head_file);
    fputc('\n',head_file); // write data
    for(i = 0; i < file_size; i++)
    {
    memset(str, 0, sizeof(str));
    sprintf(str, "%s%0.2x%c", "0x", buf[i], ',');
    fwrite(str, 1, strlen(str), head_file);
    if((i == file_size - 1) || (i % 16 == 15))
    {
    fputc('\n',head_file);
    }
    } fwrite(sign_end, 1, strlen(sign_end), head_file);
    fputc('\n',head_file); fwrite(pre_compiler_str_end, 1, strlen(pre_compiler_str_end), head_file);
    fputc('\n',head_file); fclose(head_file);
    } while (0); return ret;
    } int main(int argc, char *argv[])
    {
    FILE *input = NULL;
    char *file_name = NULL;//"test.h";
    unsigned char *bmp_data = NULL;
    int i;
    int file_size = 0; if (argc <= 1)
    {
    printf("Please assign input files!\n");
    return -1;
    } for (i=1; i<argc; i++)
    {
    file_name = argv[i]; input = fopen(file_name, "rb");
    if (!input)
    {
    fprintf(stderr, "[%s] Cannot open file!\n",file_name);
    continue;
    }
    fseek(input, 0, SEEK_END);
    file_size = ftell(input);
    bmp_data = (unsigned char *)malloc(file_size + 1);
    fseek(input, 0, SEEK_SET);
    fread(bmp_data, 1, file_size, input);
    bmp_data[file_size] = '0'; if(match_file(bmp_data, file_size, file_name) == 0)
    {
    fprintf(stdout, "[%s]\tOK\n",file_name);
    }
    else
    {
    fprintf(stdout, "[%s] Failed!\n",file_name);
    }
    } return 0; }