+ //
+ // 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);
+ }
+
+ //
+ // 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