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
90 } // end of wxBitmap::Init
92 bool wxBitmap::CopyFromIconOrCursor(
93 const wxGDIImage
& rIcon
96 HPOINTER hIcon
= (HPOINTER
)rIcon
.GetHandle();
97 POINTERINFO SIconInfo
;
99 if (!::WinQueryPointerInfo(hIcon
, &SIconInfo
))
101 wxLogLastError(wxT("WinQueryPointerInfo"));
104 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
106 m_refData
= pRefData
;
108 int nWidth
= rIcon
.GetWidth();
109 int nHeight
= rIcon
.GetHeight();
111 pRefData
->m_nWidth
= nWidth
;
112 pRefData
->m_nHeight
= nHeight
;
113 pRefData
->m_nDepth
= wxDisplayDepth();
115 pRefData
->m_hBitmap
= (WXHBITMAP
)SIconInfo
.hbmColor
;
117 wxMask
* pMask
= new wxMask(SIconInfo
.hbmPointer
);
119 pMask
->SetMaskBitmap(GetHBITMAP());
123 } // end of wxBitmap::CopyFromIconOrCursor
125 bool wxBitmap::CopyFromCursor(
126 const wxCursor
& rCursor
133 return(CopyFromIconOrCursor(rCursor
));
134 } // end of wxBitmap::CopyFromCursor
136 bool wxBitmap::CopyFromIcon(
145 return CopyFromIconOrCursor(rIcon
);
146 } // end of wxBitmap::CopyFromIcon
148 wxBitmap::~wxBitmap()
150 } // end of wxBitmap::~wxBitmap
161 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
162 BITMAPINFOHEADER2 vHeader
;
166 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
167 SIZEL vSize
= {0, 0};
170 wxASSERT(vHabmain
!= NULL
);
172 m_refData
= pRefData
;
174 pRefData
->m_nWidth
= nWidth
;
175 pRefData
->m_nHeight
= nHeight
;
176 pRefData
->m_nDepth
= nDepth
;
177 pRefData
->m_nNumColors
= 0;
178 pRefData
->m_pSelectedInto
= NULL
;
180 hDc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
181 hPs
= ::GpiCreatePS(vHabmain
, hDc
, &vSize
, GPIA_ASSOC
| PU_PELS
);
184 wxLogLastError("GpiCreatePS Failure");
190 // We assume that it is in XBM format which is not quite the same as
191 // the format CreateBitmap() wants because the order of bytes in the
194 const size_t nBytesPerLine
= (nWidth
+ 7) / 8;
195 const size_t nPadding
= nBytesPerLine
% 2;
196 const size_t nLen
= nHeight
* (nPadding
+ nBytesPerLine
);
197 const char* pzSrc
= zBits
;
201 pzData
= (char *)malloc(nLen
);
203 char* pzDst
= pzData
;
205 for (nRows
= 0; nRows
< nHeight
; nRows
++)
207 for (nCols
= 0; nCols
< nBytesPerLine
; nCols
++)
209 unsigned char ucVal
= *pzSrc
++;
210 unsigned char ucReversed
= 0;
213 for (nBits
= 0; nBits
< 8; nBits
++)
216 ucReversed
|= (ucVal
& 0x01);
219 *pzDst
++ = ucReversed
;
228 // Bits should already be in Windows standard format
230 pzData
= (char *)zBits
; // const_cast is harmless
234 nDepth
= 24; // MAX supported in PM
235 memset(&vHeader
, '\0', 16);
237 vHeader
.cx
= (USHORT
)nWidth
;
238 vHeader
.cy
= (USHORT
)nHeight
;
239 vHeader
.cPlanes
= 1L;
240 vHeader
.cBitCount
= nDepth
;
241 vHeader
.usReserved
= 0;
243 memset(&vInfo
, '\0', 16);
245 vInfo
.cx
= (USHORT
)nWidth
;
246 vInfo
.cy
= (USHORT
)nHeight
;
248 vInfo
.cBitCount
= nDepth
;
250 HBITMAP hBmp
= ::GpiCreateBitmap(hPs
, &vHeader
, CBM_INIT
, (PBYTE
)pzData
, &vInfo
);
254 wxLogLastError("CreateBitmap");
258 SetHBITMAP((WXHBITMAP
)hBmp
);
259 } // end of wxBitmap::wxBitmap
273 } // end of wxBitmap::wxBitmap
291 } // end of wxBitmap::wxBitmap
294 const wxString
& rFilename
303 } // end of wxBitmap::wxBitmap
305 bool wxBitmap::Create(
312 BITMAPINFOHEADER2 vHeader
;
314 wxASSERT(vHabmain
!= NULL
);
316 m_refData
= new wxBitmapRefData
;
317 GetBitmapData()->m_nWidth
= nW
;
318 GetBitmapData()->m_nHeight
= nH
;
319 GetBitmapData()->m_nDepth
= nD
;
322 // Xpms and bitmaps from other images can also be mono's, but only
323 // mono's need help changing their colors with MemDC changes
329 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
330 SIZEL vSize
= {0, 0};
331 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
332 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
334 memset(&vHeader
, '\0', 16);
339 vHeader
.cBitCount
= 24; //nD;
341 hBmp
= ::GpiCreateBitmap( hPS
356 hPSScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
357 hDCScreen
= ::GpiQueryDevice(hPSScreen
);
358 ::DevQueryCaps(hDCScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitCount
);
363 memset(&vHeader
, '\0', 16);
368 vHeader
.cBitCount
= lBitCount
;
370 hBmp
= ::GpiCreateBitmap( hPSScreen
377 GetBitmapData()->m_nDepth
= wxDisplayDepth();
378 ::WinReleasePS(hPSScreen
);
380 SetHBITMAP((WXHBITMAP
)hBmp
);
382 #if WXWIN_COMPATIBILITY_2
383 GetBitmapData()->m_bOk
= hBmp
!= 0;
384 #endif // WXWIN_COMPATIBILITY_2
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(
411 const wxString
& rFilename
415 HPS hPs
= NULLHANDLE
;
419 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
425 m_refData
= new wxBitmapRefData
;
427 return(pHandler
->LoadFile( this
439 if (!vImage
.LoadFile(rFilename
, lType
) || !vImage
.Ok() )
442 *this = wxBitmap(vImage
);
446 } // end of wxBitmap::LoadFile
448 bool wxBitmap::Create(
458 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
464 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
465 "type %d defined."), lType
);
470 m_refData
= new wxBitmapRefData
;
472 return(pHandler
->Create( this
479 } // end of wxBitmap::Create
481 bool wxBitmap::SaveFile(
482 const wxString
& rFilename
484 , const wxPalette
* pPalette
487 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
493 return pHandler
->SaveFile( this
501 // FIXME what about palette? shouldn't we use it?
502 wxImage vImage
= ConvertToImage();
507 return(vImage
.SaveFile( rFilename
511 } // end of wxBitmap::SaveFile
514 // ----------------------------------------------------------------------------
515 // wxImage-wxBitmap convertion
516 // ----------------------------------------------------------------------------
518 bool wxBitmap::CreateFromImage (
519 const wxImage
& rImage
523 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
524 m_refData
= new wxBitmapRefData();
527 int nSizeLimit
= 1024 * 768 * 3;
528 int nWidth
= rImage
.GetWidth();
529 int nBmpHeight
= rImage
.GetHeight();
530 int nBytePerLine
= nWidth
* 3;
531 int nSizeDWORD
= sizeof(DWORD
);
532 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
535 if (nLineBoundary
> 0)
537 nPadding
= nSizeDWORD
- nLineBoundary
;
538 nBytePerLine
+= nPadding
;
542 // Calc the number of DIBs and heights of DIBs
546 int nHeight
= nSizeLimit
/ nBytePerLine
;
548 if (nHeight
>= nBmpHeight
)
549 nHeight
= nBmpHeight
;
552 nNumDIB
= nBmpHeight
/ nHeight
;
553 nHRemain
= nBmpHeight
% nHeight
;
559 // Set bitmap parameters
561 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
563 SetHeight(nBmpHeight
);
565 nDepth
= wxDisplayDepth();
570 // Copy the palette from the source image
572 SetPalette(rImage
.GetPalette());
573 #endif // wxUSE_PALETTE
576 // Create a DIB header
578 BITMAPINFOHEADER2 vHeader
;
582 // Fill in the DIB header
584 memset(&vHeader
, '\0', 16);
586 vHeader
.cx
= (ULONG
)nWidth
;
587 vHeader
.cy
= (ULONG
)nHeight
;
588 vHeader
.cPlanes
= 1L;
589 vHeader
.cBitCount
= 24;
592 // Memory for DIB data
594 unsigned char* pucBits
;
596 pucBits
= (unsigned char *)malloc(nBytePerLine
* nHeight
);
599 wxFAIL_MSG(wxT("could not allocate memory for DIB"));
602 memset(pucBits
, '\0', (nBytePerLine
* nHeight
));
605 // Create and set the device-dependent bitmap
607 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
608 SIZEL vSize
= {0, 0};
609 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
610 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
612 HDC hDCScreen
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
617 memset(&vInfo
, '\0', 16);
619 vInfo
.cx
= (ULONG
)nWidth
;
620 vInfo
.cy
= (ULONG
)nHeight
;
622 vInfo
.cBitCount
= 24; // Set to desired count going in
624 hBmp
= ::GpiCreateBitmap( hPS
631 HPAL hOldPalette
= NULLHANDLE
;
632 if (rImage
.GetPalette().Ok())
634 hOldPalette
= ::GpiSelectPalette(hPS
, (HPAL
)rImage
.GetPalette().GetHPALETTE());
636 #endif // wxUSE_PALETTE
639 // Copy image data into DIB data and then into DDB (in a loop)
641 unsigned char* pData
= rImage
.GetData();
646 unsigned char* ptdata
= pData
;
647 unsigned char* ptbits
;
649 if ((hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
)) == HBM_ERROR
)
654 vError
= ::WinGetLastError(vHabmain
);
655 sError
= wxPMErrorToStr(vError
);
657 for (n
= 0; n
< nNumDIB
; n
++)
659 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
662 // Redefine height and size of the (possibly) last smaller DIB
663 // memory is not reallocated
666 vHeader
.cy
= (DWORD
)(nHeight
);
667 vHeader
.cbImage
= nBytePerLine
* nHeight
;
670 for (j
= 0; j
< nHeight
; j
++)
672 for (i
= 0; i
< nWidth
; i
++)
674 *(ptbits
++) = *(ptdata
+ 2);
675 *(ptbits
++) = *(ptdata
+ 1);
676 *(ptbits
++) = *(ptdata
);
679 for (i
= 0; i
< nPadding
; i
++)
684 // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
685 // in combination with setting the bits into the selected bitmap
687 if ((lScans
= ::GpiSetBitmapBits( hPS
688 ,0 // Start at the bottom
689 ,(LONG
)nHeight
// One line per scan
697 vError
= ::WinGetLastError(vHabmain
);
698 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
= 1;
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
= 1;
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', sizeof(BITMAPINFOHEADER2
));
1590 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1594 vBmih
.cBitCount
= 1;
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