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