如何创建一个1bpp位图 [英] How to create a 1bpp Bitmap
问题描述
大家好,
我正在尝试开发一个使用VS2008(vc ++)将像素信息保存为位图文件的应用程序.目前,我有一个包含像素数据的字节缓冲区,每个像素格式为1位.图像的宽度和列为512 x512.我使用Lockbits()方法创建位图数据,并使用Bitmap :: Save()方法将其保存在bmp文件中.但是问题在于,从位图看来,一些数据已经丢失并且图像不清楚.
您能帮我创建一个1bpp位图吗?
我使用的代码是:
Hi All,
I''m trying to develop an application that saves a pixel information as bitmap file using VS2008 ( vc++ ). Currently, i have a byte buffer containing pixel data, which is in 1 bit per pixel format. The width and column of the image is 512 x 512. I used Lockbits() method to create the bitmap data and used Bitmap::Save() method to save it in a bmp file. But the problem is that, from the bitmap it seems that some data has been lost and image is not clear.
Could you please help me to create a 1bpp Bitmap ?
The code i used is:
<br />
int nNumComps = pstImageInfo_i->usBitsAllocated;<br />
short sSamplesPerPixel = 1;<br />
int nWidth = pstImageInfo_i->usColumns;<br />
int nHeight = pstImageInfo_i->usRows; <br />
int nFormat = PixelFormat1bppIndexed;<br />
int nNumColors = 2;<br />
ColorPalette* pPalette = 0;<br />
pPalette = reinterpret_cast< ColorPalette* >( malloc( sizeof( ColorPalette ) + <br />
( nNumColors - 1 )<br />
* sizeof( ARGB )));<br />
if( 0 != pPalette )<br />
{<br />
pPalette->Flags = PaletteFlagsGrayScale;<br />
pPalette->Count = nNumColors;<br />
Color clr( 255, 0, 0, 0 );<br />
pPalette->Entries[0] = clr.GetValue();<br />
Color clrWhite( 255, 255, 255, 255 );<br />
pPalette->Entries[1] = clrWhite.GetValue();<br />
}<br />
Bitmap bmpOverlay( nWidth, nHeight, nFormat );<br />
BitmapData btmpData;<br />
ZeroMemory( &btmpData, sizeof( btmpData ));<br />
Rect BMPRect( 0, 0, nWidth, nHeight );<br />
bmpOverlay.LockBits( &BMPRect, ImageLockModeWrite,<br />
nFormat, &btmpData );<br />
//btmpData.Stride = -btmpData.Stride;<br />
int nSize = pstImageInfo_i->dwBufferSize;<br />
// Considering only 1 frame<br />
if( pstImageInfo_i->usFramesInOverlay > 1 )<br />
{<br />
nSize = nSize / pstImageInfo_i->usFramesInOverlay;<br />
}<br />
memcpy( btmpData.Scan0, pstImageInfo_i->pbData,<br />
nSize );<br />
bmpOverlay.UnlockBits( &btmpData );<br />
Status ReturnStatus = bmpOverlay.RotateFlip( RotateNoneFlipY );<br />
if( Ok == ReturnStatus )<br />
{<br />
if( pPalette )<br />
{<br />
ReturnStatus = bmpOverlay.SetPalette( pPalette );<br />
delete pPalette;<br />
pPalette = 0;<br />
}<br />
}<br />
CLSID bmpClsid;<br />
GetEncoderClsid(L"image/bmp", &bmpClsid);<br />
bmpOverlay.Save( _T( "ImageOverlay.bmp" ), &bmpClsid, NULL );<br />
在此先感谢,
Aneev
Thanks in Advance,
Aneev
推荐答案
但是问题是,从位图,似乎有些数据已丢失并且图像不清楚.
But the problem is that, from the bitmap it seems that some data has been lost and image is not clear.
我假设您将其另存为jpg. jpg文件格式可以做到这一点.
I assume you saved it as a jpg. The jpg file format does that.
显示用于创建&的代码.保存BMP.通常,用于减少BPP的功能会使图像抖动,这很可能是您解释为丢失的数据".
Show the code you used to create & save the BMP. It is common that the functions used to reduce the BPP will dither the image, which is likely what you are interpreting as "lost data".
我将创建并保存位图,如下所示: ):
I would create and save the bitmap as following :) :
...
HBITMAP hBMP = CreateDIBSection(..);
...
CImage cImage;
cImage.Attach(hBMP);
cImage.Save(..);
...
这篇关于如何创建一个1bpp位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!