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