bmp转十六进制hex样例

// $  xxd -i logo.bmp logo.h


#include <stdio.h>

//TO_DO ++++++++

unsigned char logo_bmp[] = {
	  0x42, 0x4d, 0xde, 0xc9, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00,
	  0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x86, 0x00, 0x00, 0x00, 0x22, 0x01,
	  0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	  0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0x00, 0x00,
	  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	...
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
unsigned int logo_bmp_len = 117214;


//bmp width, height
int bytes_per_bpp = 3;
int bmp_width = 134;
int bmp_height = 290;
unsigned int file_len = 117214;
unsigned char *bmp_file_data = logo_bmp;
#define REVERT_BMP  1   //revert the bmp: 1, don't revert: 0
//TO_DO  --------


/*bmp文件的前部是文件头信息,aboot中画图不需要这些
 * 1 删除文件头信息,
 * 
 * 以 24bit bmp: 134X290 为例
 * 图片每一行134个像素,每个像素3个字节,所以每一行是134*3 == 402个字节,
 * bmp会在每一行的最后添加两个字节对齐。
 * aboot中要用这个图片的话,
 * 2.要把每行最后的两个对齐字节删除
 * 
 * 每行最后的两个字节删除以后
 * 因为bmp本身的图片是倒的,
 * 3.反转整张图
 * 
 * 反转整张图以后,R和B也互换了,导致颜色不对
 * R、G、B各占一个字节,
 * 4.这里是使其中的R 和 B互换。
 * 
 * 所以,如果把拿到手的图片先在windows上做一下180度的旋转,
 * 应该可以省略步骤3和4,把上面的 REVERT_BMP 设定为0 即可。
 */
int main()
{
	unsigned int i = 0;
	unsigned int j = 0;
	unsigned int bmp_info_len = 0;
	unsigned int temp = 0;
	unsigned int actual_data_len = 0;
	unsigned char result_arry[file_len];
	unsigned char result_arry2[file_len];
	unsigned int data_bytes_per_row = (bmp_width*bytes_per_bpp);
	unsigned int total_bytes_per_row = 0;
	unsigned int null_bytes_per_row = 0;
	unsigned int array_len = 0; 
	if(data_bytes_per_row%4 != 0)
	{
		null_bytes_per_row = 4-data_bytes_per_row%4;
	}

	total_bytes_per_row = data_bytes_per_row + null_bytes_per_row;//total bytes per row
	bmp_info_len = file_len - total_bytes_per_row*bmp_height;//bmp file info len,or len to delete
	printf("file_len:---------------> %d \n",file_len);
	printf("bmp_info_len:-----------> %d \nnull_bytes_per_row:-----> %d\n",bmp_info_len,null_bytes_per_row);


	//1 删除文件头信息,
	for(i=bmp_info_len,j=0;i<file_len;i++,j++)
	{
		result_arry2[j] = bmp_file_data[i];
	}
	array_len = j;
	printf("step 1 over.\n");
	printf("data total_bytes:-------> %d \n",file_len-bmp_info_len);
	printf("total_bytes_per_row:----> %d \n",total_bytes_per_row);

	//in data <-- bmp_file_data[..]
	//2.删除每行最后的若干个字节,存于result_arry2
	for(i=1,j=0;i<=array_len;i++)
	{
		//delete null data at the end of row 
		result_arry[j] = result_arry2[i-1];
		j++;
		if(i%total_bytes_per_row==0)
		{
			j -= null_bytes_per_row;
		}

	}// out data to -->result_arry[..]
	printf("step 2 over.\n");

	array_len = j;

#if REVERT_BMP
	//3.这里是反转整张图
	for(i=0,j=0;i<array_len;i++,j++)
	{
		result_arry2[j] = result_arry[array_len-i-1];
	}
	printf("step 3 over.\n");
	//in data <-- result_arry[..]
	//4. R、G、B各占一个字节,这里是使其中的R 和 B互换。
	for(i=0,j=0;i<array_len;)
	{
		result_arry[j] = result_arry2[i+2];
		result_arry[j+1] = result_arry2[i+1];
		result_arry[j+2] = result_arry2[i];
		i += 3;
		j += 3;
	}
	printf("step 4 over.\n");
#endif
	printf("actual data array_len:---> %d \n",array_len);

	FILE* fout = fopen("resutfile.h","w+");
	if (fout == NULL) {
		printf("Failed to open\n");
		return;
	}
	for (i=0;i<array_len;)
	{
		fprintf(fout,"0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, \n",
				result_arry[i],result_arry[i+1],result_arry[i+2],result_arry[i+3],
				result_arry[i+4],result_arry[i+5],result_arry[i+6],result_arry[i+7],
				result_arry[i+8],result_arry[i+9],result_arry[i+10],result_arry[i+11]);
		i +=12;
	}

	if (fclose(fout) != 0) {
		printf("Failed to close \n");
		return;
	}


}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值