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)");
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("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
|= (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
= nDepth
;
246 vHeader
.usReserved
= 0;
248 memset(&vInfo
, '\0', 16);
250 vInfo
.cx
= (USHORT
)nWidth
;
251 vInfo
.cy
= (USHORT
)nHeight
;
253 vInfo
.cBitCount
= nDepth
;
255 HBITMAP hBmp
= ::GpiCreateBitmap(hPs
, &vHeader
, CBM_INIT
, (PBYTE
)pzData
, &vInfo
);
259 wxLogLastError("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
= 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(
415 HPS hPs
= NULLHANDLE
;
419 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
425 m_refData
= new wxBitmapRefData
;
427 return(pHandler
->LoadFile( this
438 } // end of wxBitmap::LoadFile
440 bool wxBitmap::Create(
450 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
456 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
457 "type %d defined."), lType
);
462 m_refData
= new wxBitmapRefData
;
464 return(pHandler
->Create( this
471 } // end of wxBitmap::Create
473 bool wxBitmap::SaveFile(
474 const wxString
& rFilename
476 , const wxPalette
* pPalette
479 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
485 return pHandler
->SaveFile( this
493 // FIXME what about palette? shouldn't we use it?
494 wxImage vImage
= ConvertToImage();
499 return(vImage
.SaveFile( rFilename
503 } // end of wxBitmap::SaveFile
506 // ----------------------------------------------------------------------------
507 // wxImage-wxBitmap conversion
508 // ----------------------------------------------------------------------------
510 bool wxBitmap::CreateFromImage (
511 const wxImage
& rImage
515 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
516 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
);
560 nDepth
= wxDisplayDepth();
565 // Copy the palette from the source image
567 SetPalette(rImage
.GetPalette());
568 #endif // wxUSE_PALETTE
571 // Create a DIB header
573 BITMAPINFOHEADER2 vHeader
;
577 // Fill in the DIB header
579 memset(&vHeader
, '\0', 16);
581 vHeader
.cx
= (ULONG
)nWidth
;
582 vHeader
.cy
= (ULONG
)nHeight
;
583 vHeader
.cPlanes
= 1L;
584 vHeader
.cBitCount
= 24;
587 // Memory for DIB data
589 unsigned char* pucBits
;
591 pucBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
594 wxFAIL_MSG(wxT("could not allocate memory for DIB"));
597 memset(pucBits
, '\0', (nBytePerLine
* nHeight
));
600 // Create and set the device-dependent bitmap
602 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
603 SIZEL vSize
= {0, 0};
604 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
605 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
607 HDC hDCScreen
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
612 memset(&vInfo
, '\0', 16);
614 vInfo
.cx
= (ULONG
)nWidth
;
615 vInfo
.cy
= (ULONG
)nHeight
;
617 vInfo
.cBitCount
= 24; // Set to desired count going in
619 hBmp
= ::GpiCreateBitmap( hPS
626 HPAL hOldPalette
= NULLHANDLE
;
627 if (rImage
.GetPalette().Ok())
629 hOldPalette
= ::GpiSelectPalette(hPS
, (HPAL
)rImage
.GetPalette().GetHPALETTE());
631 #endif // wxUSE_PALETTE
634 // Copy image data into DIB data and then into DDB (in a loop)
636 unsigned char* pData
= rImage
.GetData();
641 unsigned char* ptdata
= pData
;
642 unsigned char* ptbits
;
644 if ((hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
)) == HBM_ERROR
)
649 vError
= ::WinGetLastError(vHabmain
);
650 sError
= wxPMErrorToStr(vError
);
652 for (n
= 0; n
< nNumDIB
; n
++)
654 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
657 // Redefine height and size of the (possibly) last smaller DIB
658 // memory is not reallocated
661 vHeader
.cy
= (DWORD
)(nHeight
);
662 vHeader
.cbImage
= nBytePerLine
* nHeight
;
665 for (j
= 0; j
< nHeight
; j
++)
667 for (i
= 0; i
< nWidth
; i
++)
669 *(ptbits
++) = *(ptdata
+ 2);
670 *(ptbits
++) = *(ptdata
+ 1);
671 *(ptbits
++) = *(ptdata
);
674 for (i
= 0; i
< nPadding
; i
++)
679 // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
680 // in combination with setting the bits into the selected bitmap
682 if ((lScans
= ::GpiSetBitmapBits( hPS
683 ,0 // Start at the bottom
684 ,(LONG
)nHeight
// One line per scan
692 vError
= ::WinGetLastError(vHabmain
);
693 sError
= wxPMErrorToStr(vError
);
695 hPSScreen
= ::GpiCreatePS( vHabmain
698 ,PU_PELS
| GPIA_ASSOC
701 POINTL vPoint
[4] = { 0, nOrigin
,
703 0, 0, nWidth
, nHeight
707 ::GpiBitBlt( hPSScreen
714 ::GpiDestroyPS(hPSScreen
);
717 SetHBITMAP((WXHBITMAP
)hBmp
);
720 ::GpiSelectPalette(hPS
, hOldPalette
);
721 #endif // wxUSE_PALETTE
724 // Similarly, created an mono-bitmap for the possible mask
726 if (rImage
.HasMask())
730 vHeader
.cy
= nHeight
;
732 vHeader
.cBitCount
= 24;
733 hBmp
= ::GpiCreateBitmap( hPS
739 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
741 nHeight
= nBmpHeight
;
743 nHeight
= nSizeLimit
/ nBytePerLine
;
744 vHeader
.cy
= (DWORD
)(nHeight
);
747 unsigned char cRed
= rImage
.GetMaskRed();
748 unsigned char cGreen
= rImage
.GetMaskGreen();
749 unsigned char cBlue
= rImage
.GetMaskBlue();
750 unsigned char cZero
= 0;
751 unsigned char cOne
= 255;
754 for (n
= 0; n
< nNumDIB
; n
++)
756 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
759 // Redefine height and size of the (possibly) last smaller DIB
760 // memory is not reallocated
763 vHeader
.cy
= (DWORD
)(nHeight
);
764 vHeader
.cbImage
= nBytePerLine
* nHeight
;
767 for (int j
= 0; j
< nHeight
; j
++)
769 for (i
= 0; i
< nWidth
; i
++)
771 unsigned char cRedImage
= (*(ptdata
++)) ;
772 unsigned char cGreenImage
= (*(ptdata
++)) ;
773 unsigned char cBlueImage
= (*(ptdata
++)) ;
775 if ((cRedImage
!= cRed
) || (cGreenImage
!= cGreen
) || (cBlueImage
!= cBlue
))
788 for (i
= 0; i
< nPadding
; i
++)
791 lScans
= ::GpiSetBitmapBits( hPS
792 ,0 // Start at the bottom
793 ,(LONG
)nHeight
// One line per scan
797 hPSScreen
= ::GpiCreatePS( vHabmain
800 ,PU_PELS
| GPIA_ASSOC
802 POINTL vPoint2
[4] = { 0, nOrigin
,
804 0, 0, nWidth
, nHeight
806 ::GpiBitBlt( hPSScreen
813 ::GpiDestroyPS(hPSScreen
);
818 // Create a wxMask object
820 wxMask
* pMask
= new wxMask();
822 pMask
->SetMaskBitmap((WXHBITMAP
)hBmp
);
824 hBmpOld
= ::GpiSetBitmap(hPS
, hBmpOld
);
828 // Free allocated resources
830 ::GpiSetBitmap(hPS
, NULLHANDLE
);
832 ::DevCloseDC(hDCScreen
);
836 } // end of wxBitmap::CreateFromImage
838 wxImage
wxBitmap::ConvertToImage() const
843 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
846 // Create an wxImage object
848 int nWidth
= GetWidth();
849 int nHeight
= GetHeight();
852 int nBytePerLine
= nWidth
* 3;
853 int nSizeDWORD
= sizeof(DWORD
);
854 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
856 unsigned char* pData
;
857 unsigned char* lpBits
;
859 BITMAPINFOHEADER2 vDIBh
;
860 BITMAPINFO2 vDIBInfo
;
865 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
866 SIZEL vSizlPage
= {0,0};
869 vImage
.Create( nWidth
872 pData
= vImage
.GetData();
875 wxFAIL_MSG( wxT("could not allocate data for image") );
878 if(nLineBoundary
> 0)
880 nPadding
= nSizeDWORD
- nLineBoundary
;
881 nBytePerLine
+= nPadding
;
883 wxDisplaySize( &nDevWidth
887 // Create and fill a DIB header
889 memset(&vDIBh
, '\0', 16);
894 vDIBh
.cBitCount
= 24;
896 memset(&vDIBInfo
, '\0', 16);
898 vDIBInfo
.cx
= nWidth
;
899 vDIBInfo
.cy
= nHeight
;
900 vDIBInfo
.cPlanes
= 1;
901 vDIBInfo
.cBitCount
= 24;
903 lpBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
906 wxFAIL_MSG(wxT("could not allocate data for DIB"));
910 memset(lpBits
, '\0', (nBytePerLine
* nHeight
));
911 hBitmap
= (HBITMAP
)GetHBITMAP();
914 // May already be selected into a PS
916 if ((pDC
= GetSelectedInto()) != NULL
)
918 hPSMem
= pDC
->GetHPS();
922 hDCMem
= ::DevOpenDC( vHabmain
929 hPSMem
= ::GpiCreatePS( vHabmain
932 ,PU_PELS
| GPIA_ASSOC
935 if ((hOldBitmap
= ::GpiSetBitmap(hPSMem
, hBitmap
)) == HBM_ERROR
)
940 vError
= ::WinGetLastError(vHabmain
);
941 sError
= wxPMErrorToStr(vError
);
945 // Copy data from the device-dependent bitmap to the DIB
947 if ((lScans
= ::GpiQueryBitmapBits( hPSMem
957 vError
= ::WinGetLastError(vHabmain
);
958 sError
= wxPMErrorToStr(vError
);
962 // Copy DIB data into the wxImage object
966 unsigned char* ptdata
= pData
;
967 unsigned char* ptbits
= lpBits
;
969 for (i
= 0; i
< nHeight
; i
++)
971 for (j
= 0; j
< nWidth
; j
++)
973 *(ptdata
++) = *(ptbits
+2);
974 *(ptdata
++) = *(ptbits
+1);
975 *(ptdata
++) = *(ptbits
);
980 if ((pDC
= GetSelectedInto()) == NULL
)
982 ::GpiSetBitmap(hPSMem
, NULLHANDLE
);
983 ::GpiDestroyPS(hPSMem
);
984 ::DevCloseDC(hDCMem
);
988 // Similarly, set data according to the possible mask bitmap
990 if (GetMask() && GetMask()->GetMaskBitmap())
992 hBitmap
= (HBITMAP
)GetMask()->GetMaskBitmap();
995 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
997 HDC hMemDC
= ::DevOpenDC( vHabmain
1001 ,(PDEVOPENDATA
)&vDop
1004 HPS hMemPS
= ::GpiCreatePS( vHabmain
1007 ,PU_PELS
| GPIA_ASSOC
1009 ::GpiSetColor(hMemPS
, OS2RGB(0, 0, 0));
1010 ::GpiSetBackColor(hMemPS
, OS2RGB(255, 255, 255) );
1011 ::GpiSetBitmap(hMemPS
, hBitmap
);
1012 ::GpiQueryBitmapBits( hPSMem
1018 ::GpiSetBitmap(hMemPS
, NULLHANDLE
);
1019 ::GpiDestroyPS(hMemPS
);
1020 ::DevCloseDC(hMemDC
);
1023 // Background color set to RGB(16,16,16) in consistent with wxGTK
1025 unsigned char ucRed
= 16;
1026 unsigned char ucGreen
= 16;
1027 unsigned char ucBlue
= 16;
1031 for (i
= 0; i
< nHeight
; i
++)
1033 for (j
= 0; j
< nWidth
; j
++)
1039 *(ptdata
++) = ucRed
;
1040 *(ptdata
++) = ucGreen
;
1041 *(ptdata
++) = ucBlue
;
1047 vImage
.SetMaskColour( ucRed
1051 vImage
.SetMask(TRUE
);
1055 vImage
.SetMask(FALSE
);
1059 // Free allocated resources
1063 } // end of wxBitmap::ConvertToImage
1065 // ----------------------------------------------------------------------------
1066 // sub bitmap extraction
1067 // ----------------------------------------------------------------------------
1069 wxBitmap
wxBitmap::GetSubBitmap(
1073 wxCHECK_MSG( Ok() &&
1074 (rRect
.x
>= 0) && (rRect
.y
>= 0) &&
1075 (rRect
.x
+ rRect
.width
<= GetWidth()) &&
1076 (rRect
.y
+ rRect
.height
<= GetHeight()),
1077 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1079 wxBitmap
vRet( rRect
.width
1083 wxASSERT_MSG( vRet
.Ok(), wxT("GetSubBitmap error") );
1089 SIZEL vSize
= {0, 0};
1090 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1091 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1092 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1093 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1094 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1095 POINTL vPoint
[4] = { 0, 0, rRect
.width
, rRect
.height
,
1097 rRect
.x
+ rRect
.width
, rRect
.y
+ rRect
.height
1100 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1101 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1111 // Copy mask if there is one
1115 BITMAPINFOHEADER2 vBmih
;
1117 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1118 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1119 vBmih
.cx
= rRect
.width
;
1120 vBmih
.cy
= rRect
.height
;
1122 vBmih
.cBitCount
= 24;
1124 HBITMAP hBmpMask
= ::GpiCreateBitmap( hPSDst
1131 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1132 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1134 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1135 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpMask
);
1144 wxMask
* pMask
= new wxMask((WXHBITMAP
)hBmpMask
);
1145 vRet
.SetMask(pMask
);
1148 ::GpiSetBitmap(hPSSrc
, NULL
);
1149 ::GpiSetBitmap(hPSDst
, NULL
);
1150 ::GpiDestroyPS(hPSSrc
);
1151 ::GpiDestroyPS(hPSDst
);
1152 ::DevCloseDC(hDCSrc
);
1153 ::DevCloseDC(hDCDst
);
1155 } // end of wxBitmap::GetSubBitmap
1157 // ----------------------------------------------------------------------------
1158 // wxBitmap accessors
1159 // ----------------------------------------------------------------------------
1161 void wxBitmap::SetQuality(
1167 GetBitmapData()->m_nQuality
= nQ
;
1168 } // end of wxBitmap::SetQuality
1170 void wxBitmap::SetPalette(
1171 const wxPalette
& rPalette
1176 GetBitmapData()->m_vBitmapPalette
= rPalette
;
1177 } // end of wxBitmap::SetPalette
1179 void wxBitmap::SetMask(
1185 GetBitmapData()->m_pBitmapMask
= pMask
;
1186 } // end of wxBitmap::SetMask
1188 wxBitmap
wxBitmap::GetBitmapForDC(
1193 } // end of wxBitmap::GetBitmapForDC
1195 // ----------------------------------------------------------------------------
1197 // ----------------------------------------------------------------------------
1202 } // end of wxMask::wxMask
1204 // Construct a mask from a bitmap and a colour indicating
1205 // the transparent area
1207 const wxBitmap
& rBitmap
1208 , const wxColour
& rColour
1215 } // end of wxMask::wxMask
1217 // Construct a mask from a bitmap and a palette index indicating
1218 // the transparent area
1220 const wxBitmap
& rBitmap
1228 } // end of wxMask::wxMask
1230 // Construct a mask from a mono bitmap (copies the bitmap).
1232 const wxBitmap
& rBitmap
1237 } // end of wxMask::wxMask
1242 ::GpiDeleteBitmap((HBITMAP
)m_hMaskBitmap
);
1243 } // end of wxMask::~wxMask
1245 // Create a mask from a mono bitmap (copies the bitmap).
1246 bool wxMask::Create(
1247 const wxBitmap
& rBitmap
1250 BITMAPINFOHEADER2 vBmih
;
1251 SIZEL vSize
= {0, 0};
1252 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1253 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1254 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1255 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1256 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1257 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1258 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1263 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1266 if (!rBitmap
.Ok() || rBitmap
.GetDepth() != 1)
1271 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1272 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1273 vBmih
.cx
= rBitmap
.GetWidth();
1274 vBmih
.cy
= rBitmap
.GetHeight();
1276 vBmih
.cBitCount
= 24;
1278 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1285 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1286 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1295 ::GpiDestroyPS(hPSSrc
);
1296 ::GpiDestroyPS(hPSDst
);
1297 ::DevCloseDC(hDCSrc
);
1298 ::DevCloseDC(hDCDst
);
1300 } // end of wxMask::Create
1302 // Create a mask from a bitmap and a palette index indicating
1303 // the transparent area
1304 bool wxMask::Create(
1305 const wxBitmap
& rBitmap
1311 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1314 if (rBitmap
.Ok() && rBitmap
.GetPalette()->Ok())
1317 unsigned char cGreen
;
1318 unsigned char cBlue
;
1320 if (rBitmap
.GetPalette()->GetRGB( nPaletteIndex
1326 wxColour
vTransparentColour( cRed
1331 return (Create( rBitmap
1337 } // end of wxMask::Create
1339 // Create a mask from a bitmap and a colour indicating
1340 // the transparent area
1341 bool wxMask::Create(
1342 const wxBitmap
& rBitmap
1343 , const wxColour
& rColour
1347 COLORREF vMaskColour
= OS2RGB( rColour
.Red()
1351 BITMAPINFOHEADER2 vBmih
;
1352 SIZEL vSize
= {0, 0};
1353 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1354 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1355 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1356 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1357 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1358 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1359 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1364 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1373 // Scan the bitmap for the transparent colour and set
1374 // the corresponding pixels in the mask to BLACK and
1375 // the rest to WHITE
1378 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1379 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1380 vBmih
.cx
= rBitmap
.GetWidth();
1381 vBmih
.cy
= rBitmap
.GetHeight();
1383 vBmih
.cBitCount
= 1;
1385 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1392 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1393 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1396 // This is not very efficient, but I can't think
1397 // of a better way of doing it
1399 for (int w
= 0; w
< rBitmap
.GetWidth(); w
++)
1401 for (int h
= 0; h
< rBitmap
.GetHeight(); h
++)
1403 POINTL vPt
= {w
, h
};
1404 COLORREF vCol
= (COLORREF
)::GpiQueryPel(hPSSrc
, &vPt
);
1405 if (vCol
== (COLORREF
)CLR_NOINDEX
)
1408 // Doesn't make sense to continue
1414 if (vCol
== vMaskColour
)
1416 ::GpiSetColor(hPSDst
, OS2RGB(0, 0, 0));
1417 ::GpiSetPel(hPSDst
, &vPt
);
1421 ::GpiSetColor(hPSDst
, OS2RGB(255, 255, 255));
1422 ::GpiSetPel(hPSDst
, &vPt
);
1426 ::GpiSetBitmap(hPSSrc
, NULL
);
1427 ::GpiSetBitmap(hPSDst
, NULL
);
1428 ::GpiDestroyPS(hPSSrc
);
1429 ::GpiDestroyPS(hPSDst
);
1430 ::DevCloseDC(hDCSrc
);
1431 ::DevCloseDC(hDCDst
);
1433 } // end of wxMask::Create
1435 // ----------------------------------------------------------------------------
1437 // ----------------------------------------------------------------------------
1439 bool wxBitmapHandler::Create(
1448 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1452 return(pBitmap
? Create( pBitmap
1460 bool wxBitmapHandler::Load(
1468 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1472 return(pBitmap
? LoadFile( pBitmap
1480 bool wxBitmapHandler::Save(
1482 , const wxString
& rName
1486 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1490 return(pBitmap
? SaveFile( pBitmap
1496 bool wxBitmapHandler::Create(
1497 wxBitmap
* WXUNUSED(pBitmap
)
1498 , void* WXUNUSED(pData
)
1499 , long WXUNUSED(lType
)
1500 , int WXUNUSED(nWidth
)
1501 , int WXUNUSED(nHeight
)
1502 , int WXUNUSED(nDepth
)
1508 bool wxBitmapHandler::LoadFile(
1509 wxBitmap
* WXUNUSED(pBitmap
)
1511 , long WXUNUSED(lType
)
1512 , int WXUNUSED(nDesiredWidth
)
1513 , int WXUNUSED(nDesiredHeight
)
1519 bool wxBitmapHandler::SaveFile(
1520 wxBitmap
* WXUNUSED(pBitmap
)
1521 , const wxString
& WXUNUSED(rName
)
1522 , int WXUNUSED(nType
)
1523 , const wxPalette
* WXUNUSED(pPalette
)
1529 // ----------------------------------------------------------------------------
1530 // Utility functions
1531 // ----------------------------------------------------------------------------
1532 HBITMAP
wxInvertMask(
1538 HBITMAP hBmpInvMask
= 0;
1540 wxCHECK_MSG( hBmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1543 // Get width/height from the bitmap if not given
1545 if (!nWidth
|| !nHeight
)
1547 BITMAPINFOHEADER2 vBmhdr
;
1549 ::GpiQueryBitmapInfoHeader( hBmpMask
1552 nWidth
= (int)vBmhdr
.cx
;
1553 nHeight
= (int)vBmhdr
.cy
;
1556 BITMAPINFOHEADER2 vBmih
;
1557 SIZEL vSize
= {0, 0};
1558 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1559 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1560 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1561 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1562 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1563 POINTL vPoint
[4] = { 0 ,0, nWidth
, nHeight
,
1564 0, 0, nWidth
, nHeight
1567 memset(&vBmih
, '\0', 16);
1572 vBmih
.cBitCount
= 24;
1574 hBmpInvMask
= ::GpiCreateBitmap( hPSDst
1581 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) hBmpMask
);
1582 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpInvMask
);
1592 ::GpiDestroyPS(hPSSrc
);
1593 ::GpiDestroyPS(hPSDst
);
1594 ::DevCloseDC(hDCSrc
);
1595 ::DevCloseDC(hDCDst
);
1598 } // end of WxWinGdi_InvertMask