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
= 16; // 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 lScans
= ::GpiSetBitmapBits( hPS
665 ,0 // Start at the bottom
666 ,(LONG
)nHeight
// One line per scan
670 hPSScreen
= ::GpiCreatePS( vHabmain
673 ,PU_PELS
| GPIA_ASSOC
676 POINTL vPoint
[4] = { 0, nOrigin
,
678 0, 0, nWidth
, nHeight
682 ::GpiBitBlt( hPSScreen
689 ::GpiDestroyPS(hPSScreen
);
692 SetHBITMAP((WXHBITMAP
)hBmp
);
695 ::GpiSelectPalette(hPS
, hOldPalette
);
696 #endif // wxUSE_PALETTE
699 // Similarly, created an mono-bitmap for the possible mask
701 if (rImage
.HasMask())
703 vHeader
.cbFix
= sizeof(BITMAPINFOHEADER2
);
705 vHeader
.cy
= nHeight
;
707 vHeader
.cBitCount
= 1;
708 hBmp
= ::GpiCreateBitmap( hPS
714 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
716 nHeight
= nBmpHeight
;
718 nHeight
= nSizeLimit
/ nBytePerLine
;
719 vHeader
.cy
= (DWORD
)(nHeight
);
720 vHeader
.cbImage
= nBytePerLine
* nHeight
;
723 unsigned char cRed
= rImage
.GetMaskRed();
724 unsigned char cGreen
= rImage
.GetMaskGreen();
725 unsigned char cBlue
= rImage
.GetMaskBlue();
726 unsigned char cZero
= 0;
727 unsigned char cOne
= 255;
730 for (n
= 0; n
< nNumDIB
; n
++)
732 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
735 // Redefine height and size of the (possibly) last smaller DIB
736 // memory is not reallocated
739 vHeader
.cy
= (DWORD
)(nHeight
);
740 vHeader
.cbImage
= nBytePerLine
* nHeight
;
743 for (int j
= 0; j
< nHeight
; j
++)
745 for (i
= 0; i
< nWidth
; i
++)
747 if ((*(ptdata
++) != cRed
) || (*(ptdata
++) != cGreen
) || (*(ptdata
++) != cBlue
))
760 for (i
= 0; i
< nPadding
; i
++)
763 lScans
= ::GpiSetBitmapBits( hPS
764 ,0 // Start at the bottom
765 ,(LONG
)nHeight
// One line per scan
769 hPSScreen
= ::GpiCreatePS( vHabmain
772 ,PU_PELS
| GPIA_ASSOC
774 POINTL vPoint2
[4] = { 0, nOrigin
,
776 0, 0, nWidth
, nHeight
778 ::GpiBitBlt( hPSScreen
785 ::GpiDestroyPS(hPSScreen
);
790 // Create a wxMask object
792 wxMask
* pMask
= new wxMask();
794 pMask
->SetMaskBitmap((WXHBITMAP
)hBmp
);
796 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
800 // Free allocated resources
802 ::GpiSetBitmap(hPS
, NULLHANDLE
);
807 } // end of wxBitmap::CreateFromImage
809 wxImage
wxBitmap::ConvertToImage() const
813 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
816 // Create an wxImage object
818 int nWidth
= GetWidth();
819 int nHeight
= GetHeight();
822 int nBytePerLine
= nWidth
* 3;
823 int nSizeDWORD
= sizeof(DWORD
);
824 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
826 unsigned char* pData
;
827 unsigned char* lpBits
;
829 BITMAPINFOHEADER2 vDIBh
;
830 BITMAPINFO2 vDIBInfo
;
832 PSZ pszData
[4] = { "Display", NULL
, NULL
, NULL
};
835 SIZEL vSizlPage
= {0,0};
838 vImage
.Create( nWidth
841 pData
= vImage
.GetData();
844 wxFAIL_MSG( wxT("could not allocate data for image") );
847 if(nLineBoundary
> 0)
849 nPadding
= nSizeDWORD
- nLineBoundary
;
850 nBytePerLine
+= nPadding
;
852 wxDisplaySize( &nDevWidth
856 // Create and fill a DIB header
858 memset(&vDIBh
, '\0', 16);
863 vDIBh
.cBitCount
= 24;
865 memset(&vDIBInfo
, '\0', 16);
867 vDIBInfo
.cx
= nWidth
;
868 vDIBInfo
.cy
= nHeight
;
869 vDIBInfo
.cPlanes
= 1;
870 vDIBInfo
.cBitCount
= 24;
872 lpBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
875 wxFAIL_MSG(wxT("could not allocate data for DIB"));
881 // Copy data from the device-dependent bitmap to the DIB
883 hDCMem
= ::DevOpenDC( vHabmain
887 ,(PDEVOPENDATA
)pszData
890 hPSMem
= ::GpiCreatePS( vHabmain
893 ,PU_PELS
| GPIA_ASSOC
| GPIT_MICRO
895 hBitmap
= ::GpiCreateBitmap( hPSMem
901 ::GpiSetBitmap(hPSMem
, hBitmap
);
902 lScans
= ::GpiQueryBitmapBits( hPSMem
910 // Copy DIB data into the wxImage object
914 unsigned char* ptdata
= pData
;
915 unsigned char* ptbits
= lpBits
;
917 for (i
= 0; i
< nHeight
; i
++)
919 for (j
= 0; j
< nWidth
; j
++)
921 *(ptdata
++) = *(ptbits
+2);
922 *(ptdata
++) = *(ptbits
+1);
923 *(ptdata
++) = *(ptbits
);
928 ::GpiSetBitmap(hPSMem
, NULLHANDLE
);
931 // Similarly, set data according to the possible mask bitmap
933 if (GetMask() && GetMask()->GetMaskBitmap())
935 hBitmap
= (HBITMAP
)GetMask()->GetMaskBitmap();
938 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
940 HDC hMemDC
= ::DevOpenDC( vHabmain
944 ,(PDEVOPENDATA
)pszData
947 HPS hMemPS
= ::GpiCreatePS( vHabmain
950 ,PU_PELS
| GPIA_ASSOC
| GPIT_MICRO
952 ::GpiSetColor(hMemPS
, OS2RGB(0, 0, 0));
953 ::GpiSetBackColor(hMemPS
, OS2RGB(255, 255, 255) );
954 ::GpiSetBitmap(hMemPS
, hBitmap
);
955 ::GpiQueryBitmapBits( hPSMem
961 ::GpiSetBitmap(hMemPS
, NULLHANDLE
);
962 ::GpiDestroyPS(hMemPS
);
963 ::DevCloseDC(hMemDC
);
966 // Background color set to RGB(16,16,16) in consistent with wxGTK
968 unsigned char ucRed
= 16;
969 unsigned char ucGreen
= 16;
970 unsigned char ucBlue
= 16;
974 for (i
= 0; i
< nHeight
; i
++)
976 for (j
= 0; j
< nWidth
; j
++)
983 *(ptdata
++) = ucGreen
;
984 *(ptdata
++) = ucBlue
;
990 vImage
.SetMaskColour( ucRed
994 vImage
.SetMask(TRUE
);
998 vImage
.SetMask(FALSE
);
1002 // Free allocated resources
1004 ::GpiDestroyPS(hPSMem
);
1005 ::DevCloseDC(hDCMem
);
1008 } // end of wxBitmap::ConvertToImage
1010 // ----------------------------------------------------------------------------
1011 // sub bitmap extraction
1012 // ----------------------------------------------------------------------------
1014 wxBitmap
wxBitmap::GetSubBitmap(
1018 wxCHECK_MSG( Ok() &&
1019 (rRect
.x
>= 0) && (rRect
.y
>= 0) &&
1020 (rRect
.x
+ rRect
.width
<= GetWidth()) &&
1021 (rRect
.y
+ rRect
.height
<= GetHeight()),
1022 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1024 wxBitmap
vRet( rRect
.width
1028 wxASSERT_MSG( vRet
.Ok(), wxT("GetSubBitmap error") );
1034 SIZEL vSize
= {0, 0};
1035 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1036 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1037 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1038 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1039 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1040 POINTL vPoint
[4] = { 0, 0, rRect
.width
, rRect
.height
,
1042 rRect
.x
+ rRect
.width
, rRect
.y
+ rRect
.height
1045 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1046 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1056 // Copy mask if there is one
1060 BITMAPINFOHEADER2 vBmih
;
1062 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1063 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1064 vBmih
.cx
= rRect
.width
;
1065 vBmih
.cy
= rRect
.height
;
1067 vBmih
.cBitCount
= 1;
1069 HBITMAP hBmpMask
= ::GpiCreateBitmap( hPSDst
1076 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1077 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1079 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1080 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpMask
);
1089 wxMask
* pMask
= new wxMask((WXHBITMAP
)hBmpMask
);
1090 vRet
.SetMask(pMask
);
1093 ::GpiSetBitmap(hPSSrc
, NULL
);
1094 ::GpiSetBitmap(hPSDst
, NULL
);
1095 ::GpiDestroyPS(hPSSrc
);
1096 ::GpiDestroyPS(hPSDst
);
1097 ::DevCloseDC(hDCSrc
);
1098 ::DevCloseDC(hDCDst
);
1100 } // end of wxBitmap::GetSubBitmap
1102 // ----------------------------------------------------------------------------
1103 // wxBitmap accessors
1104 // ----------------------------------------------------------------------------
1106 void wxBitmap::SetQuality(
1112 GetBitmapData()->m_nQuality
= nQ
;
1113 } // end of wxBitmap::SetQuality
1115 #if WXWIN_COMPATIBILITY_2
1116 void wxBitmap::SetOk(
1122 GetBitmapData()->m_bOk
= bOk
;
1123 } // end of wxBitmap::SetOk
1124 #endif // WXWIN_COMPATIBILITY_2
1126 void wxBitmap::SetPalette(
1127 const wxPalette
& rPalette
1132 GetBitmapData()->m_vBitmapPalette
= rPalette
;
1133 } // end of wxBitmap::SetPalette
1135 void wxBitmap::SetMask(
1141 GetBitmapData()->m_pBitmapMask
= pMask
;
1142 } // end of wxBitmap::SetMask
1144 wxBitmap
wxBitmap::GetBitmapForDC(
1149 } // end of wxBitmap::GetBitmapForDC
1151 // ----------------------------------------------------------------------------
1153 // ----------------------------------------------------------------------------
1158 } // end of wxMask::wxMask
1160 // Construct a mask from a bitmap and a colour indicating
1161 // the transparent area
1163 const wxBitmap
& rBitmap
1164 , const wxColour
& rColour
1171 } // end of wxMask::wxMask
1173 // Construct a mask from a bitmap and a palette index indicating
1174 // the transparent area
1176 const wxBitmap
& rBitmap
1184 } // end of wxMask::wxMask
1186 // Construct a mask from a mono bitmap (copies the bitmap).
1188 const wxBitmap
& rBitmap
1193 } // end of wxMask::wxMask
1198 ::GpiDeleteBitmap((HBITMAP
)m_hMaskBitmap
);
1199 } // end of wxMask::~wxMask
1201 // Create a mask from a mono bitmap (copies the bitmap).
1202 bool wxMask::Create(
1203 const wxBitmap
& rBitmap
1206 BITMAPINFOHEADER2 vBmih
;
1207 SIZEL vSize
= {0, 0};
1208 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1209 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1210 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1211 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1212 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1213 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1214 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1219 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1222 if (!rBitmap
.Ok() || rBitmap
.GetDepth() != 1)
1227 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1228 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1229 vBmih
.cx
= rBitmap
.GetWidth();
1230 vBmih
.cy
= rBitmap
.GetHeight();
1232 vBmih
.cBitCount
= 1;
1234 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1241 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1242 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1251 ::GpiDestroyPS(hPSSrc
);
1252 ::GpiDestroyPS(hPSDst
);
1253 ::DevCloseDC(hDCSrc
);
1254 ::DevCloseDC(hDCDst
);
1256 } // end of wxMask::Create
1258 // Create a mask from a bitmap and a palette index indicating
1259 // the transparent area
1260 bool wxMask::Create(
1261 const wxBitmap
& rBitmap
1267 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1270 if (rBitmap
.Ok() && rBitmap
.GetPalette()->Ok())
1273 unsigned char cGreen
;
1274 unsigned char cBlue
;
1276 if (rBitmap
.GetPalette()->GetRGB( nPaletteIndex
1282 wxColour
vTransparentColour( cRed
1287 return (Create( rBitmap
1293 } // end of wxMask::Create
1295 // Create a mask from a bitmap and a colour indicating
1296 // the transparent area
1297 bool wxMask::Create(
1298 const wxBitmap
& rBitmap
1299 , const wxColour
& rColour
1303 COLORREF vMaskColour
= OS2RGB( rColour
.Red()
1307 BITMAPINFOHEADER2 vBmih
;
1308 SIZEL vSize
= {0, 0};
1309 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1310 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1311 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1312 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1313 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1314 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1315 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1320 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1329 // Scan the bitmap for the transparent colour and set
1330 // the corresponding pixels in the mask to BLACK and
1331 // the rest to WHITE
1334 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1335 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1336 vBmih
.cx
= rBitmap
.GetWidth();
1337 vBmih
.cy
= rBitmap
.GetHeight();
1339 vBmih
.cBitCount
= 1;
1341 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1348 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1349 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1352 // This is not very efficient, but I can't think
1353 // of a better way of doing it
1355 for (int w
= 0; w
< rBitmap
.GetWidth(); w
++)
1357 for (int h
= 0; h
< rBitmap
.GetHeight(); h
++)
1359 POINTL vPt
= {w
, h
};
1360 COLORREF vCol
= (COLORREF
)::GpiQueryPel(hPSSrc
, &vPt
);
1361 if (vCol
== (COLORREF
)CLR_NOINDEX
)
1364 // Doesn't make sense to continue
1370 if (vCol
== vMaskColour
)
1372 ::GpiSetColor(hPSDst
, OS2RGB(0, 0, 0));
1373 ::GpiSetPel(hPSDst
, &vPt
);
1377 ::GpiSetColor(hPSDst
, OS2RGB(255, 255, 255));
1378 ::GpiSetPel(hPSDst
, &vPt
);
1382 ::GpiSetBitmap(hPSSrc
, NULL
);
1383 ::GpiSetBitmap(hPSDst
, NULL
);
1384 ::GpiDestroyPS(hPSSrc
);
1385 ::GpiDestroyPS(hPSDst
);
1386 ::DevCloseDC(hDCSrc
);
1387 ::DevCloseDC(hDCDst
);
1389 } // end of wxMask::Create
1391 // ----------------------------------------------------------------------------
1393 // ----------------------------------------------------------------------------
1395 bool wxBitmapHandler::Create(
1404 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1408 return(pBitmap
? Create( pBitmap
1416 bool wxBitmapHandler::Load(
1418 , const wxString
& rName
1425 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1429 return(pBitmap
? LoadFile( pBitmap
1438 bool wxBitmapHandler::Save(
1440 , const wxString
& rName
1444 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1448 return(pBitmap
? SaveFile( pBitmap
1454 bool wxBitmapHandler::Create(
1455 wxBitmap
* WXUNUSED(pBitmap
)
1456 , void* WXUNUSED(pData
)
1457 , long WXUNUSED(lType
)
1458 , int WXUNUSED(nWidth
)
1459 , int WXUNUSED(nHeight
)
1460 , int WXUNUSED(nDepth
)
1466 bool wxBitmapHandler::LoadFile(
1467 wxBitmap
* WXUNUSED(pBitmap
)
1468 , const wxString
& WXUNUSED(rName
)
1470 , long WXUNUSED(lType
)
1471 , int WXUNUSED(nDesiredWidth
)
1472 , int WXUNUSED(nDesiredHeight
)
1478 bool wxBitmapHandler::SaveFile(
1479 wxBitmap
* WXUNUSED(pBitmap
)
1480 , const wxString
& WXUNUSED(rName
)
1481 , int WXUNUSED(nType
)
1482 , const wxPalette
* WXUNUSED(pPalette
)
1488 // ----------------------------------------------------------------------------
1489 // Utility functions
1490 // ----------------------------------------------------------------------------
1491 HBITMAP
wxInvertMask(
1497 HBITMAP hBmpInvMask
= 0;
1499 wxCHECK_MSG( hBmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1502 // Get width/height from the bitmap if not given
1504 if (!nWidth
|| !nHeight
)
1506 BITMAPINFOHEADER2 vBmhdr
;
1508 ::GpiQueryBitmapInfoHeader( hBmpMask
1511 nWidth
= (int)vBmhdr
.cx
;
1512 nHeight
= (int)vBmhdr
.cy
;
1515 BITMAPINFOHEADER2 vBmih
;
1516 SIZEL vSize
= {0, 0};
1517 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1518 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1519 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1520 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1521 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1522 POINTL vPoint
[4] = { 0 ,0, nWidth
, nHeight
,
1523 0, 0, nWidth
, nHeight
1526 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1527 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1531 vBmih
.cBitCount
= 1;
1533 hBmpInvMask
= ::GpiCreateBitmap( hPSDst
1540 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) hBmpMask
);
1541 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpInvMask
);
1551 ::GpiDestroyPS(hPSSrc
);
1552 ::GpiDestroyPS(hPSDst
);
1553 ::DevCloseDC(hDCSrc
);
1554 ::DevCloseDC(hDCDst
);
1557 } // end of WxWinGdi_InvertMask