+} // end of wxBitmap::SaveFile
+
+
+// ----------------------------------------------------------------------------
+// wxImage-wxBitmap convertion
+// ----------------------------------------------------------------------------
+
+bool wxBitmap::CreateFromImage (
+ const wxImage& rImage
+, int nDepth
+)
+{
+ wxCHECK_MSG(rImage.Ok(), FALSE, wxT("invalid image"));
+ m_refData = new wxBitmapRefData();
+
+ int nSizeLimit = 1024 * 768 * 3;
+ int nWidth = rImage.GetWidth();
+ int nBmpHeight = rImage.GetHeight();
+ int nBytePerLine = nWidth * 3;
+ int nSizeDWORD = sizeof(DWORD);
+ int nLineBoundary = nBytePerLine % nSizeDWORD;
+ int nPadding = 0;
+
+ if (nLineBoundary > 0)
+ {
+ nPadding = nSizeDWORD - nLineBoundary;
+ nBytePerLine += nPadding;
+ }
+
+ //
+ // 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)
+ 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;
+
+ //
+ // Fill in the DIB header
+ //
+ memset(&vHeader, '\0', sizeof(BITMAPINFOHEADER2));
+ vHeader.cbFix = sizeof(vHeader);
+ vHeader.cx = (USHORT)nWidth;
+ vHeader.cy = (USHORT)nHeight;
+ vHeader.cPlanes = 1L;
+ vHeader.cBitCount = 24;
+ vHeader.ulCompression = BCA_UNCOMP;
+ vHeader.cbImage = nBytePerLine * nHeight;
+ vHeader.cclrUsed = 0;
+
+ //
+ // These seem not really needed for our purpose here.
+ //
+ vHeader.cxResolution = 0;
+ vHeader.cyResolution = 0;
+ vHeader.cclrImportant = 0;
+ vHeader.usUnits = BRU_METRIC;
+ vHeader.usReserved = 0;
+ vHeader.cSize1 = 0;
+ vHeader.cSize2 = 0;
+ vHeader.usRecording = BRA_BOTTOMUP;
+ vHeader.usRendering = BRH_NOTHALFTONED;
+ vHeader.ulColorEncoding = BCE_RGB;
+ vHeader.ulIdentifier = 0;
+
+ //
+ // Memory for DIB data
+ //
+ unsigned char* pucBits;
+
+ pucBits = (unsigned char *)malloc(vHeader.cbImage);
+ if(!pucBits)
+ {
+ wxFAIL_MSG(wxT("could not allocate memory for DIB"));
+ return FALSE;
+ }
+
+ //
+ // 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);
+ HBITMAP hBmp;
+ HBITMAP hBmpOld;
+
+ hBmp = ::GpiCreateBitmap( hPS
+ ,&vHeader
+ ,0L
+ ,NULL
+ ,NULL
+ );
+ hBmpOld = ::GpiSetBitmap(hPS, hBmp);
+#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;
+
+ 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
+ //
+ POINTL vPoint[4] = { 0, nOrigin,
+ nWidth, nHeight,
+ 0, 0, nWidth, nHeight
+ };
+
+ ::GpiBitBlt( hPS
+ ,hPS
+ ,4
+ ,vPoint
+ ,ROP_SRCCOPY
+ ,BBO_IGNORE
+ );
+ 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())
+ {
+ memset(&vHeader, '\0', sizeof(BITMAPINFOHEADER2));
+ vHeader.cbFix = sizeof(BITMAPINFOHEADER2);
+ vHeader.cx = nWidth;
+ vHeader.cy = nHeight;
+ vHeader.cPlanes = 1;
+ vHeader.cBitCount = 1;
+ hBmp = ::GpiCreateBitmap( hPS
+ ,&vHeader
+ ,0L
+ ,NULL
+ ,NULL
+ );
+ hBmpOld = ::GpiSetBitmap(hPS, hBmp);
+ if (nNumDIB == 1)
+ nHeight = nBmpHeight;
+ else
+ nHeight = nSizeLimit / nBytePerLine;
+ vHeader.cy = (DWORD)(nHeight);
+ vHeader.cbImage = nBytePerLine * 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++)
+ {
+ if ((*(ptdata++) != cRed) || (*(ptdata++) != cGreen) || (*(ptdata++) != cBlue))
+ {
+ *(ptbits++) = cOne;
+ *(ptbits++) = cOne;
+ *(ptbits++) = cOne;
+ }
+ else
+ {
+ *(ptbits++) = cZero;
+ *(ptbits++) = cZero;
+ *(ptbits++) = cZero;
+ }
+ }
+ for (i = 0; i < nPadding; i++)
+ *(ptbits++) = cZero;
+ }
+ POINTL vPoint[4] = { 0, nOrigin,
+ nWidth, nHeight,
+ 0, 0, nWidth, nHeight
+ };
+
+ ::GpiBitBlt( hPS
+ ,hPS
+ ,4
+ ,vPoint
+ ,ROP_SRCCOPY
+ ,BBO_IGNORE
+ );
+ nOrigin += nHeight;
+ }
+
+ //
+ // Create a wxMask object
+ //
+ wxMask* pMask = new wxMask();
+
+ pMask->SetMaskBitmap((WXHBITMAP)hBmp);
+ SetMask(pMask);
+ hBmpOld = ::GpiSetBitmap(hPS, hBmp);
+ }
+
+ //
+ // Free allocated resources
+ //
+ ::GpiSetBitmap(hPS, NULLHANDLE);
+ ::GpiDestroyPS(hPS);
+ ::DevCloseDC(hDC);
+ free(pucBits);
+ return TRUE;
+} // end of wxBitmap::CreateFromImage
+
+wxImage wxBitmap::ConvertToImage() const
+{
+ wxImage vImage;
+
+ 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;
+ HDC hDCMem;
+ PSZ pszData[4] = { "Display", NULL, NULL, NULL };
+ HPS hPSMem;
+ HPS hPS;
+ SIZEL vSizlPage = {0,0};
+ HBITMAP hBitmap;
+
+ 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', sizeof(BITMAPINFOHEADER2));
+ vDIBh.cbFix = sizeof(BITMAPINFOHEADER2);
+ vDIBh.cx = nWidth;
+ vDIBh.cy = nHeight;
+ vDIBh.cPlanes = 1;
+ vDIBh.cbImage = nBytePerLine * nHeight;
+ vDIBh.cBitCount = 24;
+
+ memset(&vDIBInfo, '\0', sizeof(BITMAPINFO2));
+ vDIBInfo.cbFix = sizeof(BITMAPINFO2);
+ vDIBInfo.cPlanes = 1;
+ vDIBInfo.cBitCount = 24;
+ vDIBInfo.ulCompression = BCA_UNCOMP;
+ vDIBInfo.usReserved = 0;
+ vDIBInfo.usRecording = BRA_BOTTOMUP;
+ vDIBInfo.usRendering = BRH_NOTHALFTONED;
+ vDIBInfo.ulColorEncoding = BCE_RGB;
+ vDIBInfo.ulIdentifier = 0;
+
+ lpBits = (unsigned char *)malloc(vDIBh.cbImage);
+ if (!lpBits)
+ {
+ wxFAIL_MSG(wxT("could not allocate data for DIB"));
+ free(pData);
+ return wxNullImage;
+ }
+
+ //
+ // Copy data from the device-dependent bitmap to the DIB
+ //
+ hDCMem = ::DevOpenDC( vHabmain
+ ,OD_MEMORY
+ ,"*"
+ ,4
+ ,(PDEVOPENDATA)pszData
+ ,NULLHANDLE
+ );
+ hPSMem = ::GpiCreatePS( vHabmain
+ ,hDCMem
+ ,&vSizlPage
+ ,PU_PELS | GPIA_ASSOC | GPIT_MICRO
+ );
+ hBitmap = ::GpiCreateBitmap( hPSMem
+ ,&vDIBh
+ ,0L
+ ,NULL
+ ,NULL
+ );
+ lScans = ::GpiQueryBitmapBits( hPSMem
+ ,0L
+ ,(LONG)nHeight
+ ,(PBYTE)lpBits
+ ,&vDIBInfo
+ );
+
+ //
+ // 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;
+ }
+
+ //
+ // 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
+ ,"*"
+ ,4
+ ,(PDEVOPENDATA)pszData
+ ,NULLHANDLE
+ );
+ HPS hMemPS = ::GpiCreatePS( vHabmain
+ ,hMemDC
+ ,&vSizlPage
+ ,PU_PELS | GPIA_ASSOC | GPIT_MICRO
+ );
+ ::GpiSetColor(hMemPS, OS2RGB(0, 0, 0));
+ ::GpiSetBackColor(hMemPS, OS2RGB(255, 255, 255) );
+ ::GpiQueryBitmapBits( hPSMem
+ ,0L
+ ,(LONG)nHeight
+ ,(PBYTE)lpBits
+ ,&vDIBInfo
+ );
+ ::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
+ //
+ ::GpiDestroyPS(hPSMem);
+ ::DevCloseDC(hDCMem);
+ 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] = { rRect.x, rRect.y,
+ rRect.x + rRect.width, rRect.y + rRect.height,
+ 0, 0, GetWidth(), GetHeight()
+ };
+
+ ::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 = 1;
+
+ 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