原创 VC根据位图的宽、高、位数来建立BMP文件头和信息头

2009-11-1 20:00 3431 9 9 分类: 工程师职场

VC根据位图的宽、高、位数来建立BMP文件头和信息头<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


2009.9


程序如下:


///////////////////////////////////////////////////////////////////////////////////////////////////////


BOOL  myCreateBitmap2( int w, int h,int pixbit,


PBITMAPFILEHEADER &outheadbuf,long *outheadsize,


PBITMAPINFO &outinfobuf,long *outinfosize)


//我:根据参数,来建立文件头、信息头,含调色板空间,但没有调色板数据


//pixbit 是自定义的每像素BIT  (如果为0,则按HBITMAP里的不变)


//输入


//输出文件头指针,文件头字节数,信息指针,信息字节数


//函数会GlobalAlloc申请内存,主程序不用申请内存,但要GlobalFree掉内存


//成功时返回TRUE


{


WORD cClrBits;


DWORD my_biClrUsed=0;


outinfobuf=NULL;


outheadbuf=NULL;


*outheadsize=0;


*outinfosize=0;


if(w<0 || h<0 || pixbit<0 ) return FALSE;


cClrBits=(WORD)pixbit;


 if (cClrBits <= 1)        cClrBits = 1;


 else if (cClrBits <= 4)   cClrBits = 4;


 else if (cClrBits <= 8)   cClrBits = 8;


 else if (cClrBits <= 16)  cClrBits = 16;


 else if (cClrBits <= 24)  cClrBits = 24;


 else                      cClrBits = 32;


 // Allocate memory for the BITMAPINFO structure. (This structure


 // contains a BITMAPINFOHEADER structure and an array of RGBQUAD


 // data structures.)


  if (cClrBits != 24)


       {


   *outinfosize= sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1<< cClrBits);


   outinfobuf = (PBITMAPINFO) GlobalAlloc (GPTR,   *outinfosize);


                                 //分配内存大小,信息头+调色板大小


    // There is no RGBQUAD array for the 24-bit-per-pixel format.


       }


  else //24BIT的,没有调色板


       {


   *outinfosize= sizeof(BITMAPINFOHEADER);


   outinfobuf = (PBITMAPINFO) GlobalAlloc (GPTR,  *outinfosize);


}


if(outinfobuf==NULL) return FALSE;


 // Initialize the fields in the BITMAPINFO structure.


 outinfobuf->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  //信息头大小(不含调色板)


 outinfobuf->bmiHeader.biWidth = w;


 outinfobuf->bmiHeader.biHeight = h;


 outinfobuf->bmiHeader.biPlanes = 1;


 outinfobuf->bmiHeader.biBitCount = cClrBits;


 if (cClrBits < 24) 


 {


  my_biClrUsed=(1<<cClrBits); 


  outinfobuf->bmiHeader.biClrUsed = my_biClrUsed;


 }    //位图实际使用的彩色表中的颜色索引数


 // If the bitmap is not compressed, set the BI_RGB flag.


 outinfobuf->bmiHeader.biCompression = BI_RGB;


 // Compute the number of bytes in the array of color


 // indices and store the result in biSizeImage.


 // For Windows NT/2000, the width must be DWORD aligned unless


 // the bitmap is RLE compressed. This example shows this.


 // For Windows 95/98, the width must be WORD aligned unless the


 // bitmap is RLE compressed.


 outinfobuf->bmiHeader.biSizeImage = ((outinfobuf->bmiHeader.biWidth * cClrBits +31) & ~31)\


         /8 * outinfobuf->bmiHeader.biHeight;


        //图像大小


 // Set biClrImportant to 0, indicating that all of the


 // device colors are important.


  outinfobuf->bmiHeader.biClrImportant = 0;


 


/////////////////////////////////得到文件头


*outheadsize= sizeof(BITMAPFILEHEADER);


outheadbuf = (PBITMAPFILEHEADER) GlobalAlloc(GPTR,  *outheadsize);


//根据位图大小分配内存


if (!outheadbuf)    goto errout;


 outheadbuf->bfType = 0x4d42;  // 0x42 = "B" 0x4d = "M"


       // Compute the size of the entire file.


 outheadbuf->bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + \


                          outinfobuf->bmiHeader.biSize +\


                          my_biClrUsed * sizeof(RGBQUAD) +\


                          outinfobuf->bmiHeader.biSizeImage);


 outheadbuf->bfReserved1 = 0;


 outheadbuf->bfReserved2 = 0;


      // Compute the offset to the array of color indices.


 outheadbuf->bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +


                    outinfobuf->bmiHeader.biSize +\


                    my_biClrUsed * sizeof (RGBQUAD);


return TRUE;


 


//////////////////////错误处理


errout:


if(outinfobuf) GlobalFree(outinfobuf);


if(outheadbuf) GlobalFree(outheadbuf);


outinfobuf=NULL;


outheadbuf=NULL;


*outheadsize=0;


*outinfosize=0;


return FALSE;


 


}


 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
9
关闭 站长推荐上一条 /3 下一条