/*************** input data *********** |
***********************************************/ |
char * filename = "rgb565_800_480_woman" ; |
char * newFile = "rgb565_800_480_woman_0x7f.bmp" ; |
p = fopen (filename, "rb" ); |
printf ( "!!!file %s open failed.n" , filename); |
printf ( "file %s open success.n" ,filename); |
/*********** read Image Data **************/ |
long nData = nWidth*nHeight; |
unsigned short * rgb_buffer = malloc (nData* sizeof ( short )); |
fread (rgb_buffer,2,nData,p); |
long total = nWidth*nHeight*3; |
unsigned char * pVisit = malloc (total* sizeof ( char )); |
unsigned char * tmp = pVisit; |
G = (*rgb_buffer>>5)&0x3f; |
B = (*rgb_buffer>>11)&0x1f; |
printf ( "read file over.nData%ldn" ,nData); |
/*****************************************************/ |
/*********** write file *******************/ |
FILE *result = fopen (newFile, "wb" ); |
printf ( "open new file failed.n" ); |
RGB2BMP(tmp,nWidth,nHeight,result); |
typedef unsigned char BYTE ; |
typedef unsigned short WORD ; |
该结构的长度是固定的,为14个字节,各个域的依次如下: |
2byte :文件类型,必须是0x4d42,即字符串"BM"。 |
4byte :从文件头到实际的位图图像数据的偏移字节数。 |
该结构的长度也是固定的,为40个字节,各个域的依次说明如下: |
2Byte :表示颜色时用到的位数,常用的值为1(黑白二色图)、4(16色图)、8(256色图)、24(真彩色图)。 |
4byte :指定位图是否压缩,有效值为BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS。Windows位图可采用RLE4和RLE8的压缩格式,BI_RGB表示不压缩。 |
4byte :指定实际的位图图像数据占用的字节数,可用以下的公式计算出来: |
图像数据 = Width' * Height * 表示每个象素颜色占用的byte数(即颜色位数/8,24bit图为3,256色为1) |
要注意的是:上述公式中的biWidth'必须是4的整数倍(不是biWidth,而是大于或等于biWidth的最小4的整数倍)。 |
如果biCompression为BI_RGB,则该项可能为0。 |
4byte :本图像实际用到的颜色数,如果该值为0,则用到的颜色数为2的(颜色位数)次幂,如颜色位数为8,2^8=256,即256色的位图 |
4byte :指定本图像中重要的颜色数,如果该值为0,则认为所有的颜色都是重要的。 |
***********************************/ |
printf("infoHead Length:%dn",Length); |
printf("width&height:%d*%dn",width,height); |
printf("colorPlane:%dn",colorPlane); |
printf("bitColor:%dn",bitColor); |
printf("Compression Format:%dn",zipFormat); |
printf("Image Real Size:%dn",realSize); |
printf("Pels(X,Y):(%d,%d)n",xPels,yPels); |
printf("colorUse:%dn",colorUse); |
printf("Important Color:%dn",colorImportant); |
/*************************** |
/*************************** |
对于256色BMP位图,颜色位数为8,需要2^8 = 256个调色盘; |
对于24bitBMP位图,各象素RGB值直接保存在图像数据区,不需要调色盘,不存在调色盘区 |
************************/ |
printf("Mix Plate B,G,R:%d %d %dn",rgbBlue,rgbGreen,rgbRed); |
/**************************** |
*****************************/ |
int RGB2BMP( char *rgb_buffer, int nWidth, int nHeight, FILE *fp1) |
char bfType[2]={ 'B' , 'M' }; |
m_BMPHeader.imageSize=3*nWidth*nHeight+54; |
m_BMPHeader.startPosition=54; |
fwrite (bfType,1, sizeof (bfType),fp1); |
fwrite (&m_BMPHeader.imageSize,1, sizeof (m_BMPHeader.imageSize),fp1); |
fwrite (&m_BMPHeader.blank,1, sizeof (m_BMPHeader.blank),fp1); |
fwrite (&m_BMPHeader.startPosition,1, sizeof (m_BMPHeader.startPosition),fp1); |
InfoHead m_BMPInfoHeader; |
m_BMPInfoHeader.Length=40; |
m_BMPInfoHeader.width=nWidth; |
m_BMPInfoHeader.height=nHeight; |
m_BMPInfoHeader.colorPlane=1; |
m_BMPInfoHeader.bitColor=24; |
m_BMPInfoHeader.zipFormat=0; |
m_BMPInfoHeader.realSize=3*nWidth*nHeight; |
m_BMPInfoHeader.colorUse=0; |
m_BMPInfoHeader.colorImportant=0; |
fwrite (&m_BMPInfoHeader.Length,1, sizeof (m_BMPInfoHeader.Length),fp1); |
fwrite (&m_BMPInfoHeader.width,1, sizeof (m_BMPInfoHeader.width),fp1); |
fwrite (&m_BMPInfoHeader.height,1, sizeof (m_BMPInfoHeader.height),fp1); |
fwrite (&m_BMPInfoHeader.colorPlane,1, sizeof (m_BMPInfoHeader.colorPlane),fp1); |
fwrite (&m_BMPInfoHeader.bitColor,1, sizeof (m_BMPInfoHeader.bitColor),fp1); |
fwrite (&m_BMPInfoHeader.zipFormat,1, sizeof (m_BMPInfoHeader.zipFormat),fp1); |
fwrite (&m_BMPInfoHeader.realSize,1, sizeof (m_BMPInfoHeader.realSize),fp1); |
fwrite (&m_BMPInfoHeader.xPels,1, sizeof (m_BMPInfoHeader.xPels),fp1); |
fwrite (&m_BMPInfoHeader.yPels,1, sizeof (m_BMPInfoHeader.yPels),fp1); |
fwrite (&m_BMPInfoHeader.colorUse,1, sizeof (m_BMPInfoHeader.colorUse),fp1); |
fwrite (&m_BMPInfoHeader.colorImportant,1, sizeof (m_BMPInfoHeader.colorImportant),fp1); |
fwrite (rgb_buffer,3*nWidth*nHeight,1,fp1); |
|