+ //
+ // Calc the number of DIBs and heights of DIBs
+ //
+ int nNumDIB = 1;
+ int nHRemain = 0;
+ int nHeight = nSizeLimit / nBytePerLine;
+
+ if (nHeight >= nBmpHeight)
+ nHeight = nBmpHeight;
+ else
+ {
+ nNumDIB = nBmpHeight / nHeight;
+ nHRemain = nBmpHeight % nHeight;
+ if (nHRemain > 0)
+ nNumDIB++;
+ }
+
+ //
+ // Set bitmap parameters
+ //
+ wxCHECK_MSG(rImage.Ok(), false, wxT("invalid image"));
+ SetWidth(nWidth);
+ SetHeight(nBmpHeight);
+ if (nDepth == 1)
+ m_bIsMono = true;
+ else
+ m_bIsMono = false;
+ if (nDepth == -1)
+ nDepth = wxDisplayDepth();
+ SetDepth(nDepth);
+
+#if wxUSE_PALETTE
+ //
+ // Copy the palette from the source image
+ //
+ SetPalette(rImage.GetPalette());
+#endif // wxUSE_PALETTE
+
+ //
+ // Create a DIB header
+ //
+ BITMAPINFOHEADER2 vHeader;
+ BITMAPINFO2 vInfo;
+
+ //
+ // Fill in the DIB header
+ //
+ memset(&vHeader, '\0', 16);
+ vHeader.cbFix = 16;
+ vHeader.cx = (ULONG)nWidth;
+ vHeader.cy = (ULONG)nHeight;
+ vHeader.cPlanes = 1L;
+ vHeader.cBitCount = 24;
+
+ //
+ // Memory for DIB data
+ //
+ unsigned char* pucBits;
+
+ pucBits = (unsigned char *)malloc(nBytePerLine * nHeight);
+ if(!pucBits)
+ {
+ wxFAIL_MSG(wxT("could not allocate memory for DIB"));
+ return false;
+ }
+ memset(pucBits, '\0', (nBytePerLine * nHeight));
+
+ //
+ // Create and set the device-dependent bitmap
+ //
+ DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
+ SIZEL vSize = {0, 0};
+ HDC hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
+ HPS hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIA_ASSOC);
+ LONG lScans;
+ HDC hDCScreen = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
+ HPS hPSScreen;
+ HBITMAP hBmp;
+ HBITMAP hBmpOld;
+
+ memset(&vInfo, '\0', 16);
+ vInfo.cbFix = 16;
+ vInfo.cx = (ULONG)nWidth;
+ vInfo.cy = (ULONG)nHeight;
+ vInfo.cPlanes = 1;
+ vInfo.cBitCount = 24; // Set to desired count going in
+
+ hBmp = ::GpiCreateBitmap( hPS
+ ,&vHeader
+ ,0L
+ ,NULL
+ ,NULL
+ );
+#if wxUSE_PALETTE
+ HPAL hOldPalette = NULLHANDLE;
+ if (rImage.GetPalette().Ok())
+ {
+ hOldPalette = ::GpiSelectPalette(hPS, (HPAL)rImage.GetPalette().GetHPALETTE());
+ }
+#endif // wxUSE_PALETTE
+
+ //
+ // Copy image data into DIB data and then into DDB (in a loop)
+ //
+ unsigned char* pData = rImage.GetData();
+ int i;
+ int j;
+ int n;
+ int nOrigin = 0;
+ unsigned char* ptdata = pData;
+ unsigned char* ptbits;
+
+ if ((hBmpOld = ::GpiSetBitmap(hPS, hBmp)) == HBM_ERROR)
+ {
+ ERRORID vError;
+ wxString sError;
+
+ vError = ::WinGetLastError(vHabmain);
+ sError = wxPMErrorToStr(vError);
+ }
+ for (n = 0; n < nNumDIB; n++)
+ {
+ if (nNumDIB > 1 && n == nNumDIB - 1 && nHRemain > 0)
+ {
+ //
+ // Redefine height and size of the (possibly) last smaller DIB
+ // memory is not reallocated
+ //
+ nHeight = nHRemain;
+ vHeader.cy = (DWORD)(nHeight);
+ vHeader.cbImage = nBytePerLine * nHeight;
+ }
+ ptbits = pucBits;
+ for (j = 0; j < nHeight; j++)
+ {
+ for (i = 0; i < nWidth; i++)
+ {
+ *(ptbits++) = *(ptdata + 2);
+ *(ptbits++) = *(ptdata + 1);
+ *(ptbits++) = *(ptdata);
+ ptdata += 3;
+ }
+ for (i = 0; i < nPadding; i++)
+ *(ptbits++) = 0;
+ }
+
+ //
+ // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
+ // in combination with setting the bits into the selected bitmap
+ //
+ if ((lScans = ::GpiSetBitmapBits( hPS
+ ,0 // Start at the bottom
+ ,(LONG)nHeight // One line per scan
+ ,(PBYTE)pucBits
+ ,&vInfo
+ )) == GPI_ALTERROR)
+ {
+ ERRORID vError;
+ wxString sError;
+
+ vError = ::WinGetLastError(vHabmain);
+ sError = wxPMErrorToStr(vError);
+ }
+ hPSScreen = ::GpiCreatePS( vHabmain
+ ,hDCScreen
+ ,&vSize
+ ,PU_PELS | GPIA_ASSOC
+ );
+
+ POINTL vPoint[4] = { {0, nOrigin},
+ {nWidth, nHeight},
+ {0, 0}, {nWidth, nHeight}
+ };
+
+
+ ::GpiBitBlt( hPSScreen
+ ,hPS
+ ,4
+ ,vPoint
+ ,ROP_SRCCOPY
+ ,BBO_IGNORE
+ );
+ ::GpiDestroyPS(hPSScreen);
+ nOrigin += nHeight;
+ }
+ SetHBITMAP((WXHBITMAP)hBmp);
+#if wxUSE_PALETTE
+ if (hOldPalette)
+ ::GpiSelectPalette(hPS, hOldPalette);
+#endif // wxUSE_PALETTE
+
+ //
+ // Similarly, created an mono-bitmap for the possible mask
+ //
+ if (rImage.HasMask())
+ {
+ vHeader.cbFix = 16;
+ vHeader.cx = nWidth;
+ vHeader.cy = nHeight;
+ vHeader.cPlanes = 1;
+ vHeader.cBitCount = 24;
+ hBmp = ::GpiCreateBitmap( hPS
+ ,&vHeader
+ ,0L
+ ,NULL
+ ,NULL
+ );
+ hBmpOld = ::GpiSetBitmap(hPS, hBmp);
+ if (nNumDIB == 1)
+ nHeight = nBmpHeight;
+ else
+ nHeight = nSizeLimit / nBytePerLine;
+ vHeader.cy = (DWORD)(nHeight);
+ nOrigin = 0;
+
+ unsigned char cRed = rImage.GetMaskRed();
+ unsigned char cGreen = rImage.GetMaskGreen();
+ unsigned char cBlue = rImage.GetMaskBlue();
+ unsigned char cZero = 0;
+ unsigned char cOne = 255;
+
+ ptdata = pData;
+ for (n = 0; n < nNumDIB; n++)
+ {
+ if (nNumDIB > 1 && n == nNumDIB - 1 && nHRemain > 0)
+ {
+ //
+ // Redefine height and size of the (possibly) last smaller DIB
+ // memory is not reallocated
+ //
+ nHeight = nHRemain;
+ vHeader.cy = (DWORD)(nHeight);
+ vHeader.cbImage = nBytePerLine * nHeight;
+ }
+ ptbits = pucBits;
+ for (int j = 0; j < nHeight; j++)
+ {
+ for (i = 0; i < nWidth; i++)
+ {
+ unsigned char cRedImage = (*(ptdata++)) ;
+ unsigned char cGreenImage = (*(ptdata++)) ;
+ unsigned char cBlueImage = (*(ptdata++)) ;
+
+ if ((cRedImage != cRed) || (cGreenImage != cGreen) || (cBlueImage != cBlue))
+ {
+ *(ptbits++) = cOne;
+ *(ptbits++) = cOne;
+ *(ptbits++) = cOne;
+ }
+ else
+ {
+ *(ptbits++) = cZero;
+ *(ptbits++) = cZero;
+ *(ptbits++) = cZero;
+ }
+ }
+ for (i = 0; i < nPadding; i++)
+ *(ptbits++) = cZero;
+ }
+ lScans = ::GpiSetBitmapBits( hPS
+ ,0 // Start at the bottom
+ ,(LONG)nHeight // One line per scan
+ ,(PBYTE)pucBits
+ ,&vInfo
+ );
+ hPSScreen = ::GpiCreatePS( vHabmain
+ ,hDCScreen
+ ,&vSize
+ ,PU_PELS | GPIA_ASSOC
+ );
+ POINTL vPoint2[4] = { {0, nOrigin},
+ {nWidth, nHeight},
+ {0, 0}, {nWidth, nHeight}
+ };
+ ::GpiBitBlt( hPSScreen
+ ,hPS
+ ,4
+ ,vPoint2
+ ,ROP_SRCCOPY
+ ,BBO_IGNORE
+ );
+ ::GpiDestroyPS(hPSScreen);
+ nOrigin += nHeight;
+ }
+
+ //
+ // Create a wxMask object
+ //
+ wxMask* pMask = new wxMask();
+
+ pMask->SetMaskBitmap((WXHBITMAP)hBmp);
+ SetMask(pMask);
+ hBmpOld = ::GpiSetBitmap(hPS, hBmpOld);
+ }
+
+ //
+ // Free allocated resources
+ //
+ ::GpiSetBitmap(hPS, NULLHANDLE);
+ ::GpiDestroyPS(hPS);
+ ::DevCloseDC(hDCScreen);
+ ::DevCloseDC(hDC);
+ free(pucBits);
+ return true;
+} // end of wxBitmap::CreateFromImage
+
+wxImage wxBitmap::ConvertToImage() const
+{
+ wxImage vImage;
+ wxDC* pDC;
+
+ wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
+
+ //
+ // Create an wxImage object
+ //
+ int nWidth = GetWidth();
+ int nHeight = GetHeight();
+ int nDevWidth;
+ int nDevHeight;
+ int nBytePerLine = nWidth * 3;
+ int nSizeDWORD = sizeof(DWORD);
+ int nLineBoundary = nBytePerLine % nSizeDWORD;
+ int nPadding = 0;
+ unsigned char* pData;
+ unsigned char* lpBits;
+ long lScans;
+ BITMAPINFOHEADER2 vDIBh;
+ BITMAPINFO2 vDIBInfo;
+ HPS hPSMem;
+ HBITMAP hBitmap;
+ HBITMAP hOldBitmap;
+ DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
+ SIZEL vSizlPage = {0,0};
+ HDC hDCMem = NULLHANDLE;
+
+ vImage.Create( nWidth
+ ,nHeight
+ );
+ pData = vImage.GetData();
+ if(!pData)
+ {
+ wxFAIL_MSG( wxT("could not allocate data for image") );
+ return wxNullImage;
+ }
+ if(nLineBoundary > 0)
+ {
+ nPadding = nSizeDWORD - nLineBoundary;
+ nBytePerLine += nPadding;
+ }
+ wxDisplaySize( &nDevWidth
+ ,&nDevHeight
+ );
+ //
+ // Create and fill a DIB header
+ //
+ memset(&vDIBh, '\0', 16);
+ vDIBh.cbFix = 16;
+ vDIBh.cx = nWidth;
+ vDIBh.cy = nHeight;
+ vDIBh.cPlanes = 1;
+ vDIBh.cBitCount = 24;
+
+ memset(&vDIBInfo, '\0', 16);
+ vDIBInfo.cbFix = 16;
+ vDIBInfo.cx = nWidth;
+ vDIBInfo.cy = nHeight;
+ vDIBInfo.cPlanes = 1;
+ vDIBInfo.cBitCount = 24;
+
+ lpBits = (unsigned char *)malloc(nBytePerLine * nHeight);
+ if (!lpBits)
+ {
+ wxFAIL_MSG(wxT("could not allocate data for DIB"));
+ free(pData);
+ return wxNullImage;
+ }
+ memset(lpBits, '\0', (nBytePerLine * nHeight));
+ hBitmap = (HBITMAP)GetHBITMAP();
+
+ //
+ // May already be selected into a PS
+ //
+ pDC = GetSelectedInto();
+ const wxPMDCImpl *impl;
+ if (pDC != NULL &&
+ (impl = wxDynamicCast( pDC->GetImpl(), wxPMDCImpl )) != NULL)
+ {
+ hPSMem = impl->GetHPS();
+ }
+ else
+ {
+ hDCMem = ::DevOpenDC( vHabmain
+ ,OD_MEMORY
+ ,"*"
+ ,5L
+ ,(PDEVOPENDATA)&vDop
+ ,NULLHANDLE
+ );
+ hPSMem = ::GpiCreatePS( vHabmain
+ ,hDCMem
+ ,&vSizlPage
+ ,PU_PELS | GPIA_ASSOC
+ );
+ }
+ if ((hOldBitmap = ::GpiSetBitmap(hPSMem, hBitmap)) == HBM_ERROR)
+ {
+ ERRORID vError;
+ wxString sError;
+
+ vError = ::WinGetLastError(vHabmain);
+ sError = wxPMErrorToStr(vError);
+ }
+
+ //
+ // Copy data from the device-dependent bitmap to the DIB
+ //
+ if ((lScans = ::GpiQueryBitmapBits( hPSMem
+ ,0L
+ ,(LONG)nHeight
+ ,(PBYTE)lpBits
+ ,&vDIBInfo
+ )) == GPI_ALTERROR)
+ {
+ ERRORID vError;
+ wxString sError;
+
+ vError = ::WinGetLastError(vHabmain);
+ sError = wxPMErrorToStr(vError);
+ }
+
+ //
+ // Copy DIB data into the wxImage object
+ //
+ int i;
+ int j;
+ unsigned char* ptdata = pData;
+ unsigned char* ptbits = lpBits;
+
+ for (i = 0; i < nHeight; i++)
+ {
+ for (j = 0; j < nWidth; j++)
+ {
+ *(ptdata++) = *(ptbits+2);
+ *(ptdata++) = *(ptbits+1);
+ *(ptdata++) = *(ptbits );
+ ptbits += 3;
+ }
+ ptbits += nPadding;
+ }
+ if ((pDC = GetSelectedInto()) == NULL)
+ {
+ ::GpiSetBitmap(hPSMem, NULLHANDLE);
+ ::GpiDestroyPS(hPSMem);
+ ::DevCloseDC(hDCMem);
+ }