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
231 nDepth
= 24; // MAX supported in PM
232 memset(&vHeader
, '\0', 16);
234 vHeader
.cx
= (USHORT
)nWidth
;
235 vHeader
.cy
= (USHORT
)nHeight
;
236 vHeader
.cPlanes
= 1L;
237 vHeader
.cBitCount
= nDepth
;
238 vHeader
.usReserved
= 0;
240 memset(&vInfo
, '\0', 16);
242 vInfo
.cx
= (USHORT
)nWidth
;
243 vInfo
.cy
= (USHORT
)nHeight
;
245 vInfo
.cBitCount
= nDepth
;
247 HBITMAP hBmp
= ::GpiCreateBitmap(hPs
, &vHeader
, CBM_INIT
, (PBYTE
)pzData
, &vInfo
);
251 wxLogLastError("CreateBitmap");
255 SetHBITMAP((WXHBITMAP
)hBmp
);
256 } // end of wxBitmap::wxBitmap
270 } // end of wxBitmap::wxBitmap
288 } // end of wxBitmap::wxBitmap
291 const wxString
& rFilename
300 } // end of wxBitmap::wxBitmap
302 bool wxBitmap::Create(
309 BITMAPINFOHEADER2 vHeader
;
311 wxASSERT(vHabmain
!= NULL
);
313 m_refData
= new wxBitmapRefData
;
314 GetBitmapData()->m_nWidth
= nW
;
315 GetBitmapData()->m_nHeight
= nH
;
316 GetBitmapData()->m_nDepth
= nD
;
320 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
321 SIZEL vSize
= {0, 0};
322 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
323 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
325 memset(&vHeader
, '\0', 16);
330 vHeader
.cBitCount
= nD
;
332 hBmp
= ::GpiCreateBitmap( hPS
347 hPSScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
348 hDCScreen
= ::GpiQueryDevice(hPSScreen
);
349 ::DevQueryCaps(hDCScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitCount
);
354 memset(&vHeader
, '\0', 16);
359 vHeader
.cBitCount
= lBitCount
;
361 hBmp
= ::GpiCreateBitmap( hPSScreen
368 GetBitmapData()->m_nDepth
= wxDisplayDepth();
369 ::WinReleasePS(hPSScreen
);
371 SetHBITMAP((WXHBITMAP
)hBmp
);
373 #if WXWIN_COMPATIBILITY_2
374 GetBitmapData()->m_bOk
= hBmp
!= 0;
375 #endif // WXWIN_COMPATIBILITY_2
378 } // end of wxBitmap::Create
380 bool wxBitmap::CreateFromXpm(
384 #if wxUSE_IMAGE && wxUSE_XPM
387 wxCHECK_MSG(ppData
!= NULL
, FALSE
, wxT("invalid bitmap data"))
389 wxXPMDecoder vDecoder
;
390 wxImage vImg
= vDecoder
.ReadData(ppData
);
392 wxCHECK_MSG(vImg
.Ok(), FALSE
, wxT("invalid bitmap data"))
394 *this = wxBitmap(vImg
);
399 } // end of wxBitmap::CreateFromXpm
401 bool wxBitmap::LoadFile(
402 const wxString
& rFilename
406 HPS hPs
= NULLHANDLE
;
410 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
416 m_refData
= new wxBitmapRefData
;
418 return(pHandler
->LoadFile( this
430 if (!vImage
.LoadFile(rFilename
, lType
) || !vImage
.Ok() )
433 *this = wxBitmap(vImage
);
437 } // end of wxBitmap::LoadFile
439 bool wxBitmap::Create(
449 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
455 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
456 "type %d defined."), lType
);
461 m_refData
= new wxBitmapRefData
;
463 return(pHandler
->Create( this
470 } // end of wxBitmap::Create
472 bool wxBitmap::SaveFile(
473 const wxString
& rFilename
475 , const wxPalette
* pPalette
478 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
484 return pHandler
->SaveFile( this
492 // FIXME what about palette? shouldn't we use it?
493 wxImage vImage
= ConvertToImage();
498 return(vImage
.SaveFile( rFilename
502 } // end of wxBitmap::SaveFile
505 // ----------------------------------------------------------------------------
506 // wxImage-wxBitmap convertion
507 // ----------------------------------------------------------------------------
509 bool wxBitmap::CreateFromImage (
510 const wxImage
& rImage
514 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
515 m_refData
= new wxBitmapRefData();
518 int nSizeLimit
= 1024 * 768 * 3;
519 int nWidth
= rImage
.GetWidth();
520 int nBmpHeight
= rImage
.GetHeight();
521 int nBytePerLine
= nWidth
* 3;
522 int nSizeDWORD
= sizeof(DWORD
);
523 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
526 if (nLineBoundary
> 0)
528 nPadding
= nSizeDWORD
- nLineBoundary
;
529 nBytePerLine
+= nPadding
;
533 // Calc the number of DIBs and heights of DIBs
537 int nHeight
= nSizeLimit
/ nBytePerLine
;
539 if (nHeight
>= nBmpHeight
)
540 nHeight
= nBmpHeight
;
543 nNumDIB
= nBmpHeight
/ nHeight
;
544 nHRemain
= nBmpHeight
% nHeight
;
550 // Set bitmap parameters
552 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
554 SetHeight(nBmpHeight
);
556 nDepth
= wxDisplayDepth();
561 // Copy the palette from the source image
563 SetPalette(rImage
.GetPalette());
564 #endif // wxUSE_PALETTE
567 // Create a DIB header
569 BITMAPINFOHEADER2 vHeader
;
573 // Fill in the DIB header
575 memset(&vHeader
, '\0', 16);
577 vHeader
.cx
= (ULONG
)nWidth
;
578 vHeader
.cy
= (ULONG
)nHeight
;
579 vHeader
.cPlanes
= 1L;
580 vHeader
.cBitCount
= 24;
583 // Memory for DIB data
585 unsigned char* pucBits
;
587 pucBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
590 wxFAIL_MSG(wxT("could not allocate memory for DIB"));
593 memset(pucBits
, '\0', (nBytePerLine
* nHeight
));
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 memset(&vInfo
, '\0', 16);
610 vInfo
.cx
= (ULONG
)nWidth
;
611 vInfo
.cy
= (ULONG
)nHeight
;
613 vInfo
.cBitCount
= 24; // Set to desired count going in
615 hBmp
= ::GpiCreateBitmap( hPS
621 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
623 HPAL hOldPalette
= NULLHANDLE
;
624 if (rImage
.GetPalette().Ok())
626 hOldPalette
= ::GpiSelectPalette(hPS
, (HPAL
)rImage
.GetPalette().GetHPALETTE());
628 #endif // wxUSE_PALETTE
631 // Copy image data into DIB data and then into DDB (in a loop)
633 unsigned char* pData
= rImage
.GetData();
638 unsigned char* ptdata
= pData
;
639 unsigned char* ptbits
;
641 for (n
= 0; n
< nNumDIB
; n
++)
643 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
646 // Redefine height and size of the (possibly) last smaller DIB
647 // memory is not reallocated
650 vHeader
.cy
= (DWORD
)(nHeight
);
651 vHeader
.cbImage
= nBytePerLine
* nHeight
;
654 for (j
= 0; j
< nHeight
; j
++)
656 for (i
= 0; i
< nWidth
; i
++)
658 *(ptbits
++) = *(ptdata
+ 2);
659 *(ptbits
++) = *(ptdata
+ 1);
660 *(ptbits
++) = *(ptdata
);
663 for (i
= 0; i
< nPadding
; i
++)
668 // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
669 // in combination with setting the bits into the selected bitmap
671 if ((lScans
= ::GpiSetBitmapBits( hPS
672 ,0 // Start at the bottom
673 ,(LONG
)nHeight
// One line per scan
681 vError
= ::WinGetLastError(vHabmain
);
682 sError
= wxPMErrorToStr(vError
);
688 hPSScreen
= ::GpiCreatePS( vHabmain
691 ,PU_PELS
| GPIA_ASSOC
694 POINTL vPoint
[4] = { 0, nOrigin
,
696 0, 0, nWidth
, nHeight
700 ::GpiBitBlt( hPSScreen
707 ::GpiDestroyPS(hPSScreen
);
710 SetHBITMAP((WXHBITMAP
)hBmp
);
713 ::GpiSelectPalette(hPS
, hOldPalette
);
714 #endif // wxUSE_PALETTE
717 // Similarly, created an mono-bitmap for the possible mask
719 if (rImage
.HasMask())
723 vHeader
.cy
= nHeight
;
725 vHeader
.cBitCount
= 24;
726 hBmp
= ::GpiCreateBitmap( hPS
732 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
734 nHeight
= nBmpHeight
;
736 nHeight
= nSizeLimit
/ nBytePerLine
;
737 vHeader
.cy
= (DWORD
)(nHeight
);
740 unsigned char cRed
= rImage
.GetMaskRed();
741 unsigned char cGreen
= rImage
.GetMaskGreen();
742 unsigned char cBlue
= rImage
.GetMaskBlue();
743 unsigned char cZero
= 0;
744 unsigned char cOne
= 255;
747 for (n
= 0; n
< nNumDIB
; n
++)
749 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
752 // Redefine height and size of the (possibly) last smaller DIB
753 // memory is not reallocated
756 vHeader
.cy
= (DWORD
)(nHeight
);
759 for (int j
= 0; j
< nHeight
; j
++)
761 for (i
= 0; i
< nWidth
; i
++)
763 if ((*(ptdata
++) != cRed
) || (*(ptdata
++) != cGreen
) || (*(ptdata
++) != cBlue
))
776 for (i
= 0; i
< nPadding
; i
++)
779 lScans
= ::GpiSetBitmapBits( hPS
780 ,0 // Start at the bottom
781 ,(LONG
)nHeight
// One line per scan
785 hPSScreen
= ::GpiCreatePS( vHabmain
788 ,PU_PELS
| GPIA_ASSOC
790 POINTL vPoint2
[4] = { 0, nOrigin
,
792 0, 0, nWidth
, nHeight
794 ::GpiBitBlt( hPSScreen
801 ::GpiDestroyPS(hPSScreen
);
806 // Create a wxMask object
808 wxMask
* pMask
= new wxMask();
810 pMask
->SetMaskBitmap((WXHBITMAP
)hBmp
);
812 hBmpOld
= ::GpiSetBitmap(hPS
, hBmpOld
);
816 // Free allocated resources
818 ::GpiSetBitmap(hPS
, NULLHANDLE
);
820 ::DevCloseDC(hDCScreen
);
824 } // end of wxBitmap::CreateFromImage
826 wxImage
wxBitmap::ConvertToImage() const
831 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
834 // Create an wxImage object
836 int nWidth
= GetWidth();
837 int nHeight
= GetHeight();
840 int nBytePerLine
= nWidth
* 3;
841 int nSizeDWORD
= sizeof(DWORD
);
842 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
844 unsigned char* pData
;
845 unsigned char* lpBits
;
847 BITMAPINFOHEADER2 vDIBh
;
848 BITMAPINFO2 vDIBInfo
;
853 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
854 SIZEL vSizlPage
= {0,0};
857 vImage
.Create( nWidth
860 pData
= vImage
.GetData();
863 wxFAIL_MSG( wxT("could not allocate data for image") );
866 if(nLineBoundary
> 0)
868 nPadding
= nSizeDWORD
- nLineBoundary
;
869 nBytePerLine
+= nPadding
;
871 wxDisplaySize( &nDevWidth
875 // Create and fill a DIB header
877 memset(&vDIBh
, '\0', 16);
882 vDIBh
.cBitCount
= 24;
884 memset(&vDIBInfo
, '\0', 16);
886 vDIBInfo
.cx
= nWidth
;
887 vDIBInfo
.cy
= nHeight
;
888 vDIBInfo
.cPlanes
= 1;
889 vDIBInfo
.cBitCount
= 24;
891 lpBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
894 wxFAIL_MSG(wxT("could not allocate data for DIB"));
898 memset(lpBits
, '\0', (nBytePerLine
* nHeight
));
899 hBitmap
= (HBITMAP
)GetHBITMAP();
902 // May already be selected into a PS
904 if ((pDC
= GetSelectedInto()) != NULL
)
906 hPSMem
= pDC
->GetHPS();
910 hDCMem
= ::DevOpenDC( vHabmain
917 hPSMem
= ::GpiCreatePS( vHabmain
920 ,PU_PELS
| GPIA_ASSOC
922 if ((hOldBitmap
= ::GpiSetBitmap(hPSMem
, hBitmap
)) == HBM_ERROR
)
927 vError
= ::WinGetLastError(vHabmain
);
928 sError
= wxPMErrorToStr(vError
);
933 // Copy data from the device-dependent bitmap to the DIB
935 if ((lScans
= ::GpiQueryBitmapBits( hPSMem
945 vError
= ::WinGetLastError(vHabmain
);
946 sError
= wxPMErrorToStr(vError
);
950 // Copy DIB data into the wxImage object
954 unsigned char* ptdata
= pData
;
955 unsigned char* ptbits
= lpBits
;
957 for (i
= 0; i
< nHeight
; i
++)
959 for (j
= 0; j
< nWidth
; j
++)
961 *(ptdata
++) = *(ptbits
+2);
962 *(ptdata
++) = *(ptbits
+1);
963 *(ptdata
++) = *(ptbits
);
968 if ((pDC
= GetSelectedInto()) == NULL
)
970 ::GpiSetBitmap(hPSMem
, NULLHANDLE
);
971 ::GpiDestroyPS(hPSMem
);
972 ::DevCloseDC(hDCMem
);
976 // Similarly, set data according to the possible mask bitmap
978 if (GetMask() && GetMask()->GetMaskBitmap())
980 hBitmap
= (HBITMAP
)GetMask()->GetMaskBitmap();
983 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
985 HDC hMemDC
= ::DevOpenDC( vHabmain
992 HPS hMemPS
= ::GpiCreatePS( vHabmain
995 ,PU_PELS
| GPIA_ASSOC
997 ::GpiSetColor(hMemPS
, OS2RGB(0, 0, 0));
998 ::GpiSetBackColor(hMemPS
, OS2RGB(255, 255, 255) );
999 ::GpiSetBitmap(hMemPS
, hBitmap
);
1000 ::GpiQueryBitmapBits( hPSMem
1006 ::GpiSetBitmap(hMemPS
, NULLHANDLE
);
1007 ::GpiDestroyPS(hMemPS
);
1008 ::DevCloseDC(hMemDC
);
1011 // Background color set to RGB(16,16,16) in consistent with wxGTK
1013 unsigned char ucRed
= 16;
1014 unsigned char ucGreen
= 16;
1015 unsigned char ucBlue
= 16;
1019 for (i
= 0; i
< nHeight
; i
++)
1021 for (j
= 0; j
< nWidth
; j
++)
1027 *(ptdata
++) = ucRed
;
1028 *(ptdata
++) = ucGreen
;
1029 *(ptdata
++) = ucBlue
;
1035 vImage
.SetMaskColour( ucRed
1039 vImage
.SetMask(TRUE
);
1043 vImage
.SetMask(FALSE
);
1047 // Free allocated resources
1051 } // end of wxBitmap::ConvertToImage
1053 // ----------------------------------------------------------------------------
1054 // sub bitmap extraction
1055 // ----------------------------------------------------------------------------
1057 wxBitmap
wxBitmap::GetSubBitmap(
1061 wxCHECK_MSG( Ok() &&
1062 (rRect
.x
>= 0) && (rRect
.y
>= 0) &&
1063 (rRect
.x
+ rRect
.width
<= GetWidth()) &&
1064 (rRect
.y
+ rRect
.height
<= GetHeight()),
1065 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1067 wxBitmap
vRet( rRect
.width
1071 wxASSERT_MSG( vRet
.Ok(), wxT("GetSubBitmap error") );
1077 SIZEL vSize
= {0, 0};
1078 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1079 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1080 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1081 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1082 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1083 POINTL vPoint
[4] = { 0, 0, rRect
.width
, rRect
.height
,
1085 rRect
.x
+ rRect
.width
, rRect
.y
+ rRect
.height
1088 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1089 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1099 // Copy mask if there is one
1103 BITMAPINFOHEADER2 vBmih
;
1105 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1106 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1107 vBmih
.cx
= rRect
.width
;
1108 vBmih
.cy
= rRect
.height
;
1110 vBmih
.cBitCount
= 1;
1112 HBITMAP hBmpMask
= ::GpiCreateBitmap( hPSDst
1119 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1120 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1122 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1123 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpMask
);
1132 wxMask
* pMask
= new wxMask((WXHBITMAP
)hBmpMask
);
1133 vRet
.SetMask(pMask
);
1136 ::GpiSetBitmap(hPSSrc
, NULL
);
1137 ::GpiSetBitmap(hPSDst
, NULL
);
1138 ::GpiDestroyPS(hPSSrc
);
1139 ::GpiDestroyPS(hPSDst
);
1140 ::DevCloseDC(hDCSrc
);
1141 ::DevCloseDC(hDCDst
);
1143 } // end of wxBitmap::GetSubBitmap
1145 // ----------------------------------------------------------------------------
1146 // wxBitmap accessors
1147 // ----------------------------------------------------------------------------
1149 void wxBitmap::SetQuality(
1155 GetBitmapData()->m_nQuality
= nQ
;
1156 } // end of wxBitmap::SetQuality
1158 #if WXWIN_COMPATIBILITY_2
1159 void wxBitmap::SetOk(
1165 GetBitmapData()->m_bOk
= bOk
;
1166 } // end of wxBitmap::SetOk
1167 #endif // WXWIN_COMPATIBILITY_2
1169 void wxBitmap::SetPalette(
1170 const wxPalette
& rPalette
1175 GetBitmapData()->m_vBitmapPalette
= rPalette
;
1176 } // end of wxBitmap::SetPalette
1178 void wxBitmap::SetMask(
1184 GetBitmapData()->m_pBitmapMask
= pMask
;
1185 } // end of wxBitmap::SetMask
1187 wxBitmap
wxBitmap::GetBitmapForDC(
1192 } // end of wxBitmap::GetBitmapForDC
1194 // ----------------------------------------------------------------------------
1196 // ----------------------------------------------------------------------------
1201 } // end of wxMask::wxMask
1203 // Construct a mask from a bitmap and a colour indicating
1204 // the transparent area
1206 const wxBitmap
& rBitmap
1207 , const wxColour
& rColour
1214 } // end of wxMask::wxMask
1216 // Construct a mask from a bitmap and a palette index indicating
1217 // the transparent area
1219 const wxBitmap
& rBitmap
1227 } // end of wxMask::wxMask
1229 // Construct a mask from a mono bitmap (copies the bitmap).
1231 const wxBitmap
& rBitmap
1236 } // end of wxMask::wxMask
1241 ::GpiDeleteBitmap((HBITMAP
)m_hMaskBitmap
);
1242 } // end of wxMask::~wxMask
1244 // Create a mask from a mono bitmap (copies the bitmap).
1245 bool wxMask::Create(
1246 const wxBitmap
& rBitmap
1249 BITMAPINFOHEADER2 vBmih
;
1250 SIZEL vSize
= {0, 0};
1251 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1252 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1253 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1254 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1255 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1256 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1257 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1262 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1265 if (!rBitmap
.Ok() || rBitmap
.GetDepth() != 1)
1270 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1271 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1272 vBmih
.cx
= rBitmap
.GetWidth();
1273 vBmih
.cy
= rBitmap
.GetHeight();
1275 vBmih
.cBitCount
= 1;
1277 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1284 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1285 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1294 ::GpiDestroyPS(hPSSrc
);
1295 ::GpiDestroyPS(hPSDst
);
1296 ::DevCloseDC(hDCSrc
);
1297 ::DevCloseDC(hDCDst
);
1299 } // end of wxMask::Create
1301 // Create a mask from a bitmap and a palette index indicating
1302 // the transparent area
1303 bool wxMask::Create(
1304 const wxBitmap
& rBitmap
1310 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1313 if (rBitmap
.Ok() && rBitmap
.GetPalette()->Ok())
1316 unsigned char cGreen
;
1317 unsigned char cBlue
;
1319 if (rBitmap
.GetPalette()->GetRGB( nPaletteIndex
1325 wxColour
vTransparentColour( cRed
1330 return (Create( rBitmap
1336 } // end of wxMask::Create
1338 // Create a mask from a bitmap and a colour indicating
1339 // the transparent area
1340 bool wxMask::Create(
1341 const wxBitmap
& rBitmap
1342 , const wxColour
& rColour
1346 COLORREF vMaskColour
= OS2RGB( rColour
.Red()
1350 BITMAPINFOHEADER2 vBmih
;
1351 SIZEL vSize
= {0, 0};
1352 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1353 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1354 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1355 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1356 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1357 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1358 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1363 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1372 // Scan the bitmap for the transparent colour and set
1373 // the corresponding pixels in the mask to BLACK and
1374 // the rest to WHITE
1377 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1378 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1379 vBmih
.cx
= rBitmap
.GetWidth();
1380 vBmih
.cy
= rBitmap
.GetHeight();
1382 vBmih
.cBitCount
= 1;
1384 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1391 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1392 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1395 // This is not very efficient, but I can't think
1396 // of a better way of doing it
1398 for (int w
= 0; w
< rBitmap
.GetWidth(); w
++)
1400 for (int h
= 0; h
< rBitmap
.GetHeight(); h
++)
1402 POINTL vPt
= {w
, h
};
1403 COLORREF vCol
= (COLORREF
)::GpiQueryPel(hPSSrc
, &vPt
);
1404 if (vCol
== (COLORREF
)CLR_NOINDEX
)
1407 // Doesn't make sense to continue
1413 if (vCol
== vMaskColour
)
1415 ::GpiSetColor(hPSDst
, OS2RGB(0, 0, 0));
1416 ::GpiSetPel(hPSDst
, &vPt
);
1420 ::GpiSetColor(hPSDst
, OS2RGB(255, 255, 255));
1421 ::GpiSetPel(hPSDst
, &vPt
);
1425 ::GpiSetBitmap(hPSSrc
, NULL
);
1426 ::GpiSetBitmap(hPSDst
, NULL
);
1427 ::GpiDestroyPS(hPSSrc
);
1428 ::GpiDestroyPS(hPSDst
);
1429 ::DevCloseDC(hDCSrc
);
1430 ::DevCloseDC(hDCDst
);
1432 } // end of wxMask::Create
1434 // ----------------------------------------------------------------------------
1436 // ----------------------------------------------------------------------------
1438 bool wxBitmapHandler::Create(
1447 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1451 return(pBitmap
? Create( pBitmap
1459 bool wxBitmapHandler::Load(
1461 , const wxString
& rName
1468 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1472 return(pBitmap
? LoadFile( pBitmap
1481 bool wxBitmapHandler::Save(
1483 , const wxString
& rName
1487 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1491 return(pBitmap
? SaveFile( pBitmap
1497 bool wxBitmapHandler::Create(
1498 wxBitmap
* WXUNUSED(pBitmap
)
1499 , void* WXUNUSED(pData
)
1500 , long WXUNUSED(lType
)
1501 , int WXUNUSED(nWidth
)
1502 , int WXUNUSED(nHeight
)
1503 , int WXUNUSED(nDepth
)
1509 bool wxBitmapHandler::LoadFile(
1510 wxBitmap
* WXUNUSED(pBitmap
)
1511 , const wxString
& WXUNUSED(rName
)
1513 , long WXUNUSED(lType
)
1514 , int WXUNUSED(nDesiredWidth
)
1515 , int WXUNUSED(nDesiredHeight
)
1521 bool wxBitmapHandler::SaveFile(
1522 wxBitmap
* WXUNUSED(pBitmap
)
1523 , const wxString
& WXUNUSED(rName
)
1524 , int WXUNUSED(nType
)
1525 , const wxPalette
* WXUNUSED(pPalette
)
1531 // ----------------------------------------------------------------------------
1532 // Utility functions
1533 // ----------------------------------------------------------------------------
1534 HBITMAP
wxInvertMask(
1540 HBITMAP hBmpInvMask
= 0;
1542 wxCHECK_MSG( hBmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1545 // Get width/height from the bitmap if not given
1547 if (!nWidth
|| !nHeight
)
1549 BITMAPINFOHEADER2 vBmhdr
;
1551 ::GpiQueryBitmapInfoHeader( hBmpMask
1554 nWidth
= (int)vBmhdr
.cx
;
1555 nHeight
= (int)vBmhdr
.cy
;
1558 BITMAPINFOHEADER2 vBmih
;
1559 SIZEL vSize
= {0, 0};
1560 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1561 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1562 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1563 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1564 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1565 POINTL vPoint
[4] = { 0 ,0, nWidth
, nHeight
,
1566 0, 0, nWidth
, nHeight
1569 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1570 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1574 vBmih
.cBitCount
= 1;
1576 hBmpInvMask
= ::GpiCreateBitmap( hPSDst
1583 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) hBmpMask
);
1584 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpInvMask
);
1594 ::GpiDestroyPS(hPSSrc
);
1595 ::GpiDestroyPS(hPSDst
);
1596 ::DevCloseDC(hDCSrc
);
1597 ::DevCloseDC(hDCDst
);
1600 } // end of WxWinGdi_InvertMask