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
91 // True for all bitmaps created from bits, wxImages, Xpms
94 } // end of wxBitmap::Init
96 bool wxBitmap::CopyFromIconOrCursor(
97 const wxGDIImage
& rIcon
100 HPOINTER hIcon
= (HPOINTER
)rIcon
.GetHandle();
101 POINTERINFO SIconInfo
;
103 if (!::WinQueryPointerInfo(hIcon
, &SIconInfo
))
105 wxLogLastError(wxT("WinQueryPointerInfo"));
108 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
110 m_refData
= pRefData
;
112 int nWidth
= rIcon
.GetWidth();
113 int nHeight
= rIcon
.GetHeight();
115 pRefData
->m_nWidth
= nWidth
;
116 pRefData
->m_nHeight
= nHeight
;
117 pRefData
->m_nDepth
= wxDisplayDepth();
119 pRefData
->m_hBitmap
= (WXHBITMAP
)SIconInfo
.hbmColor
;
121 wxMask
* pMask
= new wxMask(SIconInfo
.hbmPointer
);
123 pMask
->SetMaskBitmap(GetHBITMAP());
127 } // end of wxBitmap::CopyFromIconOrCursor
129 bool wxBitmap::CopyFromCursor(
130 const wxCursor
& rCursor
137 return(CopyFromIconOrCursor(rCursor
));
138 } // end of wxBitmap::CopyFromCursor
140 bool wxBitmap::CopyFromIcon(
149 return CopyFromIconOrCursor(rIcon
);
150 } // end of wxBitmap::CopyFromIcon
152 wxBitmap::~wxBitmap()
154 } // end of wxBitmap::~wxBitmap
165 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
166 BITMAPINFOHEADER2 vHeader
;
170 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
171 SIZEL vSize
= {0, 0};
174 wxASSERT(vHabmain
!= NULL
);
176 m_refData
= pRefData
;
178 pRefData
->m_nWidth
= nWidth
;
179 pRefData
->m_nHeight
= nHeight
;
180 pRefData
->m_nDepth
= nDepth
;
181 pRefData
->m_nNumColors
= 0;
182 pRefData
->m_pSelectedInto
= NULL
;
184 hDc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
185 hPs
= ::GpiCreatePS(vHabmain
, hDc
, &vSize
, GPIA_ASSOC
| PU_PELS
);
188 wxLogLastError("GpiCreatePS Failure");
194 // We assume that it is in XBM format which is not quite the same as
195 // the format CreateBitmap() wants because the order of bytes in the
198 const size_t nBytesPerLine
= (nWidth
+ 7) / 8;
199 const size_t nPadding
= nBytesPerLine
% 2;
200 const size_t nLen
= nHeight
* (nPadding
+ nBytesPerLine
);
201 const char* pzSrc
= zBits
;
205 pzData
= (char *)malloc(nLen
);
207 char* pzDst
= pzData
;
209 for (nRows
= 0; nRows
< nHeight
; nRows
++)
211 for (nCols
= 0; nCols
< nBytesPerLine
; nCols
++)
213 unsigned char ucVal
= *pzSrc
++;
214 unsigned char ucReversed
= 0;
217 for (nBits
= 0; nBits
< 8; nBits
++)
220 ucReversed
|= (ucVal
& 0x01);
223 *pzDst
++ = ucReversed
;
232 // Bits should already be in Windows standard format
234 pzData
= (char *)zBits
; // const_cast is harmless
238 nDepth
= 24; // MAX supported in PM
239 memset(&vHeader
, '\0', 16);
241 vHeader
.cx
= (USHORT
)nWidth
;
242 vHeader
.cy
= (USHORT
)nHeight
;
243 vHeader
.cPlanes
= 1L;
244 vHeader
.cBitCount
= nDepth
;
245 vHeader
.usReserved
= 0;
247 memset(&vInfo
, '\0', 16);
249 vInfo
.cx
= (USHORT
)nWidth
;
250 vInfo
.cy
= (USHORT
)nHeight
;
252 vInfo
.cBitCount
= nDepth
;
254 HBITMAP hBmp
= ::GpiCreateBitmap(hPs
, &vHeader
, CBM_INIT
, (PBYTE
)pzData
, &vInfo
);
258 wxLogLastError("CreateBitmap");
262 SetHBITMAP((WXHBITMAP
)hBmp
);
263 } // end of wxBitmap::wxBitmap
277 } // end of wxBitmap::wxBitmap
295 } // end of wxBitmap::wxBitmap
298 const wxString
& rFilename
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
333 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
334 SIZEL vSize
= {0, 0};
335 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
336 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
);
386 #if WXWIN_COMPATIBILITY_2
387 GetBitmapData()->m_bOk
= hBmp
!= 0;
388 #endif // WXWIN_COMPATIBILITY_2
391 } // end of wxBitmap::Create
393 bool wxBitmap::CreateFromXpm(
397 #if wxUSE_IMAGE && wxUSE_XPM
400 wxCHECK_MSG(ppData
!= NULL
, FALSE
, wxT("invalid bitmap data"))
402 wxXPMDecoder vDecoder
;
403 wxImage vImg
= vDecoder
.ReadData(ppData
);
405 wxCHECK_MSG(vImg
.Ok(), FALSE
, wxT("invalid bitmap data"))
407 *this = wxBitmap(vImg
);
412 } // end of wxBitmap::CreateFromXpm
414 bool wxBitmap::LoadFile(
415 const wxString
& rFilename
419 HPS hPs
= NULLHANDLE
;
423 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
429 m_refData
= new wxBitmapRefData
;
431 return(pHandler
->LoadFile( this
443 if (!vImage
.LoadFile(rFilename
, lType
) || !vImage
.Ok() )
446 *this = wxBitmap(vImage
);
450 } // end of wxBitmap::LoadFile
452 bool wxBitmap::Create(
462 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
468 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
469 "type %d defined."), lType
);
474 m_refData
= new wxBitmapRefData
;
476 return(pHandler
->Create( this
483 } // end of wxBitmap::Create
485 bool wxBitmap::SaveFile(
486 const wxString
& rFilename
488 , const wxPalette
* pPalette
491 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
497 return pHandler
->SaveFile( this
505 // FIXME what about palette? shouldn't we use it?
506 wxImage vImage
= ConvertToImage();
511 return(vImage
.SaveFile( rFilename
515 } // end of wxBitmap::SaveFile
518 // ----------------------------------------------------------------------------
519 // wxImage-wxBitmap convertion
520 // ----------------------------------------------------------------------------
522 bool wxBitmap::CreateFromImage (
523 const wxImage
& rImage
527 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
528 m_refData
= new wxBitmapRefData();
531 int nSizeLimit
= 1024 * 768 * 3;
532 int nWidth
= rImage
.GetWidth();
533 int nBmpHeight
= rImage
.GetHeight();
534 int nBytePerLine
= nWidth
* 3;
535 int nSizeDWORD
= sizeof(DWORD
);
536 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
539 if (nLineBoundary
> 0)
541 nPadding
= nSizeDWORD
- nLineBoundary
;
542 nBytePerLine
+= nPadding
;
546 // Calc the number of DIBs and heights of DIBs
550 int nHeight
= nSizeLimit
/ nBytePerLine
;
552 if (nHeight
>= nBmpHeight
)
553 nHeight
= nBmpHeight
;
556 nNumDIB
= nBmpHeight
/ nHeight
;
557 nHRemain
= nBmpHeight
% nHeight
;
563 // Set bitmap parameters
565 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
567 SetHeight(nBmpHeight
);
569 nDepth
= wxDisplayDepth();
574 // Copy the palette from the source image
576 SetPalette(rImage
.GetPalette());
577 #endif // wxUSE_PALETTE
580 // Create a DIB header
582 BITMAPINFOHEADER2 vHeader
;
586 // Fill in the DIB header
588 memset(&vHeader
, '\0', 16);
590 vHeader
.cx
= (ULONG
)nWidth
;
591 vHeader
.cy
= (ULONG
)nHeight
;
592 vHeader
.cPlanes
= 1L;
593 vHeader
.cBitCount
= 24;
596 // Memory for DIB data
598 unsigned char* pucBits
;
600 pucBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
603 wxFAIL_MSG(wxT("could not allocate memory for DIB"));
606 memset(pucBits
, '\0', (nBytePerLine
* nHeight
));
609 // Create and set the device-dependent bitmap
611 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
612 SIZEL vSize
= {0, 0};
613 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
614 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
616 HDC hDCScreen
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
621 memset(&vInfo
, '\0', 16);
623 vInfo
.cx
= (ULONG
)nWidth
;
624 vInfo
.cy
= (ULONG
)nHeight
;
626 vInfo
.cBitCount
= 24; // Set to desired count going in
628 hBmp
= ::GpiCreateBitmap( hPS
635 HPAL hOldPalette
= NULLHANDLE
;
636 if (rImage
.GetPalette().Ok())
638 hOldPalette
= ::GpiSelectPalette(hPS
, (HPAL
)rImage
.GetPalette().GetHPALETTE());
640 #endif // wxUSE_PALETTE
643 // Copy image data into DIB data and then into DDB (in a loop)
645 unsigned char* pData
= rImage
.GetData();
650 unsigned char* ptdata
= pData
;
651 unsigned char* ptbits
;
653 if ((hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
)) == HBM_ERROR
)
658 vError
= ::WinGetLastError(vHabmain
);
659 sError
= wxPMErrorToStr(vError
);
661 for (n
= 0; n
< nNumDIB
; n
++)
663 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
666 // Redefine height and size of the (possibly) last smaller DIB
667 // memory is not reallocated
670 vHeader
.cy
= (DWORD
)(nHeight
);
671 vHeader
.cbImage
= nBytePerLine
* nHeight
;
674 for (j
= 0; j
< nHeight
; j
++)
676 for (i
= 0; i
< nWidth
; i
++)
678 *(ptbits
++) = *(ptdata
+ 2);
679 *(ptbits
++) = *(ptdata
+ 1);
680 *(ptbits
++) = *(ptdata
);
683 for (i
= 0; i
< nPadding
; i
++)
688 // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
689 // in combination with setting the bits into the selected bitmap
691 if ((lScans
= ::GpiSetBitmapBits( hPS
692 ,0 // Start at the bottom
693 ,(LONG
)nHeight
// One line per scan
701 vError
= ::WinGetLastError(vHabmain
);
702 sError
= wxPMErrorToStr(vError
);
704 hPSScreen
= ::GpiCreatePS( vHabmain
707 ,PU_PELS
| GPIA_ASSOC
710 POINTL vPoint
[4] = { 0, nOrigin
,
712 0, 0, nWidth
, nHeight
716 ::GpiBitBlt( hPSScreen
723 ::GpiDestroyPS(hPSScreen
);
726 SetHBITMAP((WXHBITMAP
)hBmp
);
729 ::GpiSelectPalette(hPS
, hOldPalette
);
730 #endif // wxUSE_PALETTE
733 // Similarly, created an mono-bitmap for the possible mask
735 if (rImage
.HasMask())
739 vHeader
.cy
= nHeight
;
741 vHeader
.cBitCount
= 24;
742 hBmp
= ::GpiCreateBitmap( hPS
748 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
750 nHeight
= nBmpHeight
;
752 nHeight
= nSizeLimit
/ nBytePerLine
;
753 vHeader
.cy
= (DWORD
)(nHeight
);
756 unsigned char cRed
= rImage
.GetMaskRed();
757 unsigned char cGreen
= rImage
.GetMaskGreen();
758 unsigned char cBlue
= rImage
.GetMaskBlue();
759 unsigned char cZero
= 0;
760 unsigned char cOne
= 255;
763 for (n
= 0; n
< nNumDIB
; n
++)
765 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
768 // Redefine height and size of the (possibly) last smaller DIB
769 // memory is not reallocated
772 vHeader
.cy
= (DWORD
)(nHeight
);
775 for (int j
= 0; j
< nHeight
; j
++)
777 for (i
= 0; i
< nWidth
; i
++)
779 unsigned char cRedImage
= (*(ptdata
++)) ;
780 unsigned char cGreenImage
= (*(ptdata
++)) ;
781 unsigned char cBlueImage
= (*(ptdata
++)) ;
783 if ((cRedImage
!= cRed
) || (cGreenImage
!= cGreen
) || (cBlueImage
!= cBlue
))
796 for (i
= 0; i
< nPadding
; i
++)
799 lScans
= ::GpiSetBitmapBits( hPS
800 ,0 // Start at the bottom
801 ,(LONG
)nHeight
// One line per scan
805 hPSScreen
= ::GpiCreatePS( vHabmain
808 ,PU_PELS
| GPIA_ASSOC
810 POINTL vPoint2
[4] = { 0, nOrigin
,
812 0, 0, nWidth
, nHeight
814 ::GpiBitBlt( hPSScreen
821 ::GpiDestroyPS(hPSScreen
);
826 // Create a wxMask object
828 wxMask
* pMask
= new wxMask();
830 pMask
->SetMaskBitmap((WXHBITMAP
)hBmp
);
832 hBmpOld
= ::GpiSetBitmap(hPS
, hBmpOld
);
836 // Free allocated resources
838 ::GpiSetBitmap(hPS
, NULLHANDLE
);
840 ::DevCloseDC(hDCScreen
);
844 } // end of wxBitmap::CreateFromImage
846 wxImage
wxBitmap::ConvertToImage() const
851 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
854 // Create an wxImage object
856 int nWidth
= GetWidth();
857 int nHeight
= GetHeight();
860 int nBytePerLine
= nWidth
* 3;
861 int nSizeDWORD
= sizeof(DWORD
);
862 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
864 unsigned char* pData
;
865 unsigned char* lpBits
;
867 BITMAPINFOHEADER2 vDIBh
;
868 BITMAPINFO2 vDIBInfo
;
873 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
874 SIZEL vSizlPage
= {0,0};
877 vImage
.Create( nWidth
880 pData
= vImage
.GetData();
883 wxFAIL_MSG( wxT("could not allocate data for image") );
886 if(nLineBoundary
> 0)
888 nPadding
= nSizeDWORD
- nLineBoundary
;
889 nBytePerLine
+= nPadding
;
891 wxDisplaySize( &nDevWidth
895 // Create and fill a DIB header
897 memset(&vDIBh
, '\0', 16);
902 vDIBh
.cBitCount
= 24;
904 memset(&vDIBInfo
, '\0', 16);
906 vDIBInfo
.cx
= nWidth
;
907 vDIBInfo
.cy
= nHeight
;
908 vDIBInfo
.cPlanes
= 1;
909 vDIBInfo
.cBitCount
= 24;
911 lpBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
914 wxFAIL_MSG(wxT("could not allocate data for DIB"));
918 memset(lpBits
, '\0', (nBytePerLine
* nHeight
));
919 hBitmap
= (HBITMAP
)GetHBITMAP();
922 // May already be selected into a PS
924 if ((pDC
= GetSelectedInto()) != NULL
)
926 hPSMem
= pDC
->GetHPS();
930 hDCMem
= ::DevOpenDC( vHabmain
937 hPSMem
= ::GpiCreatePS( vHabmain
940 ,PU_PELS
| GPIA_ASSOC
943 if ((hOldBitmap
= ::GpiSetBitmap(hPSMem
, hBitmap
)) == HBM_ERROR
)
948 vError
= ::WinGetLastError(vHabmain
);
949 sError
= wxPMErrorToStr(vError
);
953 // Copy data from the device-dependent bitmap to the DIB
955 if ((lScans
= ::GpiQueryBitmapBits( hPSMem
965 vError
= ::WinGetLastError(vHabmain
);
966 sError
= wxPMErrorToStr(vError
);
970 // Copy DIB data into the wxImage object
974 unsigned char* ptdata
= pData
;
975 unsigned char* ptbits
= lpBits
;
977 for (i
= 0; i
< nHeight
; i
++)
979 for (j
= 0; j
< nWidth
; j
++)
981 *(ptdata
++) = *(ptbits
+2);
982 *(ptdata
++) = *(ptbits
+1);
983 *(ptdata
++) = *(ptbits
);
988 if ((pDC
= GetSelectedInto()) == NULL
)
990 ::GpiSetBitmap(hPSMem
, NULLHANDLE
);
991 ::GpiDestroyPS(hPSMem
);
992 ::DevCloseDC(hDCMem
);
996 // Similarly, set data according to the possible mask bitmap
998 if (GetMask() && GetMask()->GetMaskBitmap())
1000 hBitmap
= (HBITMAP
)GetMask()->GetMaskBitmap();
1003 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
1005 HDC hMemDC
= ::DevOpenDC( vHabmain
1009 ,(PDEVOPENDATA
)&vDop
1012 HPS hMemPS
= ::GpiCreatePS( vHabmain
1015 ,PU_PELS
| GPIA_ASSOC
1017 ::GpiSetColor(hMemPS
, OS2RGB(0, 0, 0));
1018 ::GpiSetBackColor(hMemPS
, OS2RGB(255, 255, 255) );
1019 ::GpiSetBitmap(hMemPS
, hBitmap
);
1020 ::GpiQueryBitmapBits( hPSMem
1026 ::GpiSetBitmap(hMemPS
, NULLHANDLE
);
1027 ::GpiDestroyPS(hMemPS
);
1028 ::DevCloseDC(hMemDC
);
1031 // Background color set to RGB(16,16,16) in consistent with wxGTK
1033 unsigned char ucRed
= 16;
1034 unsigned char ucGreen
= 16;
1035 unsigned char ucBlue
= 16;
1039 for (i
= 0; i
< nHeight
; i
++)
1041 for (j
= 0; j
< nWidth
; j
++)
1047 *(ptdata
++) = ucRed
;
1048 *(ptdata
++) = ucGreen
;
1049 *(ptdata
++) = ucBlue
;
1055 vImage
.SetMaskColour( ucRed
1059 vImage
.SetMask(TRUE
);
1063 vImage
.SetMask(FALSE
);
1067 // Free allocated resources
1071 } // end of wxBitmap::ConvertToImage
1073 // ----------------------------------------------------------------------------
1074 // sub bitmap extraction
1075 // ----------------------------------------------------------------------------
1077 wxBitmap
wxBitmap::GetSubBitmap(
1081 wxCHECK_MSG( Ok() &&
1082 (rRect
.x
>= 0) && (rRect
.y
>= 0) &&
1083 (rRect
.x
+ rRect
.width
<= GetWidth()) &&
1084 (rRect
.y
+ rRect
.height
<= GetHeight()),
1085 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1087 wxBitmap
vRet( rRect
.width
1091 wxASSERT_MSG( vRet
.Ok(), wxT("GetSubBitmap error") );
1097 SIZEL vSize
= {0, 0};
1098 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1099 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1100 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1101 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1102 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1103 POINTL vPoint
[4] = { 0, 0, rRect
.width
, rRect
.height
,
1105 rRect
.x
+ rRect
.width
, rRect
.y
+ rRect
.height
1108 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1109 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1119 // Copy mask if there is one
1123 BITMAPINFOHEADER2 vBmih
;
1125 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1126 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1127 vBmih
.cx
= rRect
.width
;
1128 vBmih
.cy
= rRect
.height
;
1130 vBmih
.cBitCount
= 24;
1132 HBITMAP hBmpMask
= ::GpiCreateBitmap( hPSDst
1139 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1140 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1142 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1143 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpMask
);
1152 wxMask
* pMask
= new wxMask((WXHBITMAP
)hBmpMask
);
1153 vRet
.SetMask(pMask
);
1156 ::GpiSetBitmap(hPSSrc
, NULL
);
1157 ::GpiSetBitmap(hPSDst
, NULL
);
1158 ::GpiDestroyPS(hPSSrc
);
1159 ::GpiDestroyPS(hPSDst
);
1160 ::DevCloseDC(hDCSrc
);
1161 ::DevCloseDC(hDCDst
);
1163 } // end of wxBitmap::GetSubBitmap
1165 // ----------------------------------------------------------------------------
1166 // wxBitmap accessors
1167 // ----------------------------------------------------------------------------
1169 void wxBitmap::SetQuality(
1175 GetBitmapData()->m_nQuality
= nQ
;
1176 } // end of wxBitmap::SetQuality
1178 #if WXWIN_COMPATIBILITY_2
1179 void wxBitmap::SetOk(
1185 GetBitmapData()->m_bOk
= bOk
;
1186 } // end of wxBitmap::SetOk
1187 #endif // WXWIN_COMPATIBILITY_2
1189 void wxBitmap::SetPalette(
1190 const wxPalette
& rPalette
1195 GetBitmapData()->m_vBitmapPalette
= rPalette
;
1196 } // end of wxBitmap::SetPalette
1198 void wxBitmap::SetMask(
1204 GetBitmapData()->m_pBitmapMask
= pMask
;
1205 } // end of wxBitmap::SetMask
1207 wxBitmap
wxBitmap::GetBitmapForDC(
1212 } // end of wxBitmap::GetBitmapForDC
1214 // ----------------------------------------------------------------------------
1216 // ----------------------------------------------------------------------------
1221 } // end of wxMask::wxMask
1223 // Construct a mask from a bitmap and a colour indicating
1224 // the transparent area
1226 const wxBitmap
& rBitmap
1227 , const wxColour
& rColour
1234 } // end of wxMask::wxMask
1236 // Construct a mask from a bitmap and a palette index indicating
1237 // the transparent area
1239 const wxBitmap
& rBitmap
1247 } // end of wxMask::wxMask
1249 // Construct a mask from a mono bitmap (copies the bitmap).
1251 const wxBitmap
& rBitmap
1256 } // end of wxMask::wxMask
1261 ::GpiDeleteBitmap((HBITMAP
)m_hMaskBitmap
);
1262 } // end of wxMask::~wxMask
1264 // Create a mask from a mono bitmap (copies the bitmap).
1265 bool wxMask::Create(
1266 const wxBitmap
& rBitmap
1269 BITMAPINFOHEADER2 vBmih
;
1270 SIZEL vSize
= {0, 0};
1271 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1272 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1273 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1274 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1275 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1276 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1277 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1282 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1285 if (!rBitmap
.Ok() || rBitmap
.GetDepth() != 1)
1290 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1291 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1292 vBmih
.cx
= rBitmap
.GetWidth();
1293 vBmih
.cy
= rBitmap
.GetHeight();
1295 vBmih
.cBitCount
= 24;
1297 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1304 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1305 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1314 ::GpiDestroyPS(hPSSrc
);
1315 ::GpiDestroyPS(hPSDst
);
1316 ::DevCloseDC(hDCSrc
);
1317 ::DevCloseDC(hDCDst
);
1319 } // end of wxMask::Create
1321 // Create a mask from a bitmap and a palette index indicating
1322 // the transparent area
1323 bool wxMask::Create(
1324 const wxBitmap
& rBitmap
1330 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1333 if (rBitmap
.Ok() && rBitmap
.GetPalette()->Ok())
1336 unsigned char cGreen
;
1337 unsigned char cBlue
;
1339 if (rBitmap
.GetPalette()->GetRGB( nPaletteIndex
1345 wxColour
vTransparentColour( cRed
1350 return (Create( rBitmap
1356 } // end of wxMask::Create
1358 // Create a mask from a bitmap and a colour indicating
1359 // the transparent area
1360 bool wxMask::Create(
1361 const wxBitmap
& rBitmap
1362 , const wxColour
& rColour
1366 COLORREF vMaskColour
= OS2RGB( rColour
.Red()
1370 BITMAPINFOHEADER2 vBmih
;
1371 SIZEL vSize
= {0, 0};
1372 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1373 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1374 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1375 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1376 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1377 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1378 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
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(
1467 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1471 return(pBitmap
? Create( pBitmap
1479 bool wxBitmapHandler::Load(
1481 , const wxString
& rName
1488 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1492 return(pBitmap
? LoadFile( pBitmap
1501 bool wxBitmapHandler::Save(
1503 , const wxString
& rName
1507 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1511 return(pBitmap
? SaveFile( pBitmap
1517 bool wxBitmapHandler::Create(
1518 wxBitmap
* WXUNUSED(pBitmap
)
1519 , void* WXUNUSED(pData
)
1520 , long WXUNUSED(lType
)
1521 , int WXUNUSED(nWidth
)
1522 , int WXUNUSED(nHeight
)
1523 , int WXUNUSED(nDepth
)
1529 bool wxBitmapHandler::LoadFile(
1530 wxBitmap
* WXUNUSED(pBitmap
)
1531 , const wxString
& WXUNUSED(rName
)
1533 , long WXUNUSED(lType
)
1534 , int WXUNUSED(nDesiredWidth
)
1535 , int WXUNUSED(nDesiredHeight
)
1541 bool wxBitmapHandler::SaveFile(
1542 wxBitmap
* WXUNUSED(pBitmap
)
1543 , const wxString
& WXUNUSED(rName
)
1544 , int WXUNUSED(nType
)
1545 , const wxPalette
* WXUNUSED(pPalette
)
1551 // ----------------------------------------------------------------------------
1552 // Utility functions
1553 // ----------------------------------------------------------------------------
1554 HBITMAP
wxInvertMask(
1560 HBITMAP hBmpInvMask
= 0;
1562 wxCHECK_MSG( hBmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1565 // Get width/height from the bitmap if not given
1567 if (!nWidth
|| !nHeight
)
1569 BITMAPINFOHEADER2 vBmhdr
;
1571 ::GpiQueryBitmapInfoHeader( hBmpMask
1574 nWidth
= (int)vBmhdr
.cx
;
1575 nHeight
= (int)vBmhdr
.cy
;
1578 BITMAPINFOHEADER2 vBmih
;
1579 SIZEL vSize
= {0, 0};
1580 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1581 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1582 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1583 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1584 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1585 POINTL vPoint
[4] = { 0 ,0, nWidth
, nHeight
,
1586 0, 0, nWidth
, nHeight
1589 memset(&vBmih
, '\0', 16);
1594 vBmih
.cBitCount
= 24;
1596 hBmpInvMask
= ::GpiCreateBitmap( hPSDst
1603 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) hBmpMask
);
1604 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpInvMask
);
1614 ::GpiDestroyPS(hPSSrc
);
1615 ::GpiDestroyPS(hPSDst
);
1616 ::DevCloseDC(hDCSrc
);
1617 ::DevCloseDC(hDCDst
);
1620 } // end of WxWinGdi_InvertMask