www.pudn.com > bmp2include.rar > bmp2include.cpp


// bmp2include.cpp : Defines the entry point for the console application. 
// 
 
#include "stdafx.h" 
#include "stdlib.h" 
#include "stdio.h" 
 
#define UINT16 unsigned short 
#define UINT32 unsigned int 
#define UINT8  unsigned char 
#pragma pack(1) 
struct BMP_HEADER_TAG{ 
UINT16		signature;			// signature, must be 4D42 hex  
UINT32		bmp_size;			// size of BMP file in bytes (unreliable) 
UINT16		reserved[2];		// reserved, must be zero 
UINT32		offset;				// offset to start of image data in bytes 
UINT32		header_size;		// size of BITMAPINFOHEADER structure, must be 40 
UINT32		bmp_width;			// image width in pixels 
UINT32		bmp_height;			// image height in pixels 
UINT16		plane_num;			// number of planes in the image, must be 1 
UINT16		bit_deep;			// number of bits per pixel (1, 4, 8, or 24) 
UINT32		compress_type;		// compression type (0=none, 1=RLE-8, 2=RLE-4) 
UINT32		image_size;			// size of image data in bytes (including padding) 
UINT32		hor_res;			// horizontal resolution in pixels per meter (unreliable) 
UINT32		vert_res;			// vertical resolution in pixels per meter (unreliable) 
UINT32		color_num;			// number of colors in image, or zero 
UINT32		imp_color_num;		// number of important colors, or zero 
}; 
typedef struct BMP_HEADER_TAG BMP_HEADER; 
#pragma pack() 
 
void usage(void) 
{ 
	printf("Usage:bmp2include source destinate\n"); 
	return; 
} 
void convert(unsigned char *pimage,int width,int height,unsigned char *pbuf) 
{ 
int i,j; 
int pimage_width = (width-1)/8+1; 
	for(i=0;i=0;i--){ 
		for(j=0;j<(int)((bmpheader.bmp_width-1)/8+1);j++){ 
			fprintf(wfd,"0x%02x,",tbuf[i*((bmpheader.bmp_width-1)/8+1)+j]); 
		} 
		fprintf(wfd,"\\\n"); 
	} 
	fprintf(wfd,"0x00\n};"); 
	fclose(rfd); 
	fclose(wfd); 
// test code for covert function 
	convert(tbuf,bmpheader.bmp_width,bmpheader.bmp_height,pbuf); 
	wfd2 = fopen("test_new.bmp","wb"); 
	if(wfd2==NULL){ 
		printf("open file test_new.bmp failed\n"); 
		return -1; 
	} 
	// write bmp header 
	byte_write = fwrite(&bmpheader,1,sizeof(bmpheader),wfd2); 
	if(byte_write!=sizeof(bmpheader)){ 
		printf("write file test_new.bmp bmp header failed\n"); 
		return -1; 
	} 
	// write index pallete 
	byte_write = fwrite(pallete,1,sizeof(pallete),wfd2); 
	if(byte_write!=sizeof(pallete)){ 
		printf("write file test_new.bmp bmp pallete failed\n"); 
		return -1; 
	} 
 
	// write pixel data 
	byte_write = fwrite(pbuf,1,bmpheader.bmp_height*bmpheader.bmp_width,wfd2); 
	if(byte_write!=(int)(bmpheader.bmp_height*bmpheader.bmp_width)){ 
		printf("write file test_new.bmp bmp pixel data failed\n"); 
		return -1; 
	} 
	return 0; 
}