1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bitmap.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/palette.h"
26 #include "wx/dcmemory.h"
27 #include "wx/bitmap.h"
31 #include "wx/os2/private.h"
34 //#include "wx/msw/dib.h"
36 #include "wx/xpmdecod.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
43 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
45 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 wxBitmapRefData::wxBitmapRefData()
58 m_pSelectedInto
= NULL
;
61 m_hBitmap
= (WXHBITMAP
) NULL
;
62 } // end of wxBitmapRefData::wxBitmapRefData
64 void wxBitmapRefData::Free()
66 if ( m_pSelectedInto
)
68 wxLogLastError("GpiDeleteBitmap(hbitmap)");
72 if (!::GpiDeleteBitmap((HBITMAP
)m_hBitmap
))
74 wxLogLastError("GpiDeleteBitmap(hbitmap)");
82 } // end of wxBitmapRefData::Free
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // this function should be called from all wxBitmap ctors
93 // True for all bitmaps created from bits, wxImages, Xpms
95 } // end of wxBitmap::Init
97 bool wxBitmap::CopyFromIconOrCursor(
98 const wxGDIImage
& rIcon
101 HPOINTER hIcon
= (HPOINTER
)rIcon
.GetHandle();
102 POINTERINFO SIconInfo
;
104 if (!::WinQueryPointerInfo(hIcon
, &SIconInfo
))
106 wxLogLastError(wxT("WinQueryPointerInfo"));
109 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
111 m_refData
= pRefData
;
113 int nWidth
= rIcon
.GetWidth();
114 int nHeight
= rIcon
.GetHeight();
116 pRefData
->m_nWidth
= nWidth
;
117 pRefData
->m_nHeight
= nHeight
;
118 pRefData
->m_nDepth
= wxDisplayDepth();
120 pRefData
->m_hBitmap
= (WXHBITMAP
)SIconInfo
.hbmColor
;
122 wxMask
* pMask
= new wxMask(SIconInfo
.hbmPointer
);
124 pMask
->SetMaskBitmap(GetHBITMAP());
128 } // end of wxBitmap::CopyFromIconOrCursor
130 bool wxBitmap::CopyFromCursor(
131 const wxCursor
& rCursor
138 return(CopyFromIconOrCursor(rCursor
));
139 } // end of wxBitmap::CopyFromCursor
141 bool wxBitmap::CopyFromIcon(
150 return CopyFromIconOrCursor(rIcon
);
151 } // end of wxBitmap::CopyFromIcon
153 wxBitmap::~wxBitmap()
155 } // end of wxBitmap::~wxBitmap
166 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
167 BITMAPINFOHEADER2 vHeader
;
171 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
172 SIZEL vSize
= {0, 0};
175 wxASSERT(vHabmain
!= NULL
);
177 m_refData
= pRefData
;
179 pRefData
->m_nWidth
= nWidth
;
180 pRefData
->m_nHeight
= nHeight
;
181 pRefData
->m_nDepth
= nDepth
;
182 pRefData
->m_nNumColors
= 0;
183 pRefData
->m_pSelectedInto
= NULL
;
185 hDc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
186 hPs
= ::GpiCreatePS(vHabmain
, hDc
, &vSize
, GPIA_ASSOC
| PU_PELS
);
189 wxLogLastError("GpiCreatePS Failure");
195 // We assume that it is in XBM format which is not quite the same as
196 // the format CreateBitmap() wants because the order of bytes in the
199 const size_t nBytesPerLine
= (nWidth
+ 7) / 8;
200 const size_t nPadding
= nBytesPerLine
% 2;
201 const size_t nLen
= nHeight
* (nPadding
+ nBytesPerLine
);
202 const char* pzSrc
= zBits
;
206 pzData
= (char *)malloc(nLen
);
208 char* pzDst
= pzData
;
210 for (nRows
= 0; nRows
< nHeight
; nRows
++)
212 for (nCols
= 0; nCols
< nBytesPerLine
; nCols
++)
214 unsigned char ucVal
= *pzSrc
++;
215 unsigned char ucReversed
= 0;
218 for (nBits
= 0; nBits
< 8; nBits
++)
221 ucReversed
|= (ucVal
& 0x01);
224 *pzDst
++ = ucReversed
;
233 // Bits should already be in Windows standard format
235 pzData
= (char *)zBits
; // const_cast is harmless
239 nDepth
= 24; // MAX supported in PM
240 memset(&vHeader
, '\0', 16);
242 vHeader
.cx
= (USHORT
)nWidth
;
243 vHeader
.cy
= (USHORT
)nHeight
;
244 vHeader
.cPlanes
= 1L;
245 vHeader
.cBitCount
= nDepth
;
246 vHeader
.usReserved
= 0;
248 memset(&vInfo
, '\0', 16);
250 vInfo
.cx
= (USHORT
)nWidth
;
251 vInfo
.cy
= (USHORT
)nHeight
;
253 vInfo
.cBitCount
= nDepth
;
255 HBITMAP hBmp
= ::GpiCreateBitmap(hPs
, &vHeader
, CBM_INIT
, (PBYTE
)pzData
, &vInfo
);
259 wxLogLastError("CreateBitmap");
263 SetHBITMAP((WXHBITMAP
)hBmp
);
264 } // end of wxBitmap::wxBitmap
277 } // end of wxBitmap::wxBitmap
295 } // end of wxBitmap::wxBitmap
307 } // end of wxBitmap::wxBitmap
309 bool wxBitmap::Create(
316 BITMAPINFOHEADER2 vHeader
;
318 wxASSERT(vHabmain
!= NULL
);
320 m_refData
= new wxBitmapRefData
;
321 GetBitmapData()->m_nWidth
= nW
;
322 GetBitmapData()->m_nHeight
= nH
;
323 GetBitmapData()->m_nDepth
= nD
;
326 // Xpms and bitmaps from other images can also be mono's, but only
327 // mono's need help changing their colors with MemDC changes
331 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
332 SIZEL vSize
= {0, 0};
333 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
334 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
338 memset(&vHeader
, '\0', 16);
343 vHeader
.cBitCount
= 24; //nD;
345 hBmp
= ::GpiCreateBitmap( hPS
360 hPSScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
361 hDCScreen
= ::GpiQueryDevice(hPSScreen
);
362 ::DevQueryCaps(hDCScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitCount
);
367 memset(&vHeader
, '\0', 16);
372 vHeader
.cBitCount
= lBitCount
;
374 hBmp
= ::GpiCreateBitmap( hPSScreen
381 GetBitmapData()->m_nDepth
= wxDisplayDepth();
382 ::WinReleasePS(hPSScreen
);
384 SetHBITMAP((WXHBITMAP
)hBmp
);
386 #if WXWIN_COMPATIBILITY_2
387 GetBitmapData()->m_bOk
= hBmp
!= 0;
388 #endif // WXWIN_COMPATIBILITY_2
391 } // end of wxBitmap::Create
393 bool wxBitmap::CreateFromXpm(
397 #if wxUSE_IMAGE && wxUSE_XPM
400 wxCHECK_MSG(ppData
!= NULL
, FALSE
, wxT("invalid bitmap data"))
402 wxXPMDecoder vDecoder
;
403 wxImage vImg
= vDecoder
.ReadData(ppData
);
405 wxCHECK_MSG(vImg
.Ok(), FALSE
, wxT("invalid bitmap data"))
407 *this = wxBitmap(vImg
);
412 } // end of wxBitmap::CreateFromXpm
414 bool wxBitmap::LoadFile(
419 HPS hPs
= NULLHANDLE
;
423 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
429 m_refData
= new wxBitmapRefData
;
431 return(pHandler
->LoadFile( this
442 } // end of wxBitmap::LoadFile
444 bool wxBitmap::Create(
454 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
460 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
461 "type %d defined."), lType
);
466 m_refData
= new wxBitmapRefData
;
468 return(pHandler
->Create( this
475 } // end of wxBitmap::Create
477 bool wxBitmap::SaveFile(
478 const wxString
& rFilename
480 , const wxPalette
* pPalette
483 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
489 return pHandler
->SaveFile( this
497 // FIXME what about palette? shouldn't we use it?
498 wxImage vImage
= ConvertToImage();
503 return(vImage
.SaveFile( rFilename
507 } // end of wxBitmap::SaveFile
510 // ----------------------------------------------------------------------------
511 // wxImage-wxBitmap convertion
512 // ----------------------------------------------------------------------------
514 bool wxBitmap::CreateFromImage (
515 const wxImage
& rImage
519 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
520 m_refData
= new wxBitmapRefData();
522 int nSizeLimit
= 1024 * 768 * 3;
523 int nWidth
= rImage
.GetWidth();
524 int nBmpHeight
= rImage
.GetHeight();
525 int nBytePerLine
= nWidth
* 3;
526 int nSizeDWORD
= sizeof(DWORD
);
527 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
530 if (nLineBoundary
> 0)
532 nPadding
= nSizeDWORD
- nLineBoundary
;
533 nBytePerLine
+= nPadding
;
537 // Calc the number of DIBs and heights of DIBs
541 int nHeight
= nSizeLimit
/ nBytePerLine
;
543 if (nHeight
>= nBmpHeight
)
544 nHeight
= nBmpHeight
;
547 nNumDIB
= nBmpHeight
/ nHeight
;
548 nHRemain
= nBmpHeight
% nHeight
;
554 // Set bitmap parameters
556 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
558 SetHeight(nBmpHeight
);
564 nDepth
= wxDisplayDepth();
569 // Copy the palette from the source image
571 SetPalette(rImage
.GetPalette());
572 #endif // wxUSE_PALETTE
575 // Create a DIB header
577 BITMAPINFOHEADER2 vHeader
;
581 // Fill in the DIB header
583 memset(&vHeader
, '\0', 16);
585 vHeader
.cx
= (ULONG
)nWidth
;
586 vHeader
.cy
= (ULONG
)nHeight
;
587 vHeader
.cPlanes
= 1L;
588 vHeader
.cBitCount
= 24;
591 // Memory for DIB data
593 unsigned char* pucBits
;
595 pucBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
598 wxFAIL_MSG(wxT("could not allocate memory for DIB"));
601 memset(pucBits
, '\0', (nBytePerLine
* nHeight
));
604 // Create and set the device-dependent bitmap
606 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
607 SIZEL vSize
= {0, 0};
608 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
609 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
611 HDC hDCScreen
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
616 memset(&vInfo
, '\0', 16);
618 vInfo
.cx
= (ULONG
)nWidth
;
619 vInfo
.cy
= (ULONG
)nHeight
;
621 vInfo
.cBitCount
= 24; // Set to desired count going in
623 hBmp
= ::GpiCreateBitmap( hPS
630 HPAL hOldPalette
= NULLHANDLE
;
631 if (rImage
.GetPalette().Ok())
633 hOldPalette
= ::GpiSelectPalette(hPS
, (HPAL
)rImage
.GetPalette().GetHPALETTE());
635 #endif // wxUSE_PALETTE
638 // Copy image data into DIB data and then into DDB (in a loop)
640 unsigned char* pData
= rImage
.GetData();
645 unsigned char* ptdata
= pData
;
646 unsigned char* ptbits
;
648 if ((hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
)) == HBM_ERROR
)
653 vError
= ::WinGetLastError(vHabmain
);
654 sError
= wxPMErrorToStr(vError
);
656 for (n
= 0; n
< nNumDIB
; n
++)
658 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
661 // Redefine height and size of the (possibly) last smaller DIB
662 // memory is not reallocated
665 vHeader
.cy
= (DWORD
)(nHeight
);
666 vHeader
.cbImage
= nBytePerLine
* nHeight
;
669 for (j
= 0; j
< nHeight
; j
++)
671 for (i
= 0; i
< nWidth
; i
++)
673 *(ptbits
++) = *(ptdata
+ 2);
674 *(ptbits
++) = *(ptdata
+ 1);
675 *(ptbits
++) = *(ptdata
);
678 for (i
= 0; i
< nPadding
; i
++)
683 // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
684 // in combination with setting the bits into the selected bitmap
686 if ((lScans
= ::GpiSetBitmapBits( hPS
687 ,0 // Start at the bottom
688 ,(LONG
)nHeight
// One line per scan
696 vError
= ::WinGetLastError(vHabmain
);
697 sError
= wxPMErrorToStr(vError
);
699 hPSScreen
= ::GpiCreatePS( vHabmain
702 ,PU_PELS
| GPIA_ASSOC
705 POINTL vPoint
[4] = { 0, nOrigin
,
707 0, 0, nWidth
, nHeight
711 ::GpiBitBlt( hPSScreen
718 ::GpiDestroyPS(hPSScreen
);
721 SetHBITMAP((WXHBITMAP
)hBmp
);
724 ::GpiSelectPalette(hPS
, hOldPalette
);
725 #endif // wxUSE_PALETTE
728 // Similarly, created an mono-bitmap for the possible mask
730 if (rImage
.HasMask())
734 vHeader
.cy
= nHeight
;
736 vHeader
.cBitCount
= 24;
737 hBmp
= ::GpiCreateBitmap( hPS
743 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
745 nHeight
= nBmpHeight
;
747 nHeight
= nSizeLimit
/ nBytePerLine
;
748 vHeader
.cy
= (DWORD
)(nHeight
);
751 unsigned char cRed
= rImage
.GetMaskRed();
752 unsigned char cGreen
= rImage
.GetMaskGreen();
753 unsigned char cBlue
= rImage
.GetMaskBlue();
754 unsigned char cZero
= 0;
755 unsigned char cOne
= 255;
758 for (n
= 0; n
< nNumDIB
; n
++)
760 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
763 // Redefine height and size of the (possibly) last smaller DIB
764 // memory is not reallocated
767 vHeader
.cy
= (DWORD
)(nHeight
);
768 vHeader
.cbImage
= nBytePerLine
* nHeight
;
771 for (int j
= 0; j
< nHeight
; j
++)
773 for (i
= 0; i
< nWidth
; i
++)
775 unsigned char cRedImage
= (*(ptdata
++)) ;
776 unsigned char cGreenImage
= (*(ptdata
++)) ;
777 unsigned char cBlueImage
= (*(ptdata
++)) ;
779 if ((cRedImage
!= cRed
) || (cGreenImage
!= cGreen
) || (cBlueImage
!= cBlue
))
792 for (i
= 0; i
< nPadding
; i
++)
795 lScans
= ::GpiSetBitmapBits( hPS
796 ,0 // Start at the bottom
797 ,(LONG
)nHeight
// One line per scan
801 hPSScreen
= ::GpiCreatePS( vHabmain
804 ,PU_PELS
| GPIA_ASSOC
806 POINTL vPoint2
[4] = { 0, nOrigin
,
808 0, 0, nWidth
, nHeight
810 ::GpiBitBlt( hPSScreen
817 ::GpiDestroyPS(hPSScreen
);
822 // Create a wxMask object
824 wxMask
* pMask
= new wxMask();
826 pMask
->SetMaskBitmap((WXHBITMAP
)hBmp
);
828 hBmpOld
= ::GpiSetBitmap(hPS
, hBmpOld
);
832 // Free allocated resources
834 ::GpiSetBitmap(hPS
, NULLHANDLE
);
836 ::DevCloseDC(hDCScreen
);
840 } // end of wxBitmap::CreateFromImage
842 wxImage
wxBitmap::ConvertToImage() const
847 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
850 // Create an wxImage object
852 int nWidth
= GetWidth();
853 int nHeight
= GetHeight();
856 int nBytePerLine
= nWidth
* 3;
857 int nSizeDWORD
= sizeof(DWORD
);
858 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
860 unsigned char* pData
;
861 unsigned char* lpBits
;
863 BITMAPINFOHEADER2 vDIBh
;
864 BITMAPINFO2 vDIBInfo
;
869 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
870 SIZEL vSizlPage
= {0,0};
873 vImage
.Create( nWidth
876 pData
= vImage
.GetData();
879 wxFAIL_MSG( wxT("could not allocate data for image") );
882 if(nLineBoundary
> 0)
884 nPadding
= nSizeDWORD
- nLineBoundary
;
885 nBytePerLine
+= nPadding
;
887 wxDisplaySize( &nDevWidth
891 // Create and fill a DIB header
893 memset(&vDIBh
, '\0', 16);
898 vDIBh
.cBitCount
= 24;
900 memset(&vDIBInfo
, '\0', 16);
902 vDIBInfo
.cx
= nWidth
;
903 vDIBInfo
.cy
= nHeight
;
904 vDIBInfo
.cPlanes
= 1;
905 vDIBInfo
.cBitCount
= 24;
907 lpBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
910 wxFAIL_MSG(wxT("could not allocate data for DIB"));
914 memset(lpBits
, '\0', (nBytePerLine
* nHeight
));
915 hBitmap
= (HBITMAP
)GetHBITMAP();
918 // May already be selected into a PS
920 if ((pDC
= GetSelectedInto()) != NULL
)
922 hPSMem
= pDC
->GetHPS();
926 hDCMem
= ::DevOpenDC( vHabmain
933 hPSMem
= ::GpiCreatePS( vHabmain
936 ,PU_PELS
| GPIA_ASSOC
939 if ((hOldBitmap
= ::GpiSetBitmap(hPSMem
, hBitmap
)) == HBM_ERROR
)
944 vError
= ::WinGetLastError(vHabmain
);
945 sError
= wxPMErrorToStr(vError
);
949 // Copy data from the device-dependent bitmap to the DIB
951 if ((lScans
= ::GpiQueryBitmapBits( hPSMem
961 vError
= ::WinGetLastError(vHabmain
);
962 sError
= wxPMErrorToStr(vError
);
966 // Copy DIB data into the wxImage object
970 unsigned char* ptdata
= pData
;
971 unsigned char* ptbits
= lpBits
;
973 for (i
= 0; i
< nHeight
; i
++)
975 for (j
= 0; j
< nWidth
; j
++)
977 *(ptdata
++) = *(ptbits
+2);
978 *(ptdata
++) = *(ptbits
+1);
979 *(ptdata
++) = *(ptbits
);
984 if ((pDC
= GetSelectedInto()) == NULL
)
986 ::GpiSetBitmap(hPSMem
, NULLHANDLE
);
987 ::GpiDestroyPS(hPSMem
);
988 ::DevCloseDC(hDCMem
);
992 // Similarly, set data according to the possible mask bitmap
994 if (GetMask() && GetMask()->GetMaskBitmap())
996 hBitmap
= (HBITMAP
)GetMask()->GetMaskBitmap();
999 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
1001 HDC hMemDC
= ::DevOpenDC( vHabmain
1005 ,(PDEVOPENDATA
)&vDop
1008 HPS hMemPS
= ::GpiCreatePS( vHabmain
1011 ,PU_PELS
| GPIA_ASSOC
1013 ::GpiSetColor(hMemPS
, OS2RGB(0, 0, 0));
1014 ::GpiSetBackColor(hMemPS
, OS2RGB(255, 255, 255) );
1015 ::GpiSetBitmap(hMemPS
, hBitmap
);
1016 ::GpiQueryBitmapBits( hPSMem
1022 ::GpiSetBitmap(hMemPS
, NULLHANDLE
);
1023 ::GpiDestroyPS(hMemPS
);
1024 ::DevCloseDC(hMemDC
);
1027 // Background color set to RGB(16,16,16) in consistent with wxGTK
1029 unsigned char ucRed
= 16;
1030 unsigned char ucGreen
= 16;
1031 unsigned char ucBlue
= 16;
1035 for (i
= 0; i
< nHeight
; i
++)
1037 for (j
= 0; j
< nWidth
; j
++)
1043 *(ptdata
++) = ucRed
;
1044 *(ptdata
++) = ucGreen
;
1045 *(ptdata
++) = ucBlue
;
1051 vImage
.SetMaskColour( ucRed
1055 vImage
.SetMask(TRUE
);
1059 vImage
.SetMask(FALSE
);
1063 // Free allocated resources
1067 } // end of wxBitmap::ConvertToImage
1069 // ----------------------------------------------------------------------------
1070 // sub bitmap extraction
1071 // ----------------------------------------------------------------------------
1073 wxBitmap
wxBitmap::GetSubBitmap(
1077 wxCHECK_MSG( Ok() &&
1078 (rRect
.x
>= 0) && (rRect
.y
>= 0) &&
1079 (rRect
.x
+ rRect
.width
<= GetWidth()) &&
1080 (rRect
.y
+ rRect
.height
<= GetHeight()),
1081 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1083 wxBitmap
vRet( rRect
.width
1087 wxASSERT_MSG( vRet
.Ok(), wxT("GetSubBitmap error") );
1093 SIZEL vSize
= {0, 0};
1094 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1095 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1096 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1097 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1098 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1099 POINTL vPoint
[4] = { 0, 0, rRect
.width
, rRect
.height
,
1101 rRect
.x
+ rRect
.width
, rRect
.y
+ rRect
.height
1104 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1105 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1115 // Copy mask if there is one
1119 BITMAPINFOHEADER2 vBmih
;
1121 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1122 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1123 vBmih
.cx
= rRect
.width
;
1124 vBmih
.cy
= rRect
.height
;
1126 vBmih
.cBitCount
= 24;
1128 HBITMAP hBmpMask
= ::GpiCreateBitmap( hPSDst
1135 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1136 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1138 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1139 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpMask
);
1148 wxMask
* pMask
= new wxMask((WXHBITMAP
)hBmpMask
);
1149 vRet
.SetMask(pMask
);
1152 ::GpiSetBitmap(hPSSrc
, NULL
);
1153 ::GpiSetBitmap(hPSDst
, NULL
);
1154 ::GpiDestroyPS(hPSSrc
);
1155 ::GpiDestroyPS(hPSDst
);
1156 ::DevCloseDC(hDCSrc
);
1157 ::DevCloseDC(hDCDst
);
1159 } // end of wxBitmap::GetSubBitmap
1161 // ----------------------------------------------------------------------------
1162 // wxBitmap accessors
1163 // ----------------------------------------------------------------------------
1165 void wxBitmap::SetQuality(
1171 GetBitmapData()->m_nQuality
= nQ
;
1172 } // end of wxBitmap::SetQuality
1174 #if WXWIN_COMPATIBILITY_2
1175 void wxBitmap::SetOk(
1181 GetBitmapData()->m_bOk
= bOk
;
1182 } // end of wxBitmap::SetOk
1183 #endif // WXWIN_COMPATIBILITY_2
1185 void wxBitmap::SetPalette(
1186 const wxPalette
& rPalette
1191 GetBitmapData()->m_vBitmapPalette
= rPalette
;
1192 } // end of wxBitmap::SetPalette
1194 void wxBitmap::SetMask(
1200 GetBitmapData()->m_pBitmapMask
= pMask
;
1201 } // end of wxBitmap::SetMask
1203 wxBitmap
wxBitmap::GetBitmapForDC(
1208 } // end of wxBitmap::GetBitmapForDC
1210 // ----------------------------------------------------------------------------
1212 // ----------------------------------------------------------------------------
1217 } // end of wxMask::wxMask
1219 // Construct a mask from a bitmap and a colour indicating
1220 // the transparent area
1222 const wxBitmap
& rBitmap
1223 , const wxColour
& rColour
1230 } // end of wxMask::wxMask
1232 // Construct a mask from a bitmap and a palette index indicating
1233 // the transparent area
1235 const wxBitmap
& rBitmap
1243 } // end of wxMask::wxMask
1245 // Construct a mask from a mono bitmap (copies the bitmap).
1247 const wxBitmap
& rBitmap
1252 } // end of wxMask::wxMask
1257 ::GpiDeleteBitmap((HBITMAP
)m_hMaskBitmap
);
1258 } // end of wxMask::~wxMask
1260 // Create a mask from a mono bitmap (copies the bitmap).
1261 bool wxMask::Create(
1262 const wxBitmap
& rBitmap
1265 BITMAPINFOHEADER2 vBmih
;
1266 SIZEL vSize
= {0, 0};
1267 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1268 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1269 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1270 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1271 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1272 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1273 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1278 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1281 if (!rBitmap
.Ok() || rBitmap
.GetDepth() != 1)
1286 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1287 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1288 vBmih
.cx
= rBitmap
.GetWidth();
1289 vBmih
.cy
= rBitmap
.GetHeight();
1291 vBmih
.cBitCount
= 24;
1293 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1300 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1301 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1310 ::GpiDestroyPS(hPSSrc
);
1311 ::GpiDestroyPS(hPSDst
);
1312 ::DevCloseDC(hDCSrc
);
1313 ::DevCloseDC(hDCDst
);
1315 } // end of wxMask::Create
1317 // Create a mask from a bitmap and a palette index indicating
1318 // the transparent area
1319 bool wxMask::Create(
1320 const wxBitmap
& rBitmap
1326 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1329 if (rBitmap
.Ok() && rBitmap
.GetPalette()->Ok())
1332 unsigned char cGreen
;
1333 unsigned char cBlue
;
1335 if (rBitmap
.GetPalette()->GetRGB( nPaletteIndex
1341 wxColour
vTransparentColour( cRed
1346 return (Create( rBitmap
1352 } // end of wxMask::Create
1354 // Create a mask from a bitmap and a colour indicating
1355 // the transparent area
1356 bool wxMask::Create(
1357 const wxBitmap
& rBitmap
1358 , const wxColour
& rColour
1362 COLORREF vMaskColour
= OS2RGB( rColour
.Red()
1366 BITMAPINFOHEADER2 vBmih
;
1367 SIZEL vSize
= {0, 0};
1368 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1369 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1370 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1371 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1372 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1373 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1374 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1379 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1388 // Scan the bitmap for the transparent colour and set
1389 // the corresponding pixels in the mask to BLACK and
1390 // the rest to WHITE
1393 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1394 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1395 vBmih
.cx
= rBitmap
.GetWidth();
1396 vBmih
.cy
= rBitmap
.GetHeight();
1398 vBmih
.cBitCount
= 1;
1400 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1407 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1408 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1411 // This is not very efficient, but I can't think
1412 // of a better way of doing it
1414 for (int w
= 0; w
< rBitmap
.GetWidth(); w
++)
1416 for (int h
= 0; h
< rBitmap
.GetHeight(); h
++)
1418 POINTL vPt
= {w
, h
};
1419 COLORREF vCol
= (COLORREF
)::GpiQueryPel(hPSSrc
, &vPt
);
1420 if (vCol
== (COLORREF
)CLR_NOINDEX
)
1423 // Doesn't make sense to continue
1429 if (vCol
== vMaskColour
)
1431 ::GpiSetColor(hPSDst
, OS2RGB(0, 0, 0));
1432 ::GpiSetPel(hPSDst
, &vPt
);
1436 ::GpiSetColor(hPSDst
, OS2RGB(255, 255, 255));
1437 ::GpiSetPel(hPSDst
, &vPt
);
1441 ::GpiSetBitmap(hPSSrc
, NULL
);
1442 ::GpiSetBitmap(hPSDst
, NULL
);
1443 ::GpiDestroyPS(hPSSrc
);
1444 ::GpiDestroyPS(hPSDst
);
1445 ::DevCloseDC(hDCSrc
);
1446 ::DevCloseDC(hDCDst
);
1448 } // end of wxMask::Create
1450 // ----------------------------------------------------------------------------
1452 // ----------------------------------------------------------------------------
1454 bool wxBitmapHandler::Create(
1463 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1467 return(pBitmap
? Create( pBitmap
1475 bool wxBitmapHandler::Load(
1483 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1487 return(pBitmap
? LoadFile( pBitmap
1495 bool wxBitmapHandler::Save(
1497 , const wxString
& rName
1501 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1505 return(pBitmap
? SaveFile( pBitmap
1511 bool wxBitmapHandler::Create(
1512 wxBitmap
* WXUNUSED(pBitmap
)
1513 , void* WXUNUSED(pData
)
1514 , long WXUNUSED(lType
)
1515 , int WXUNUSED(nWidth
)
1516 , int WXUNUSED(nHeight
)
1517 , int WXUNUSED(nDepth
)
1523 bool wxBitmapHandler::LoadFile(
1524 wxBitmap
* WXUNUSED(pBitmap
)
1526 , long WXUNUSED(lType
)
1527 , int WXUNUSED(nDesiredWidth
)
1528 , int WXUNUSED(nDesiredHeight
)
1534 bool wxBitmapHandler::SaveFile(
1535 wxBitmap
* WXUNUSED(pBitmap
)
1536 , const wxString
& WXUNUSED(rName
)
1537 , int WXUNUSED(nType
)
1538 , const wxPalette
* WXUNUSED(pPalette
)
1544 // ----------------------------------------------------------------------------
1545 // Utility functions
1546 // ----------------------------------------------------------------------------
1547 HBITMAP
wxInvertMask(
1553 HBITMAP hBmpInvMask
= 0;
1555 wxCHECK_MSG( hBmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1558 // Get width/height from the bitmap if not given
1560 if (!nWidth
|| !nHeight
)
1562 BITMAPINFOHEADER2 vBmhdr
;
1564 ::GpiQueryBitmapInfoHeader( hBmpMask
1567 nWidth
= (int)vBmhdr
.cx
;
1568 nHeight
= (int)vBmhdr
.cy
;
1571 BITMAPINFOHEADER2 vBmih
;
1572 SIZEL vSize
= {0, 0};
1573 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1574 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1575 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1576 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1577 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1578 POINTL vPoint
[4] = { 0 ,0, nWidth
, nHeight
,
1579 0, 0, nWidth
, nHeight
1582 memset(&vBmih
, '\0', 16);
1587 vBmih
.cBitCount
= 24;
1589 hBmpInvMask
= ::GpiCreateBitmap( hPSDst
1596 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) hBmpMask
);
1597 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpInvMask
);
1607 ::GpiDestroyPS(hPSSrc
);
1608 ::GpiDestroyPS(hPSDst
);
1609 ::DevCloseDC(hDCSrc
);
1610 ::DevCloseDC(hDCDst
);
1613 } // end of WxWinGdi_InvertMask