Problem with bmp.biBitCount =24(or)8(or)16 + vc++
Posted: 2010-03-12T05:34:30-07:00
Hello All,
I am new to ImageMagick and doing a small application using VC++.
I have a problem with loading image.
Please observe the following VC++ code once.
#if (QuantumDepth == 8 )
#define ScaleQuantumToChar(quantum) ((unsigned char) (quantum))
#elif (QuantumDepth == 16)
#define ScaleQuantumToChar(quantum) ((unsigned char) ((quantum)/257))
#elif (QuantumDepth == 32)
#define ScaleQuantumToChar(quantum) ((unsigned char) ((quantum)/16843009UL))
#elif (QuantumDepth == 64)
#define ScaleQuantumToChar(quantum) \
((unsigned char) ((quantum)/71777214294589695))
#endif
PreparedDC (Image m_Image,int x, int y)
{
HBITMAP hBitmap;
BITMAPINFO bitinfo;
memset(&bitinfo, 0, sizeof(bitinfo));
BITMAPINFOHEADER bmi;
// make sure we're getting a valid image!
if (!m_Image.isValid())
{
return;
}
// if the view is dirty, dispose the old offscreen!
if ( mViewDirty == true ) {
delete mOffscreenDC;
mOffscreenDC = NULL;
}
// if we don't already have a ready offscreen, then prepare one
if ( !mOffscreenDC ) {
//
// Set up the Windows bitmap header
//
//BITMAPINFOHEADER bmi;
bmi.biSize = sizeof(BITMAPINFOHEADER); // Size of structure
bmi.biWidth = m_Image.columns(); // Bitmaps width in pixels
bmi.biHeight = (-1)*m_Image.rows(); // Bitmaps height n pixels
bmi.biPlanes = 1; // Number of planes in the image
bmi.biBitCount = 32; // The number of bits per pixel
bmi.biCompression = BI_RGB; // The type of compression used
bmi.biSizeImage = 0; // The size of the image in bytes
bmi.biXPelsPerMeter = 0; // Horizontal resolution
bmi.biYPelsPerMeter = 0; // Veritical resolution
bmi.biClrUsed = 0; // Number of colors actually used
bmi.biClrImportant = 0; // Colors most important
mBMI = bmi; // keep it for clipboard use...
mOffscreenDC = new CDC();
RGBQUAD *prgbaDIB = 0;
hBitmap = CreateDIBSection
(
mOffscreenDC->GetSafeHdc(), // handle to device context
(BITMAPINFO *)&bmi, // pointer to structure containing bitmap size, format, and color data
DIB_RGB_COLORS, // color data type indicator: RGB values or palette indices
(void**)&prgbaDIB, // pointer to variable to receive a pointer to the bitmap's bit values
NULL, // optional handle to a file mapping object
0 // offset to the bitmap bit values within the file mapping object
);
if ( !hBitmap )
{
CString message;
message.FormatMessage("Windows failed to allocate bitmap of size %1!d!x%2!d!!",
m_Image.rows(), m_Image.columns());
return;
}
//
// If image is non-opaque, create overlay the image on top of
// a pattern background so non-opaque regions become evident.
//
/*Magick::Image image=m_Image;
if (m_Image.matte())
{
Magick::Image matteImage;
matteImage.size(Magick::Geometry(m_Image.columns(), m_Image.rows()));
matteImage.read("pattern:checkerboard");
matteImage.composite(m_Image,0,0,AtopCompositeOp);
image=matteImage;
}*/
//
// Extract the pixels from Magick++ image object and convert to a DIB section
//
const unsigned int columns = m_Image.columns();
const unsigned int rows = m_Image.rows();
RGBQUAD *pDestPixel = prgbaDIB;
for( unsigned int row = 0 ; row < rows ; row++ )
{
const PixelPacket *pPixels = m_Image.getConstPixels(0,row,columns,1);
#if QuantumDepth == 8
// Form of PixelPacket is identical to RGBQUAD when QuantumDepth==8
memcpy((void*)pDestPixel,(const void*)pPixels,sizeof(PixelPacket)*columns);
pDestPixel += columns;
#else // 16 or 32 bit Quantum
// Transfer pixels, scaling to Quantum
for( unsigned long nPixelCount = columns; nPixelCount ; nPixelCount-- )
{
pDestPixel->rgbRed = ScaleQuantumToChar(pPixels->red);
pDestPixel->rgbGreen = ScaleQuantumToChar(pPixels->green);
pDestPixel->rgbBlue = ScaleQuantumToChar(pPixels->blue);
pDestPixel->rgbReserved = 0;
++pDestPixel;
++pPixels;
}
#endif
}
// Create a display surface
mOffscreenDC->CreateCompatibleDC( NULL );
// Now copy the bitmap to device
mOffscreenDC->SelectObject( hBitmap );
DeleteObject( hBitmap );
}
mViewDirty = false; // not any more!
}
From above code..
If i entered ....bmi.biBitCount = 32 (or) 64 (or) 128...
Its working good...
Otherwise if i entered ... bmi.biBitCount = 8 (or) 16 (or) 24....
It gives runtime error.
Please Help me....
Thanks in Advance...
I am new to ImageMagick and doing a small application using VC++.
I have a problem with loading image.
Please observe the following VC++ code once.
#if (QuantumDepth == 8 )
#define ScaleQuantumToChar(quantum) ((unsigned char) (quantum))
#elif (QuantumDepth == 16)
#define ScaleQuantumToChar(quantum) ((unsigned char) ((quantum)/257))
#elif (QuantumDepth == 32)
#define ScaleQuantumToChar(quantum) ((unsigned char) ((quantum)/16843009UL))
#elif (QuantumDepth == 64)
#define ScaleQuantumToChar(quantum) \
((unsigned char) ((quantum)/71777214294589695))
#endif
PreparedDC (Image m_Image,int x, int y)
{
HBITMAP hBitmap;
BITMAPINFO bitinfo;
memset(&bitinfo, 0, sizeof(bitinfo));
BITMAPINFOHEADER bmi;
// make sure we're getting a valid image!
if (!m_Image.isValid())
{
return;
}
// if the view is dirty, dispose the old offscreen!
if ( mViewDirty == true ) {
delete mOffscreenDC;
mOffscreenDC = NULL;
}
// if we don't already have a ready offscreen, then prepare one
if ( !mOffscreenDC ) {
//
// Set up the Windows bitmap header
//
//BITMAPINFOHEADER bmi;
bmi.biSize = sizeof(BITMAPINFOHEADER); // Size of structure
bmi.biWidth = m_Image.columns(); // Bitmaps width in pixels
bmi.biHeight = (-1)*m_Image.rows(); // Bitmaps height n pixels
bmi.biPlanes = 1; // Number of planes in the image
bmi.biBitCount = 32; // The number of bits per pixel
bmi.biCompression = BI_RGB; // The type of compression used
bmi.biSizeImage = 0; // The size of the image in bytes
bmi.biXPelsPerMeter = 0; // Horizontal resolution
bmi.biYPelsPerMeter = 0; // Veritical resolution
bmi.biClrUsed = 0; // Number of colors actually used
bmi.biClrImportant = 0; // Colors most important
mBMI = bmi; // keep it for clipboard use...
mOffscreenDC = new CDC();
RGBQUAD *prgbaDIB = 0;
hBitmap = CreateDIBSection
(
mOffscreenDC->GetSafeHdc(), // handle to device context
(BITMAPINFO *)&bmi, // pointer to structure containing bitmap size, format, and color data
DIB_RGB_COLORS, // color data type indicator: RGB values or palette indices
(void**)&prgbaDIB, // pointer to variable to receive a pointer to the bitmap's bit values
NULL, // optional handle to a file mapping object
0 // offset to the bitmap bit values within the file mapping object
);
if ( !hBitmap )
{
CString message;
message.FormatMessage("Windows failed to allocate bitmap of size %1!d!x%2!d!!",
m_Image.rows(), m_Image.columns());
return;
}
//
// If image is non-opaque, create overlay the image on top of
// a pattern background so non-opaque regions become evident.
//
/*Magick::Image image=m_Image;
if (m_Image.matte())
{
Magick::Image matteImage;
matteImage.size(Magick::Geometry(m_Image.columns(), m_Image.rows()));
matteImage.read("pattern:checkerboard");
matteImage.composite(m_Image,0,0,AtopCompositeOp);
image=matteImage;
}*/
//
// Extract the pixels from Magick++ image object and convert to a DIB section
//
const unsigned int columns = m_Image.columns();
const unsigned int rows = m_Image.rows();
RGBQUAD *pDestPixel = prgbaDIB;
for( unsigned int row = 0 ; row < rows ; row++ )
{
const PixelPacket *pPixels = m_Image.getConstPixels(0,row,columns,1);
#if QuantumDepth == 8
// Form of PixelPacket is identical to RGBQUAD when QuantumDepth==8
memcpy((void*)pDestPixel,(const void*)pPixels,sizeof(PixelPacket)*columns);
pDestPixel += columns;
#else // 16 or 32 bit Quantum
// Transfer pixels, scaling to Quantum
for( unsigned long nPixelCount = columns; nPixelCount ; nPixelCount-- )
{
pDestPixel->rgbRed = ScaleQuantumToChar(pPixels->red);
pDestPixel->rgbGreen = ScaleQuantumToChar(pPixels->green);
pDestPixel->rgbBlue = ScaleQuantumToChar(pPixels->blue);
pDestPixel->rgbReserved = 0;
++pDestPixel;
++pPixels;
}
#endif
}
// Create a display surface
mOffscreenDC->CreateCompatibleDC( NULL );
// Now copy the bitmap to device
mOffscreenDC->SelectObject( hBitmap );
DeleteObject( hBitmap );
}
mViewDirty = false; // not any more!
}
From above code..
If i entered ....bmi.biBitCount = 32 (or) 64 (or) 128...
Its working good...
Otherwise if i entered ... bmi.biBitCount = 8 (or) 16 (or) 24....
It gives runtime error.
Please Help me....
Thanks in Advance...