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)");
80 } // end of wxBitmapRefData::Free
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 // this function should be called from all wxBitmap ctors
89 } // end of wxBitmap::Init
91 bool wxBitmap::CopyFromIconOrCursor(
92 const wxGDIImage
& rIcon
95 HPOINTER hIcon
= (HPOINTER
)rIcon
.GetHandle();
96 POINTERINFO SIconInfo
;
98 if (!::WinQueryPointerInfo(hIcon
, &SIconInfo
))
100 wxLogLastError(wxT("WinQueryPointerInfo"));
103 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
105 m_refData
= pRefData
;
107 int nWidth
= rIcon
.GetWidth();
108 int nHeight
= rIcon
.GetHeight();
110 pRefData
->m_nWidth
= nWidth
;
111 pRefData
->m_nHeight
= nHeight
;
112 pRefData
->m_nDepth
= wxDisplayDepth();
114 pRefData
->m_hBitmap
= (WXHBITMAP
)SIconInfo
.hbmColor
;
117 // No mask in the Info struct in OS/2
120 } // end of wxBitmap::CopyFromIconOrCursor
122 bool wxBitmap::CopyFromCursor(
123 const wxCursor
& rCursor
130 return(CopyFromIconOrCursor(rCursor
));
131 } // end of wxBitmap::CopyFromCursor
133 bool wxBitmap::CopyFromIcon(
142 return CopyFromIconOrCursor(rIcon
);
143 } // end of wxBitmap::CopyFromIcon
145 wxBitmap::~wxBitmap()
147 } // end of wxBitmap::~wxBitmap
158 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
159 BITMAPINFOHEADER2 vHeader
;
163 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
164 SIZEL vSize
= {0, 0};
167 wxASSERT(vHabmain
!= NULL
);
169 m_refData
= pRefData
;
171 pRefData
->m_nWidth
= nWidth
;
172 pRefData
->m_nHeight
= nHeight
;
173 pRefData
->m_nDepth
= nDepth
;
174 pRefData
->m_nNumColors
= 0;
175 pRefData
->m_pSelectedInto
= NULL
;
177 hDc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
178 hPs
= ::GpiCreatePS(vHabmain
, hDc
, &vSize
, GPIA_ASSOC
| PU_PELS
);
181 wxLogLastError("GpiCreatePS Failure");
187 // We assume that it is in XBM format which is not quite the same as
188 // the format CreateBitmap() wants because the order of bytes in the
191 const size_t nBytesPerLine
= (nWidth
+ 7) / 8;
192 const size_t nPadding
= nBytesPerLine
% 2;
193 const size_t nLen
= nHeight
* (nPadding
+ nBytesPerLine
);
194 const char* pzSrc
= zBits
;
198 pzData
= (char *)malloc(nLen
);
200 char* pzDst
= pzData
;
202 for (nRows
= 0; nRows
< nHeight
; nRows
++)
204 for (nCols
= 0; nCols
< nBytesPerLine
; nCols
++)
206 unsigned char ucVal
= *pzSrc
++;
207 unsigned char ucReversed
= 0;
210 for (nBits
= 0; nBits
< 8; nBits
++)
213 ucReversed
|= (ucVal
& 0x01);
216 *pzDst
++ = ucReversed
;
225 // Bits should already be in Windows standard format
227 pzData
= (char *)zBits
; // const_cast is harmless
230 memset(&vHeader
, '\0', 16);
232 vHeader
.cx
= (USHORT
)nWidth
;
233 vHeader
.cy
= (USHORT
)nHeight
;
234 vHeader
.cPlanes
= 1L;
235 vHeader
.cBitCount
= nDepth
;
236 vHeader
.usReserved
= 0;
238 memset(&vInfo
, '\0', 16);
240 vInfo
.cx
= (USHORT
)nWidth
;
241 vInfo
.cy
= (USHORT
)nHeight
;
243 vInfo
.cBitCount
= nDepth
;
245 HBITMAP hBmp
= ::GpiCreateBitmap(hPs
, &vHeader
, CBM_INIT
, (PBYTE
)pzData
, &vInfo
);
249 wxLogLastError("CreateBitmap");
253 SetHBITMAP((WXHBITMAP
)hBmp
);
254 } // end of wxBitmap::wxBitmap
268 } // end of wxBitmap::wxBitmap
286 } // end of wxBitmap::wxBitmap
289 const wxString
& rFilename
298 } // end of wxBitmap::wxBitmap
300 bool wxBitmap::Create(
307 BITMAPINFOHEADER2 vHeader
;
309 wxASSERT(vHabmain
!= NULL
);
311 m_refData
= new wxBitmapRefData
;
312 GetBitmapData()->m_nWidth
= nW
;
313 GetBitmapData()->m_nHeight
= nH
;
314 GetBitmapData()->m_nDepth
= nD
;
318 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
319 SIZEL vSize
= {0, 0};
320 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
321 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
323 memset(&vHeader
, '\0', 16);
328 vHeader
.cBitCount
= nD
;
330 hBmp
= ::GpiCreateBitmap( hPS
345 hPSScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
346 hDCScreen
= ::GpiQueryDevice(hPSScreen
);
347 ::DevQueryCaps(hDCScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitCount
);
349 memset(&vHeader
, '\0', 16);
354 vHeader
.cBitCount
= lBitCount
;
356 hBmp
= ::GpiCreateBitmap( hPSScreen
363 GetBitmapData()->m_nDepth
= wxDisplayDepth();
364 ::WinReleasePS(hPSScreen
);
366 SetHBITMAP((WXHBITMAP
)hBmp
);
368 #if WXWIN_COMPATIBILITY_2
369 GetBitmapData()->m_bOk
= hBmp
!= 0;
370 #endif // WXWIN_COMPATIBILITY_2
373 } // end of wxBitmap::Create
375 bool wxBitmap::CreateFromXpm(
379 #if wxUSE_IMAGE && wxUSE_XPM
382 wxCHECK_MSG(ppData
!= NULL
, FALSE
, wxT("invalid bitmap data"))
384 wxXPMDecoder vDecoder
;
385 wxImage vImg
= vDecoder
.ReadData(ppData
);
387 wxCHECK_MSG(vImg
.Ok(), FALSE
, wxT("invalid bitmap data"))
389 *this = wxBitmap(vImg
);
394 } // end of wxBitmap::CreateFromXpm
396 bool wxBitmap::LoadFile(
397 const wxString
& rFilename
401 HPS hPs
= NULLHANDLE
;
405 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
411 m_refData
= new wxBitmapRefData
;
413 return(pHandler
->LoadFile( this
425 if (!vImage
.LoadFile(rFilename
, lType
) || !vImage
.Ok() )
428 *this = wxBitmap(vImage
);
432 } // end of wxBitmap::LoadFile
434 bool wxBitmap::Create(
444 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
450 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
451 "type %d defined."), lType
);
456 m_refData
= new wxBitmapRefData
;
458 return(pHandler
->Create( this
465 } // end of wxBitmap::Create
467 bool wxBitmap::SaveFile(
468 const wxString
& rFilename
470 , const wxPalette
* pPalette
473 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
479 return pHandler
->SaveFile( this
487 // FIXME what about palette? shouldn't we use it?
488 wxImage vImage
= ConvertToImage();
493 return(vImage
.SaveFile( rFilename
497 } // end of wxBitmap::SaveFile
500 // ----------------------------------------------------------------------------
501 // wxImage-wxBitmap convertion
502 // ----------------------------------------------------------------------------
504 bool wxBitmap::CreateFromImage (
505 const wxImage
& rImage
509 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
510 m_refData
= new wxBitmapRefData();
513 int nSizeLimit
= 1024 * 768 * 3;
514 int nWidth
= rImage
.GetWidth();
515 int nBmpHeight
= rImage
.GetHeight();
516 int nBytePerLine
= nWidth
* 3;
517 int nSizeDWORD
= sizeof(DWORD
);
518 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
521 if (nLineBoundary
> 0)
523 nPadding
= nSizeDWORD
- nLineBoundary
;
524 nBytePerLine
+= nPadding
;
528 // Calc the number of DIBs and heights of DIBs
532 int nHeight
= nSizeLimit
/ nBytePerLine
;
534 if (nHeight
>= nBmpHeight
)
535 nHeight
= nBmpHeight
;
538 nNumDIB
= nBmpHeight
/ nHeight
;
539 nHRemain
= nBmpHeight
% nHeight
;
545 // Set bitmap parameters
547 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
549 SetHeight(nBmpHeight
);
551 nDepth
= wxDisplayDepth();
556 // Copy the palette from the source image
558 SetPalette(rImage
.GetPalette());
559 #endif // wxUSE_PALETTE
562 // Create a DIB header
564 BITMAPINFOHEADER2 vHeader
;
568 // Fill in the DIB header
570 memset(&vHeader
, '\0', 16);
572 vHeader
.cx
= (ULONG
)nWidth
;
573 vHeader
.cy
= (ULONG
)nHeight
;
574 vHeader
.cPlanes
= 1L;
575 vHeader
.cBitCount
= 24;
577 memset(&vInfo
, '\0', 16);
579 vInfo
.cx
= (ULONG
)nWidth
;
580 vInfo
.cy
= (ULONG
)nHeight
;
582 vInfo
.cBitCount
= 24;
584 // Memory for DIB data
586 unsigned char* pucBits
;
588 pucBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
591 wxFAIL_MSG(wxT("could not allocate memory for DIB"));
596 // Create and set the device-dependent bitmap
598 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
599 SIZEL vSize
= {0, 0};
600 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
601 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
603 HDC hDCScreen
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
608 hBmp
= ::GpiCreateBitmap( hPS
614 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
616 HPAL hOldPalette
= NULLHANDLE
;
617 if (rImage
.GetPalette().Ok())
619 hOldPalette
= ::GpiSelectPalette(hPS
, (HPAL
)rImage
.GetPalette().GetHPALETTE());
621 #endif // wxUSE_PALETTE
624 // Copy image data into DIB data and then into DDB (in a loop)
626 unsigned char* pData
= rImage
.GetData();
631 unsigned char* ptdata
= pData
;
632 unsigned char* ptbits
;
634 for (n
= 0; n
< nNumDIB
; n
++)
636 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
639 // Redefine height and size of the (possibly) last smaller DIB
640 // memory is not reallocated
643 vHeader
.cy
= (DWORD
)(nHeight
);
644 vHeader
.cbImage
= nBytePerLine
* nHeight
;
647 for (j
= 0; j
< nHeight
; j
++)
649 for (i
= 0; i
< nWidth
; i
++)
651 *(ptbits
++) = *(ptdata
+ 2);
652 *(ptbits
++) = *(ptdata
+ 1);
653 *(ptbits
++) = *(ptdata
);
656 for (i
= 0; i
< nPadding
; i
++)
661 // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
662 // in combination with setting the bits into the selected bitmap
664 vInfo
.cBitCount
= 16;
665 if ((lScans
= ::GpiSetBitmapBits( hPS
666 ,0 // Start at the bottom
667 ,(LONG
)nHeight
// One line per scan
675 vError
= ::WinGetLastError(vHabmain
);
676 sError
= wxPMErrorToStr(vError
);
683 ::GpiQueryDeviceBitmapFormats(hPS
, 24, alFormats
);
684 if ((lScans
= ::GpiQueryBitmapBits( hPS
694 vError
= ::WinGetLastError(vHabmain
);
695 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
);
769 for (int j
= 0; j
< nHeight
; j
++)
771 for (i
= 0; i
< nWidth
; i
++)
773 if ((*(ptdata
++) != cRed
) || (*(ptdata
++) != cGreen
) || (*(ptdata
++) != cBlue
))
786 for (i
= 0; i
< nPadding
; i
++)
789 lScans
= ::GpiSetBitmapBits( hPS
790 ,0 // Start at the bottom
791 ,(LONG
)nHeight
// One line per scan
795 hPSScreen
= ::GpiCreatePS( vHabmain
798 ,PU_PELS
| GPIA_ASSOC
800 POINTL vPoint2
[4] = { 0, nOrigin
,
802 0, 0, nWidth
, nHeight
804 ::GpiBitBlt( hPSScreen
811 ::GpiDestroyPS(hPSScreen
);
816 // Create a wxMask object
818 wxMask
* pMask
= new wxMask();
820 pMask
->SetMaskBitmap((WXHBITMAP
)hBmp
);
822 hBmpOld
= ::GpiSetBitmap(hPS
, hBmpOld
);
826 // Free allocated resources
828 ::GpiSetBitmap(hPS
, NULLHANDLE
);
833 } // end of wxBitmap::CreateFromImage
835 wxImage
wxBitmap::ConvertToImage() const
840 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
843 // Create an wxImage object
845 int nWidth
= GetWidth();
846 int nHeight
= GetHeight();
849 int nBytePerLine
= nWidth
* 3;
850 int nSizeDWORD
= sizeof(DWORD
);
851 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
853 unsigned char* pData
;
854 unsigned char* lpBits
;
856 BITMAPINFOHEADER2 vDIBh
;
857 BITMAPINFO2 vDIBInfo
;
862 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
863 SIZEL vSizlPage
= {0,0};
866 vImage
.Create( nWidth
869 pData
= vImage
.GetData();
872 wxFAIL_MSG( wxT("could not allocate data for image") );
875 if(nLineBoundary
> 0)
877 nPadding
= nSizeDWORD
- nLineBoundary
;
878 nBytePerLine
+= nPadding
;
880 wxDisplaySize( &nDevWidth
884 // Create and fill a DIB header
886 memset(&vDIBh
, '\0', 16);
891 vDIBh
.cBitCount
= 24;
893 memset(&vDIBInfo
, '\0', 16);
895 vDIBInfo
.cx
= nWidth
;
896 vDIBInfo
.cy
= nHeight
;
897 vDIBInfo
.cPlanes
= 1;
898 vDIBInfo
.cBitCount
= 24;
900 lpBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
903 wxFAIL_MSG(wxT("could not allocate data for DIB"));
909 // May already be selected into a PS
911 if ((pDC
= GetSelectedInto()) != NULL
)
913 hPSMem
= pDC
->GetHPS();
917 hDCMem
= ::DevOpenDC( vHabmain
924 hPSMem
= ::GpiCreatePS( vHabmain
927 ,PU_PELS
| GPIA_ASSOC
929 hBitmap
= (HBITMAP
)GetHBITMAP();
930 if ((hOldBitmap
= ::GpiSetBitmap(hPSMem
, hBitmap
)) == HBM_ERROR
)
935 vError
= ::WinGetLastError(vHabmain
);
936 sError
= wxPMErrorToStr(vError
);
941 // Copy data from the device-dependent bitmap to the DIB
943 if ((lScans
= ::GpiQueryBitmapBits( hPSMem
953 vError
= ::WinGetLastError(vHabmain
);
954 sError
= wxPMErrorToStr(vError
);
958 // Copy DIB data into the wxImage object
962 unsigned char* ptdata
= pData
;
963 unsigned char* ptbits
= lpBits
;
965 for (i
= 0; i
< nHeight
; i
++)
967 for (j
= 0; j
< nWidth
; j
++)
969 *(ptdata
++) = *(ptbits
+2);
970 *(ptdata
++) = *(ptbits
+1);
971 *(ptdata
++) = *(ptbits
);
976 if ((pDC
= GetSelectedInto()) == NULL
)
978 ::GpiSetBitmap(hPSMem
, NULLHANDLE
);
979 ::GpiDestroyPS(hPSMem
);
980 ::DevCloseDC(hDCMem
);
984 // Similarly, set data according to the possible mask bitmap
986 if (GetMask() && GetMask()->GetMaskBitmap())
988 hBitmap
= (HBITMAP
)GetMask()->GetMaskBitmap();
991 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
993 HDC hMemDC
= ::DevOpenDC( vHabmain
1000 HPS hMemPS
= ::GpiCreatePS( vHabmain
1003 ,PU_PELS
| GPIA_ASSOC
1005 ::GpiSetColor(hMemPS
, OS2RGB(0, 0, 0));
1006 ::GpiSetBackColor(hMemPS
, OS2RGB(255, 255, 255) );
1007 ::GpiSetBitmap(hMemPS
, hBitmap
);
1008 ::GpiQueryBitmapBits( hPSMem
1014 ::GpiSetBitmap(hMemPS
, NULLHANDLE
);
1015 ::GpiDestroyPS(hMemPS
);
1016 ::DevCloseDC(hMemDC
);
1019 // Background color set to RGB(16,16,16) in consistent with wxGTK
1021 unsigned char ucRed
= 16;
1022 unsigned char ucGreen
= 16;
1023 unsigned char ucBlue
= 16;
1027 for (i
= 0; i
< nHeight
; i
++)
1029 for (j
= 0; j
< nWidth
; j
++)
1035 *(ptdata
++) = ucRed
;
1036 *(ptdata
++) = ucGreen
;
1037 *(ptdata
++) = ucBlue
;
1043 vImage
.SetMaskColour( ucRed
1047 vImage
.SetMask(TRUE
);
1051 vImage
.SetMask(FALSE
);
1055 // Free allocated resources
1059 } // end of wxBitmap::ConvertToImage
1061 // ----------------------------------------------------------------------------
1062 // sub bitmap extraction
1063 // ----------------------------------------------------------------------------
1065 wxBitmap
wxBitmap::GetSubBitmap(
1069 wxCHECK_MSG( Ok() &&
1070 (rRect
.x
>= 0) && (rRect
.y
>= 0) &&
1071 (rRect
.x
+ rRect
.width
<= GetWidth()) &&
1072 (rRect
.y
+ rRect
.height
<= GetHeight()),
1073 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1075 wxBitmap
vRet( rRect
.width
1079 wxASSERT_MSG( vRet
.Ok(), wxT("GetSubBitmap error") );
1085 SIZEL vSize
= {0, 0};
1086 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1087 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1088 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1089 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1090 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1091 POINTL vPoint
[4] = { 0, 0, rRect
.width
, rRect
.height
,
1093 rRect
.x
+ rRect
.width
, rRect
.y
+ rRect
.height
1096 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1097 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1107 // Copy mask if there is one
1111 BITMAPINFOHEADER2 vBmih
;
1113 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1114 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1115 vBmih
.cx
= rRect
.width
;
1116 vBmih
.cy
= rRect
.height
;
1118 vBmih
.cBitCount
= 1;
1120 HBITMAP hBmpMask
= ::GpiCreateBitmap( hPSDst
1127 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1128 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1130 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1131 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpMask
);
1140 wxMask
* pMask
= new wxMask((WXHBITMAP
)hBmpMask
);
1141 vRet
.SetMask(pMask
);
1144 ::GpiSetBitmap(hPSSrc
, NULL
);
1145 ::GpiSetBitmap(hPSDst
, NULL
);
1146 ::GpiDestroyPS(hPSSrc
);
1147 ::GpiDestroyPS(hPSDst
);
1148 ::DevCloseDC(hDCSrc
);
1149 ::DevCloseDC(hDCDst
);
1151 } // end of wxBitmap::GetSubBitmap
1153 // ----------------------------------------------------------------------------
1154 // wxBitmap accessors
1155 // ----------------------------------------------------------------------------
1157 void wxBitmap::SetQuality(
1163 GetBitmapData()->m_nQuality
= nQ
;
1164 } // end of wxBitmap::SetQuality
1166 #if WXWIN_COMPATIBILITY_2
1167 void wxBitmap::SetOk(
1173 GetBitmapData()->m_bOk
= bOk
;
1174 } // end of wxBitmap::SetOk
1175 #endif // WXWIN_COMPATIBILITY_2
1177 void wxBitmap::SetPalette(
1178 const wxPalette
& rPalette
1183 GetBitmapData()->m_vBitmapPalette
= rPalette
;
1184 } // end of wxBitmap::SetPalette
1186 void wxBitmap::SetMask(
1192 GetBitmapData()->m_pBitmapMask
= pMask
;
1193 } // end of wxBitmap::SetMask
1195 wxBitmap
wxBitmap::GetBitmapForDC(
1200 } // end of wxBitmap::GetBitmapForDC
1202 // ----------------------------------------------------------------------------
1204 // ----------------------------------------------------------------------------
1209 } // end of wxMask::wxMask
1211 // Construct a mask from a bitmap and a colour indicating
1212 // the transparent area
1214 const wxBitmap
& rBitmap
1215 , const wxColour
& rColour
1222 } // end of wxMask::wxMask
1224 // Construct a mask from a bitmap and a palette index indicating
1225 // the transparent area
1227 const wxBitmap
& rBitmap
1235 } // end of wxMask::wxMask
1237 // Construct a mask from a mono bitmap (copies the bitmap).
1239 const wxBitmap
& rBitmap
1244 } // end of wxMask::wxMask
1249 ::GpiDeleteBitmap((HBITMAP
)m_hMaskBitmap
);
1250 } // end of wxMask::~wxMask
1252 // Create a mask from a mono bitmap (copies the bitmap).
1253 bool wxMask::Create(
1254 const wxBitmap
& rBitmap
1257 BITMAPINFOHEADER2 vBmih
;
1258 SIZEL vSize
= {0, 0};
1259 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1260 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1261 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1262 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1263 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1264 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1265 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1270 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1273 if (!rBitmap
.Ok() || rBitmap
.GetDepth() != 1)
1278 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1279 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1280 vBmih
.cx
= rBitmap
.GetWidth();
1281 vBmih
.cy
= rBitmap
.GetHeight();
1283 vBmih
.cBitCount
= 1;
1285 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1292 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1293 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1302 ::GpiDestroyPS(hPSSrc
);
1303 ::GpiDestroyPS(hPSDst
);
1304 ::DevCloseDC(hDCSrc
);
1305 ::DevCloseDC(hDCDst
);
1307 } // end of wxMask::Create
1309 // Create a mask from a bitmap and a palette index indicating
1310 // the transparent area
1311 bool wxMask::Create(
1312 const wxBitmap
& rBitmap
1318 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1321 if (rBitmap
.Ok() && rBitmap
.GetPalette()->Ok())
1324 unsigned char cGreen
;
1325 unsigned char cBlue
;
1327 if (rBitmap
.GetPalette()->GetRGB( nPaletteIndex
1333 wxColour
vTransparentColour( cRed
1338 return (Create( rBitmap
1344 } // end of wxMask::Create
1346 // Create a mask from a bitmap and a colour indicating
1347 // the transparent area
1348 bool wxMask::Create(
1349 const wxBitmap
& rBitmap
1350 , const wxColour
& rColour
1354 COLORREF vMaskColour
= OS2RGB( rColour
.Red()
1358 BITMAPINFOHEADER2 vBmih
;
1359 SIZEL vSize
= {0, 0};
1360 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1361 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1362 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1363 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1364 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1365 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1366 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1371 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1380 // Scan the bitmap for the transparent colour and set
1381 // the corresponding pixels in the mask to BLACK and
1382 // the rest to WHITE
1385 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1386 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1387 vBmih
.cx
= rBitmap
.GetWidth();
1388 vBmih
.cy
= rBitmap
.GetHeight();
1390 vBmih
.cBitCount
= 1;
1392 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1399 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1400 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1403 // This is not very efficient, but I can't think
1404 // of a better way of doing it
1406 for (int w
= 0; w
< rBitmap
.GetWidth(); w
++)
1408 for (int h
= 0; h
< rBitmap
.GetHeight(); h
++)
1410 POINTL vPt
= {w
, h
};
1411 COLORREF vCol
= (COLORREF
)::GpiQueryPel(hPSSrc
, &vPt
);
1412 if (vCol
== (COLORREF
)CLR_NOINDEX
)
1415 // Doesn't make sense to continue
1421 if (vCol
== vMaskColour
)
1423 ::GpiSetColor(hPSDst
, OS2RGB(0, 0, 0));
1424 ::GpiSetPel(hPSDst
, &vPt
);
1428 ::GpiSetColor(hPSDst
, OS2RGB(255, 255, 255));
1429 ::GpiSetPel(hPSDst
, &vPt
);
1433 ::GpiSetBitmap(hPSSrc
, NULL
);
1434 ::GpiSetBitmap(hPSDst
, NULL
);
1435 ::GpiDestroyPS(hPSSrc
);
1436 ::GpiDestroyPS(hPSDst
);
1437 ::DevCloseDC(hDCSrc
);
1438 ::DevCloseDC(hDCDst
);
1440 } // end of wxMask::Create
1442 // ----------------------------------------------------------------------------
1444 // ----------------------------------------------------------------------------
1446 bool wxBitmapHandler::Create(
1455 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1459 return(pBitmap
? Create( pBitmap
1467 bool wxBitmapHandler::Load(
1469 , const wxString
& rName
1476 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1480 return(pBitmap
? LoadFile( pBitmap
1489 bool wxBitmapHandler::Save(
1491 , const wxString
& rName
1495 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1499 return(pBitmap
? SaveFile( pBitmap
1505 bool wxBitmapHandler::Create(
1506 wxBitmap
* WXUNUSED(pBitmap
)
1507 , void* WXUNUSED(pData
)
1508 , long WXUNUSED(lType
)
1509 , int WXUNUSED(nWidth
)
1510 , int WXUNUSED(nHeight
)
1511 , int WXUNUSED(nDepth
)
1517 bool wxBitmapHandler::LoadFile(
1518 wxBitmap
* WXUNUSED(pBitmap
)
1519 , const wxString
& WXUNUSED(rName
)
1521 , long WXUNUSED(lType
)
1522 , int WXUNUSED(nDesiredWidth
)
1523 , int WXUNUSED(nDesiredHeight
)
1529 bool wxBitmapHandler::SaveFile(
1530 wxBitmap
* WXUNUSED(pBitmap
)
1531 , const wxString
& WXUNUSED(rName
)
1532 , int WXUNUSED(nType
)
1533 , const wxPalette
* WXUNUSED(pPalette
)
1539 // ----------------------------------------------------------------------------
1540 // Utility functions
1541 // ----------------------------------------------------------------------------
1542 HBITMAP
wxInvertMask(
1548 HBITMAP hBmpInvMask
= 0;
1550 wxCHECK_MSG( hBmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1553 // Get width/height from the bitmap if not given
1555 if (!nWidth
|| !nHeight
)
1557 BITMAPINFOHEADER2 vBmhdr
;
1559 ::GpiQueryBitmapInfoHeader( hBmpMask
1562 nWidth
= (int)vBmhdr
.cx
;
1563 nHeight
= (int)vBmhdr
.cy
;
1566 BITMAPINFOHEADER2 vBmih
;
1567 SIZEL vSize
= {0, 0};
1568 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1569 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1570 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1571 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1572 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1573 POINTL vPoint
[4] = { 0 ,0, nWidth
, nHeight
,
1574 0, 0, nWidth
, nHeight
1577 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1578 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1582 vBmih
.cBitCount
= 1;
1584 hBmpInvMask
= ::GpiCreateBitmap( hPSDst
1591 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) hBmpMask
);
1592 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpInvMask
);
1602 ::GpiDestroyPS(hPSSrc
);
1603 ::GpiDestroyPS(hPSDst
);
1604 ::DevCloseDC(hDCSrc
);
1605 ::DevCloseDC(hDCDst
);
1608 } // end of WxWinGdi_InvertMask