• GIF文件转换为头文件工具


    • 目的:

      GIF文件转为头文件

    • 举例:

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

      image
      图1 test.gif文件

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

      image
      图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!
      ", 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('
      ',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('
      ',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('
      ',head_file);
      			}
      		}
      
      		fwrite(sign_end, 1, strlen(sign_end), head_file);
      		fputc('
      ',head_file);
      
      		fwrite(pre_compiler_str_end, 1, strlen(pre_compiler_str_end), head_file);
      		fputc('
      ',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!
      ");
      		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!
      ",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]	OK
      ",file_name);
      		}
      		else
      		{
      			fprintf(stdout, "[%s] Failed!
      ",file_name);
      		}
      	}
      
      	return 0;
      
      }
  • 相关阅读:
    指针型函数与函数型指针 -2021.08.04
    Ubuntu18.04 NAT模式下配置静态IP地址 -2020.11.09
    Linux编译内核 Ubuntu18.04 -2020.11.04
    以PING为例,利用Wireshark深入理解网络层、数据链路层的工作原理 -2020.10.30
    Ubuntu18.04虚拟机的安装
    UNIX/Linux系统中的文件属性
    【计算机四级嵌入式】内存管理
    利用预编译解决C/C++重复定义的错误 -2020.09.13
    使用镜像安装cygwin、gcc并配置CLion IDE -2020.09.12
    Android Studio 4.0.1 找不到R.java 2020.09.08
  • 原文地址:https://www.cnblogs.com/matrix77/p/3388866.html
Copyright © 2020-2023  润新知