bmp图片转16进制数据序列(更新)

上一篇,http://blog.csdn.net/jorney_dong/article/details/17411057 写了用来处理bmp数据的一个小算法,只是发现是不完善的。

这里再更新一下,之前的就保留在那吧。

那篇小文提到,做完前两个步骤以后,图片是颠倒的,先在windows上旋转以后再来处理。其实,windows旋转以后,左右又颠倒了。

说到底,还是bmp图片存储数据的方式的问题。我们把bmp图片存储的数据看成一个二维数组,第一行的数据对应的就是我们看到图片的最后一行,第二行,对应图片的倒数第二行,依次,最后一行,就对应看到图片的第一行。这里要注意的是,左右是不颠倒的。

所以我们在处理完上篇提到的前两步以后,只要再把各行换一下次序就可以了。

下面是完整的代码,用法和上一篇一样:

main.c

#include <stdio.h>

//TO_DO ++++++++

#include "test1.h"

//bmp width, height
int bytes_per_bpp = 3;
int bmp_width = 73;
int bmp_height = 73;
unsigned int file_len = 16114;
unsigned char *bmp_file_data = test1_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.反转整张图,把最后一行,写到第一行,
* 第一行,写到最后一行,但是左右不要反。
* 
*/
int main()
{
	unsigned int i = 0;
	unsigned int j = 0;
	 int off = 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 char *result_arry3 = NULL;
	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;
	printf("array_len: %d \n",array_len);
	
	#if REVERT_BMP
	//3.这里是反转整张图
	for(i=array_len - data_bytes_per_row,j=0; i>=0; )
	{
		for( off = 0;off<data_bytes_per_row;off++,j++)
		{
			result_arry2[j] = result_arry[i+off];
		}

		if(i==0)
		{
		break;
		}
		i -= data_bytes_per_row;
	}
	result_arry3 = result_arry2;
	printf("steps 3 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_arry3[i],result_arry3[i+1],result_arry3[i+2],result_arry3[i+3],
		               result_arry3[i+4],result_arry3[i+5],result_arry3[i+6],result_arry3[i+7],
		              result_arry3[i+8],result_arry3[i+9],result_arry3[i+10],result_arry3[i+11]);
		              i +=12;
	}

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

	
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值