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
306 } // end of wxBitmap::wxBitmap
308 bool wxBitmap::Create(
315 BITMAPINFOHEADER2 vHeader
;
317 wxASSERT(vHabmain
!= NULL
);
319 m_refData
= new wxBitmapRefData
;
320 GetBitmapData()->m_nWidth
= nW
;
321 GetBitmapData()->m_nHeight
= nH
;
322 GetBitmapData()->m_nDepth
= nD
;
325 // Xpms and bitmaps from other images can also be mono's, but only
326 // mono's need help changing their colors with MemDC changes
330 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
331 SIZEL vSize
= {0, 0};
332 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
333 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
337 memset(&vHeader
, '\0', 16);
342 vHeader
.cBitCount
= 24; //nD;
344 hBmp
= ::GpiCreateBitmap( hPS
359 hPSScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
360 hDCScreen
= ::GpiQueryDevice(hPSScreen
);
361 ::DevQueryCaps(hDCScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitCount
);
366 memset(&vHeader
, '\0', 16);
371 vHeader
.cBitCount
= lBitCount
;
373 hBmp
= ::GpiCreateBitmap( hPSScreen
380 GetBitmapData()->m_nDepth
= wxDisplayDepth();
381 ::WinReleasePS(hPSScreen
);
383 SetHBITMAP((WXHBITMAP
)hBmp
);
385 #if WXWIN_COMPATIBILITY_2
386 GetBitmapData()->m_bOk
= hBmp
!= 0;
387 #endif // WXWIN_COMPATIBILITY_2
390 } // end of wxBitmap::Create
392 bool wxBitmap::CreateFromXpm(
396 #if wxUSE_IMAGE && wxUSE_XPM
399 wxCHECK_MSG(ppData
!= NULL
, FALSE
, wxT("invalid bitmap data"))
401 wxXPMDecoder vDecoder
;
402 wxImage vImg
= vDecoder
.ReadData(ppData
);
404 wxCHECK_MSG(vImg
.Ok(), FALSE
, wxT("invalid bitmap data"))
406 *this = wxBitmap(vImg
);
411 } // end of wxBitmap::CreateFromXpm
413 bool wxBitmap::LoadFile(
418 HPS hPs
= NULLHANDLE
;
422 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
428 m_refData
= new wxBitmapRefData
;
430 return(pHandler
->LoadFile( this
441 } // end of wxBitmap::LoadFile
443 bool wxBitmap::Create(
453 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
459 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
460 "type %d defined."), lType
);
465 m_refData
= new wxBitmapRefData
;
467 return(pHandler
->Create( this
474 } // end of wxBitmap::Create
476 bool wxBitmap::SaveFile(
477 const wxString
& rFilename
479 , const wxPalette
* pPalette
482 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
488 return pHandler
->SaveFile( this
496 // FIXME what about palette? shouldn't we use it?
497 wxImage vImage
= ConvertToImage();
502 return(vImage
.SaveFile( rFilename
506 } // end of wxBitmap::SaveFile
509 // ----------------------------------------------------------------------------
510 // wxImage-wxBitmap convertion
511 // ----------------------------------------------------------------------------
513 bool wxBitmap::CreateFromImage (
514 const wxImage
& rImage
518 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
519 m_refData
= new wxBitmapRefData();
521 int nSizeLimit
= 1024 * 768 * 3;
522 int nWidth
= rImage
.GetWidth();
523 int nBmpHeight
= rImage
.GetHeight();
524 int nBytePerLine
= nWidth
* 3;
525 int nSizeDWORD
= sizeof(DWORD
);
526 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
529 if (nLineBoundary
> 0)
531 nPadding
= nSizeDWORD
- nLineBoundary
;
532 nBytePerLine
+= nPadding
;
536 // Calc the number of DIBs and heights of DIBs
540 int nHeight
= nSizeLimit
/ nBytePerLine
;
542 if (nHeight
>= nBmpHeight
)
543 nHeight
= nBmpHeight
;
546 nNumDIB
= nBmpHeight
/ nHeight
;
547 nHRemain
= nBmpHeight
% nHeight
;
553 // Set bitmap parameters
555 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
557 SetHeight(nBmpHeight
);
563 nDepth
= wxDisplayDepth();
568 // Copy the palette from the source image
570 SetPalette(rImage
.GetPalette());
571 #endif // wxUSE_PALETTE
574 // Create a DIB header
576 BITMAPINFOHEADER2 vHeader
;
580 // Fill in the DIB header
582 memset(&vHeader
, '\0', 16);
584 vHeader
.cx
= (ULONG
)nWidth
;
585 vHeader
.cy
= (ULONG
)nHeight
;
586 vHeader
.cPlanes
= 1L;
587 vHeader
.cBitCount
= 24;
590 // Memory for DIB data
592 unsigned char* pucBits
;
594 pucBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
597 wxFAIL_MSG(wxT("could not allocate memory for DIB"));
600 memset(pucBits
, '\0', (nBytePerLine
* nHeight
));
603 // Create and set the device-dependent bitmap
605 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
606 SIZEL vSize
= {0, 0};
607 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
608 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
610 HDC hDCScreen
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
615 memset(&vInfo
, '\0', 16);
617 vInfo
.cx
= (ULONG
)nWidth
;
618 vInfo
.cy
= (ULONG
)nHeight
;
620 vInfo
.cBitCount
= 24; // Set to desired count going in
622 hBmp
= ::GpiCreateBitmap( hPS
629 HPAL hOldPalette
= NULLHANDLE
;
630 if (rImage
.GetPalette().Ok())
632 hOldPalette
= ::GpiSelectPalette(hPS
, (HPAL
)rImage
.GetPalette().GetHPALETTE());
634 #endif // wxUSE_PALETTE
637 // Copy image data into DIB data and then into DDB (in a loop)
639 unsigned char* pData
= rImage
.GetData();
644 unsigned char* ptdata
= pData
;
645 unsigned char* ptbits
;
647 if ((hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
)) == HBM_ERROR
)
652 vError
= ::WinGetLastError(vHabmain
);
653 sError
= wxPMErrorToStr(vError
);
655 for (n
= 0; n
< nNumDIB
; n
++)
657 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
660 // Redefine height and size of the (possibly) last smaller DIB
661 // memory is not reallocated
664 vHeader
.cy
= (DWORD
)(nHeight
);
665 vHeader
.cbImage
= nBytePerLine
* nHeight
;
668 for (j
= 0; j
< nHeight
; j
++)
670 for (i
= 0; i
< nWidth
; i
++)
672 *(ptbits
++) = *(ptdata
+ 2);
673 *(ptbits
++) = *(ptdata
+ 1);
674 *(ptbits
++) = *(ptdata
);
677 for (i
= 0; i
< nPadding
; i
++)
682 // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
683 // in combination with setting the bits into the selected bitmap
685 if ((lScans
= ::GpiSetBitmapBits( hPS
686 ,0 // Start at the bottom
687 ,(LONG
)nHeight
// One line per scan
695 vError
= ::WinGetLastError(vHabmain
);
696 sError
= wxPMErrorToStr(vError
);
698 hPSScreen
= ::GpiCreatePS( vHabmain
701 ,PU_PELS
| GPIA_ASSOC
704 POINTL vPoint
[4] = { 0, nOrigin
,
706 0, 0, nWidth
, nHeight
710 ::GpiBitBlt( hPSScreen
717 ::GpiDestroyPS(hPSScreen
);
720 SetHBITMAP((WXHBITMAP
)hBmp
);
723 ::GpiSelectPalette(hPS
, hOldPalette
);
724 #endif // wxUSE_PALETTE
727 // Similarly, created an mono-bitmap for the possible mask
729 if (rImage
.HasMask())
733 vHeader
.cy
= nHeight
;
735 vHeader
.cBitCount
= 24;
736 hBmp
= ::GpiCreateBitmap( hPS
742 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
744 nHeight
= nBmpHeight
;
746 nHeight
= nSizeLimit
/ nBytePerLine
;
747 vHeader
.cy
= (DWORD
)(nHeight
);
750 unsigned char cRed
= rImage
.GetMaskRed();
751 unsigned char cGreen
= rImage
.GetMaskGreen();
752 unsigned char cBlue
= rImage
.GetMaskBlue();
753 unsigned char cZero
= 0;
754 unsigned char cOne
= 255;
757 for (n
= 0; n
< nNumDIB
; n
++)
759 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
762 // Redefine height and size of the (possibly) last smaller DIB
763 // memory is not reallocated
766 vHeader
.cy
= (DWORD
)(nHeight
);
767 vHeader
.cbImage
= nBytePerLine
* nHeight
;
770 for (int j
= 0; j
< nHeight
; j
++)
772 for (i
= 0; i
< nWidth
; i
++)
774 unsigned char cRedImage
= (*(ptdata
++)) ;
775 unsigned char cGreenImage
= (*(ptdata
++)) ;
776 unsigned char cBlueImage
= (*(ptdata
++)) ;
778 if ((cRedImage
!= cRed
) || (cGreenImage
!= cGreen
) || (cBlueImage
!= cBlue
))
791 for (i
= 0; i
< nPadding
; i
++)
794 lScans
= ::GpiSetBitmapBits( hPS
795 ,0 // Start at the bottom
796 ,(LONG
)nHeight
// One line per scan
800 hPSScreen
= ::GpiCreatePS( vHabmain
803 ,PU_PELS
| GPIA_ASSOC
805 POINTL vPoint2
[4] = { 0, nOrigin
,
807 0, 0, nWidth
, nHeight
809 ::GpiBitBlt( hPSScreen
816 ::GpiDestroyPS(hPSScreen
);
821 // Create a wxMask object
823 wxMask
* pMask
= new wxMask();
825 pMask
->SetMaskBitmap((WXHBITMAP
)hBmp
);
827 hBmpOld
= ::GpiSetBitmap(hPS
, hBmpOld
);
831 // Free allocated resources
833 ::GpiSetBitmap(hPS
, NULLHANDLE
);
835 ::DevCloseDC(hDCScreen
);
839 } // end of wxBitmap::CreateFromImage
841 wxImage
wxBitmap::ConvertToImage() const
846 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
849 // Create an wxImage object
851 int nWidth
= GetWidth();
852 int nHeight
= GetHeight();
855 int nBytePerLine
= nWidth
* 3;
856 int nSizeDWORD
= sizeof(DWORD
);
857 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
859 unsigned char* pData
;
860 unsigned char* lpBits
;
862 BITMAPINFOHEADER2 vDIBh
;
863 BITMAPINFO2 vDIBInfo
;
868 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
869 SIZEL vSizlPage
= {0,0};
872 vImage
.Create( nWidth
875 pData
= vImage
.GetData();
878 wxFAIL_MSG( wxT("could not allocate data for image") );
881 if(nLineBoundary
> 0)
883 nPadding
= nSizeDWORD
- nLineBoundary
;
884 nBytePerLine
+= nPadding
;
886 wxDisplaySize( &nDevWidth
890 // Create and fill a DIB header
892 memset(&vDIBh
, '\0', 16);
897 vDIBh
.cBitCount
= 24;
899 memset(&vDIBInfo
, '\0', 16);
901 vDIBInfo
.cx
= nWidth
;
902 vDIBInfo
.cy
= nHeight
;
903 vDIBInfo
.cPlanes
= 1;
904 vDIBInfo
.cBitCount
= 24;
906 lpBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
909 wxFAIL_MSG(wxT("could not allocate data for DIB"));
913 memset(lpBits
, '\0', (nBytePerLine
* nHeight
));
914 hBitmap
= (HBITMAP
)GetHBITMAP();
917 // May already be selected into a PS
919 if ((pDC
= GetSelectedInto()) != NULL
)
921 hPSMem
= pDC
->GetHPS();
925 hDCMem
= ::DevOpenDC( vHabmain
932 hPSMem
= ::GpiCreatePS( vHabmain
935 ,PU_PELS
| GPIA_ASSOC
938 if ((hOldBitmap
= ::GpiSetBitmap(hPSMem
, hBitmap
)) == HBM_ERROR
)
943 vError
= ::WinGetLastError(vHabmain
);
944 sError
= wxPMErrorToStr(vError
);
948 // Copy data from the device-dependent bitmap to the DIB
950 if ((lScans
= ::GpiQueryBitmapBits( hPSMem
960 vError
= ::WinGetLastError(vHabmain
);
961 sError
= wxPMErrorToStr(vError
);
965 // Copy DIB data into the wxImage object
969 unsigned char* ptdata
= pData
;
970 unsigned char* ptbits
= lpBits
;
972 for (i
= 0; i
< nHeight
; i
++)
974 for (j
= 0; j
< nWidth
; j
++)
976 *(ptdata
++) = *(ptbits
+2);
977 *(ptdata
++) = *(ptbits
+1);
978 *(ptdata
++) = *(ptbits
);
983 if ((pDC
= GetSelectedInto()) == NULL
)
985 ::GpiSetBitmap(hPSMem
, NULLHANDLE
);
986 ::GpiDestroyPS(hPSMem
);
987 ::DevCloseDC(hDCMem
);
991 // Similarly, set data according to the possible mask bitmap
993 if (GetMask() && GetMask()->GetMaskBitmap())
995 hBitmap
= (HBITMAP
)GetMask()->GetMaskBitmap();
998 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
1000 HDC hMemDC
= ::DevOpenDC( vHabmain
1004 ,(PDEVOPENDATA
)&vDop
1007 HPS hMemPS
= ::GpiCreatePS( vHabmain
1010 ,PU_PELS
| GPIA_ASSOC
1012 ::GpiSetColor(hMemPS
, OS2RGB(0, 0, 0));
1013 ::GpiSetBackColor(hMemPS
, OS2RGB(255, 255, 255) );
1014 ::GpiSetBitmap(hMemPS
, hBitmap
);
1015 ::GpiQueryBitmapBits( hPSMem
1021 ::GpiSetBitmap(hMemPS
, NULLHANDLE
);
1022 ::GpiDestroyPS(hMemPS
);
1023 ::DevCloseDC(hMemDC
);
1026 // Background color set to RGB(16,16,16) in consistent with wxGTK
1028 unsigned char ucRed
= 16;
1029 unsigned char ucGreen
= 16;
1030 unsigned char ucBlue
= 16;
1034 for (i
= 0; i
< nHeight
; i
++)
1036 for (j
= 0; j
< nWidth
; j
++)
1042 *(ptdata
++) = ucRed
;
1043 *(ptdata
++) = ucGreen
;
1044 *(ptdata
++) = ucBlue
;
1050 vImage
.SetMaskColour( ucRed
1054 vImage
.SetMask(TRUE
);
1058 vImage
.SetMask(FALSE
);
1062 // Free allocated resources
1066 } // end of wxBitmap::ConvertToImage
1068 // ----------------------------------------------------------------------------
1069 // sub bitmap extraction
1070 // ----------------------------------------------------------------------------
1072 wxBitmap
wxBitmap::GetSubBitmap(
1076 wxCHECK_MSG( Ok() &&
1077 (rRect
.x
>= 0) && (rRect
.y
>= 0) &&
1078 (rRect
.x
+ rRect
.width
<= GetWidth()) &&
1079 (rRect
.y
+ rRect
.height
<= GetHeight()),
1080 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1082 wxBitmap
vRet( rRect
.width
1086 wxASSERT_MSG( vRet
.Ok(), wxT("GetSubBitmap error") );
1092 SIZEL vSize
= {0, 0};
1093 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1094 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1095 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1096 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1097 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1098 POINTL vPoint
[4] = { 0, 0, rRect
.width
, rRect
.height
,
1100 rRect
.x
+ rRect
.width
, rRect
.y
+ rRect
.height
1103 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1104 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1114 // Copy mask if there is one
1118 BITMAPINFOHEADER2 vBmih
;
1120 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1121 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1122 vBmih
.cx
= rRect
.width
;
1123 vBmih
.cy
= rRect
.height
;
1125 vBmih
.cBitCount
= 24;
1127 HBITMAP hBmpMask
= ::GpiCreateBitmap( hPSDst
1134 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1135 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1137 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1138 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpMask
);
1147 wxMask
* pMask
= new wxMask((WXHBITMAP
)hBmpMask
);
1148 vRet
.SetMask(pMask
);
1151 ::GpiSetBitmap(hPSSrc
, NULL
);
1152 ::GpiSetBitmap(hPSDst
, NULL
);
1153 ::GpiDestroyPS(hPSSrc
);
1154 ::GpiDestroyPS(hPSDst
);
1155 ::DevCloseDC(hDCSrc
);
1156 ::DevCloseDC(hDCDst
);
1158 } // end of wxBitmap::GetSubBitmap
1160 // ----------------------------------------------------------------------------
1161 // wxBitmap accessors
1162 // ----------------------------------------------------------------------------
1164 void wxBitmap::SetQuality(
1170 GetBitmapData()->m_nQuality
= nQ
;
1171 } // end of wxBitmap::SetQuality
1173 #if WXWIN_COMPATIBILITY_2
1174 void wxBitmap::SetOk(
1180 GetBitmapData()->m_bOk
= bOk
;
1181 } // end of wxBitmap::SetOk
1182 #endif // WXWIN_COMPATIBILITY_2
1184 void wxBitmap::SetPalette(
1185 const wxPalette
& rPalette
1190 GetBitmapData()->m_vBitmapPalette
= rPalette
;
1191 } // end of wxBitmap::SetPalette
1193 void wxBitmap::SetMask(
1199 GetBitmapData()->m_pBitmapMask
= pMask
;
1200 } // end of wxBitmap::SetMask
1202 wxBitmap
wxBitmap::GetBitmapForDC(
1207 } // end of wxBitmap::GetBitmapForDC
1209 // ----------------------------------------------------------------------------
1211 // ----------------------------------------------------------------------------
1216 } // end of wxMask::wxMask
1218 // Construct a mask from a bitmap and a colour indicating
1219 // the transparent area
1221 const wxBitmap
& rBitmap
1222 , const wxColour
& rColour
1229 } // end of wxMask::wxMask
1231 // Construct a mask from a bitmap and a palette index indicating
1232 // the transparent area
1234 const wxBitmap
& rBitmap
1242 } // end of wxMask::wxMask
1244 // Construct a mask from a mono bitmap (copies the bitmap).
1246 const wxBitmap
& rBitmap
1251 } // end of wxMask::wxMask
1256 ::GpiDeleteBitmap((HBITMAP
)m_hMaskBitmap
);
1257 } // end of wxMask::~wxMask
1259 // Create a mask from a mono bitmap (copies the bitmap).
1260 bool wxMask::Create(
1261 const wxBitmap
& rBitmap
1264 BITMAPINFOHEADER2 vBmih
;
1265 SIZEL vSize
= {0, 0};
1266 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1267 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1268 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1269 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1270 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1271 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1272 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1277 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1280 if (!rBitmap
.Ok() || rBitmap
.GetDepth() != 1)
1285 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1286 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1287 vBmih
.cx
= rBitmap
.GetWidth();
1288 vBmih
.cy
= rBitmap
.GetHeight();
1290 vBmih
.cBitCount
= 24;
1292 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1299 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1300 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1309 ::GpiDestroyPS(hPSSrc
);
1310 ::GpiDestroyPS(hPSDst
);
1311 ::DevCloseDC(hDCSrc
);
1312 ::DevCloseDC(hDCDst
);
1314 } // end of wxMask::Create
1316 // Create a mask from a bitmap and a palette index indicating
1317 // the transparent area
1318 bool wxMask::Create(
1319 const wxBitmap
& rBitmap
1325 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1328 if (rBitmap
.Ok() && rBitmap
.GetPalette()->Ok())
1331 unsigned char cGreen
;
1332 unsigned char cBlue
;
1334 if (rBitmap
.GetPalette()->GetRGB( nPaletteIndex
1340 wxColour
vTransparentColour( cRed
1345 return (Create( rBitmap
1351 } // end of wxMask::Create
1353 // Create a mask from a bitmap and a colour indicating
1354 // the transparent area
1355 bool wxMask::Create(
1356 const wxBitmap
& rBitmap
1357 , const wxColour
& rColour
1361 COLORREF vMaskColour
= OS2RGB( rColour
.Red()
1365 BITMAPINFOHEADER2 vBmih
;
1366 SIZEL vSize
= {0, 0};
1367 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1368 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1369 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1370 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1371 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1372 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1373 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1378 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1387 // Scan the bitmap for the transparent colour and set
1388 // the corresponding pixels in the mask to BLACK and
1389 // the rest to WHITE
1392 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1393 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1394 vBmih
.cx
= rBitmap
.GetWidth();
1395 vBmih
.cy
= rBitmap
.GetHeight();
1397 vBmih
.cBitCount
= 1;
1399 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1406 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1407 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1410 // This is not very efficient, but I can't think
1411 // of a better way of doing it
1413 for (int w
= 0; w
< rBitmap
.GetWidth(); w
++)
1415 for (int h
= 0; h
< rBitmap
.GetHeight(); h
++)
1417 POINTL vPt
= {w
, h
};
1418 COLORREF vCol
= (COLORREF
)::GpiQueryPel(hPSSrc
, &vPt
);
1419 if (vCol
== (COLORREF
)CLR_NOINDEX
)
1422 // Doesn't make sense to continue
1428 if (vCol
== vMaskColour
)
1430 ::GpiSetColor(hPSDst
, OS2RGB(0, 0, 0));
1431 ::GpiSetPel(hPSDst
, &vPt
);
1435 ::GpiSetColor(hPSDst
, OS2RGB(255, 255, 255));
1436 ::GpiSetPel(hPSDst
, &vPt
);
1440 ::GpiSetBitmap(hPSSrc
, NULL
);
1441 ::GpiSetBitmap(hPSDst
, NULL
);
1442 ::GpiDestroyPS(hPSSrc
);
1443 ::GpiDestroyPS(hPSDst
);
1444 ::DevCloseDC(hDCSrc
);
1445 ::DevCloseDC(hDCDst
);
1447 } // end of wxMask::Create
1449 // ----------------------------------------------------------------------------
1451 // ----------------------------------------------------------------------------
1453 bool wxBitmapHandler::Create(
1462 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1466 return(pBitmap
? Create( pBitmap
1474 bool wxBitmapHandler::Load(
1482 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1486 return(pBitmap
? LoadFile( pBitmap
1494 bool wxBitmapHandler::Save(
1496 , const wxString
& rName
1500 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1504 return(pBitmap
? SaveFile( pBitmap
1510 bool wxBitmapHandler::Create(
1511 wxBitmap
* WXUNUSED(pBitmap
)
1512 , void* WXUNUSED(pData
)
1513 , long WXUNUSED(lType
)
1514 , int WXUNUSED(nWidth
)
1515 , int WXUNUSED(nHeight
)
1516 , int WXUNUSED(nDepth
)
1522 bool wxBitmapHandler::LoadFile(
1523 wxBitmap
* WXUNUSED(pBitmap
)
1525 , long WXUNUSED(lType
)
1526 , int WXUNUSED(nDesiredWidth
)
1527 , int WXUNUSED(nDesiredHeight
)
1533 bool wxBitmapHandler::SaveFile(
1534 wxBitmap
* WXUNUSED(pBitmap
)
1535 , const wxString
& WXUNUSED(rName
)
1536 , int WXUNUSED(nType
)
1537 , const wxPalette
* WXUNUSED(pPalette
)
1543 // ----------------------------------------------------------------------------
1544 // Utility functions
1545 // ----------------------------------------------------------------------------
1546 HBITMAP
wxInvertMask(
1552 HBITMAP hBmpInvMask
= 0;
1554 wxCHECK_MSG( hBmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1557 // Get width/height from the bitmap if not given
1559 if (!nWidth
|| !nHeight
)
1561 BITMAPINFOHEADER2 vBmhdr
;
1563 ::GpiQueryBitmapInfoHeader( hBmpMask
1566 nWidth
= (int)vBmhdr
.cx
;
1567 nHeight
= (int)vBmhdr
.cy
;
1570 BITMAPINFOHEADER2 vBmih
;
1571 SIZEL vSize
= {0, 0};
1572 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1573 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1574 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1575 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1576 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1577 POINTL vPoint
[4] = { 0 ,0, nWidth
, nHeight
,
1578 0, 0, nWidth
, nHeight
1581 memset(&vBmih
, '\0', 16);
1586 vBmih
.cBitCount
= 24;
1588 hBmpInvMask
= ::GpiCreateBitmap( hPSDst
1595 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) hBmpMask
);
1596 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpInvMask
);
1606 ::GpiDestroyPS(hPSSrc
);
1607 ::GpiDestroyPS(hPSDst
);
1608 ::DevCloseDC(hDCSrc
);
1609 ::DevCloseDC(hDCDst
);
1612 } // end of WxWinGdi_InvertMask