#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)) #endifCTestMagickMFCView::CTestMagickMFCView() : mOffscreenDC(NULL), m_pImage(NULL) { } CTestMagickMFCView::~CTestMagickMFCView() { if (mOffscreenDC) { delete mOffscreenDC; } } void CTestMagickMFCView::OnDraw(CDC* pDC) { CTestMagickMFCDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; DoDisplayImage(pDC); } void CTestMagickMFCView::OnInitialUpdate() { CView::OnInitialUpdate(); m_pImage.read("E:\\MyPrivate\\1.jpg"); } void CTestMagickMFCView::DoDisplayImage(CDC* pDC) { if (!m_pImage.isValid()) { return; } if (!pDC) { return; } // 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 = (LONG) m_pImage.columns(); // Bitmaps width in pixels bmi.biHeight = (-1)*(LONG) m_pImage.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
RGBQUAD *prgbaDIB = 0; HBITMAP hBitmap; hBitmap = CreateDIBSection ( pDC->m_hDC, // 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) { 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_pImage; if (m_pImage.matte()) { Magick::Image matteImage; matteImage.size(Magick::Geometry(m_pImage.columns(), m_pImage.rows())); matteImage.read("pattern:checkerboard"); matteImage.composite(m_pImage,0,0,Magick::AtopCompositeOp); image = matteImage; } // // Extract the pixels from Magick++ image object and convert to a DIB section //
const unsigned int columns = (unsigned int)image.columns(); const unsigned int rows = (unsigned int)image.rows(); RGBQUAD *pDestPixel = prgbaDIB; for( unsigned int row = 0 ; row < rows ; row++ ) { const Magick::PixelPacket *pPixels = 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 = new CDC(); mOffscreenDC->CreateCompatibleDC(pDC); // Now copy the bitmap to device mOffscreenDC->SelectObject(hBitmap); DeleteObject(hBitmap); } pDC->BitBlt(0, 0, (unsigned int)m_pImage.columns(), (unsigned int)m_pImage.rows(), mOffscreenDC, 0, 0, SRCCOPY); } |