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

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

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;

 

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值