24位BMP 转 16位BMP-RGB565 代码

由于需要bpp-16 的BMP文件,只是现在Window 10自带的画图程序没法直接转换出来。只好上网找下代码,去转原24-bit 的BMP出来。找了好几个程序,转出来的东西都无法显示,没办法只得自己研究下,搞了一下午,总算把这些代码的窟窿都堵上了,差不多重写了一遍,实测成功!
分享给其他有同样需求的人。

     编译: c++ -o convert_bmp  /mnt/hgfs/WIN_D/share/convert_bmp24to16.cpp

    测试:./convert_bmp  1.bmp 

SrcCode: convert_bmp24to16.cpp

/*
 *  24位深的bmp图片转换为16位深RGB565格式的bmp图片
 *  Original: from network
 *  Modified by Golden.Chen 2019/02/16
 */
#include 
#include 
#include 

using namespace std;

typedef unsigned char  uint8;
typedef unsigned short uint16; //32bit system
typedef unsigned int   uint32;//32bit system

#define SRC_BMP_BPP    24   /* bits per pixel */
#define DST_BMP_BPP    16

struct bmp_filehead //size: 14B
{
    uint16 bmtype; //0x424d ='BM'
    uint16 bmtotalsize; //total file size 
    uint16 bmtotalsizeH;
    uint16 bmReserved;//reserve word,fixed to : 0x0000
    uint16 bmReservedH; 
    uint16 bmOffBits;//offset of image data
    uint16 bmOffBitsH;
}bmphead;

struct bmp_infohead //size: 40B
{
    uint32 bmpheadsize;//size of infoheader
    uint32 bmp_width;//image width
    uint32 bmp_height;//image height
    uint16 bmplans;//planes,0 or 1
    uint16 bitcount;//bits per pixel,24
    uint32 compress_type;/* 0 - rgb, 1 - rle8, 2 - rle4, 3 - BITFEILDS */
    uint32 imagesize;//size of image data

    uint32 biXPelsPerMeter;
    uint32 biYPelsPerMeter;
    uint32 biClrUsed;
    uint32 biClrImportant;
}infohead;

int bmp565_write_header(struct bmp_filehead *pfhd,struct bmp_infohead *pinfo,FILE *fp)
{
	int ret;
	uint32 widthsize,head_size,imgdata_offset;
	uint8 rgb_mask[12] = {// RGBQUAD MASK     
        	0x00, 0xF8, 0x00, 0x00, //red mask     
 		0xE0, 0x07, 0x00, 0x00, //green mask    
		0x1F, 0x00, 0x00, 0x00  //blue mask
	}; 
	
	widthsize = (((uint32)pinfo->bmp_width * DST_BMP_BPP + 31) >> 5) << 2;
	head_size = sizeof(struct bmp_filehead) + sizeof(struct bmp_infohead) + sizeof(rgb_mask);

	/* base on src data ,initialize bmp_infoheader*/
	pinfo->bitcount = DST_BMP_BPP;
	pinfo->compress_type = 3 ; //only valid when BPP == 16 or 32
	pinfo->imagesize = pinfo->bmp_height * widthsize;
	pinfo->biXPelsPerMeter = 0;
	pinfo->biYPelsPerMeter = 0;
	pinfo->biClrUsed = 0;
	pinfo->biClrImportant = 0;

	/* base on src data ,initialize bmp_fileheader*/
	pfhd->bmtotalsize  = (head_size + pinfo->imagesize) & 0xFFFF;
	pfhd->bmtotalsizeH = (head_size + pinfo->imagesize) >> 16;
	pfhd->bmOffBits  = head_size & 0xFFFF;
	pfhd->bmOffBitsH = head_size >> 16;

	//write header data to fp
	ret = fwrite(pfhd,  1, sizeof(struct bmp_filehead), fp);
	if(ret != sizeof(struct bmp_filehead)) return -1;

	ret = fwrite(pinfo, 1, sizeof(struct bmp_infohead), fp);
	if(ret != sizeof(struct bmp_infohead)) return -1;

	ret = fwrite(rgb_mask, 1, sizeof(rgb_mask), fp);
	if(ret != sizeof(rgb_mask)) return -1;

	return 0;
}

#define bmp565_write_imgdata(buf,block,count,fp)  \
	fwrite(buf, block, count, fp);

int main(int argc, char *argv[])
{
	FILE *src_fp,*dst_fp;
	char src_file[64] = "";
	char dst_file[64] = "";
	uint8 *imagebuf=NULL,*bmpwidbuf=NULL;
	uint32 src_widsize,dst_widsize;
	uint16 k,q,d;
	int ret=0,n;

	if(argc < 2)
	{
		printf("\nUsage: %s  \n",argv[0]);
		printf("\n       %s   \n",argv[0]);
		return -1;
	}

	strcpy(src_file, argv[1]);
	if(argc == 3) {
		strcpy(dst_file, argv[2]);
		cout << "dst file name : " << dst_file << endl;
	} else {
		strncpy(dst_file, src_file, strlen(src_file)-4);
		strcat(dst_file, "_rgb565.bmp");
		cout << "dst file default name : " << dst_file << endl;
	}


	if((src_fp=fopen(src_file,"r")) == NULL) {
		cout<<"Can't Open file: "<< src_file <>5)<<2;
	if((bmpwidbuf=(uint8 *)malloc(src_widsize)) == NULL) {
		cout << "Fail to malloc bmpwidbuf!"<>5)<<2;
	if((imagebuf=(uint8 *)malloc(dst_widsize * infohead.bmp_height)) == NULL) {
		cout << "Fail to malloc imagebuf!"<>3)<<11)//R G B
			+((uint16)(bmpwidbuf[3*q + 1]>>2)<<5)
			+ (uint16)(bmpwidbuf[3*q + 0]>>3);
		    imagebuf[n++] = d & 0xff;
		    imagebuf[n++] = d >> 8;
		}
		while(n%dst_widsize != 0) imagebuf[n++] = 0x00;
	}

	fclose(src_fp);
	free(bmpwidbuf);

	cout<<"write bmp 565 start !"<

 

你可能感兴趣的:(uboot移植)