]> git.saurik.com Git - wxWidgets.git/blame - src/os2/bitmap.cpp
removed a wxPaintDC::Clear() fatal to wxGTK
[wxWidgets.git] / src / os2 / bitmap.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: bitmap.cpp
3// Purpose: wxBitmap
f0a56ab0 4// Author: David Webster
0e320a79 5// Modified by:
f0a56ab0 6// Created: 08/08/99
0e320a79 7// RCS-ID: $Id$
f0a56ab0 8// Copyright: (c) David Webster
0e320a79
DW
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
aa213887
SN
12#ifdef __GNUG__
13 #pragma implementation "bitmap.h"
14#endif
15
d88de032
DW
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifndef WX_PRECOMP
20 #include <stdio.h>
21
22 #include "wx/list.h"
23 #include "wx/utils.h"
24 #include "wx/app.h"
25 #include "wx/palette.h"
26 #include "wx/dcmemory.h"
27 #include "wx/bitmap.h"
28 #include "wx/icon.h"
0e320a79
DW
29#endif
30
d88de032 31#include "wx/os2/private.h"
0e320a79
DW
32#include "wx/log.h"
33
3b9e3455
DW
34//#include "wx/msw/dib.h"
35#include "wx/image.h"
1c344e89 36#include "wx/xpmdecod.h"
3b9e3455 37
d88de032
DW
38// ----------------------------------------------------------------------------
39// macros
40// ----------------------------------------------------------------------------
41
0e320a79
DW
42IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
43IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
3b9e3455
DW
44
45IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
0e320a79 46
3b9e3455
DW
47// ============================================================================
48// implementation
49// ============================================================================
50
51// ----------------------------------------------------------------------------
52// wxBitmapRefData
53// ----------------------------------------------------------------------------
54
0e320a79
DW
55wxBitmapRefData::wxBitmapRefData()
56{
3b9e3455
DW
57 m_nQuality = 0;
58 m_pSelectedInto = NULL;
59 m_nNumColors = 0;
60 m_pBitmapMask = NULL;
341366c6
DW
61 m_hBitmap = (WXHBITMAP) NULL;
62} // end of wxBitmapRefData::wxBitmapRefData
0e320a79 63
3b9e3455 64void wxBitmapRefData::Free()
0e320a79 65{
1d0edc0f
DW
66 if ( m_pSelectedInto )
67 {
68 wxLogLastError("GpiDeleteBitmap(hbitmap)");
69 }
3b9e3455
DW
70 if (m_hBitmap)
71 {
3437f881 72 if (!::GpiDeleteBitmap((HBITMAP)m_hBitmap))
3b9e3455
DW
73 {
74 wxLogLastError("GpiDeleteBitmap(hbitmap)");
75 }
76 }
77
78 delete m_pBitmapMask;
79 m_pBitmapMask = NULL;
341366c6 80} // end of wxBitmapRefData::Free
0e320a79 81
3b9e3455
DW
82// ----------------------------------------------------------------------------
83// wxBitmap creation
84// ----------------------------------------------------------------------------
0e320a79 85
3b9e3455
DW
86// this function should be called from all wxBitmap ctors
87void wxBitmap::Init()
0e320a79 88{
6f38c86f 89} // end of wxBitmap::Init
0e320a79 90
3b9e3455
DW
91bool wxBitmap::CopyFromIconOrCursor(
92 const wxGDIImage& rIcon
93)
d88de032 94{
6f38c86f
DW
95 HPOINTER hIcon = (HPOINTER)rIcon.GetHandle();
96 POINTERINFO SIconInfo;
97
98 if (!::WinQueryPointerInfo(hIcon, &SIconInfo))
99 {
100 wxLogLastError(wxT("WinQueryPointerInfo"));
101 return FALSE;
102 }
3b9e3455 103 wxBitmapRefData* pRefData = new wxBitmapRefData;
d88de032 104
3b9e3455 105 m_refData = pRefData;
d88de032 106
6f38c86f
DW
107 int nWidth = rIcon.GetWidth();
108 int nHeight = rIcon.GetHeight();
d88de032 109
6f38c86f
DW
110 pRefData->m_nWidth = nWidth;
111 pRefData->m_nHeight = nHeight;
112 pRefData->m_nDepth = wxDisplayDepth();
d88de032 113
6f38c86f
DW
114 pRefData->m_hBitmap = (WXHBITMAP)SIconInfo.hbmColor;
115
116 //
117 // No mask in the Info struct in OS/2
118 //
3b9e3455 119 return(TRUE);
6f38c86f 120} // end of wxBitmap::CopyFromIconOrCursor
0e320a79 121
3b9e3455
DW
122bool wxBitmap::CopyFromCursor(
123 const wxCursor& rCursor
124)
d88de032 125{
3b9e3455 126 UnRef();
d88de032 127
3b9e3455
DW
128 if (!rCursor.Ok())
129 return(FALSE);
43543d98 130 return(CopyFromIconOrCursor(rCursor));
6f38c86f 131} // end of wxBitmap::CopyFromCursor
d88de032 132
3b9e3455
DW
133bool wxBitmap::CopyFromIcon(
134 const wxIcon& rIcon
135)
0e320a79 136{
3b9e3455 137 UnRef();
0e320a79 138
3b9e3455
DW
139 if (!rIcon.Ok())
140 return(FALSE);
0e320a79 141
43543d98 142 return CopyFromIconOrCursor(rIcon);
6f38c86f 143} // end of wxBitmap::CopyFromIcon
0e320a79 144
3b9e3455 145wxBitmap::~wxBitmap()
d88de032 146{
6f38c86f 147} // end of wxBitmap::~wxBitmap
d88de032 148
3b9e3455
DW
149wxBitmap::wxBitmap(
150 const char zBits[]
3437f881
DW
151, int nWidth
152, int nHeight
153, int nDepth
3b9e3455
DW
154)
155{
156 Init();
157
158 wxBitmapRefData* pRefData = new wxBitmapRefData;
159 BITMAPINFOHEADER2 vHeader;
160 BITMAPINFO2 vInfo;
161 HDC hDc;
162 HPS hPs;
43543d98 163 DEVOPENSTRUC vDop = { NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL, NULL };
3b9e3455 164 SIZEL vSize = {0, 0};
3437f881 165 char* pzData;
3b9e3455 166
43543d98 167 wxASSERT(vHabmain != NULL);
3b9e3455 168
3437f881 169 m_refData = pRefData;
3b9e3455 170
3437f881
DW
171 pRefData->m_nWidth = nWidth;
172 pRefData->m_nHeight = nHeight;
173 pRefData->m_nDepth = nDepth;
174 pRefData->m_nNumColors = 0;
175 pRefData->m_pSelectedInto = NULL;
3b9e3455 176
c354beea 177 hDc = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
43543d98 178 hPs = ::GpiCreatePS(vHabmain, hDc, &vSize, GPIA_ASSOC | PU_PELS);
4f72fe4f 179 if (hPs == 0)
3b9e3455
DW
180 {
181 wxLogLastError("GpiCreatePS Failure");
182 }
0e320a79 183
3437f881
DW
184 if (nDepth == 1)
185 {
186 //
187 // We assume that it is in XBM format which is not quite the same as
188 // the format CreateBitmap() wants because the order of bytes in the
189 // line is inversed!
190 //
191 const size_t nBytesPerLine = (nWidth + 7) / 8;
192 const size_t nPadding = nBytesPerLine % 2;
193 const size_t nLen = nHeight * (nPadding + nBytesPerLine);
194 const char* pzSrc = zBits;
195 int nRows;
196 size_t nCols;
197
198 pzData = (char *)malloc(nLen);
199
200 char* pzDst = pzData;
201
202 for (nRows = 0; nRows < nHeight; nRows++)
203 {
204 for (nCols = 0; nCols < nBytesPerLine; nCols++)
205 {
206 unsigned char ucVal = *pzSrc++;
207 unsigned char ucReversed = 0;
208 int nBits;
0e320a79 209
3437f881
DW
210 for (nBits = 0; nBits < 8; nBits++)
211 {
212 ucReversed <<= 1;
213 ucReversed |= (ucVal & 0x01);
214 ucVal >>= 1;
215 }
216 *pzDst++ = ucReversed;
217 }
218 if (nPadding)
219 *pzDst++ = 0;
220 }
221 }
222 else
223 {
224 //
225 // Bits should already be in Windows standard format
226 //
227 pzData = (char *)zBits; // const_cast is harmless
228 }
229
c354beea
DW
230 memset(&vHeader, '\0', 16);
231 vHeader.cbFix = 16;
3437f881
DW
232 vHeader.cx = (USHORT)nWidth;
233 vHeader.cy = (USHORT)nHeight;
234 vHeader.cPlanes = 1L;
235 vHeader.cBitCount = nDepth;
236 vHeader.usReserved = 0;
c354beea
DW
237
238 memset(&vInfo, '\0', 16);
239 vInfo.cbFix = 16;
3437f881
DW
240 vInfo.cx = (USHORT)nWidth;
241 vInfo.cy = (USHORT)nHeight;
242 vInfo.cPlanes = 1L;
243 vInfo.cBitCount = nDepth;
3437f881
DW
244
245 HBITMAP hBmp = ::GpiCreateBitmap(hPs, &vHeader, CBM_INIT, (PBYTE)pzData, &vInfo);
0e320a79 246
43543d98 247 if (!hBmp)
3b9e3455
DW
248 {
249 wxLogLastError("CreateBitmap");
250 }
c354beea
DW
251 ::GpiDestroyPS(hPs);
252 ::DevCloseDC(hDc);
43543d98 253 SetHBITMAP((WXHBITMAP)hBmp);
6f38c86f 254} // end of wxBitmap::wxBitmap
0e320a79 255
3b9e3455
DW
256wxBitmap::wxBitmap(
257 int nW
258, int nH
259, int nD
260)
261{
262 Init();
263
264 (void)Create( nW
265 ,nH
266 ,nD
267 );
6f38c86f 268} // end of wxBitmap::wxBitmap
3b9e3455
DW
269
270wxBitmap::wxBitmap(
271 void* pData
272, long lType
273, int nWidth
274, int nHeight
275, int nDepth
276)
277{
278 Init();
279
280 (void)Create( pData
281 ,lType
282 ,nWidth
283 ,nHeight
284 ,nDepth
285 );
6f38c86f 286} // end of wxBitmap::wxBitmap
3b9e3455
DW
287
288wxBitmap::wxBitmap(
289 const wxString& rFilename
290, long lType
291)
292{
293 Init();
294
295 LoadFile( rFilename
296 ,(int)lType
297 );
6f38c86f 298} // end of wxBitmap::wxBitmap
3b9e3455
DW
299
300bool wxBitmap::Create(
301 int nW
302, int nH
303, int nD
304)
305{
306 HBITMAP hBmp;
307 BITMAPINFOHEADER2 vHeader;
3b9e3455 308
43543d98 309 wxASSERT(vHabmain != NULL);
0e320a79 310 UnRef();
0e320a79 311 m_refData = new wxBitmapRefData;
43543d98
DW
312 GetBitmapData()->m_nWidth = nW;
313 GetBitmapData()->m_nHeight = nH;
314 GetBitmapData()->m_nDepth = nD;
0e320a79 315
3b9e3455
DW
316 if (nD > 0)
317 {
6f38c86f
DW
318 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
319 SIZEL vSize = {0, 0};
320 HDC hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
321 HPS hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIA_ASSOC);
322
c354beea
DW
323 memset(&vHeader, '\0', 16);
324 vHeader.cbFix = 16;
6f38c86f
DW
325 vHeader.cx = nW;
326 vHeader.cy = nH;
327 vHeader.cPlanes = 1;
328 vHeader.cBitCount = nD;
329
330 hBmp = ::GpiCreateBitmap( hPS
331 ,&vHeader
332 ,0L
333 ,NULL
334 ,NULL
335 );
336 ::GpiDestroyPS(hPS);
337 ::DevCloseDC(hDC);
3b9e3455
DW
338 }
339 else
340 {
6f38c86f
DW
341 HPS hPSScreen;
342 HDC hDCScreen;
343 LONG lBitCount;
344
345 hPSScreen = ::WinGetScreenPS(HWND_DESKTOP);
346 hDCScreen = ::GpiQueryDevice(hPSScreen);
347 ::DevQueryCaps(hDCScreen, CAPS_COLOR_BITCOUNT, 1L, &lBitCount);
348
c354beea
DW
349 memset(&vHeader, '\0', 16);
350 vHeader.cbFix = 16;
6f38c86f
DW
351 vHeader.cx = nW;
352 vHeader.cy = nH;
353 vHeader.cPlanes = 1;
354 vHeader.cBitCount = lBitCount;
355
356 hBmp = ::GpiCreateBitmap( hPSScreen
357 ,&vHeader
358 ,0L
359 ,NULL
360 ,NULL
361 );
4f72fe4f 362
43543d98 363 GetBitmapData()->m_nDepth = wxDisplayDepth();
6f38c86f 364 ::WinReleasePS(hPSScreen);
3b9e3455 365 }
4f72fe4f 366 SetHBITMAP((WXHBITMAP)hBmp);
0e320a79 367
3b9e3455 368#if WXWIN_COMPATIBILITY_2
4f72fe4f 369 GetBitmapData()->m_bOk = hBmp != 0;
3b9e3455
DW
370#endif // WXWIN_COMPATIBILITY_2
371
372 return Ok();
6f38c86f 373} // end of wxBitmap::Create
0e320a79 374
1c344e89
DW
375bool wxBitmap::CreateFromXpm(
376 const char** ppData
377)
378{
379#if wxUSE_IMAGE && wxUSE_XPM
380 Init();
381
382 wxCHECK_MSG(ppData != NULL, FALSE, wxT("invalid bitmap data"))
383
384 wxXPMDecoder vDecoder;
385 wxImage vImg = vDecoder.ReadData(ppData);
386
387 wxCHECK_MSG(vImg.Ok(), FALSE, wxT("invalid bitmap data"))
388
389 *this = wxBitmap(vImg);
390 return TRUE;
391#else
392 return FALSE;
393#endif
394} // end of wxBitmap::CreateFromXpm
395
4f72fe4f
DW
396bool wxBitmap::LoadFile(
397 const wxString& rFilename
398, long lType
399)
0e320a79 400{
4e0f4f97 401 HPS hPs = NULLHANDLE;
8ea3f821 402
0e320a79
DW
403 UnRef();
404
4f72fe4f
DW
405 wxBitmapHandler* pHandler = wxDynamicCast( FindHandler(lType)
406 ,wxBitmapHandler
407 );
0e320a79 408
4f72fe4f 409 if (pHandler)
3b9e3455
DW
410 {
411 m_refData = new wxBitmapRefData;
0e320a79 412
4f72fe4f
DW
413 return(pHandler->LoadFile( this
414 ,rFilename
8ea3f821 415 ,hPs
4f72fe4f
DW
416 ,lType
417 , -1
418 , -1
43543d98 419 ));
0e320a79 420 }
3b9e3455
DW
421 else
422 {
4f72fe4f 423 wxImage vImage;
0e320a79 424
43543d98 425 if (!vImage.LoadFile(rFilename, lType) || !vImage.Ok() )
4f72fe4f 426 return(FALSE);
3b9e3455 427
f95255e2 428 *this = wxBitmap(vImage);
4f72fe4f
DW
429
430 return(TRUE);
3b9e3455 431 }
6f38c86f 432} // end of wxBitmap::LoadFile
0e320a79 433
4f72fe4f
DW
434bool wxBitmap::Create(
435 void* pData
436, long lType
437, int nWidth
438, int nHeight
439, int nDepth
440)
0e320a79
DW
441{
442 UnRef();
443
4f72fe4f
DW
444 wxBitmapHandler* pHandler = wxDynamicCast( FindHandler(lType)
445 ,wxBitmapHandler
446 );
0e320a79 447
4f72fe4f 448 if (!pHandler)
3b9e3455
DW
449 {
450 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
43543d98 451 "type %d defined."), lType);
0e320a79 452
4f72fe4f 453 return(FALSE);
0e320a79
DW
454 }
455
3b9e3455
DW
456 m_refData = new wxBitmapRefData;
457
43543d98
DW
458 return(pHandler->Create( this
459 ,pData
460 ,lType
461 ,nWidth
462 ,nHeight
463 ,nDepth
464 ));
6f38c86f 465} // end of wxBitmap::Create
0e320a79 466
58b16424 467bool wxBitmap::SaveFile(
4f72fe4f
DW
468 const wxString& rFilename
469, int lType
470, const wxPalette* pPalette
471)
0e320a79 472{
4f72fe4f
DW
473 wxBitmapHandler* pHandler = wxDynamicCast( FindHandler(lType)
474 ,wxBitmapHandler
475 );
0e320a79 476
4f72fe4f 477 if (pHandler)
3b9e3455 478 {
4f72fe4f
DW
479 return pHandler->SaveFile( this
480 ,rFilename
481 ,lType
482 ,pPalette
483 );
3b9e3455
DW
484 }
485 else
486 {
487 // FIXME what about palette? shouldn't we use it?
f95255e2 488 wxImage vImage = ConvertToImage();
4f72fe4f
DW
489
490 if (!vImage.Ok())
491 return(FALSE);
0e320a79 492
4f72fe4f
DW
493 return(vImage.SaveFile( rFilename
494 ,lType
495 ));
3b9e3455 496 }
6f38c86f
DW
497} // end of wxBitmap::SaveFile
498
fec19ea9
VS
499
500// ----------------------------------------------------------------------------
501// wxImage-wxBitmap convertion
502// ----------------------------------------------------------------------------
503
3437f881
DW
504bool wxBitmap::CreateFromImage (
505 const wxImage& rImage
506, int nDepth
507)
fec19ea9 508{
3437f881 509 wxCHECK_MSG(rImage.Ok(), FALSE, wxT("invalid image"));
a086de98 510 m_refData = new wxBitmapRefData();
3437f881 511
c354beea 512
3437f881 513 int nSizeLimit = 1024 * 768 * 3;
a086de98
DW
514 int nWidth = rImage.GetWidth();
515 int nBmpHeight = rImage.GetHeight();
3437f881
DW
516 int nBytePerLine = nWidth * 3;
517 int nSizeDWORD = sizeof(DWORD);
518 int nLineBoundary = nBytePerLine % nSizeDWORD;
519 int nPadding = 0;
520
521 if (nLineBoundary > 0)
fec19ea9 522 {
3437f881
DW
523 nPadding = nSizeDWORD - nLineBoundary;
524 nBytePerLine += nPadding;
fec19ea9 525 }
3437f881
DW
526
527 //
528 // Calc the number of DIBs and heights of DIBs
529 //
530 int nNumDIB = 1;
531 int nHRemain = 0;
532 int nHeight = nSizeLimit / nBytePerLine;
533
534 if (nHeight >= nBmpHeight)
535 nHeight = nBmpHeight;
fec19ea9
VS
536 else
537 {
3437f881
DW
538 nNumDIB = nBmpHeight / nHeight;
539 nHRemain = nBmpHeight % nHeight;
540 if (nHRemain > 0)
541 nNumDIB++;
fec19ea9
VS
542 }
543
3437f881
DW
544 //
545 // Set bitmap parameters
546 //
547 wxCHECK_MSG(rImage.Ok(), FALSE, wxT("invalid image"));
548 SetWidth(nWidth);
549 SetHeight(nBmpHeight);
550 if (nDepth == -1)
1cee3f60 551 nDepth = wxDisplayDepth();
3437f881
DW
552 SetDepth(nDepth);
553
554#if wxUSE_PALETTE
555 //
556 // Copy the palette from the source image
557 //
558 SetPalette(rImage.GetPalette());
559#endif // wxUSE_PALETTE
560
561 //
562 // Create a DIB header
563 //
564 BITMAPINFOHEADER2 vHeader;
c354beea 565 BITMAPINFO2 vInfo;
3437f881
DW
566
567 //
fec19ea9 568 // Fill in the DIB header
3437f881 569 //
c354beea
DW
570 memset(&vHeader, '\0', 16);
571 vHeader.cbFix = 16;
572 vHeader.cx = (ULONG)nWidth;
573 vHeader.cy = (ULONG)nHeight;
3437f881
DW
574 vHeader.cPlanes = 1L;
575 vHeader.cBitCount = 24;
3437f881 576
c354beea
DW
577 memset(&vInfo, '\0', 16);
578 vInfo.cbFix = 16;
579 vInfo.cx = (ULONG)nWidth;
580 vInfo.cy = (ULONG)nHeight;
581 vInfo.cPlanes = 1L;
582 vInfo.cBitCount = 24;
3437f881
DW
583 //
584 // Memory for DIB data
585 //
586 unsigned char* pucBits;
587
c354beea 588 pucBits = (unsigned char *)malloc(nBytePerLine * nHeight);
3437f881
DW
589 if(!pucBits)
590 {
591 wxFAIL_MSG(wxT("could not allocate memory for DIB"));
592 return FALSE;
593 }
594
595 //
596 // Create and set the device-dependent bitmap
597 //
598 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
599 SIZEL vSize = {0, 0};
600 HDC hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
601 HPS hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIA_ASSOC);
c354beea 602 LONG lScans;
b1b054da 603 HDC hDCScreen = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
c354beea 604 HPS hPSScreen;
3437f881
DW
605 HBITMAP hBmp;
606 HBITMAP hBmpOld;
607
608 hBmp = ::GpiCreateBitmap( hPS
609 ,&vHeader
610 ,0L
611 ,NULL
612 ,NULL
613 );
614 hBmpOld = ::GpiSetBitmap(hPS, hBmp);
615#if wxUSE_PALETTE
616 HPAL hOldPalette = NULLHANDLE;
617 if (rImage.GetPalette().Ok())
fec19ea9 618 {
3437f881 619 hOldPalette = ::GpiSelectPalette(hPS, (HPAL)rImage.GetPalette().GetHPALETTE());
fec19ea9 620 }
3437f881 621#endif // wxUSE_PALETTE
fec19ea9 622
3437f881
DW
623 //
624 // Copy image data into DIB data and then into DDB (in a loop)
625 //
626 unsigned char* pData = rImage.GetData();
627 int i;
628 int j;
629 int n;
630 int nOrigin = 0;
631 unsigned char* ptdata = pData;
632 unsigned char* ptbits;
633
634 for (n = 0; n < nNumDIB; n++)
fec19ea9 635 {
3437f881 636 if (nNumDIB > 1 && n == nNumDIB - 1 && nHRemain > 0)
fec19ea9 637 {
3437f881
DW
638 //
639 // Redefine height and size of the (possibly) last smaller DIB
fec19ea9 640 // memory is not reallocated
3437f881
DW
641 //
642 nHeight = nHRemain;
643 vHeader.cy = (DWORD)(nHeight);
644 vHeader.cbImage = nBytePerLine * nHeight;
fec19ea9 645 }
3437f881
DW
646 ptbits = pucBits;
647 for (j = 0; j < nHeight; j++)
fec19ea9 648 {
3437f881 649 for (i = 0; i < nWidth; i++)
fec19ea9 650 {
3437f881
DW
651 *(ptbits++) = *(ptdata + 2);
652 *(ptbits++) = *(ptdata + 1);
653 *(ptbits++) = *(ptdata);
fec19ea9
VS
654 ptdata += 3;
655 }
3437f881
DW
656 for (i = 0; i < nPadding; i++)
657 *(ptbits++) = 0;
fec19ea9 658 }
3437f881
DW
659
660 //
661 // Have to do something similar to WIN32's StretchDIBits, use GpiBitBlt
c354beea 662 // in combination with setting the bits into the selected bitmap
3437f881 663 //
b1b054da 664 vInfo.cBitCount = 16;
f16e72c8
DW
665 if ((lScans = ::GpiSetBitmapBits( hPS
666 ,0 // Start at the bottom
667 ,(LONG)nHeight // One line per scan
668 ,(PBYTE)pucBits
669 ,&vInfo
670 )) == GPI_ALTERROR)
671 {
672 ERRORID vError;
673 wxString sError;
674
675 vError = ::WinGetLastError(vHabmain);
676 sError = wxPMErrorToStr(vError);
677 }
678
b1b054da
DW
679 //
680 // for debugging----
681 //
682LONG alFormats[24];
683::GpiQueryDeviceBitmapFormats(hPS, 24, alFormats);
684if ((lScans = ::GpiQueryBitmapBits( hPS
685 ,0L
686 ,(LONG)nHeight
687 ,(PBYTE)pucBits
688 ,&vInfo
689 )) == GPI_ALTERROR)
690{
691 ERRORID vError;
692 wxString sError;
693
694 vError = ::WinGetLastError(vHabmain);
695 sError = wxPMErrorToStr(vError);
696}
697
c354beea
DW
698 hPSScreen = ::GpiCreatePS( vHabmain
699 ,hDCScreen
700 ,&vSize
701 ,PU_PELS | GPIA_ASSOC
702 );
703
3437f881
DW
704 POINTL vPoint[4] = { 0, nOrigin,
705 nWidth, nHeight,
706 0, 0, nWidth, nHeight
707 };
708
c354beea
DW
709
710 ::GpiBitBlt( hPSScreen
3437f881
DW
711 ,hPS
712 ,4
713 ,vPoint
714 ,ROP_SRCCOPY
715 ,BBO_IGNORE
716 );
c354beea 717 ::GpiDestroyPS(hPSScreen);
3437f881 718 nOrigin += nHeight;
fec19ea9 719 }
3437f881
DW
720 SetHBITMAP((WXHBITMAP)hBmp);
721#if wxUSE_PALETTE
722 if (hOldPalette)
723 ::GpiSelectPalette(hPS, hOldPalette);
724#endif // wxUSE_PALETTE
fec19ea9 725
3437f881
DW
726 //
727 // Similarly, created an mono-bitmap for the possible mask
728 //
729 if (rImage.HasMask())
fec19ea9 730 {
b1b054da 731 vHeader.cbFix = 16;
3437f881
DW
732 vHeader.cx = nWidth;
733 vHeader.cy = nHeight;
734 vHeader.cPlanes = 1;
b1b054da 735 vHeader.cBitCount = 24;
3437f881
DW
736 hBmp = ::GpiCreateBitmap( hPS
737 ,&vHeader
738 ,0L
739 ,NULL
740 ,NULL
741 );
1d0edc0f 742 hBmpOld = ::GpiSetBitmap(hPS, hBmp);
3437f881
DW
743 if (nNumDIB == 1)
744 nHeight = nBmpHeight;
745 else
746 nHeight = nSizeLimit / nBytePerLine;
747 vHeader.cy = (DWORD)(nHeight);
3437f881
DW
748 nOrigin = 0;
749
750 unsigned char cRed = rImage.GetMaskRed();
751 unsigned char cGreen = rImage.GetMaskGreen();
752 unsigned char cBlue = rImage.GetMaskBlue();
753 unsigned char cZero = 0;
754 unsigned char cOne = 255;
755
756 ptdata = pData;
757 for (n = 0; n < nNumDIB; n++)
fec19ea9 758 {
3437f881 759 if (nNumDIB > 1 && n == nNumDIB - 1 && nHRemain > 0)
fec19ea9 760 {
3437f881
DW
761 //
762 // Redefine height and size of the (possibly) last smaller DIB
fec19ea9 763 // memory is not reallocated
3437f881
DW
764 //
765 nHeight = nHRemain;
766 vHeader.cy = (DWORD)(nHeight);
fec19ea9 767 }
3437f881
DW
768 ptbits = pucBits;
769 for (int j = 0; j < nHeight; j++)
fec19ea9 770 {
3437f881 771 for (i = 0; i < nWidth; i++)
fec19ea9 772 {
3437f881 773 if ((*(ptdata++) != cRed) || (*(ptdata++) != cGreen) || (*(ptdata++) != cBlue))
fec19ea9 774 {
3437f881
DW
775 *(ptbits++) = cOne;
776 *(ptbits++) = cOne;
777 *(ptbits++) = cOne;
fec19ea9
VS
778 }
779 else
780 {
3437f881
DW
781 *(ptbits++) = cZero;
782 *(ptbits++) = cZero;
783 *(ptbits++) = cZero;
fec19ea9
VS
784 }
785 }
3437f881
DW
786 for (i = 0; i < nPadding; i++)
787 *(ptbits++) = cZero;
fec19ea9 788 }
c354beea
DW
789 lScans = ::GpiSetBitmapBits( hPS
790 ,0 // Start at the bottom
791 ,(LONG)nHeight // One line per scan
792 ,(PBYTE)pucBits
793 ,&vInfo
794 );
795 hPSScreen = ::GpiCreatePS( vHabmain
796 ,hDCScreen
797 ,&vSize
798 ,PU_PELS | GPIA_ASSOC
799 );
800 POINTL vPoint2[4] = { 0, nOrigin,
801 nWidth, nHeight,
802 0, 0, nWidth, nHeight
803 };
804 ::GpiBitBlt( hPSScreen
3437f881
DW
805 ,hPS
806 ,4
c354beea 807 ,vPoint2
3437f881
DW
808 ,ROP_SRCCOPY
809 ,BBO_IGNORE
c354beea
DW
810 );
811 ::GpiDestroyPS(hPSScreen);
3437f881 812 nOrigin += nHeight;
fec19ea9 813 }
fec19ea9 814
3437f881
DW
815 //
816 // Create a wxMask object
817 //
818 wxMask* pMask = new wxMask();
fec19ea9 819
3437f881
DW
820 pMask->SetMaskBitmap((WXHBITMAP)hBmp);
821 SetMask(pMask);
b1b054da 822 hBmpOld = ::GpiSetBitmap(hPS, hBmpOld);
3437f881 823 }
fec19ea9 824
3437f881
DW
825 //
826 // Free allocated resources
827 //
828 ::GpiSetBitmap(hPS, NULLHANDLE);
829 ::GpiDestroyPS(hPS);
830 ::DevCloseDC(hDC);
831 free(pucBits);
fec19ea9 832 return TRUE;
3437f881 833} // end of wxBitmap::CreateFromImage
fec19ea9
VS
834
835wxImage wxBitmap::ConvertToImage() const
836{
3437f881 837 wxImage vImage;
1cee3f60 838 wxDC* pDC;
1c344e89 839
fec19ea9
VS
840 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
841
3437f881
DW
842 //
843 // Create an wxImage object
844 //
845 int nWidth = GetWidth();
846 int nHeight = GetHeight();
847 int nDevWidth;
848 int nDevHeight;
849 int nBytePerLine = nWidth * 3;
850 int nSizeDWORD = sizeof(DWORD);
851 int nLineBoundary = nBytePerLine % nSizeDWORD;
852 int nPadding = 0;
853 unsigned char* pData;
854 unsigned char* lpBits;
855 long lScans;
856 BITMAPINFOHEADER2 vDIBh;
857 BITMAPINFO2 vDIBInfo;
3437f881
DW
858 HPS hPSMem;
859 HPS hPS;
3437f881 860 HBITMAP hBitmap;
1cee3f60
DW
861 HBITMAP hOldBitmap;
862 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
863 SIZEL vSizlPage = {0,0};
864 HDC hDCMem;
3437f881
DW
865
866 vImage.Create( nWidth
867 ,nHeight
868 );
869 pData = vImage.GetData();
870 if(!pData)
fec19ea9
VS
871 {
872 wxFAIL_MSG( wxT("could not allocate data for image") );
873 return wxNullImage;
874 }
3437f881 875 if(nLineBoundary > 0)
fec19ea9 876 {
3437f881
DW
877 nPadding = nSizeDWORD - nLineBoundary;
878 nBytePerLine += nPadding;
fec19ea9 879 }
3437f881
DW
880 wxDisplaySize( &nDevWidth
881 ,&nDevHeight
882 );
883 //
884 // Create and fill a DIB header
885 //
c354beea
DW
886 memset(&vDIBh, '\0', 16);
887 vDIBh.cbFix = 16;
3437f881
DW
888 vDIBh.cx = nWidth;
889 vDIBh.cy = nHeight;
890 vDIBh.cPlanes = 1;
3437f881
DW
891 vDIBh.cBitCount = 24;
892
c354beea
DW
893 memset(&vDIBInfo, '\0', 16);
894 vDIBInfo.cbFix = 16;
895 vDIBInfo.cx = nWidth;
896 vDIBInfo.cy = nHeight;
3437f881
DW
897 vDIBInfo.cPlanes = 1;
898 vDIBInfo.cBitCount = 24;
c354beea
DW
899
900 lpBits = (unsigned char *)malloc(nBytePerLine * nHeight);
3437f881 901 if (!lpBits)
fec19ea9 902 {
3437f881
DW
903 wxFAIL_MSG(wxT("could not allocate data for DIB"));
904 free(pData);
905 return wxNullImage;
fec19ea9
VS
906 }
907
1cee3f60
DW
908 //
909 // May already be selected into a PS
910 //
911 if ((pDC = GetSelectedInto()) != NULL)
912 {
913 hPSMem = pDC->GetHPS();
914 }
915 else
916 {
917 hDCMem = ::DevOpenDC( vHabmain
918 ,OD_MEMORY
919 ,"*"
920 ,5L
921 ,(PDEVOPENDATA)&vDop
922 ,NULLHANDLE
923 );
924 hPSMem = ::GpiCreatePS( vHabmain
925 ,hDCMem
926 ,&vSizlPage
927 ,PU_PELS | GPIA_ASSOC
928 );
929 hBitmap = (HBITMAP)GetHBITMAP();
930 if ((hOldBitmap = ::GpiSetBitmap(hPSMem, hBitmap)) == HBM_ERROR)
931 {
932 ERRORID vError;
933 wxString sError;
934
935 vError = ::WinGetLastError(vHabmain);
936 sError = wxPMErrorToStr(vError);
937 }
938 }
939
3437f881
DW
940 //
941 // Copy data from the device-dependent bitmap to the DIB
942 //
1cee3f60
DW
943 if ((lScans = ::GpiQueryBitmapBits( hPSMem
944 ,0L
945 ,(LONG)nHeight
946 ,(PBYTE)lpBits
947 ,&vDIBInfo
948 )) == GPI_ALTERROR)
949 {
950 ERRORID vError;
951 wxString sError;
952
953 vError = ::WinGetLastError(vHabmain);
954 sError = wxPMErrorToStr(vError);
955 }
3437f881
DW
956
957 //
958 // Copy DIB data into the wxImage object
959 //
960 int i;
961 int j;
962 unsigned char* ptdata = pData;
963 unsigned char* ptbits = lpBits;
964
965 for (i = 0; i < nHeight; i++)
fec19ea9 966 {
3437f881 967 for (j = 0; j < nWidth; j++)
fec19ea9
VS
968 {
969 *(ptdata++) = *(ptbits+2);
970 *(ptdata++) = *(ptbits+1);
971 *(ptdata++) = *(ptbits );
972 ptbits += 3;
973 }
3437f881 974 ptbits += nPadding;
fec19ea9 975 }
1cee3f60
DW
976 if ((pDC = GetSelectedInto()) == NULL)
977 {
978 ::GpiSetBitmap(hPSMem, NULLHANDLE);
979 ::GpiDestroyPS(hPSMem);
980 ::DevCloseDC(hDCMem);
981 }
fec19ea9 982
3437f881
DW
983 //
984 // Similarly, set data according to the possible mask bitmap
985 //
986 if (GetMask() && GetMask()->GetMaskBitmap())
fec19ea9 987 {
3437f881
DW
988 hBitmap = (HBITMAP)GetMask()->GetMaskBitmap();
989
990 //
991 // Memory DC/PS created, color set, data copied, and memory DC/PS deleted
992 //
1cee3f60
DW
993 HDC hMemDC = ::DevOpenDC( vHabmain
994 ,OD_MEMORY
995 ,"*"
996 ,5L
997 ,(PDEVOPENDATA)&vDop
998 ,NULLHANDLE
999 );
3437f881
DW
1000 HPS hMemPS = ::GpiCreatePS( vHabmain
1001 ,hMemDC
1002 ,&vSizlPage
b1b054da 1003 ,PU_PELS | GPIA_ASSOC
3437f881
DW
1004 );
1005 ::GpiSetColor(hMemPS, OS2RGB(0, 0, 0));
1006 ::GpiSetBackColor(hMemPS, OS2RGB(255, 255, 255) );
1d0edc0f 1007 ::GpiSetBitmap(hMemPS, hBitmap);
3437f881
DW
1008 ::GpiQueryBitmapBits( hPSMem
1009 ,0L
1010 ,(LONG)nHeight
1011 ,(PBYTE)lpBits
1012 ,&vDIBInfo
1013 );
1d0edc0f 1014 ::GpiSetBitmap(hMemPS, NULLHANDLE);
3437f881
DW
1015 ::GpiDestroyPS(hMemPS);
1016 ::DevCloseDC(hMemDC);
1017
1018 //
1019 // Background color set to RGB(16,16,16) in consistent with wxGTK
1020 //
1021 unsigned char ucRed = 16;
1022 unsigned char ucGreen = 16;
1023 unsigned char ucBlue = 16;
1024
1025 ptdata = pData;
fec19ea9 1026 ptbits = lpBits;
3437f881 1027 for (i = 0; i < nHeight; i++)
fec19ea9 1028 {
3437f881 1029 for (j = 0; j < nWidth; j++)
fec19ea9 1030 {
3437f881 1031 if (*ptbits != 0)
fec19ea9
VS
1032 ptdata += 3;
1033 else
1034 {
3437f881
DW
1035 *(ptdata++) = ucRed;
1036 *(ptdata++) = ucGreen;
1037 *(ptdata++) = ucBlue;
fec19ea9
VS
1038 }
1039 ptbits += 3;
1040 }
3437f881 1041 ptbits += nPadding;
fec19ea9 1042 }
3437f881
DW
1043 vImage.SetMaskColour( ucRed
1044 ,ucGreen
1045 ,ucBlue
1046 );
1047 vImage.SetMask(TRUE);
fec19ea9
VS
1048 }
1049 else
1050 {
3437f881 1051 vImage.SetMask(FALSE);
fec19ea9 1052 }
fec19ea9 1053
3437f881
DW
1054 //
1055 // Free allocated resources
1056 //
3437f881
DW
1057 free(lpBits);
1058 return vImage;
1059} // end of wxBitmap::ConvertToImage
fec19ea9 1060
6f38c86f
DW
1061// ----------------------------------------------------------------------------
1062// sub bitmap extraction
1063// ----------------------------------------------------------------------------
1064
1065wxBitmap wxBitmap::GetSubBitmap(
1066 const wxRect& rRect
1067) const
1068{
1069 wxCHECK_MSG( Ok() &&
1070 (rRect.x >= 0) && (rRect.y >= 0) &&
1071 (rRect.x + rRect.width <= GetWidth()) &&
1072 (rRect.y + rRect.height <= GetHeight()),
1073 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
1074
1075 wxBitmap vRet( rRect.width
1076 ,rRect.height
1077 ,GetDepth()
1078 );
1079 wxASSERT_MSG( vRet.Ok(), wxT("GetSubBitmap error") );
1080
1081
1082 //
1083 // Copy bitmap data
1084 //
1085 SIZEL vSize = {0, 0};
1086 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
1087 HDC hDCSrc = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
1088 HDC hDCDst = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
1089 HPS hPSSrc = ::GpiCreatePS(vHabmain, hDCSrc, &vSize, PU_PELS | GPIA_ASSOC);
1090 HPS hPSDst = ::GpiCreatePS(vHabmain, hDCDst, &vSize, PU_PELS | GPIA_ASSOC);
c354beea 1091 POINTL vPoint[4] = { 0, 0, rRect.width, rRect.height,
1d0edc0f 1092 rRect.x, rRect.y,
c354beea 1093 rRect.x + rRect.width, rRect.y + rRect.height
6f38c86f
DW
1094 };
1095
1096 ::GpiSetBitmap(hPSSrc, (HBITMAP) GetHBITMAP());
1097 ::GpiSetBitmap(hPSDst, (HBITMAP) vRet.GetHBITMAP());
1098 ::GpiBitBlt( hPSDst
1099 ,hPSSrc
1100 ,4L
1101 ,vPoint
1102 ,ROP_SRCCOPY
1103 ,BBO_IGNORE
1104 );
1105
1106 //
1107 // Copy mask if there is one
1108 //
1109 if (GetMask())
1110 {
1111 BITMAPINFOHEADER2 vBmih;
1112
1113 memset(&vBmih, '\0', sizeof(BITMAPINFOHEADER2));
1114 vBmih.cbFix = sizeof(BITMAPINFOHEADER2);
1115 vBmih.cx = rRect.width;
1116 vBmih.cy = rRect.height;
1117 vBmih.cPlanes = 1;
1118 vBmih.cBitCount = 1;
1119
1120 HBITMAP hBmpMask = ::GpiCreateBitmap( hPSDst
1121 ,&vBmih
1122 ,0L
1123 ,NULL
1124 ,NULL
1125 );
1126
1127 ::GpiSetBitmap(hPSSrc, (HBITMAP) GetHBITMAP());
1128 ::GpiSetBitmap(hPSDst, (HBITMAP) vRet.GetHBITMAP());
1129
1130 ::GpiSetBitmap(hPSSrc, (HBITMAP) GetMask()->GetMaskBitmap());
1131 ::GpiSetBitmap(hPSDst, (HBITMAP) hBmpMask);
1132 ::GpiBitBlt( hPSDst
1133 ,hPSSrc
1134 ,4L
1135 ,vPoint
1136 ,ROP_SRCCOPY
1137 ,BBO_IGNORE
1138 );
1139
1140 wxMask* pMask = new wxMask((WXHBITMAP)hBmpMask);
1141 vRet.SetMask(pMask);
1142 }
1143
1144 ::GpiSetBitmap(hPSSrc, NULL);
1145 ::GpiSetBitmap(hPSDst, NULL);
1146 ::GpiDestroyPS(hPSSrc);
1147 ::GpiDestroyPS(hPSDst);
1148 ::DevCloseDC(hDCSrc);
1149 ::DevCloseDC(hDCDst);
1150 return vRet;
1151} // end of wxBitmap::GetSubBitmap
0e320a79 1152
3b9e3455
DW
1153// ----------------------------------------------------------------------------
1154// wxBitmap accessors
1155// ----------------------------------------------------------------------------
0e320a79 1156
4f72fe4f
DW
1157void wxBitmap::SetQuality(
1158 int nQ
1159)
0e320a79 1160{
3b9e3455 1161 EnsureHasData();
0e320a79 1162
4f72fe4f 1163 GetBitmapData()->m_nQuality = nQ;
6f38c86f 1164} // end of wxBitmap::SetQuality
0e320a79 1165
3b9e3455 1166#if WXWIN_COMPATIBILITY_2
4f72fe4f
DW
1167void wxBitmap::SetOk(
1168 bool bOk
1169)
0e320a79 1170{
3b9e3455 1171 EnsureHasData();
0e320a79 1172
4f72fe4f 1173 GetBitmapData()->m_bOk = bOk;
6f38c86f 1174} // end of wxBitmap::SetOk
3b9e3455 1175#endif // WXWIN_COMPATIBILITY_2
0e320a79 1176
4f72fe4f
DW
1177void wxBitmap::SetPalette(
1178 const wxPalette& rPalette
1179)
0e320a79 1180{
3b9e3455 1181 EnsureHasData();
0e320a79 1182
4f72fe4f 1183 GetBitmapData()->m_vBitmapPalette = rPalette;
6f38c86f 1184} // end of wxBitmap::SetPalette
0e320a79 1185
4f72fe4f
DW
1186void wxBitmap::SetMask(
1187 wxMask* pMask
1188)
0e320a79 1189{
3b9e3455 1190 EnsureHasData();
0e320a79 1191
4f72fe4f 1192 GetBitmapData()->m_pBitmapMask = pMask;
6f38c86f 1193} // end of wxBitmap::SetMask
0e320a79 1194
4f72fe4f
DW
1195wxBitmap wxBitmap::GetBitmapForDC(
1196 wxDC& rDc
1197) const
d88de032 1198{
3437f881 1199 return(*this);
6f38c86f 1200} // end of wxBitmap::GetBitmapForDC
d88de032 1201
3b9e3455
DW
1202// ----------------------------------------------------------------------------
1203// wxMask
1204// ----------------------------------------------------------------------------
0e320a79
DW
1205
1206wxMask::wxMask()
1207{
4f72fe4f 1208 m_hMaskBitmap = 0;
6f38c86f 1209} // end of wxMask::wxMask
0e320a79
DW
1210
1211// Construct a mask from a bitmap and a colour indicating
1212// the transparent area
4f72fe4f
DW
1213wxMask::wxMask(
1214 const wxBitmap& rBitmap
1215, const wxColour& rColour
1216)
0e320a79 1217{
4f72fe4f
DW
1218 m_hMaskBitmap = 0;
1219 Create( rBitmap
1220 ,rColour
1221 );
6f38c86f 1222} // end of wxMask::wxMask
0e320a79
DW
1223
1224// Construct a mask from a bitmap and a palette index indicating
1225// the transparent area
4f72fe4f
DW
1226wxMask::wxMask(
1227 const wxBitmap& rBitmap
1228, int nPaletteIndex
1229)
0e320a79 1230{
4f72fe4f
DW
1231 m_hMaskBitmap = 0;
1232 Create( rBitmap
1233 ,nPaletteIndex
1234 );
6f38c86f 1235} // end of wxMask::wxMask
0e320a79
DW
1236
1237// Construct a mask from a mono bitmap (copies the bitmap).
4f72fe4f
DW
1238wxMask::wxMask(
1239 const wxBitmap& rBitmap
1240)
0e320a79 1241{
4f72fe4f
DW
1242 m_hMaskBitmap = 0;
1243 Create(rBitmap);
6f38c86f 1244} // end of wxMask::wxMask
0e320a79
DW
1245
1246wxMask::~wxMask()
1247{
4f72fe4f
DW
1248 if (m_hMaskBitmap)
1249 ::GpiDeleteBitmap((HBITMAP)m_hMaskBitmap);
6f38c86f 1250} // end of wxMask::~wxMask
0e320a79
DW
1251
1252// Create a mask from a mono bitmap (copies the bitmap).
58b16424
DW
1253bool wxMask::Create(
1254 const wxBitmap& rBitmap
1255)
0e320a79 1256{
6f38c86f 1257 BITMAPINFOHEADER2 vBmih;
58b16424 1258 SIZEL vSize = {0, 0};
6f38c86f
DW
1259 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
1260 HDC hDCSrc = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
1261 HDC hDCDst = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
1262 HPS hPSSrc = ::GpiCreatePS(vHabmain, hDCSrc, &vSize, PU_PELS | GPIA_ASSOC);
1263 HPS hPSDst = ::GpiCreatePS(vHabmain, hDCDst, &vSize, PU_PELS | GPIA_ASSOC);
1264 POINTL vPoint[4] = { 0 ,0, rBitmap.GetWidth(), rBitmap.GetHeight(),
1265 0, 0, rBitmap.GetWidth(), rBitmap.GetHeight()
1266 };
58b16424
DW
1267
1268 if (m_hMaskBitmap)
3b9e3455 1269 {
58b16424
DW
1270 ::GpiDeleteBitmap((HBITMAP) m_hMaskBitmap);
1271 m_hMaskBitmap = 0;
3b9e3455 1272 }
58b16424 1273 if (!rBitmap.Ok() || rBitmap.GetDepth() != 1)
3b9e3455 1274 {
58b16424 1275 return(FALSE);
3b9e3455 1276 }
6f38c86f
DW
1277
1278 memset(&vBmih, '\0', sizeof(BITMAPINFOHEADER2));
1279 vBmih.cbFix = sizeof(BITMAPINFOHEADER2);
1280 vBmih.cx = rBitmap.GetWidth();
1281 vBmih.cy = rBitmap.GetHeight();
1282 vBmih.cPlanes = 1;
1283 vBmih.cBitCount = 1;
1284
1285 m_hMaskBitmap = ::GpiCreateBitmap( hPSDst
1286 ,&vBmih
1287 ,0L
1288 ,NULL
1289 ,NULL
1290 );
1291
1292 ::GpiSetBitmap(hPSSrc, (HBITMAP) rBitmap.GetHBITMAP());
1293 ::GpiSetBitmap(hPSDst, (HBITMAP) m_hMaskBitmap);
1294 ::GpiBitBlt( hPSDst
1295 ,hPSSrc
1296 ,4L
1297 ,vPoint
1298 ,ROP_SRCCOPY
1299 ,BBO_IGNORE
1300 );
1301
1302 ::GpiDestroyPS(hPSSrc);
1303 ::GpiDestroyPS(hPSDst);
1304 ::DevCloseDC(hDCSrc);
1305 ::DevCloseDC(hDCDst);
58b16424 1306 return(TRUE);
6f38c86f 1307} // end of wxMask::Create
0e320a79
DW
1308
1309// Create a mask from a bitmap and a palette index indicating
1310// the transparent area
58b16424
DW
1311bool wxMask::Create(
1312 const wxBitmap& rBitmap
1313, int nPaletteIndex
1314)
0e320a79 1315{
58b16424 1316 if (m_hMaskBitmap)
3b9e3455 1317 {
58b16424
DW
1318 ::GpiDeleteBitmap((HBITMAP) m_hMaskBitmap);
1319 m_hMaskBitmap = 0;
3b9e3455 1320 }
58b16424 1321 if (rBitmap.Ok() && rBitmap.GetPalette()->Ok())
3b9e3455 1322 {
43543d98
DW
1323 unsigned char cRed;
1324 unsigned char cGreen;
1325 unsigned char cBlue;
1326
58b16424 1327 if (rBitmap.GetPalette()->GetRGB( nPaletteIndex
43543d98
DW
1328 ,&cRed
1329 ,&cGreen
1330 ,&cBlue
58b16424 1331 ))
3b9e3455 1332 {
43543d98
DW
1333 wxColour vTransparentColour( cRed
1334 ,cGreen
1335 ,cBlue
58b16424
DW
1336 );
1337
1338 return (Create( rBitmap
1339 ,vTransparentColour
1340 ));
3b9e3455
DW
1341 }
1342 }
58b16424 1343 return(FALSE);
6f38c86f 1344} // end of wxMask::Create
0e320a79
DW
1345
1346// Create a mask from a bitmap and a colour indicating
1347// the transparent area
58b16424
DW
1348bool wxMask::Create(
1349 const wxBitmap& rBitmap
1350, const wxColour& rColour
1351)
0e320a79 1352{
6f38c86f
DW
1353 bool bOk = TRUE;
1354 COLORREF vMaskColour = OS2RGB( rColour.Red()
1355 ,rColour.Green()
1356 ,rColour.Blue()
1357 );
1358 BITMAPINFOHEADER2 vBmih;
58b16424 1359 SIZEL vSize = {0, 0};
6f38c86f
DW
1360 DEVOPENSTRUC vDop = { NULL, "DISPLAY", NULL, NULL, NULL, NULL, NULL, NULL, NULL };
1361 HDC hDCSrc = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
1362 HDC hDCDst = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
1363 HPS hPSSrc = ::GpiCreatePS(vHabmain, hDCSrc, &vSize, PU_PELS | GPIA_ASSOC);
1364 HPS hPSDst = ::GpiCreatePS(vHabmain, hDCDst, &vSize, PU_PELS | GPIA_ASSOC);
1365 POINTL vPoint[4] = { 0 ,0, rBitmap.GetWidth(), rBitmap.GetHeight(),
1366 0, 0, rBitmap.GetWidth(), rBitmap.GetHeight()
1367 };
58b16424
DW
1368
1369 if (m_hMaskBitmap)
3b9e3455 1370 {
58b16424
DW
1371 ::GpiDeleteBitmap((HBITMAP) m_hMaskBitmap);
1372 m_hMaskBitmap = 0;
3b9e3455 1373 }
58b16424 1374 if (!rBitmap.Ok())
3b9e3455 1375 {
58b16424 1376 return(FALSE);
3b9e3455
DW
1377 }
1378
6f38c86f
DW
1379 //
1380 // Scan the bitmap for the transparent colour and set
3b9e3455
DW
1381 // the corresponding pixels in the mask to BLACK and
1382 // the rest to WHITE
6f38c86f
DW
1383 //
1384
1385 memset(&vBmih, '\0', sizeof(BITMAPINFOHEADER2));
1386 vBmih.cbFix = sizeof(BITMAPINFOHEADER2);
1387 vBmih.cx = rBitmap.GetWidth();
1388 vBmih.cy = rBitmap.GetHeight();
1389 vBmih.cPlanes = 1;
1390 vBmih.cBitCount = 1;
1391
1392 m_hMaskBitmap = ::GpiCreateBitmap( hPSDst
1393 ,&vBmih
1394 ,0L
1395 ,NULL
1396 ,NULL
1397 );
1398
1399 ::GpiSetBitmap(hPSSrc, (HBITMAP) rBitmap.GetHBITMAP());
1400 ::GpiSetBitmap(hPSDst, (HBITMAP) m_hMaskBitmap);
1401
1402 //
1403 // This is not very efficient, but I can't think
3b9e3455 1404 // of a better way of doing it
6f38c86f 1405 //
58b16424 1406 for (int w = 0; w < rBitmap.GetWidth(); w++)
3b9e3455 1407 {
58b16424 1408 for (int h = 0; h < rBitmap.GetHeight(); h++)
3b9e3455 1409 {
6f38c86f
DW
1410 POINTL vPt = {w, h};
1411 COLORREF vCol = (COLORREF)::GpiQueryPel(hPSSrc, &vPt);
1412 if (vCol == (COLORREF)CLR_NOINDEX)
1413 {
1414 //
1415 // Doesn't make sense to continue
1416 //
1417 bOk = FALSE;
1418 break;
1419 }
58b16424 1420
6f38c86f 1421 if (vCol == vMaskColour)
3b9e3455 1422 {
6f38c86f
DW
1423 ::GpiSetColor(hPSDst, OS2RGB(0, 0, 0));
1424 ::GpiSetPel(hPSDst, &vPt);
3b9e3455
DW
1425 }
1426 else
1427 {
6f38c86f
DW
1428 ::GpiSetColor(hPSDst, OS2RGB(255, 255, 255));
1429 ::GpiSetPel(hPSDst, &vPt);
3b9e3455
DW
1430 }
1431 }
1432 }
6f38c86f
DW
1433 ::GpiSetBitmap(hPSSrc, NULL);
1434 ::GpiSetBitmap(hPSDst, NULL);
1435 ::GpiDestroyPS(hPSSrc);
1436 ::GpiDestroyPS(hPSDst);
1437 ::DevCloseDC(hDCSrc);
1438 ::DevCloseDC(hDCDst);
58b16424 1439 return(TRUE);
6f38c86f 1440} // end of wxMask::Create
0e320a79 1441
3b9e3455
DW
1442// ----------------------------------------------------------------------------
1443// wxBitmapHandler
1444// ----------------------------------------------------------------------------
0e320a79 1445
58b16424
DW
1446bool wxBitmapHandler::Create(
1447 wxGDIImage* pImage
1448, void* pData
1449, long lFlags
1450, int nWidth
1451, int nHeight
1452, int nDepth
1453)
3b9e3455 1454{
58b16424
DW
1455 wxBitmap* pBitmap = wxDynamicCast( pImage
1456 ,wxBitmap
1457 );
3b9e3455 1458
58b16424
DW
1459 return(pBitmap ? Create( pBitmap
1460 ,pData
1461 ,nWidth
1462 ,nHeight
1463 ,nDepth
1464 ) : FALSE);
3b9e3455
DW
1465}
1466
58b16424
DW
1467bool wxBitmapHandler::Load(
1468 wxGDIImage* pImage
1469, const wxString& rName
8ea3f821 1470, HPS hPs
58b16424
DW
1471, long lFlags
1472, int nWidth
1473, int nHeight
1474)
3b9e3455 1475{
43543d98
DW
1476 wxBitmap* pBitmap = wxDynamicCast( pImage
1477 ,wxBitmap
1478 );
3b9e3455 1479
58b16424
DW
1480 return(pBitmap ? LoadFile( pBitmap
1481 ,rName
8ea3f821 1482 ,hPs
58b16424
DW
1483 ,lFlags
1484 ,nWidth
1485 ,nHeight
1486 ) : FALSE);
3b9e3455
DW
1487}
1488
58b16424
DW
1489bool wxBitmapHandler::Save(
1490 wxGDIImage* pImage
1491, const wxString& rName
1492, int lType
1493)
0e320a79 1494{
58b16424
DW
1495 wxBitmap* pBitmap = wxDynamicCast( pImage
1496 ,wxBitmap
1497 );
0e320a79 1498
58b16424
DW
1499 return(pBitmap ? SaveFile( pBitmap
1500 ,rName
1501 ,lType
1502 ) : FALSE);
0e320a79
DW
1503}
1504
58b16424
DW
1505bool wxBitmapHandler::Create(
1506 wxBitmap* WXUNUSED(pBitmap)
1507, void* WXUNUSED(pData)
1508, long WXUNUSED(lType)
1509, int WXUNUSED(nWidth)
1510, int WXUNUSED(nHeight)
1511, int WXUNUSED(nDepth)
1512)
0e320a79 1513{
58b16424 1514 return(FALSE);
0e320a79
DW
1515}
1516
58b16424
DW
1517bool wxBitmapHandler::LoadFile(
1518 wxBitmap* WXUNUSED(pBitmap)
1519, const wxString& WXUNUSED(rName)
8ea3f821 1520, HPS WXUNUSED(hPs)
58b16424
DW
1521, long WXUNUSED(lType)
1522, int WXUNUSED(nDesiredWidth)
1523, int WXUNUSED(nDesiredHeight)
1524)
0e320a79 1525{
43543d98 1526 return(FALSE);
0e320a79
DW
1527}
1528
58b16424
DW
1529bool wxBitmapHandler::SaveFile(
1530 wxBitmap* WXUNUSED(pBitmap)
1531, const wxString& WXUNUSED(rName)
1532, int WXUNUSED(nType)
1533, const wxPalette* WXUNUSED(pPalette)
1534)
0e320a79 1535{
58b16424 1536 return(FALSE);
0e320a79 1537}
ce44c50e 1538
020a1653
DW
1539// ----------------------------------------------------------------------------
1540// Utility functions
1541// ----------------------------------------------------------------------------
1542HBITMAP wxInvertMask(
1543 HBITMAP hBmpMask
1544, int nWidth
1545, int nHeight
1546)
1547{
1548 HBITMAP hBmpInvMask = 0;
1549
1550 wxCHECK_MSG( hBmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1551
1552 //
1553 // Get width/height from the bitmap if not given
1554 //
1555 if (!nWidth || !nHeight)
1556 {
1557 BITMAPINFOHEADER2 vBmhdr;
1558
1559 ::GpiQueryBitmapInfoHeader( hBmpMask
1560 ,&vBmhdr
1561 );
1562 nWidth = (int)vBmhdr.cx;
e464c180 1563 nHeight = (int)vBmhdr.cy;
020a1653
DW
1564 }
1565
1566 BITMAPINFOHEADER2 vBmih;
1567 SIZEL vSize = {0, 0};
1568 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
e464c180
DW
1569 HDC hDCSrc = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
1570 HDC hDCDst = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDop, NULLHANDLE);
1571 HPS hPSSrc = ::GpiCreatePS(vHabmain, hDCSrc, &vSize, PU_PELS | GPIA_ASSOC);
1572 HPS hPSDst = ::GpiCreatePS(vHabmain, hDCDst, &vSize, PU_PELS | GPIA_ASSOC);
1573 POINTL vPoint[4] = { 0 ,0, nWidth, nHeight,
1574 0, 0, nWidth, nHeight
020a1653
DW
1575 };
1576
e464c180 1577 memset(&vBmih, '\0', sizeof(BITMAPINFOHEADER2));
020a1653 1578 vBmih.cbFix = sizeof(BITMAPINFOHEADER2);
e464c180
DW
1579 vBmih.cx = nWidth;
1580 vBmih.cy = nHeight;
020a1653
DW
1581 vBmih.cPlanes = 1;
1582 vBmih.cBitCount = 1;
1583
1584 hBmpInvMask = ::GpiCreateBitmap( hPSDst
1585 ,&vBmih
1586 ,0L
1587 ,NULL
1588 ,NULL
1589 );
1590
1591 ::GpiSetBitmap(hPSSrc, (HBITMAP) hBmpMask);
1592 ::GpiSetBitmap(hPSDst, (HBITMAP) hBmpInvMask);
1593
1594 ::GpiBitBlt( hPSDst
1595 ,hPSSrc
1596 ,4L
e464c180 1597 ,vPoint
8e8d8eef 1598 ,ROP_SRCINVERT
020a1653
DW
1599 ,BBO_IGNORE
1600 );
1601
1602 ::GpiDestroyPS(hPSSrc);
1603 ::GpiDestroyPS(hPSDst);
1604 ::DevCloseDC(hDCSrc);
e464c180 1605 ::DevCloseDC(hDCDst);
020a1653
DW
1606
1607 return hBmpInvMask;
1608} // end of WxWinGdi_InvertMask
3437f881 1609