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(wxT("GpiDeleteBitmap(hbitmap)"));
72 if (!::GpiDeleteBitmap((HBITMAP
)m_hBitmap
))
74 wxLogLastError(wxT("GpiDeleteBitmap(hbitmap)"));
82 } // end of wxBitmapRefData::Free
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // this function should be called from all wxBitmap ctors
93 // True for all bitmaps created from bits, wxImages, Xpms
95 } // end of wxBitmap::Init
97 bool wxBitmap::CopyFromIconOrCursor(
98 const wxGDIImage
& rIcon
101 HPOINTER hIcon
= (HPOINTER
)rIcon
.GetHandle();
102 POINTERINFO SIconInfo
;
104 if (!::WinQueryPointerInfo(hIcon
, &SIconInfo
))
106 wxLogLastError(wxT("WinQueryPointerInfo"));
109 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
111 m_refData
= pRefData
;
113 int nWidth
= rIcon
.GetWidth();
114 int nHeight
= rIcon
.GetHeight();
116 pRefData
->m_nWidth
= nWidth
;
117 pRefData
->m_nHeight
= nHeight
;
118 pRefData
->m_nDepth
= wxDisplayDepth();
120 pRefData
->m_hBitmap
= (WXHBITMAP
)SIconInfo
.hbmColor
;
122 wxMask
* pMask
= new wxMask(SIconInfo
.hbmPointer
);
124 pMask
->SetMaskBitmap(GetHBITMAP());
128 } // end of wxBitmap::CopyFromIconOrCursor
130 bool wxBitmap::CopyFromCursor(
131 const wxCursor
& rCursor
138 return(CopyFromIconOrCursor(rCursor
));
139 } // end of wxBitmap::CopyFromCursor
141 bool wxBitmap::CopyFromIcon(
150 return CopyFromIconOrCursor(rIcon
);
151 } // end of wxBitmap::CopyFromIcon
153 wxBitmap::~wxBitmap()
155 } // end of wxBitmap::~wxBitmap
166 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
167 BITMAPINFOHEADER2 vHeader
;
171 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
172 SIZEL vSize
= {0, 0};
175 wxASSERT(vHabmain
!= NULL
);
177 m_refData
= pRefData
;
179 pRefData
->m_nWidth
= nWidth
;
180 pRefData
->m_nHeight
= nHeight
;
181 pRefData
->m_nDepth
= nDepth
;
182 pRefData
->m_nNumColors
= 0;
183 pRefData
->m_pSelectedInto
= NULL
;
185 hDc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
186 hPs
= ::GpiCreatePS(vHabmain
, hDc
, &vSize
, GPIA_ASSOC
| PU_PELS
);
189 wxLogLastError(wxT("GpiCreatePS Failure"));
195 // We assume that it is in XBM format which is not quite the same as
196 // the format CreateBitmap() wants because the order of bytes in the
199 const size_t nBytesPerLine
= (nWidth
+ 7) / 8;
200 const size_t nPadding
= nBytesPerLine
% 2;
201 const size_t nLen
= nHeight
* (nPadding
+ nBytesPerLine
);
202 const char* pzSrc
= zBits
;
206 pzData
= (char *)malloc(nLen
);
208 char* pzDst
= pzData
;
210 for (nRows
= 0; nRows
< nHeight
; nRows
++)
212 for (nCols
= 0; nCols
< nBytesPerLine
; nCols
++)
214 unsigned char ucVal
= *pzSrc
++;
215 unsigned char ucReversed
= 0;
218 for (nBits
= 0; nBits
< 8; nBits
++)
221 ucReversed
= (unsigned char)(ucReversed
| (ucVal
& 0x01));
224 *pzDst
++ = ucReversed
;
233 // Bits should already be in Windows standard format
235 pzData
= (char *)zBits
; // const_cast is harmless
239 nDepth
= 24; // MAX supported in PM
240 memset(&vHeader
, '\0', 16);
242 vHeader
.cx
= (USHORT
)nWidth
;
243 vHeader
.cy
= (USHORT
)nHeight
;
244 vHeader
.cPlanes
= 1L;
245 vHeader
.cBitCount
= (USHORT
)nDepth
;
246 vHeader
.usReserved
= 0;
248 memset(&vInfo
, '\0', 16);
250 vInfo
.cx
= (USHORT
)nWidth
;
251 vInfo
.cy
= (USHORT
)nHeight
;
253 vInfo
.cBitCount
= (USHORT
)nDepth
;
255 HBITMAP hBmp
= ::GpiCreateBitmap(hPs
, &vHeader
, CBM_INIT
, (PBYTE
)pzData
, &vInfo
);
259 wxLogLastError(wxT("CreateBitmap"));
263 SetHBITMAP((WXHBITMAP
)hBmp
);
264 } // end of wxBitmap::wxBitmap
277 } // end of wxBitmap::wxBitmap
295 } // end of wxBitmap::wxBitmap
307 } // end of wxBitmap::wxBitmap
309 bool wxBitmap::Create(
316 BITMAPINFOHEADER2 vHeader
;
318 wxASSERT(vHabmain
!= NULL
);
320 m_refData
= new wxBitmapRefData
;
321 GetBitmapData()->m_nWidth
= nW
;
322 GetBitmapData()->m_nHeight
= nH
;
323 GetBitmapData()->m_nDepth
= nD
;
326 // Xpms and bitmaps from other images can also be mono's, but only
327 // mono's need help changing their colors with MemDC changes
331 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
332 SIZEL vSize
= {0, 0};
333 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
334 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
338 memset(&vHeader
, '\0', 16);
343 vHeader
.cBitCount
= 24; //nD;
345 hBmp
= ::GpiCreateBitmap( hPS
360 hPSScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
361 hDCScreen
= ::GpiQueryDevice(hPSScreen
);
362 ::DevQueryCaps(hDCScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitCount
);
367 memset(&vHeader
, '\0', 16);
372 vHeader
.cBitCount
= (USHORT
)lBitCount
;
374 hBmp
= ::GpiCreateBitmap( hPSScreen
381 GetBitmapData()->m_nDepth
= wxDisplayDepth();
382 ::WinReleasePS(hPSScreen
);
384 SetHBITMAP((WXHBITMAP
)hBmp
);
387 } // end of wxBitmap::Create
389 bool wxBitmap::CreateFromXpm(
393 #if wxUSE_IMAGE && wxUSE_XPM
396 wxCHECK_MSG(ppData
!= NULL
, false, wxT("invalid bitmap data"))
398 wxXPMDecoder vDecoder
;
399 wxImage vImg
= vDecoder
.ReadData(ppData
);
401 wxCHECK_MSG(vImg
.Ok(), false, wxT("invalid bitmap data"))
403 *this = wxBitmap(vImg
);
408 } // end of wxBitmap::CreateFromXpm
410 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
414 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
418 m_refData
= new wxBitmapRefData
;
420 return handler
->LoadFile(this, filename
, type
, -1, -1);
423 else // no bitmap handler found
426 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
428 *this = wxBitmap(image
);
433 #endif // wxUSE_IMAGE
438 bool wxBitmap::LoadFile(
445 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
451 m_refData
= new wxBitmapRefData
;
453 return(pHandler
->LoadFile( this
464 } // end of wxBitmap::LoadFile
466 bool wxBitmap::Create(
476 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
482 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), lType
);
487 m_refData
= new wxBitmapRefData
;
489 return(pHandler
->Create( this
496 } // end of wxBitmap::Create
498 bool wxBitmap::SaveFile(
499 const wxString
& rFilename
501 , const wxPalette
* pPalette
504 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
510 return pHandler
->SaveFile( this
518 // FIXME what about palette? shouldn't we use it?
519 wxImage vImage
= ConvertToImage();
524 return(vImage
.SaveFile( rFilename
528 } // end of wxBitmap::SaveFile
531 // ----------------------------------------------------------------------------
532 // wxImage-wxBitmap conversion
533 // ----------------------------------------------------------------------------
535 bool wxBitmap::CreateFromImage (
536 const wxImage
& rImage
540 wxCHECK_MSG(rImage
.Ok(), false, wxT("invalid image"));
541 m_refData
= new wxBitmapRefData();
543 int nSizeLimit
= 1024 * 768 * 3;
544 int nWidth
= rImage
.GetWidth();
545 int nBmpHeight
= rImage
.GetHeight();
546 int nBytePerLine
= nWidth
* 3;
547 int nSizeDWORD
= sizeof(DWORD
);
548 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
551 if (nLineBoundary
> 0)
553 nPadding
= nSizeDWORD
- nLineBoundary
;
554 nBytePerLine
+= nPadding
;
558 // Calc the number of DIBs and heights of DIBs
562 int nHeight
= nSizeLimit
/ nBytePerLine
;
564 if (nHeight
>= nBmpHeight
)
565 nHeight
= nBmpHeight
;
568 nNumDIB
= nBmpHeight
/ nHeight
;
569 nHRemain
= nBmpHeight
% nHeight
;
575 // Set bitmap parameters
577 wxCHECK_MSG(rImage
.Ok(), false, wxT("invalid image"));
579 SetHeight(nBmpHeight
);
585 nDepth
= wxDisplayDepth();
590 // Copy the palette from the source image
592 SetPalette(rImage
.GetPalette());
593 #endif // wxUSE_PALETTE
596 // Create a DIB header
598 BITMAPINFOHEADER2 vHeader
;
602 // Fill in the DIB header
604 memset(&vHeader
, '\0', 16);
606 vHeader
.cx
= (ULONG
)nWidth
;
607 vHeader
.cy
= (ULONG
)nHeight
;
608 vHeader
.cPlanes
= 1L;
609 vHeader
.cBitCount
= 24;
612 // Memory for DIB data
614 unsigned char* pucBits
;
616 pucBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
619 wxFAIL_MSG(wxT("could not allocate memory for DIB"));
622 memset(pucBits
, '\0', (nBytePerLine
* nHeight
));
625 // Create and set the device-dependent bitmap
627 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
628 SIZEL vSize
= {0, 0};
629 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
630 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
632 HDC hDCScreen
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
637 memset(&vInfo
, '\0', 16);
639 vInfo
.cx
= (ULONG
)nWidth
;
640 vInfo
.cy
= (ULONG
)nHeight
;
642 vInfo
.cBitCount
= 24; // Set to desired count going in
644 hBmp
= ::GpiCreateBitmap( hPS
651 HPAL hOldPalette
= NULLHANDLE
;
652 if (rImage
.GetPalette().Ok())
654 hOldPalette
= ::GpiSelectPalette(hPS
, (HPAL
)rImage
.GetPalette().GetHPALETTE());
656 #endif // wxUSE_PALETTE
659 // Copy image data into DIB data and then into DDB (in a loop)
661 unsigned char* pData
= rImage
.GetData();
666 unsigned char* ptdata
= pData
;
667 unsigned char* ptbits
;
669 if ((hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
)) == HBM_ERROR
)
674 vError
= ::WinGetLastError(vHabmain
);
675 sError
= wxPMErrorToStr(vError
);
677 for (n
= 0; n
< nNumDIB
; n
++)
679 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
682 // Redefine height and size of the (possibly) last smaller DIB
683 // memory is not reallocated
686 vHeader
.cy
= (DWORD
)(nHeight
);
687 vHeader
.cbImage
= nBytePerLine
* nHeight
;
690 for (j
= 0; j
< nHeight
; j
++)
692 for (i
= 0; i
< nWidth
; i
++)
694 *(ptbits
++) = *(ptdata
+ 2);
695 *(ptbits
++) = *(ptdata
+ 1);
696 *(ptbits
++) = *(ptdata
);
699 for (i
= 0; i
< nPadding
; i
++)
704 // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
705 // in combination with setting the bits into the selected bitmap
707 if ((lScans
= ::GpiSetBitmapBits( hPS
708 ,0 // Start at the bottom
709 ,(LONG
)nHeight
// One line per scan
717 vError
= ::WinGetLastError(vHabmain
);
718 sError
= wxPMErrorToStr(vError
);
720 hPSScreen
= ::GpiCreatePS( vHabmain
723 ,PU_PELS
| GPIA_ASSOC
726 POINTL vPoint
[4] = { {0, nOrigin
},
728 {0, 0}, {nWidth
, nHeight
}
732 ::GpiBitBlt( hPSScreen
739 ::GpiDestroyPS(hPSScreen
);
742 SetHBITMAP((WXHBITMAP
)hBmp
);
745 ::GpiSelectPalette(hPS
, hOldPalette
);
746 #endif // wxUSE_PALETTE
749 // Similarly, created an mono-bitmap for the possible mask
751 if (rImage
.HasMask())
755 vHeader
.cy
= nHeight
;
757 vHeader
.cBitCount
= 24;
758 hBmp
= ::GpiCreateBitmap( hPS
764 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
766 nHeight
= nBmpHeight
;
768 nHeight
= nSizeLimit
/ nBytePerLine
;
769 vHeader
.cy
= (DWORD
)(nHeight
);
772 unsigned char cRed
= rImage
.GetMaskRed();
773 unsigned char cGreen
= rImage
.GetMaskGreen();
774 unsigned char cBlue
= rImage
.GetMaskBlue();
775 unsigned char cZero
= 0;
776 unsigned char cOne
= 255;
779 for (n
= 0; n
< nNumDIB
; n
++)
781 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
784 // Redefine height and size of the (possibly) last smaller DIB
785 // memory is not reallocated
788 vHeader
.cy
= (DWORD
)(nHeight
);
789 vHeader
.cbImage
= nBytePerLine
* nHeight
;
792 for (int j
= 0; j
< nHeight
; j
++)
794 for (i
= 0; i
< nWidth
; i
++)
796 unsigned char cRedImage
= (*(ptdata
++)) ;
797 unsigned char cGreenImage
= (*(ptdata
++)) ;
798 unsigned char cBlueImage
= (*(ptdata
++)) ;
800 if ((cRedImage
!= cRed
) || (cGreenImage
!= cGreen
) || (cBlueImage
!= cBlue
))
813 for (i
= 0; i
< nPadding
; i
++)
816 lScans
= ::GpiSetBitmapBits( hPS
817 ,0 // Start at the bottom
818 ,(LONG
)nHeight
// One line per scan
822 hPSScreen
= ::GpiCreatePS( vHabmain
825 ,PU_PELS
| GPIA_ASSOC
827 POINTL vPoint2
[4] = { {0, nOrigin
},
829 {0, 0}, {nWidth
, nHeight
}
831 ::GpiBitBlt( hPSScreen
838 ::GpiDestroyPS(hPSScreen
);
843 // Create a wxMask object
845 wxMask
* pMask
= new wxMask();
847 pMask
->SetMaskBitmap((WXHBITMAP
)hBmp
);
849 hBmpOld
= ::GpiSetBitmap(hPS
, hBmpOld
);
853 // Free allocated resources
855 ::GpiSetBitmap(hPS
, NULLHANDLE
);
857 ::DevCloseDC(hDCScreen
);
861 } // end of wxBitmap::CreateFromImage
863 wxImage
wxBitmap::ConvertToImage() const
868 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
871 // Create an wxImage object
873 int nWidth
= GetWidth();
874 int nHeight
= GetHeight();
877 int nBytePerLine
= nWidth
* 3;
878 int nSizeDWORD
= sizeof(DWORD
);
879 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
881 unsigned char* pData
;
882 unsigned char* lpBits
;
884 BITMAPINFOHEADER2 vDIBh
;
885 BITMAPINFO2 vDIBInfo
;
889 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
890 SIZEL vSizlPage
= {0,0};
891 HDC hDCMem
= NULLHANDLE
;
893 vImage
.Create( nWidth
896 pData
= vImage
.GetData();
899 wxFAIL_MSG( wxT("could not allocate data for image") );
902 if(nLineBoundary
> 0)
904 nPadding
= nSizeDWORD
- nLineBoundary
;
905 nBytePerLine
+= nPadding
;
907 wxDisplaySize( &nDevWidth
911 // Create and fill a DIB header
913 memset(&vDIBh
, '\0', 16);
918 vDIBh
.cBitCount
= 24;
920 memset(&vDIBInfo
, '\0', 16);
922 vDIBInfo
.cx
= nWidth
;
923 vDIBInfo
.cy
= nHeight
;
924 vDIBInfo
.cPlanes
= 1;
925 vDIBInfo
.cBitCount
= 24;
927 lpBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
930 wxFAIL_MSG(wxT("could not allocate data for DIB"));
934 memset(lpBits
, '\0', (nBytePerLine
* nHeight
));
935 hBitmap
= (HBITMAP
)GetHBITMAP();
938 // May already be selected into a PS
940 if ((pDC
= GetSelectedInto()) != NULL
)
942 hPSMem
= pDC
->GetHPS();
946 hDCMem
= ::DevOpenDC( vHabmain
953 hPSMem
= ::GpiCreatePS( vHabmain
956 ,PU_PELS
| GPIA_ASSOC
959 if ((hOldBitmap
= ::GpiSetBitmap(hPSMem
, hBitmap
)) == HBM_ERROR
)
964 vError
= ::WinGetLastError(vHabmain
);
965 sError
= wxPMErrorToStr(vError
);
969 // Copy data from the device-dependent bitmap to the DIB
971 if ((lScans
= ::GpiQueryBitmapBits( hPSMem
981 vError
= ::WinGetLastError(vHabmain
);
982 sError
= wxPMErrorToStr(vError
);
986 // Copy DIB data into the wxImage object
990 unsigned char* ptdata
= pData
;
991 unsigned char* ptbits
= lpBits
;
993 for (i
= 0; i
< nHeight
; i
++)
995 for (j
= 0; j
< nWidth
; j
++)
997 *(ptdata
++) = *(ptbits
+2);
998 *(ptdata
++) = *(ptbits
+1);
999 *(ptdata
++) = *(ptbits
);
1004 if ((pDC
= GetSelectedInto()) == NULL
)
1006 ::GpiSetBitmap(hPSMem
, NULLHANDLE
);
1007 ::GpiDestroyPS(hPSMem
);
1008 ::DevCloseDC(hDCMem
);
1012 // Similarly, set data according to the possible mask bitmap
1014 if (GetMask() && GetMask()->GetMaskBitmap())
1016 hBitmap
= (HBITMAP
)GetMask()->GetMaskBitmap();
1019 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
1021 HDC hMemDC
= ::DevOpenDC( vHabmain
1025 ,(PDEVOPENDATA
)&vDop
1028 HPS hMemPS
= ::GpiCreatePS( vHabmain
1031 ,PU_PELS
| GPIA_ASSOC
1033 ::GpiSetColor(hMemPS
, OS2RGB(0, 0, 0));
1034 ::GpiSetBackColor(hMemPS
, OS2RGB(255, 255, 255) );
1035 ::GpiSetBitmap(hMemPS
, hBitmap
);
1036 ::GpiQueryBitmapBits( hPSMem
1042 ::GpiSetBitmap(hMemPS
, NULLHANDLE
);
1043 ::GpiDestroyPS(hMemPS
);
1044 ::DevCloseDC(hMemDC
);
1047 // Background color set to RGB(16,16,16) in consistent with wxGTK
1049 unsigned char ucRed
= 16;
1050 unsigned char ucGreen
= 16;
1051 unsigned char ucBlue
= 16;
1055 for (i
= 0; i
< nHeight
; i
++)
1057 for (j
= 0; j
< nWidth
; j
++)
1063 *(ptdata
++) = ucRed
;
1064 *(ptdata
++) = ucGreen
;
1065 *(ptdata
++) = ucBlue
;
1071 vImage
.SetMaskColour( ucRed
1075 vImage
.SetMask(true);
1079 vImage
.SetMask(false);
1083 // Free allocated resources
1087 } // end of wxBitmap::ConvertToImage
1089 // ----------------------------------------------------------------------------
1090 // sub bitmap extraction
1091 // ----------------------------------------------------------------------------
1093 wxBitmap
wxBitmap::GetSubBitmap(
1097 wxCHECK_MSG( Ok() &&
1098 (rRect
.x
>= 0) && (rRect
.y
>= 0) &&
1099 (rRect
.x
+ rRect
.width
<= GetWidth()) &&
1100 (rRect
.y
+ rRect
.height
<= GetHeight()),
1101 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1103 wxBitmap
vRet( rRect
.width
1107 wxASSERT_MSG( vRet
.Ok(), wxT("GetSubBitmap error") );
1113 SIZEL vSize
= {0, 0};
1114 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1115 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1116 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1117 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1118 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1119 POINTL vPoint
[4] = { {0, 0}, {rRect
.width
, rRect
.height
},
1121 {rRect
.x
+ rRect
.width
, rRect
.y
+ rRect
.height
}
1124 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1125 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1135 // Copy mask if there is one
1139 BITMAPINFOHEADER2 vBmih
;
1141 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1142 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1143 vBmih
.cx
= rRect
.width
;
1144 vBmih
.cy
= rRect
.height
;
1146 vBmih
.cBitCount
= 24;
1148 HBITMAP hBmpMask
= ::GpiCreateBitmap( hPSDst
1155 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1156 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1158 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1159 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpMask
);
1168 wxMask
* pMask
= new wxMask((WXHBITMAP
)hBmpMask
);
1169 vRet
.SetMask(pMask
);
1172 ::GpiSetBitmap(hPSSrc
, NULL
);
1173 ::GpiSetBitmap(hPSDst
, NULL
);
1174 ::GpiDestroyPS(hPSSrc
);
1175 ::GpiDestroyPS(hPSDst
);
1176 ::DevCloseDC(hDCSrc
);
1177 ::DevCloseDC(hDCDst
);
1179 } // end of wxBitmap::GetSubBitmap
1181 // ----------------------------------------------------------------------------
1182 // wxBitmap accessors
1183 // ----------------------------------------------------------------------------
1185 void wxBitmap::SetQuality(
1191 GetBitmapData()->m_nQuality
= nQ
;
1192 } // end of wxBitmap::SetQuality
1194 void wxBitmap::SetPalette(
1195 const wxPalette
& rPalette
1200 GetBitmapData()->m_vBitmapPalette
= rPalette
;
1201 } // end of wxBitmap::SetPalette
1203 void wxBitmap::SetMask(
1209 GetBitmapData()->m_pBitmapMask
= pMask
;
1210 } // end of wxBitmap::SetMask
1212 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& WXUNUSED(rDc
)) const
1215 } // end of wxBitmap::GetBitmapForDC
1217 // ----------------------------------------------------------------------------
1219 // ----------------------------------------------------------------------------
1224 } // end of wxMask::wxMask
1226 // Construct a mask from a bitmap and a colour indicating
1227 // the transparent area
1229 const wxBitmap
& rBitmap
1230 , const wxColour
& rColour
1237 } // end of wxMask::wxMask
1239 // Construct a mask from a bitmap and a palette index indicating
1240 // the transparent area
1242 const wxBitmap
& rBitmap
1250 } // end of wxMask::wxMask
1252 // Construct a mask from a mono bitmap (copies the bitmap).
1254 const wxBitmap
& rBitmap
1259 } // end of wxMask::wxMask
1264 ::GpiDeleteBitmap((HBITMAP
)m_hMaskBitmap
);
1265 } // end of wxMask::~wxMask
1267 // Create a mask from a mono bitmap (copies the bitmap).
1268 bool wxMask::Create(
1269 const wxBitmap
& rBitmap
1272 BITMAPINFOHEADER2 vBmih
;
1273 SIZEL vSize
= {0, 0};
1274 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1275 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1276 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1277 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1278 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1279 POINTL vPoint
[4] = { {0 ,0}, {rBitmap
.GetWidth(), rBitmap
.GetHeight()},
1280 {0, 0}, {rBitmap
.GetWidth(), rBitmap
.GetHeight()}
1285 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1288 if (!rBitmap
.Ok() || rBitmap
.GetDepth() != 1)
1293 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1294 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1295 vBmih
.cx
= rBitmap
.GetWidth();
1296 vBmih
.cy
= rBitmap
.GetHeight();
1298 vBmih
.cBitCount
= 24;
1300 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1307 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1308 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1317 ::GpiDestroyPS(hPSSrc
);
1318 ::GpiDestroyPS(hPSDst
);
1319 ::DevCloseDC(hDCSrc
);
1320 ::DevCloseDC(hDCDst
);
1322 } // end of wxMask::Create
1324 // Create a mask from a bitmap and a palette index indicating
1325 // the transparent area
1326 bool wxMask::Create(
1327 const wxBitmap
& rBitmap
1333 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1336 if (rBitmap
.Ok() && rBitmap
.GetPalette()->Ok())
1339 unsigned char cGreen
;
1340 unsigned char cBlue
;
1342 if (rBitmap
.GetPalette()->GetRGB( nPaletteIndex
1348 wxColour
vTransparentColour( cRed
1353 return (Create( rBitmap
1359 } // end of wxMask::Create
1361 // Create a mask from a bitmap and a colour indicating
1362 // the transparent area
1363 bool wxMask::Create(
1364 const wxBitmap
& rBitmap
1365 , const wxColour
& rColour
1369 COLORREF vMaskColour
= OS2RGB( rColour
.Red()
1373 BITMAPINFOHEADER2 vBmih
;
1374 SIZEL vSize
= {0, 0};
1375 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1376 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1377 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1378 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1379 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1383 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1392 // Scan the bitmap for the transparent colour and set
1393 // the corresponding pixels in the mask to BLACK and
1394 // the rest to WHITE
1397 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1398 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1399 vBmih
.cx
= rBitmap
.GetWidth();
1400 vBmih
.cy
= rBitmap
.GetHeight();
1402 vBmih
.cBitCount
= 1;
1404 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1411 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1412 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1415 // This is not very efficient, but I can't think
1416 // of a better way of doing it
1418 for (int w
= 0; w
< rBitmap
.GetWidth(); w
++)
1420 for (int h
= 0; h
< rBitmap
.GetHeight(); h
++)
1422 POINTL vPt
= {w
, h
};
1423 COLORREF vCol
= (COLORREF
)::GpiQueryPel(hPSSrc
, &vPt
);
1424 if (vCol
== (COLORREF
)CLR_NOINDEX
)
1427 // Doesn't make sense to continue
1433 if (vCol
== vMaskColour
)
1435 ::GpiSetColor(hPSDst
, OS2RGB(0, 0, 0));
1436 ::GpiSetPel(hPSDst
, &vPt
);
1440 ::GpiSetColor(hPSDst
, OS2RGB(255, 255, 255));
1441 ::GpiSetPel(hPSDst
, &vPt
);
1445 ::GpiSetBitmap(hPSSrc
, NULL
);
1446 ::GpiSetBitmap(hPSDst
, NULL
);
1447 ::GpiDestroyPS(hPSSrc
);
1448 ::GpiDestroyPS(hPSDst
);
1449 ::DevCloseDC(hDCSrc
);
1450 ::DevCloseDC(hDCDst
);
1452 } // end of wxMask::Create
1454 // ----------------------------------------------------------------------------
1456 // ----------------------------------------------------------------------------
1458 bool wxBitmapHandler::Create( wxGDIImage
* pImage
,
1460 long WXUNUSED(lFlags
),
1465 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1469 return(pBitmap
? Create( pBitmap
1477 bool wxBitmapHandler::Load(
1485 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1489 return(pBitmap
? LoadFile( pBitmap
1497 bool wxBitmapHandler::Save(
1499 , const wxString
& rName
1503 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1507 return(pBitmap
? SaveFile( pBitmap
1513 bool wxBitmapHandler::Create(
1514 wxBitmap
* WXUNUSED(pBitmap
)
1515 , void* WXUNUSED(pData
)
1516 , long WXUNUSED(lType
)
1517 , int WXUNUSED(nWidth
)
1518 , int WXUNUSED(nHeight
)
1519 , int WXUNUSED(nDepth
)
1525 bool wxBitmapHandler::LoadFile(
1526 wxBitmap
* WXUNUSED(pBitmap
)
1528 , long WXUNUSED(lType
)
1529 , int WXUNUSED(nDesiredWidth
)
1530 , int WXUNUSED(nDesiredHeight
)
1536 bool wxBitmapHandler::LoadFile(
1537 wxBitmap
* WXUNUSED(pBitmap
)
1538 , const wxString
& WXUNUSED(rName
)
1539 , long WXUNUSED(lType
)
1540 , int WXUNUSED(nDesiredWidth
)
1541 , int WXUNUSED(nDesiredHeight
)
1547 bool wxBitmapHandler::SaveFile(
1548 wxBitmap
* WXUNUSED(pBitmap
)
1549 , const wxString
& WXUNUSED(rName
)
1550 , int WXUNUSED(nType
)
1551 , const wxPalette
* WXUNUSED(pPalette
)
1557 // ----------------------------------------------------------------------------
1558 // Utility functions
1559 // ----------------------------------------------------------------------------
1560 HBITMAP
wxInvertMask(
1566 HBITMAP hBmpInvMask
= 0;
1568 wxCHECK_MSG( hBmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1571 // Get width/height from the bitmap if not given
1573 if (!nWidth
|| !nHeight
)
1575 BITMAPINFOHEADER2 vBmhdr
;
1577 ::GpiQueryBitmapInfoHeader( hBmpMask
1580 nWidth
= (int)vBmhdr
.cx
;
1581 nHeight
= (int)vBmhdr
.cy
;
1584 BITMAPINFOHEADER2 vBmih
;
1585 SIZEL vSize
= {0, 0};
1586 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1587 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1588 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1589 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1590 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1591 POINTL vPoint
[4] = { {0 ,0}, {nWidth
, nHeight
},
1592 {0, 0}, {nWidth
, nHeight
}
1595 memset(&vBmih
, '\0', 16);
1600 vBmih
.cBitCount
= 24;
1602 hBmpInvMask
= ::GpiCreateBitmap( hPSDst
1609 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) hBmpMask
);
1610 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpInvMask
);
1620 ::GpiDestroyPS(hPSSrc
);
1621 ::GpiDestroyPS(hPSDst
);
1622 ::DevCloseDC(hDCSrc
);
1623 ::DevCloseDC(hDCDst
);
1626 } // end of WxWinGdi_InvertMask