zoukankan      html  css  js  c++  java
  • 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;
      
      }
  • 相关阅读:
    Android开发 使用 adb logcat 显示 Android 日志
    【嵌入式开发】向开发板中烧写Linux系统-型号S3C6410
    C语言 结构体相关 函数 指针 数组
    C语言 命令行参数 函数指针 gdb调试
    C语言 指针数组 多维数组
    Ubuntu 基础操作 基础命令 热键 man手册使用 关机 重启等命令使用
    C语言 内存分配 地址 指针 数组 参数 实例解析
    CRT 环境变量注意事项
    hadoop 输出文件 key val 分隔符
    com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Too many connections
  • 原文地址:https://www.cnblogs.com/matrix77/p/3388866.html
Copyright © 2011-2022 走看看