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 wxASSERT_MSG( !m_pSelectedInto
,
67 wxT("deleting bitmap still selected into wxMemoryDC") );
71 if (!::GpiDeleteBitmap((HBITMAP
)m_hBitmap
))
73 wxLogLastError("GpiDeleteBitmap(hbitmap)");
79 } // end of wxBitmapRefData::Free
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // this function should be called from all wxBitmap ctors
88 } // end of wxBitmap::Init
90 bool wxBitmap::CopyFromIconOrCursor(
91 const wxGDIImage
& rIcon
94 HPOINTER hIcon
= (HPOINTER
)rIcon
.GetHandle();
95 POINTERINFO SIconInfo
;
97 if (!::WinQueryPointerInfo(hIcon
, &SIconInfo
))
99 wxLogLastError(wxT("WinQueryPointerInfo"));
102 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
104 m_refData
= pRefData
;
106 int nWidth
= rIcon
.GetWidth();
107 int nHeight
= rIcon
.GetHeight();
109 pRefData
->m_nWidth
= nWidth
;
110 pRefData
->m_nHeight
= nHeight
;
111 pRefData
->m_nDepth
= wxDisplayDepth();
113 pRefData
->m_hBitmap
= (WXHBITMAP
)SIconInfo
.hbmColor
;
116 // No mask in the Info struct in OS/2
119 } // end of wxBitmap::CopyFromIconOrCursor
121 bool wxBitmap::CopyFromCursor(
122 const wxCursor
& rCursor
129 return(CopyFromIconOrCursor(rCursor
));
130 } // end of wxBitmap::CopyFromCursor
132 bool wxBitmap::CopyFromIcon(
141 return CopyFromIconOrCursor(rIcon
);
142 } // end of wxBitmap::CopyFromIcon
144 wxBitmap::~wxBitmap()
146 } // end of wxBitmap::~wxBitmap
157 wxBitmapRefData
* pRefData
= new wxBitmapRefData
;
158 BITMAPINFOHEADER2 vHeader
;
162 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
163 SIZEL vSize
= {0, 0};
166 wxASSERT(vHabmain
!= NULL
);
168 m_refData
= pRefData
;
170 pRefData
->m_nWidth
= nWidth
;
171 pRefData
->m_nHeight
= nHeight
;
172 pRefData
->m_nDepth
= nDepth
;
173 pRefData
->m_nNumColors
= 0;
174 pRefData
->m_pSelectedInto
= NULL
;
176 hDc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, (PSZ
)"*", 1L, (PDEVOPENDATA
)&vDop
, 0L);
177 hPs
= ::GpiCreatePS(vHabmain
, hDc
, &vSize
, GPIA_ASSOC
| PU_PELS
);
180 wxLogLastError("GpiCreatePS Failure");
186 // We assume that it is in XBM format which is not quite the same as
187 // the format CreateBitmap() wants because the order of bytes in the
190 const size_t nBytesPerLine
= (nWidth
+ 7) / 8;
191 const size_t nPadding
= nBytesPerLine
% 2;
192 const size_t nLen
= nHeight
* (nPadding
+ nBytesPerLine
);
193 const char* pzSrc
= zBits
;
197 pzData
= (char *)malloc(nLen
);
199 char* pzDst
= pzData
;
201 for (nRows
= 0; nRows
< nHeight
; nRows
++)
203 for (nCols
= 0; nCols
< nBytesPerLine
; nCols
++)
205 unsigned char ucVal
= *pzSrc
++;
206 unsigned char ucReversed
= 0;
209 for (nBits
= 0; nBits
< 8; nBits
++)
212 ucReversed
|= (ucVal
& 0x01);
215 *pzDst
++ = ucReversed
;
224 // Bits should already be in Windows standard format
226 pzData
= (char *)zBits
; // const_cast is harmless
229 memset(&vHeader
, '\0', sizeof(BITMAPINFOHEADER2
));
230 vHeader
.cbFix
= sizeof(vHeader
);
231 vHeader
.cx
= (USHORT
)nWidth
;
232 vHeader
.cy
= (USHORT
)nHeight
;
233 vHeader
.cPlanes
= 1L;
234 vHeader
.cBitCount
= nDepth
;
235 vHeader
.usReserved
= 0;
236 vHeader
.ulCompression
= BCA_UNCOMP
;
237 vHeader
.usRecording
= BRA_BOTTOMUP
;
238 vHeader
.usRendering
= BRH_NOTHALFTONED
;
239 vHeader
.ulColorEncoding
= BCE_RGB
;
240 vHeader
.ulIdentifier
= 0;
242 memset(&vInfo
, '\0', sizeof(BITMAPINFO2
));
243 vInfo
.cbFix
= sizeof(vInfo
);
244 vInfo
.cx
= (USHORT
)nWidth
;
245 vInfo
.cy
= (USHORT
)nHeight
;
247 vInfo
.cBitCount
= nDepth
;
248 vInfo
.usReserved
= 0;
249 vInfo
.ulCompression
= BCA_UNCOMP
;
250 vInfo
.usRecording
= BRA_BOTTOMUP
;
251 vInfo
.usRendering
= BRH_NOTHALFTONED
;
252 vInfo
.ulColorEncoding
= BCE_RGB
;
253 vInfo
.ulIdentifier
= 0;
255 HBITMAP hBmp
= ::GpiCreateBitmap(hPs
, &vHeader
, CBM_INIT
, (PBYTE
)pzData
, &vInfo
);
259 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
;
327 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
328 SIZEL vSize
= {0, 0};
329 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
330 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
332 memset(&vHeader
, '\0', sizeof(BITMAPINFOHEADER2
));
333 vHeader
.cbFix
= sizeof(BITMAPINFOHEADER2
);
337 vHeader
.cBitCount
= nD
;
339 hBmp
= ::GpiCreateBitmap( hPS
354 hPSScreen
= ::WinGetScreenPS(HWND_DESKTOP
);
355 hDCScreen
= ::GpiQueryDevice(hPSScreen
);
356 ::DevQueryCaps(hDCScreen
, CAPS_COLOR_BITCOUNT
, 1L, &lBitCount
);
358 memset(&vHeader
, '\0', sizeof(BITMAPINFOHEADER2
));
359 vHeader
.cbFix
= sizeof(BITMAPINFOHEADER2
);
363 vHeader
.cBitCount
= lBitCount
;
365 hBmp
= ::GpiCreateBitmap( hPSScreen
372 GetBitmapData()->m_nDepth
= wxDisplayDepth();
373 ::WinReleasePS(hPSScreen
);
375 SetHBITMAP((WXHBITMAP
)hBmp
);
377 #if WXWIN_COMPATIBILITY_2
378 GetBitmapData()->m_bOk
= hBmp
!= 0;
379 #endif // WXWIN_COMPATIBILITY_2
382 } // end of wxBitmap::Create
384 bool wxBitmap::CreateFromXpm(
388 #if wxUSE_IMAGE && wxUSE_XPM
391 wxCHECK_MSG(ppData
!= NULL
, FALSE
, wxT("invalid bitmap data"))
393 wxXPMDecoder vDecoder
;
394 wxImage vImg
= vDecoder
.ReadData(ppData
);
396 wxCHECK_MSG(vImg
.Ok(), FALSE
, wxT("invalid bitmap data"))
398 *this = wxBitmap(vImg
);
403 } // end of wxBitmap::CreateFromXpm
405 bool wxBitmap::LoadFile(
406 const wxString
& rFilename
410 HPS hPs
= NULLHANDLE
;
414 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
420 m_refData
= new wxBitmapRefData
;
422 return(pHandler
->LoadFile( this
434 if (!vImage
.LoadFile(rFilename
, lType
) || !vImage
.Ok() )
437 *this = wxBitmap(vImage
);
441 } // end of wxBitmap::LoadFile
443 bool wxBitmap::Create(
453 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
459 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
460 "type %d defined."), lType
);
465 m_refData
= new wxBitmapRefData
;
467 return(pHandler
->Create( this
474 } // end of wxBitmap::Create
476 bool wxBitmap::SaveFile(
477 const wxString
& rFilename
479 , const wxPalette
* pPalette
482 wxBitmapHandler
* pHandler
= wxDynamicCast( FindHandler(lType
)
488 return pHandler
->SaveFile( this
496 // FIXME what about palette? shouldn't we use it?
497 wxImage vImage
= ConvertToImage();
502 return(vImage
.SaveFile( rFilename
506 } // end of wxBitmap::SaveFile
509 // ----------------------------------------------------------------------------
510 // wxImage-wxBitmap convertion
511 // ----------------------------------------------------------------------------
513 bool wxBitmap::CreateFromImage (
514 const wxImage
& rImage
518 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
520 int nSizeLimit
= 1024 * 768 * 3;
521 int nWidth
= GetWidth();
522 int nBmpHeight
= GetHeight();
523 int nBytePerLine
= nWidth
* 3;
524 int nSizeDWORD
= sizeof(DWORD
);
525 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
528 if (nLineBoundary
> 0)
530 nPadding
= nSizeDWORD
- nLineBoundary
;
531 nBytePerLine
+= nPadding
;
535 // Calc the number of DIBs and heights of DIBs
539 int nHeight
= nSizeLimit
/ nBytePerLine
;
541 if (nHeight
>= nBmpHeight
)
542 nHeight
= nBmpHeight
;
545 nNumDIB
= nBmpHeight
/ nHeight
;
546 nHRemain
= nBmpHeight
% nHeight
;
552 // Set bitmap parameters
554 wxCHECK_MSG(rImage
.Ok(), FALSE
, wxT("invalid image"));
556 SetHeight(nBmpHeight
);
558 nDepth
= wxDisplayDepth();
563 // Copy the palette from the source image
565 SetPalette(rImage
.GetPalette());
566 #endif // wxUSE_PALETTE
569 // Create a DIB header
571 BITMAPINFOHEADER2 vHeader
;
574 // Fill in the DIB header
576 memset(&vHeader
, '\0', sizeof(BITMAPINFOHEADER2
));
577 vHeader
.cbFix
= sizeof(vHeader
);
578 vHeader
.cx
= (USHORT
)nWidth
;
579 vHeader
.cy
= (USHORT
)nHeight
;
580 vHeader
.cPlanes
= 1L;
581 vHeader
.cBitCount
= 24;
582 vHeader
.ulCompression
= BCA_UNCOMP
;
583 vHeader
.cbImage
= nBytePerLine
* nHeight
;
584 vHeader
.cclrUsed
= 0;
587 // These seem not really needed for our purpose here.
589 vHeader
.cxResolution
= 0;
590 vHeader
.cyResolution
= 0;
591 vHeader
.cclrImportant
= 0;
592 vHeader
.usUnits
= BRU_METRIC
;
593 vHeader
.usReserved
= 0;
596 vHeader
.usRecording
= BRA_BOTTOMUP
;
597 vHeader
.usRendering
= BRH_NOTHALFTONED
;
598 vHeader
.ulColorEncoding
= BCE_RGB
;
599 vHeader
.ulIdentifier
= 0;
602 // Memory for DIB data
604 unsigned char* pucBits
;
606 pucBits
= (unsigned char *)malloc(vHeader
.cbImage
);
609 wxFAIL_MSG(wxT("could not allocate memory for DIB"));
614 // Create and set the device-dependent bitmap
616 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
617 SIZEL vSize
= {0, 0};
618 HDC hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
619 HPS hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIA_ASSOC
);
623 hBmp
= ::GpiCreateBitmap( hPS
629 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
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 for (n
= 0; n
< nNumDIB
; n
++)
651 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
654 // Redefine height and size of the (possibly) last smaller DIB
655 // memory is not reallocated
658 vHeader
.cy
= (DWORD
)(nHeight
);
659 vHeader
.cbImage
= nBytePerLine
* nHeight
;
662 for (j
= 0; j
< nHeight
; j
++)
664 for (i
= 0; i
< nWidth
; i
++)
666 *(ptbits
++) = *(ptdata
+ 2);
667 *(ptbits
++) = *(ptdata
+ 1);
668 *(ptbits
++) = *(ptdata
);
671 for (i
= 0; i
< nPadding
; i
++)
676 // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
678 POINTL vPoint
[4] = { 0, nOrigin
,
680 0, 0, nWidth
, nHeight
692 SetHBITMAP((WXHBITMAP
)hBmp
);
695 ::GpiSelectPalette(hPS
, hOldPalette
);
696 #endif // wxUSE_PALETTE
699 // Similarly, created an mono-bitmap for the possible mask
701 if (rImage
.HasMask())
703 memset(&vHeader
, '\0', sizeof(BITMAPINFOHEADER2
));
704 vHeader
.cbFix
= sizeof(BITMAPINFOHEADER2
);
706 vHeader
.cy
= nHeight
;
708 vHeader
.cBitCount
= 1;
709 hBmp
= ::GpiCreateBitmap( hPS
715 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
717 nHeight
= nBmpHeight
;
719 nHeight
= nSizeLimit
/ nBytePerLine
;
720 vHeader
.cy
= (DWORD
)(nHeight
);
721 vHeader
.cbImage
= nBytePerLine
* nHeight
;
724 unsigned char cRed
= rImage
.GetMaskRed();
725 unsigned char cGreen
= rImage
.GetMaskGreen();
726 unsigned char cBlue
= rImage
.GetMaskBlue();
727 unsigned char cZero
= 0;
728 unsigned char cOne
= 255;
731 for (n
= 0; n
< nNumDIB
; n
++)
733 if (nNumDIB
> 1 && n
== nNumDIB
- 1 && nHRemain
> 0)
736 // Redefine height and size of the (possibly) last smaller DIB
737 // memory is not reallocated
740 vHeader
.cy
= (DWORD
)(nHeight
);
741 vHeader
.cbImage
= nBytePerLine
* nHeight
;
744 for (int j
= 0; j
< nHeight
; j
++)
746 for (i
= 0; i
< nWidth
; i
++)
748 if ((*(ptdata
++) != cRed
) || (*(ptdata
++) != cGreen
) || (*(ptdata
++) != cBlue
))
761 for (i
= 0; i
< nPadding
; i
++)
764 POINTL vPoint
[4] = { 0, nOrigin
,
766 0, 0, nWidth
, nHeight
780 // Create a wxMask object
782 wxMask
* pMask
= new wxMask();
784 pMask
->SetMaskBitmap((WXHBITMAP
)hBmp
);
786 hBmpOld
= ::GpiSetBitmap(hPS
, hBmp
);
790 // Free allocated resources
792 ::GpiSetBitmap(hPS
, NULLHANDLE
);
797 } // end of wxBitmap::CreateFromImage
799 wxImage
wxBitmap::ConvertToImage() const
803 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
806 // Create an wxImage object
808 int nWidth
= GetWidth();
809 int nHeight
= GetHeight();
812 int nBytePerLine
= nWidth
* 3;
813 int nSizeDWORD
= sizeof(DWORD
);
814 int nLineBoundary
= nBytePerLine
% nSizeDWORD
;
816 unsigned char* pData
;
817 unsigned char* lpBits
;
819 BITMAPINFOHEADER2 vDIBh
;
820 BITMAPINFO2 vDIBInfo
;
822 PSZ pszData
[4] = { "Display", NULL
, NULL
, NULL
};
825 SIZEL vSizlPage
= {0,0};
828 vImage
.Create( nWidth
831 pData
= vImage
.GetData();
834 wxFAIL_MSG( wxT("could not allocate data for image") );
837 if(nLineBoundary
> 0)
839 nPadding
= nSizeDWORD
- nLineBoundary
;
840 nBytePerLine
+= nPadding
;
842 wxDisplaySize( &nDevWidth
846 // Create and fill a DIB header
848 memset(&vDIBh
, '\0', sizeof(BITMAPINFOHEADER2
));
849 vDIBh
.cbFix
= sizeof(BITMAPINFOHEADER2
);
853 vDIBh
.cbImage
= nBytePerLine
* nHeight
;
854 vDIBh
.cBitCount
= 24;
856 memset(&vDIBInfo
, '\0', sizeof(BITMAPINFO2
));
857 vDIBInfo
.cbFix
= sizeof(BITMAPINFO2
);
858 vDIBInfo
.cPlanes
= 1;
859 vDIBInfo
.cBitCount
= 24;
860 vDIBInfo
.ulCompression
= BCA_UNCOMP
;
861 vDIBInfo
.usReserved
= 0;
862 vDIBInfo
.usRecording
= BRA_BOTTOMUP
;
863 vDIBInfo
.usRendering
= BRH_NOTHALFTONED
;
864 vDIBInfo
.ulColorEncoding
= BCE_RGB
;
865 vDIBInfo
.ulIdentifier
= 0;
867 lpBits
= (unsigned char *)malloc(vDIBh
.cbImage
);
870 wxFAIL_MSG(wxT("could not allocate data for DIB"));
876 // Copy data from the device-dependent bitmap to the DIB
878 hDCMem
= ::DevOpenDC( vHabmain
882 ,(PDEVOPENDATA
)pszData
885 hPSMem
= ::GpiCreatePS( vHabmain
888 ,PU_PELS
| GPIA_ASSOC
| GPIT_MICRO
890 hBitmap
= ::GpiCreateBitmap( hPSMem
896 lScans
= ::GpiQueryBitmapBits( hPSMem
904 // Copy DIB data into the wxImage object
908 unsigned char* ptdata
= pData
;
909 unsigned char* ptbits
= lpBits
;
911 for (i
= 0; i
< nHeight
; i
++)
913 for (j
= 0; j
< nWidth
; j
++)
915 *(ptdata
++) = *(ptbits
+2);
916 *(ptdata
++) = *(ptbits
+1);
917 *(ptdata
++) = *(ptbits
);
924 // Similarly, set data according to the possible mask bitmap
926 if (GetMask() && GetMask()->GetMaskBitmap())
928 hBitmap
= (HBITMAP
)GetMask()->GetMaskBitmap();
931 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
933 HDC hMemDC
= ::DevOpenDC( vHabmain
937 ,(PDEVOPENDATA
)pszData
940 HPS hMemPS
= ::GpiCreatePS( vHabmain
943 ,PU_PELS
| GPIA_ASSOC
| GPIT_MICRO
945 ::GpiSetColor(hMemPS
, OS2RGB(0, 0, 0));
946 ::GpiSetBackColor(hMemPS
, OS2RGB(255, 255, 255) );
947 ::GpiQueryBitmapBits( hPSMem
953 ::GpiDestroyPS(hMemPS
);
954 ::DevCloseDC(hMemDC
);
957 // Background color set to RGB(16,16,16) in consistent with wxGTK
959 unsigned char ucRed
= 16;
960 unsigned char ucGreen
= 16;
961 unsigned char ucBlue
= 16;
965 for (i
= 0; i
< nHeight
; i
++)
967 for (j
= 0; j
< nWidth
; j
++)
974 *(ptdata
++) = ucGreen
;
975 *(ptdata
++) = ucBlue
;
981 vImage
.SetMaskColour( ucRed
985 vImage
.SetMask(TRUE
);
989 vImage
.SetMask(FALSE
);
993 // Free allocated resources
995 ::GpiDestroyPS(hPSMem
);
996 ::DevCloseDC(hDCMem
);
999 } // end of wxBitmap::ConvertToImage
1001 // ----------------------------------------------------------------------------
1002 // sub bitmap extraction
1003 // ----------------------------------------------------------------------------
1005 wxBitmap
wxBitmap::GetSubBitmap(
1009 wxCHECK_MSG( Ok() &&
1010 (rRect
.x
>= 0) && (rRect
.y
>= 0) &&
1011 (rRect
.x
+ rRect
.width
<= GetWidth()) &&
1012 (rRect
.y
+ rRect
.height
<= GetHeight()),
1013 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1015 wxBitmap
vRet( rRect
.width
1019 wxASSERT_MSG( vRet
.Ok(), wxT("GetSubBitmap error") );
1025 SIZEL vSize
= {0, 0};
1026 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1027 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1028 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1029 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1030 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1031 POINTL vPoint
[4] = { rRect
.x
, rRect
.y
,
1032 rRect
.x
+ rRect
.width
, rRect
.y
+ rRect
.height
,
1033 0, 0, GetWidth(), GetHeight()
1036 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1037 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1047 // Copy mask if there is one
1051 BITMAPINFOHEADER2 vBmih
;
1053 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1054 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1055 vBmih
.cx
= rRect
.width
;
1056 vBmih
.cy
= rRect
.height
;
1058 vBmih
.cBitCount
= 1;
1060 HBITMAP hBmpMask
= ::GpiCreateBitmap( hPSDst
1067 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetHBITMAP());
1068 ::GpiSetBitmap(hPSDst
, (HBITMAP
) vRet
.GetHBITMAP());
1070 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1071 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpMask
);
1080 wxMask
* pMask
= new wxMask((WXHBITMAP
)hBmpMask
);
1081 vRet
.SetMask(pMask
);
1084 ::GpiSetBitmap(hPSSrc
, NULL
);
1085 ::GpiSetBitmap(hPSDst
, NULL
);
1086 ::GpiDestroyPS(hPSSrc
);
1087 ::GpiDestroyPS(hPSDst
);
1088 ::DevCloseDC(hDCSrc
);
1089 ::DevCloseDC(hDCDst
);
1091 } // end of wxBitmap::GetSubBitmap
1093 // ----------------------------------------------------------------------------
1094 // wxBitmap accessors
1095 // ----------------------------------------------------------------------------
1097 void wxBitmap::SetQuality(
1103 GetBitmapData()->m_nQuality
= nQ
;
1104 } // end of wxBitmap::SetQuality
1106 #if WXWIN_COMPATIBILITY_2
1107 void wxBitmap::SetOk(
1113 GetBitmapData()->m_bOk
= bOk
;
1114 } // end of wxBitmap::SetOk
1115 #endif // WXWIN_COMPATIBILITY_2
1117 void wxBitmap::SetPalette(
1118 const wxPalette
& rPalette
1123 GetBitmapData()->m_vBitmapPalette
= rPalette
;
1124 } // end of wxBitmap::SetPalette
1126 void wxBitmap::SetMask(
1132 GetBitmapData()->m_pBitmapMask
= pMask
;
1133 } // end of wxBitmap::SetMask
1135 wxBitmap
wxBitmap::GetBitmapForDC(
1140 } // end of wxBitmap::GetBitmapForDC
1142 // ----------------------------------------------------------------------------
1144 // ----------------------------------------------------------------------------
1149 } // end of wxMask::wxMask
1151 // Construct a mask from a bitmap and a colour indicating
1152 // the transparent area
1154 const wxBitmap
& rBitmap
1155 , const wxColour
& rColour
1162 } // end of wxMask::wxMask
1164 // Construct a mask from a bitmap and a palette index indicating
1165 // the transparent area
1167 const wxBitmap
& rBitmap
1175 } // end of wxMask::wxMask
1177 // Construct a mask from a mono bitmap (copies the bitmap).
1179 const wxBitmap
& rBitmap
1184 } // end of wxMask::wxMask
1189 ::GpiDeleteBitmap((HBITMAP
)m_hMaskBitmap
);
1190 } // end of wxMask::~wxMask
1192 // Create a mask from a mono bitmap (copies the bitmap).
1193 bool wxMask::Create(
1194 const wxBitmap
& rBitmap
1197 BITMAPINFOHEADER2 vBmih
;
1198 SIZEL vSize
= {0, 0};
1199 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1200 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1201 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1202 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1203 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1204 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1205 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1210 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1213 if (!rBitmap
.Ok() || rBitmap
.GetDepth() != 1)
1218 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1219 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1220 vBmih
.cx
= rBitmap
.GetWidth();
1221 vBmih
.cy
= rBitmap
.GetHeight();
1223 vBmih
.cBitCount
= 1;
1225 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1232 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1233 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1242 ::GpiDestroyPS(hPSSrc
);
1243 ::GpiDestroyPS(hPSDst
);
1244 ::DevCloseDC(hDCSrc
);
1245 ::DevCloseDC(hDCDst
);
1247 } // end of wxMask::Create
1249 // Create a mask from a bitmap and a palette index indicating
1250 // the transparent area
1251 bool wxMask::Create(
1252 const wxBitmap
& rBitmap
1258 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1261 if (rBitmap
.Ok() && rBitmap
.GetPalette()->Ok())
1264 unsigned char cGreen
;
1265 unsigned char cBlue
;
1267 if (rBitmap
.GetPalette()->GetRGB( nPaletteIndex
1273 wxColour
vTransparentColour( cRed
1278 return (Create( rBitmap
1284 } // end of wxMask::Create
1286 // Create a mask from a bitmap and a colour indicating
1287 // the transparent area
1288 bool wxMask::Create(
1289 const wxBitmap
& rBitmap
1290 , const wxColour
& rColour
1294 COLORREF vMaskColour
= OS2RGB( rColour
.Red()
1298 BITMAPINFOHEADER2 vBmih
;
1299 SIZEL vSize
= {0, 0};
1300 DEVOPENSTRUC vDop
= { NULL
, "DISPLAY", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1301 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1302 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1303 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1304 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1305 POINTL vPoint
[4] = { 0 ,0, rBitmap
.GetWidth(), rBitmap
.GetHeight(),
1306 0, 0, rBitmap
.GetWidth(), rBitmap
.GetHeight()
1311 ::GpiDeleteBitmap((HBITMAP
) m_hMaskBitmap
);
1320 // Scan the bitmap for the transparent colour and set
1321 // the corresponding pixels in the mask to BLACK and
1322 // the rest to WHITE
1325 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1326 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1327 vBmih
.cx
= rBitmap
.GetWidth();
1328 vBmih
.cy
= rBitmap
.GetHeight();
1330 vBmih
.cBitCount
= 1;
1332 m_hMaskBitmap
= ::GpiCreateBitmap( hPSDst
1339 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) rBitmap
.GetHBITMAP());
1340 ::GpiSetBitmap(hPSDst
, (HBITMAP
) m_hMaskBitmap
);
1343 // This is not very efficient, but I can't think
1344 // of a better way of doing it
1346 for (int w
= 0; w
< rBitmap
.GetWidth(); w
++)
1348 for (int h
= 0; h
< rBitmap
.GetHeight(); h
++)
1350 POINTL vPt
= {w
, h
};
1351 COLORREF vCol
= (COLORREF
)::GpiQueryPel(hPSSrc
, &vPt
);
1352 if (vCol
== (COLORREF
)CLR_NOINDEX
)
1355 // Doesn't make sense to continue
1361 if (vCol
== vMaskColour
)
1363 ::GpiSetColor(hPSDst
, OS2RGB(0, 0, 0));
1364 ::GpiSetPel(hPSDst
, &vPt
);
1368 ::GpiSetColor(hPSDst
, OS2RGB(255, 255, 255));
1369 ::GpiSetPel(hPSDst
, &vPt
);
1373 ::GpiSetBitmap(hPSSrc
, NULL
);
1374 ::GpiSetBitmap(hPSDst
, NULL
);
1375 ::GpiDestroyPS(hPSSrc
);
1376 ::GpiDestroyPS(hPSDst
);
1377 ::DevCloseDC(hDCSrc
);
1378 ::DevCloseDC(hDCDst
);
1380 } // end of wxMask::Create
1382 // ----------------------------------------------------------------------------
1384 // ----------------------------------------------------------------------------
1386 bool wxBitmapHandler::Create(
1395 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1399 return(pBitmap
? Create( pBitmap
1407 bool wxBitmapHandler::Load(
1409 , const wxString
& rName
1416 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1420 return(pBitmap
? LoadFile( pBitmap
1429 bool wxBitmapHandler::Save(
1431 , const wxString
& rName
1435 wxBitmap
* pBitmap
= wxDynamicCast( pImage
1439 return(pBitmap
? SaveFile( pBitmap
1445 bool wxBitmapHandler::Create(
1446 wxBitmap
* WXUNUSED(pBitmap
)
1447 , void* WXUNUSED(pData
)
1448 , long WXUNUSED(lType
)
1449 , int WXUNUSED(nWidth
)
1450 , int WXUNUSED(nHeight
)
1451 , int WXUNUSED(nDepth
)
1457 bool wxBitmapHandler::LoadFile(
1458 wxBitmap
* WXUNUSED(pBitmap
)
1459 , const wxString
& WXUNUSED(rName
)
1461 , long WXUNUSED(lType
)
1462 , int WXUNUSED(nDesiredWidth
)
1463 , int WXUNUSED(nDesiredHeight
)
1469 bool wxBitmapHandler::SaveFile(
1470 wxBitmap
* WXUNUSED(pBitmap
)
1471 , const wxString
& WXUNUSED(rName
)
1472 , int WXUNUSED(nType
)
1473 , const wxPalette
* WXUNUSED(pPalette
)
1479 // ----------------------------------------------------------------------------
1480 // Utility functions
1481 // ----------------------------------------------------------------------------
1482 HBITMAP
wxInvertMask(
1488 HBITMAP hBmpInvMask
= 0;
1490 wxCHECK_MSG( hBmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1493 // Get width/height from the bitmap if not given
1495 if (!nWidth
|| !nHeight
)
1497 BITMAPINFOHEADER2 vBmhdr
;
1499 ::GpiQueryBitmapInfoHeader( hBmpMask
1502 nWidth
= (int)vBmhdr
.cx
;
1503 nHeight
= (int)vBmhdr
.cy
;
1506 BITMAPINFOHEADER2 vBmih
;
1507 SIZEL vSize
= {0, 0};
1508 DEVOPENSTRUC vDop
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
1509 HDC hDCSrc
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1510 HDC hDCDst
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDop
, NULLHANDLE
);
1511 HPS hPSSrc
= ::GpiCreatePS(vHabmain
, hDCSrc
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1512 HPS hPSDst
= ::GpiCreatePS(vHabmain
, hDCDst
, &vSize
, PU_PELS
| GPIA_ASSOC
);
1513 POINTL vPoint
[4] = { 0 ,0, nWidth
, nHeight
,
1514 0, 0, nWidth
, nHeight
1517 memset(&vBmih
, '\0', sizeof(BITMAPINFOHEADER2
));
1518 vBmih
.cbFix
= sizeof(BITMAPINFOHEADER2
);
1522 vBmih
.cBitCount
= 1;
1524 hBmpInvMask
= ::GpiCreateBitmap( hPSDst
1531 ::GpiSetBitmap(hPSSrc
, (HBITMAP
) hBmpMask
);
1532 ::GpiSetBitmap(hPSDst
, (HBITMAP
) hBmpInvMask
);
1542 ::GpiDestroyPS(hPSSrc
);
1543 ::GpiDestroyPS(hPSDst
);
1544 ::DevCloseDC(hDCSrc
);
1545 ::DevCloseDC(hDCDst
);
1548 } // end of WxWinGdi_InvertMask