+ //
+ // 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;
+
+ 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
+ //
+ if ((pDC = GetSelectedInto()) != NULL)
+ {
+ hPSMem = pDC->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);
+ }
+
+ //
+ // Similarly, set data according to the possible mask bitmap
+ //
+ if (GetMask() && GetMask()->GetMaskBitmap())
+ {
+ hBitmap = (HBITMAP)GetMask()->GetMaskBitmap();
+
+ //
+ // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
+ //
+ HDC hMemDC = ::DevOpenDC( vHabmain
+ ,OD_MEMORY
+ ,"*"
+ ,5L
+ ,(PDEVOPENDATA)&vDop
+ ,NULLHANDLE
+ );
+ HPS hMemPS = ::GpiCreatePS( vHabmain
+ ,hMemDC
+ ,&vSizlPage
+ ,PU_PELS | GPIA_ASSOC
+ );
+ ::GpiSetColor(hMemPS, OS2RGB(0, 0, 0));
+ ::GpiSetBackColor(hMemPS, OS2RGB(255, 255, 255) );
+ ::GpiSetBitmap(hMemPS, hBitmap);
+ ::GpiQueryBitmapBits( hPSMem
+ ,0L
+ ,(LONG)nHeight
+ ,(PBYTE)lpBits
+ ,&vDIBInfo
+ );
+ ::GpiSetBitmap(hMemPS, NULLHANDLE);
+ ::GpiDestroyPS(hMemPS);
+ ::DevCloseDC(hMemDC);
+
+ //
+ // Background color set to RGB(16,16,16) in consistent with wxGTK
+ //
+ unsigned char ucRed = 16;
+ unsigned char ucGreen = 16;
+ unsigned char ucBlue = 16;
+
+ ptdata = pData;
+ ptbits = lpBits;
+ for (i = 0; i < nHeight; i++)
+ {
+ for (j = 0; j < nWidth; j++)
+ {
+ if (*ptbits != 0)
+ ptdata += 3;
+ else
+ {
+ *(ptdata++) = ucRed;
+ *(ptdata++) = ucGreen;
+ *(ptdata++) = ucBlue;
+ }
+ ptbits += 3;
+ }
+ ptbits += nPadding;
+ }
+ vImage.SetMaskColour( ucRed
+ ,ucGreen
+ ,ucBlue
+ );
+ vImage.SetMask(TRUE);
+ }
+ else
+ {
+ vImage.SetMask(FALSE);
+ }
+
+ //
+ // Free allocated resources
+ //
+ free(lpBits);
+ return vImage;
+} // end of wxBitmap::ConvertToImage
+
+// ----------------------------------------------------------------------------
+// sub bitmap extraction
+// ----------------------------------------------------------------------------
+
+wxBitmap wxBitmap::GetSubBitmap(
+ const wxRect& rRect
+) const
+{
+ wxCHECK_MSG( Ok() &&
+ (rRect.x >= 0) && (rRect.y >= 0) &&
+ (rRect.x + rRect.width <= GetWidth()) &&
+ (rRect.y + rRect.height <= GetHeight()),
+ wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
+
+ wxBitmap vRet( rRect.width
+ ,rRect.height
+ ,GetDepth()
+ );
+ wxASSERT_MSG( vRet.Ok(), wxT("GetSubBitmap error") );
+
+
+ //
+ // Copy bitmap data
+ //
+ SIZEL vSize = {0, 0};
+ DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
+ HDC hDCSrc = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
+ HDC hDCDst = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
+ HPS hPSSrc = ::GpiCreatePS(vHabmain, hDCSrc, &vSize, PU_PELS | GPIA_ASSOC);
+ HPS hPSDst = ::GpiCreatePS(vHabmain, hDCDst, &vSize, PU_PELS | GPIA_ASSOC);
+ POINTL vPoint[4] = { {0, 0}, {rRect.width, rRect.height},
+ {rRect.x, rRect.y},
+ {rRect.x + rRect.width, rRect.y + rRect.height}
+ };
+
+ ::GpiSetBitmap(hPSSrc, (HBITMAP) GetHBITMAP());
+ ::GpiSetBitmap(hPSDst, (HBITMAP) vRet.GetHBITMAP());
+ ::GpiBitBlt( hPSDst
+ ,hPSSrc
+ ,4L
+ ,vPoint
+ ,ROP_SRCCOPY
+ ,BBO_IGNORE
+ );
+
+ //
+ // Copy mask if there is one
+ //
+ if (GetMask())
+ {
+ BITMAPINFOHEADER2 vBmih;
+
+ memset(&vBmih, '\0', sizeof(BITMAPINFOHEADER2));
+ vBmih.cbFix = sizeof(BITMAPINFOHEADER2);
+ vBmih.cx = rRect.width;
+ vBmih.cy = rRect.height;
+ vBmih.cPlanes = 1;
+ vBmih.cBitCount = 24;
+
+ HBITMAP hBmpMask = ::GpiCreateBitmap( hPSDst
+ ,&vBmih
+ ,0L
+ ,NULL
+ ,NULL
+ );
+
+ ::GpiSetBitmap(hPSSrc, (HBITMAP) GetHBITMAP());
+ ::GpiSetBitmap(hPSDst, (HBITMAP) vRet.GetHBITMAP());
+
+ ::GpiSetBitmap(hPSSrc, (HBITMAP) GetMask()->GetMaskBitmap());
+ ::GpiSetBitmap(hPSDst, (HBITMAP) hBmpMask);
+ ::GpiBitBlt( hPSDst
+ ,hPSSrc
+ ,4L
+ ,vPoint
+ ,ROP_SRCCOPY
+ ,BBO_IGNORE
+ );
+
+ wxMask* pMask = new wxMask((WXHBITMAP)hBmpMask);
+ vRet.SetMask(pMask);
+ }
+
+ ::GpiSetBitmap(hPSSrc, NULL);
+ ::GpiSetBitmap(hPSDst, NULL);
+ ::GpiDestroyPS(hPSSrc);
+ ::GpiDestroyPS(hPSDst);
+ ::DevCloseDC(hDCSrc);
+ ::DevCloseDC(hDCDst);
+ return vRet;
+} // end of wxBitmap::GetSubBitmap
+
+// ----------------------------------------------------------------------------
+// wxBitmap accessors
+// ----------------------------------------------------------------------------
+
+void wxBitmap::SetQuality(
+ int nQ