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
= ::WinOpenWindowDC(HWND_DESKTOP
);
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 if ((lScans
= ::GpiSetBitmapBits( hPS
665 ,0 // Start at the bottom
666 ,(LONG
)nHeight
// One line per scan
674 vError
= ::WinGetLastError(vHabmain
);
675 sError
= wxPMErrorToStr(vError
);
678 hPSScreen
= ::GpiCreatePS( vHabmain
681 ,PU_PELS
| GPIA_ASSOC
684 POINTL vPoint
[4] = { 0, nOrigin
,
686 0, 0, nWidth
, nHeight
690 ::GpiBitBlt( hPSScreen
697 ::GpiDestroyPS(hPSScreen
);
700 SetHBITMAP((WXHBITMAP
)hBmp
);
703 ::GpiSelectPalette(hPS
, hOldPalette
);
704 #endif // wxUSE_PALETTE
707 // Similarly, created an mono-bitmap for the possible mask
709 if (rImage
.HasMask())
711 vHeader
.cbFix
= sizeof(BITMAPINFOHEADER2
);
713 vHeader
.cy
= nHeight
;
715 vHeader
.cBitCount
= 1;
716 hBmp
= ::GpiCreateBitmap( hPS
722 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
724 nHeight
= nBmpHeight
;
726 nHeight
= nSizeLimit
/ nBytePerLine
;
727 vHeader
.cy
= (DWORD
)(nHeight
);
728 vHeader
.cbImage
= nBytePerLine
* nHeight
;
731 unsigned char cRed
= rImage
.GetMaskRed();
732 unsigned char cGreen
= rImage
.GetMaskGreen();
733 unsigned char cBlue
= rImage
.GetMaskBlue();
734 unsigned char cZero
= 0;
735 unsigned char cOne
= 255;
738 for (n
= 0; n
< nNumDIB
; n
++)
740 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
743 // Redefine height and size of the (possibly) last smaller DIB
744 // memory is not reallocated
747 vHeader
.cy
= (DWORD
)(nHeight
);
748 vHeader
.cbImage
= nBytePerLine
* nHeight
;
751 for (int j
= 0; j
< nHeight
; j
++)
753 for (i
= 0; i
< nWidth
; i
++)
755 if ((*(ptdata
++) != cRed
) || (*(ptdata
++) != cGreen
) || (*(ptdata
++) != cBlue
))
768 for (i
= 0; i
< nPadding
; i
++)
771 lScans
= ::GpiSetBitmapBits( hPS
772 ,0 // Start at the bottom
773 ,(LONG
)nHeight
// One line per scan
777 hPSScreen
= ::GpiCreatePS( vHabmain
780 ,PU_PELS
| GPIA_ASSOC
782 POINTL vPoint2
[4] = { 0, nOrigin
,
784 0, 0, nWidth
, nHeight
786 ::GpiBitBlt( hPSScreen
793 ::GpiDestroyPS(hPSScreen
);
798 // Create a wxMask object
800 wxMask
* pMask
= new wxMask();
802 pMask
->SetMaskBitmap((WXHBITMAP
)hBmp
);
804 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
808 // Free allocated resources
810 ::GpiSetBitmap(hPS
, NULLHANDLE
);
815 } // end of wxBitmap::CreateFromImage
817 wxImage
wxBitmap::ConvertToImage() const
822 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
825 // Create an wxImage object
827 int nWidth
= GetWidth();
828 int nHeight
= GetHeight();
831 int nBytePerLine
= nWidth
* 3;
832 int nSizeDWORD
= sizeof(DWORD
);
833 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
835 unsigned char* pData
;
836 unsigned char* lpBits
;
838 BITMAPINFOHEADER2 vDIBh
;
839 BITMAPINFO2 vDIBInfo
;
844 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
845 SIZEL vSizlPage
= {0,0};
848 vImage
.Create( nWidth
851 pData
= vImage
.GetData();
854 wxFAIL_MSG( wxT("could not allocate data for image") );
857 if(nLineBoundary
> 0)
859 nPadding
= nSizeDWORD
- nLineBoundary
;
860 nBytePerLine
+= nPadding
;
862 wxDisplaySize( &nDevWidth
866 // Create and fill a DIB header
868 memset(&vDIBh
, '\0', 16);
873 vDIBh
.cBitCount
= 24;
875 memset(&vDIBInfo
, '\0', 16);
877 vDIBInfo
.cx
= nWidth
;
878 vDIBInfo
.cy
= nHeight
;
879 vDIBInfo
.cPlanes
= 1;
880 vDIBInfo
.cBitCount
= 24;
882 lpBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
885 wxFAIL_MSG(wxT("could not allocate data for DIB"));
891 // May already be selected into a PS
893 if ((pDC
= GetSelectedInto()) != NULL
)
895 hPSMem
= pDC
->GetHPS();
899 hDCMem
= ::DevOpenDC( vHabmain
906 hPSMem
= ::GpiCreatePS( vHabmain
909 ,PU_PELS
| GPIA_ASSOC
911 hBitmap
= (HBITMAP
)GetHBITMAP();
912 if ((hOldBitmap
= ::GpiSetBitmap(hPSMem
, hBitmap
)) == HBM_ERROR
)
917 vError
= ::WinGetLastError(vHabmain
);
918 sError
= wxPMErrorToStr(vError
);
923 // Copy data from the device-dependent bitmap to the DIB
925 if ((lScans
= ::GpiQueryBitmapBits( hPSMem
935 vError
= ::WinGetLastError(vHabmain
);
936 sError
= wxPMErrorToStr(vError
);
940 // Copy DIB data into the wxImage object
944 unsigned char* ptdata
= pData
;
945 unsigned char* ptbits
= lpBits
;
947 for (i
= 0; i
< nHeight
; i
++)
949 for (j
= 0; j
< nWidth
; j
++)
951 *(ptdata
++) = *(ptbits
+2);
952 *(ptdata
++) = *(ptbits
+1);
953 *(ptdata
++) = *(ptbits
);
958 if ((pDC
= GetSelectedInto()) == NULL
)
960 ::GpiSetBitmap(hPSMem
, NULLHANDLE
);
961 ::GpiDestroyPS(hPSMem
);
962 ::DevCloseDC(hDCMem
);
966 // Similarly, set data according to the possible mask bitmap
968 if (GetMask() && GetMask()->GetMaskBitmap())
970 hBitmap
= (HBITMAP
)GetMask()->GetMaskBitmap();
973 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
975 HDC hMemDC
= ::DevOpenDC( vHabmain
982 HPS hMemPS
= ::GpiCreatePS( vHabmain
985 ,PU_PELS
| GPIA_ASSOC
| GPIT_MICRO
987 ::GpiSetColor(hMemPS
, OS2RGB(0, 0, 0));
988 ::GpiSetBackColor(hMemPS
, OS2RGB(255, 255, 255) );
989 ::GpiSetBitmap(hMemPS
, hBitmap
);
990 ::GpiQueryBitmapBits( hPSMem
996 ::GpiSetBitmap(hMemPS
, NULLHANDLE
);
997 ::GpiDestroyPS(hMemPS
);
998 ::DevCloseDC(hMemDC
);
1001 // Background color set to RGB(16,16,16) in consistent with wxGTK
1003 unsigned char ucRed
= 16;
1004 unsigned char ucGreen
= 16;
1005 unsigned char ucBlue
= 16;
1009 for (i
= 0; i
< nHeight
; i
++)
1011 for (j
= 0; j
< nWidth
; j
++)
1017 *(ptdata
++) = ucRed
;
1018 *(ptdata
++) = ucGreen
;
1019 *(ptdata
++) = ucBlue
;
1025 vImage
.SetMaskColour( ucRed
1029 vImage
.SetMask(TRUE
);
1033 vImage
.SetMask(FALSE
);
1037 // Free allocated resources
1041 } // end of wxBitmap::ConvertToImage
1043 // ----------------------------------------------------------------------------
1044 // sub bitmap extraction
1045 // ----------------------------------------------------------------------------
1047 wxBitmap
wxBitmap::GetSubBitmap(
1051 wxCHECK_MSG( Ok() &&
1052 (rRect
.x
>= 0) && (rRect
.y
>= 0) &&
1053 (rRect
.x
+ rRect
.width
<= GetWidth()) &&
1054 (rRect
.y
+ rRect
.height
<= GetHeight()),
1055 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1057 wxBitmap
vRet( rRect
.width
1061 wxASSERT_MSG( vRet
.Ok(), wxT("GetSubBitmap error") );
1067 SIZEL vSize
= {0, 0};
1068 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1069 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1070 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1071 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1072 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1073 POINTL vPoint
[4] = { 0, 0, rRect
.width
, rRect
.height
,
1075 rRect
.x
+ rRect
.width
, rRect
.y
+ rRect
.height
1078 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1079 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1089 // Copy mask if there is one
1093 BITMAPINFOHEADER2 vBmih
;
1095 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1096 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1097 vBmih
.cx
= rRect
.width
;
1098 vBmih
.cy
= rRect
.height
;
1100 vBmih
.cBitCount
= 1;
1102 HBITMAP hBmpMask
= ::GpiCreateBitmap( hPSDst
1109 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1110 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1112 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1113 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpMask
);
1122 wxMask
* pMask
= new wxMask((WXHBITMAP
)hBmpMask
);
1123 vRet
.SetMask(pMask
);
1126 ::GpiSetBitmap(hPSSrc
, NULL
);
1127 ::GpiSetBitmap(hPSDst
, NULL
);
1128 ::GpiDestroyPS(hPSSrc
);
1129 ::GpiDestroyPS(hPSDst
);
1130 ::DevCloseDC(hDCSrc
);
1131 ::DevCloseDC(hDCDst
);
1133 } // end of wxBitmap::GetSubBitmap
1135 // ----------------------------------------------------------------------------
1136 // wxBitmap accessors
1137 // ----------------------------------------------------------------------------
1139 void wxBitmap::SetQuality(
1145 GetBitmapData()->m_nQuality
= nQ
;
1146 } // end of wxBitmap::SetQuality
1148 #if WXWIN_COMPATIBILITY_2
1149 void wxBitmap::SetOk(
1155 GetBitmapData()->m_bOk
= bOk
;
1156 } // end of wxBitmap::SetOk
1157 #endif // WXWIN_COMPATIBILITY_2
1159 void wxBitmap::SetPalette(
1160 const wxPalette
& rPalette
1165 GetBitmapData()->m_vBitmapPalette
= rPalette
;
1166 } // end of wxBitmap::SetPalette
1168 void wxBitmap::SetMask(
1174 GetBitmapData()->m_pBitmapMask
= pMask
;
1175 } // end of wxBitmap::SetMask
1177 wxBitmap
wxBitmap::GetBitmapForDC(
1182 } // end of wxBitmap::GetBitmapForDC
1184 // ----------------------------------------------------------------------------
1186 // ----------------------------------------------------------------------------
1191 } // end of wxMask::wxMask
1193 // Construct a mask from a bitmap and a colour indicating
1194 // the transparent area
1196 const wxBitmap
& rBitmap
1197 , const wxColour
& rColour
1204 } // end of wxMask::wxMask
1206 // Construct a mask from a bitmap and a palette index indicating
1207 // the transparent area
1209 const wxBitmap
& rBitmap
1217 } // end of wxMask::wxMask
1219 // Construct a mask from a mono bitmap (copies the bitmap).
1221 const wxBitmap
& rBitmap
1226 } // end of wxMask::wxMask
1231 ::GpiDeleteBitmap((HBITMAP
)m_hMaskBitmap
);
1232 } // end of wxMask::~wxMask
1234 // Create a mask from a mono bitmap (copies the bitmap).
1235 bool wxMask::Create(
1236 const wxBitmap
& rBitmap
1239 BITMAPINFOHEADER2 vBmih
;
1240 SIZEL vSize
= {0, 0};
1241 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1242 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1243 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1244 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1245 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1246 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1247 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1252 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1255 if (!rBitmap
.Ok() || rBitmap
.GetDepth() != 1)
1260 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1261 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1262 vBmih
.cx
= rBitmap
.GetWidth();
1263 vBmih
.cy
= rBitmap
.GetHeight();
1265 vBmih
.cBitCount
= 1;
1267 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1274 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1275 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1284 ::GpiDestroyPS(hPSSrc
);
1285 ::GpiDestroyPS(hPSDst
);
1286 ::DevCloseDC(hDCSrc
);
1287 ::DevCloseDC(hDCDst
);
1289 } // end of wxMask::Create
1291 // Create a mask from a bitmap and a palette index indicating
1292 // the transparent area
1293 bool wxMask::Create(
1294 const wxBitmap
& rBitmap
1300 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1303 if (rBitmap
.Ok() && rBitmap
.GetPalette()->Ok())
1306 unsigned char cGreen
;
1307 unsigned char cBlue
;
1309 if (rBitmap
.GetPalette()->GetRGB( nPaletteIndex
1315 wxColour
vTransparentColour( cRed
1320 return (Create( rBitmap
1326 } // end of wxMask::Create
1328 // Create a mask from a bitmap and a colour indicating
1329 // the transparent area
1330 bool wxMask::Create(
1331 const wxBitmap
& rBitmap
1332 , const wxColour
& rColour
1336 COLORREF vMaskColour
= OS2RGB( rColour
.Red()
1340 BITMAPINFOHEADER2 vBmih
;
1341 SIZEL vSize
= {0, 0};
1342 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1343 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1344 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1345 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1346 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1347 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1348 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1353 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1362 // Scan the bitmap for the transparent colour and set
1363 // the corresponding pixels in the mask to BLACK and
1364 // the rest to WHITE
1367 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1368 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1369 vBmih
.cx
= rBitmap
.GetWidth();
1370 vBmih
.cy
= rBitmap
.GetHeight();
1372 vBmih
.cBitCount
= 1;
1374 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1381 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1382 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1385 // This is not very efficient, but I can't think
1386 // of a better way of doing it
1388 for (int w
= 0; w
< rBitmap
.GetWidth(); w
++)
1390 for (int h
= 0; h
< rBitmap
.GetHeight(); h
++)
1392 POINTL vPt
= {w
, h
};
1393 COLORREF vCol
= (COLORREF
)::GpiQueryPel(hPSSrc
, &vPt
);
1394 if (vCol
== (COLORREF
)CLR_NOINDEX
)
1397 // Doesn't make sense to continue
1403 if (vCol
== vMaskColour
)
1405 ::GpiSetColor(hPSDst
, OS2RGB(0, 0, 0));
1406 ::GpiSetPel(hPSDst
, &vPt
);
1410 ::GpiSetColor(hPSDst
, OS2RGB(255, 255, 255));
1411 ::GpiSetPel(hPSDst
, &vPt
);
1415 ::GpiSetBitmap(hPSSrc
, NULL
);
1416 ::GpiSetBitmap(hPSDst
, NULL
);
1417 ::GpiDestroyPS(hPSSrc
);
1418 ::GpiDestroyPS(hPSDst
);
1419 ::DevCloseDC(hDCSrc
);
1420 ::DevCloseDC(hDCDst
);
1422 } // end of wxMask::Create
1424 // ----------------------------------------------------------------------------
1426 // ----------------------------------------------------------------------------
1428 bool wxBitmapHandler::Create(
1437 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1441 return(pBitmap
? Create( pBitmap
1449 bool wxBitmapHandler::Load(
1451 , const wxString
& rName
1458 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1462 return(pBitmap
? LoadFile( pBitmap
1471 bool wxBitmapHandler::Save(
1473 , const wxString
& rName
1477 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1481 return(pBitmap
? SaveFile( pBitmap
1487 bool wxBitmapHandler::Create(
1488 wxBitmap
* WXUNUSED(pBitmap
)
1489 , void* WXUNUSED(pData
)
1490 , long WXUNUSED(lType
)
1491 , int WXUNUSED(nWidth
)
1492 , int WXUNUSED(nHeight
)
1493 , int WXUNUSED(nDepth
)
1499 bool wxBitmapHandler::LoadFile(
1500 wxBitmap
* WXUNUSED(pBitmap
)
1501 , const wxString
& WXUNUSED(rName
)
1503 , long WXUNUSED(lType
)
1504 , int WXUNUSED(nDesiredWidth
)
1505 , int WXUNUSED(nDesiredHeight
)
1511 bool wxBitmapHandler::SaveFile(
1512 wxBitmap
* WXUNUSED(pBitmap
)
1513 , const wxString
& WXUNUSED(rName
)
1514 , int WXUNUSED(nType
)
1515 , const wxPalette
* WXUNUSED(pPalette
)
1521 // ----------------------------------------------------------------------------
1522 // Utility functions
1523 // ----------------------------------------------------------------------------
1524 HBITMAP
wxInvertMask(
1530 HBITMAP hBmpInvMask
= 0;
1532 wxCHECK_MSG( hBmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1535 // Get width/height from the bitmap if not given
1537 if (!nWidth
|| !nHeight
)
1539 BITMAPINFOHEADER2 vBmhdr
;
1541 ::GpiQueryBitmapInfoHeader( hBmpMask
1544 nWidth
= (int)vBmhdr
.cx
;
1545 nHeight
= (int)vBmhdr
.cy
;
1548 BITMAPINFOHEADER2 vBmih
;
1549 SIZEL vSize
= {0, 0};
1550 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1551 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1552 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1553 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1554 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1555 POINTL vPoint
[4] = { 0 ,0, nWidth
, nHeight
,
1556 0, 0, nWidth
, nHeight
1559 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1560 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1564 vBmih
.cBitCount
= 1;
1566 hBmpInvMask
= ::GpiCreateBitmap( hPSDst
1573 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) hBmpMask
);
1574 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpInvMask
);
1584 ::GpiDestroyPS(hPSSrc
);
1585 ::GpiDestroyPS(hPSDst
);
1586 ::DevCloseDC(hDCSrc
);
1587 ::DevCloseDC(hDCDst
);
1590 } // end of WxWinGdi_InvertMask