将rgba图片信息转换为bmp图片文件

大耗子 2020年11月01日 47次浏览

将rgba图片信息转换为bmp图片文件

#include <stdio.h>
#include <windows.h>


#pragma pack(1)
//BMP文件头(14字节)
typedef struct                       /**** BMP file header structure ****/
{
	unsigned int   bfSize;           /* Size of file */
	unsigned short bfReserved1;      /* Reserved */
	unsigned short bfReserved2;      /* ... */
	unsigned int   bfOffBits;        /* Offset to bitmap data */
} MyBITMAPFILEHEADER;

//位图信息头(40字节)
typedef struct                       /**** BMP file info structure ****/
{
	unsigned int   biSize;           /* Size of info header */
	int            biWidth;          /* Width of image */
	int            biHeight;         /* Height of image */
	unsigned short biPlanes;         /* Number of color planes */
	unsigned short biBitCount;       /* Number of bits per pixel */
	unsigned int   biCompression;    /* Type of compression to use */
	unsigned int   biSizeImage;      /* Size of image data */
	int            biXPelsPerMeter;  /* X pixels per meter */
	int            biYPelsPerMeter;  /* Y pixels per meter */
	unsigned int   biClrUsed;        /* Number of colors used */
	unsigned int   biClrImportant;   /* Number of important colors */
} MyBITMAPINFOHEADER;

void MySaveBmp(const char *filename, unsigned char *rgbbuf, int width, int height)
{
	MyBITMAPFILEHEADER bfh;
	MyBITMAPINFOHEADER bih;
	/* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */
	unsigned short bfType = 0x4d42;
	bfh.bfReserved1 = 0;
	bfh.bfReserved2 = 0;
	bfh.bfSize = sizeof(MyBITMAPFILEHEADER) + sizeof(MyBITMAPINFOHEADER) + width * height * 3;
	bfh.bfOffBits = 0x36;

	bih.biSize = sizeof(MyBITMAPINFOHEADER);
	bih.biWidth = width;
	bih.biHeight = -height;
	bih.biPlanes = 1;
	bih.biBitCount = 24;
	bih.biCompression = 0;
	bih.biSizeImage = 0;
	bih.biXPelsPerMeter = 5000;
	bih.biYPelsPerMeter = 5000;
	bih.biClrUsed = 0;
	bih.biClrImportant = 0;

	FILE *file = fopen(filename, "wb");
	if (!file)
	{
		printf("Could not write file\n");
		return;
	}

	/*Write headers*/
	fwrite(&bfType, sizeof(bfType), 1, file);
	fwrite(&bfh, sizeof(bfh), 1, file);
	fwrite(&bih, sizeof(bih), 1, file);

	fwrite(rgbbuf, width*height * 3, 1, file);
	fclose(file);
}




void RGBAtoRGB(char *buf, long lenght) {
	char *prgba = buf;
	char *prgb = buf;
	for (long i = 0; i < lenght; i++, prgba++)
	{
		if ((i + 1) % 4 == 0 && i != 0)
			continue;
		*prgb++ = *prgba;
	}
}


void rgbaTobmp(const char *filename, unsigned char *rgbbuf, int width, int height)
{
	// 因为传入的是rgba,所以*4
	RGBAtoRGB((char*)rgbbuf, height*width * 4);
	MySaveBmp(filename, rgbbuf, width, height);
}

void main(int argc, char *argv[])
{
	int conf = 0;
	int devicenum = 0;
	// image.data存储的是图片的rgba信息
	FILE * file = fopen("image.data", "rb");
	fwrite(image_data, 1, k4a_image_get_size(color_image), file);
	fclose(file);
    char temp[100] = { 0 };
    sprintf(temp, "%d.bmp", count);
    rgbaTobmp(temp, (unsigned char*)image_data, width, height);

}