]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bitmap.cpp
fix the wrong size of the first page when it's added to the notebook (replaces patch...
[wxWidgets.git] / src / msw / bitmap.cpp
CommitLineData
0becd470 1////////////////////////////////////////////////////////////////////////////
2bda0e17
KB
2// Name: bitmap.cpp
3// Purpose: wxBitmap
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
10fcf31a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
10fcf31a 21 #pragma implementation "bitmap.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
10fcf31a 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
10fcf31a
VZ
32 #include <stdio.h>
33
34 #include "wx/list.h"
35 #include "wx/utils.h"
36 #include "wx/app.h"
37 #include "wx/palette.h"
38 #include "wx/dcmemory.h"
39 #include "wx/bitmap.h"
40 #include "wx/icon.h"
2bda0e17
KB
41#endif
42
43#include "wx/msw/private.h"
1d792928
VZ
44#include "wx/log.h"
45
04ef50df 46#if !defined(__WXMICROWIN__)
2bda0e17 47#include "wx/msw/dib.h"
04ef50df
JS
48#endif
49
b75dd496 50#include "wx/image.h"
66e23ad2 51#include "wx/xpmdecod.h"
2bda0e17 52
2cf45d69
VZ
53#include "wx/rawbmp.h"
54
3c1a88d8
VZ
55// missing from mingw32 header
56#ifndef CLR_INVALID
57 #define CLR_INVALID ((COLORREF)-1)
58#endif // no CLR_INVALID
59
8bbbae21
VZ
60// ----------------------------------------------------------------------------
61// Bitmap data
62// ----------------------------------------------------------------------------
63
64class WXDLLEXPORT wxBitmapRefData : public wxGDIImageRefData
65{
66public:
67 wxBitmapRefData();
68 virtual ~wxBitmapRefData() { Free(); }
69
70 virtual void Free();
71
72 // set the mask object to use as the mask, we take ownership of it
73 void SetMask(wxMask *mask)
74 {
75 delete m_bitmapMask;
76 m_bitmapMask = mask;
77 }
78
79 // set the HBITMAP to use as the mask
80 void SetMask(HBITMAP hbmpMask)
81 {
82 SetMask(new wxMask((WXHBITMAP)hbmpMask));
83 }
84
85 // return the mask
86 wxMask *GetMask() const { return m_bitmapMask; }
87
88public:
8bbbae21
VZ
89#if wxUSE_PALETTE
90 wxPalette m_bitmapPalette;
91#endif // wxUSE_PALETTE
92
93 // MSW-specific
94 // ------------
95
acf8e3d2 96#ifdef __WXDEBUG__
8bbbae21
VZ
97 // this field is solely for error checking: we detect selecting a bitmap
98 // into more than one DC at once or deleting a bitmap still selected into a
99 // DC (both are serious programming errors under Windows)
100 wxDC *m_selectedInto;
acf8e3d2 101#endif // __WXDEBUG__
8bbbae21 102
fa275e86
VZ
103 // when GetRawData() is called for a DDB we need to convert it to a DIB
104 // first to be able to provide direct access to it and we cache that DIB
105 // here and convert it back to DDB when UngetRawData() is called
106 wxDIB *m_dib;
107
acf8e3d2
VZ
108 // true if we have alpha transparency info and can be drawn using
109 // AlphaBlend()
110 bool m_hasAlpha;
8bbbae21 111
fa275e86
VZ
112 // true if our HBITMAP is a DIB section, false if it is a DDB
113 bool m_isDIB;
114
8bbbae21
VZ
115private:
116 // optional mask for transparent drawing
117 wxMask *m_bitmapMask;
118
119 DECLARE_NO_COPY_CLASS(wxBitmapRefData)
120};
121
10fcf31a
VZ
122// ----------------------------------------------------------------------------
123// macros
124// ----------------------------------------------------------------------------
125
4b7f2165
VZ
126IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
127IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
6d167489 128
4b7f2165 129IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
2bda0e17 130
10fcf31a
VZ
131// ============================================================================
132// implementation
133// ============================================================================
134
2cf45d69
VZ
135// ----------------------------------------------------------------------------
136// helper functions
137// ----------------------------------------------------------------------------
138
139// decide whether we should create a DIB or a DDB for the given parameters
140static bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc)
141{
142 // here is the logic:
143 //
144 // (a) if hdc is specified, the caller explicitly wants DDB
145 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp or
146 // less DIBs anyhow)
147 // (c) finally, create DIBs under Win9x even if the depth hasn't been
148 // explicitly specified but the current display depth is 24 or more
149 // and the image is "big", i.e. > 16Mb which is the theoretical limit
150 // for DDBs under Win9x
151 //
152 // consequences (all of which seem to make sense):
153 //
154 // (i) by default, DDBs are created (depth == -1 usually)
155 // (ii) DIBs can be created by explicitly specifying the depth
156 // (iii) using a DC always forces creating a DDB
157 return !hdc &&
158 (d >= 24 ||
159 (d == -1 &&
160 wxDIB::GetLineSize(w, wxDisplayDepth())*h > 16*1024*1024));
161}
162
10fcf31a
VZ
163// ----------------------------------------------------------------------------
164// wxBitmapRefData
165// ----------------------------------------------------------------------------
166
167wxBitmapRefData::wxBitmapRefData()
2bda0e17 168{
3ca22d5e 169#ifdef __WXDEBUG__
6d167489 170 m_selectedInto = NULL;
3ca22d5e 171#endif
6d167489 172 m_bitmapMask = NULL;
fa275e86 173
340196c0 174 m_hBitmap = (WXHBITMAP) NULL;
fa275e86
VZ
175 m_dib = NULL;
176
177 m_isDIB =
acf8e3d2 178 m_hasAlpha = FALSE;
2bda0e17
KB
179}
180
6d167489 181void wxBitmapRefData::Free()
2bda0e17 182{
d59ceba5
VZ
183 wxASSERT_MSG( !m_selectedInto,
184 wxT("deleting bitmap still selected into wxMemoryDC") );
2bda0e17 185
fa275e86
VZ
186 wxASSERT_MSG( !m_dib, _T("forgot to call wxBitmap::UngetRawData()!") );
187
d59ceba5 188 if ( m_hBitmap)
6d167489
VZ
189 {
190 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
191 {
f6bcfd97 192 wxLogLastError(wxT("DeleteObject(hbitmap)"));
6d167489
VZ
193 }
194 }
e7003166 195
6d167489
VZ
196 delete m_bitmapMask;
197 m_bitmapMask = NULL;
2bda0e17
KB
198}
199
10fcf31a 200// ----------------------------------------------------------------------------
6d167489 201// wxBitmap creation
10fcf31a
VZ
202// ----------------------------------------------------------------------------
203
4fe5383d
VZ
204// this function should be called from all wxBitmap ctors
205void wxBitmap::Init()
2bda0e17 206{
4fe5383d 207 // m_refData = NULL; done in the base class ctor
4fe5383d
VZ
208}
209
8bbbae21
VZ
210wxGDIImageRefData *wxBitmap::CreateData() const
211{
212 return new wxBitmapRefData;
213}
214
6d167489
VZ
215#ifdef __WIN32__
216
217bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
218{
04ef50df 219#ifndef __WXMICROWIN__
6d167489
VZ
220 // it may be either HICON or HCURSOR
221 HICON hicon = (HICON)icon.GetHandle();
222
223 ICONINFO iconInfo;
224 if ( !::GetIconInfo(hicon, &iconInfo) )
225 {
f6bcfd97 226 wxLogLastError(wxT("GetIconInfo"));
6d167489
VZ
227
228 return FALSE;
229 }
230
231 wxBitmapRefData *refData = new wxBitmapRefData;
232 m_refData = refData;
233
d9c8e68e
VZ
234 int w = icon.GetWidth(),
235 h = icon.GetHeight();
236
237 refData->m_width = w;
238 refData->m_height = h;
6d167489
VZ
239 refData->m_depth = wxDisplayDepth();
240
241 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
d9c8e68e
VZ
242
243 // the mask returned by GetIconInfo() is inversed compared to the usual
244 // wxWin convention
8bbbae21 245 refData->SetMask(wxInvertMask(iconInfo.hbmMask, w, h));
6d167489 246
68f36a2c
VZ
247
248 // delete the old one now as we don't need it any more
249 ::DeleteObject(iconInfo.hbmMask);
250
6d167489
VZ
251#if WXWIN_COMPATIBILITY_2
252 refData->m_ok = TRUE;
253#endif // WXWIN_COMPATIBILITY_2
254
255 return TRUE;
04ef50df
JS
256#else
257 return FALSE;
258#endif
6d167489
VZ
259}
260
261#endif // Win32
262
263bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
4fe5383d
VZ
264{
265 UnRef();
07cf98cb 266
6d167489 267 if ( !cursor.Ok() )
4fe5383d 268 return FALSE;
07cf98cb 269
6d167489
VZ
270#ifdef __WIN16__
271 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
272
273 return FALSE;
8f177c8e 274#else
6d167489 275 return CopyFromIconOrCursor(cursor);
8f177c8e 276#endif // Win16
6d167489
VZ
277}
278
279bool wxBitmap::CopyFromIcon(const wxIcon& icon)
280{
281 UnRef();
07cf98cb 282
6d167489
VZ
283 if ( !icon.Ok() )
284 return FALSE;
4fe5383d
VZ
285
286 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
287 // to create a bitmap from icon there - but using this way we won't have
288 // the mask (FIXME)
289#ifdef __WIN16__
6d167489
VZ
290 int width = icon.GetWidth(),
291 height = icon.GetHeight();
292
4fe5383d 293 // copy the icon to the bitmap
6d167489 294 ScreenHDC hdcScreen;
4fe5383d
VZ
295 HDC hdc = ::CreateCompatibleDC(hdcScreen);
296 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
07cf98cb
VZ
297 HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
298
6d167489 299 ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
07cf98cb
VZ
300
301 ::SelectObject(hdc, hbmpOld);
302 ::DeleteDC(hdc);
4fe5383d 303
6d167489
VZ
304 wxBitmapRefData *refData = new wxBitmapRefData;
305 m_refData = refData;
4fe5383d 306
6d167489
VZ
307 refData->m_width = width;
308 refData->m_height = height;
309 refData->m_depth = wxDisplayDepth();
07cf98cb 310
6d167489 311 refData->m_hBitmap = (WXHBITMAP)hbitmap;
07cf98cb 312
6d167489
VZ
313#if WXWIN_COMPATIBILITY_2
314 refData->m_ok = TRUE;
315#endif // WXWIN_COMPATIBILITY_2
222594ea 316
4fe5383d 317 return TRUE;
6d167489
VZ
318#else // Win32
319 return CopyFromIconOrCursor(icon);
320#endif // Win16/Win32
10fcf31a
VZ
321}
322
b0ea5d96
VZ
323bool wxBitmap::CopyFromDIB(const wxDIB& dib)
324{
325 wxCHECK_MSG( dib.IsOk(), FALSE, _T("invalid DIB in CopyFromDIB") );
326
327 HBITMAP hbitmap = dib.CreateDDB();
328 if ( !hbitmap )
329 return FALSE;
330
331 UnRef();
332
333 wxBitmapRefData *refData = new wxBitmapRefData;
334 m_refData = refData;
335
336 refData->m_width = dib.GetWidth();
337 refData->m_height = dib.GetHeight();
338 refData->m_depth = dib.GetDepth();
339
340 refData->m_hBitmap = (WXHBITMAP)hbitmap;
341
342#if wxUSE_PALETTE
343 wxPalette *palette = dib.CreatePalette();
344 if ( palette )
345 {
346 refData->m_bitmapPalette = *palette;
347 }
348
349 delete palette;
350#endif // wxUSE_PALETTE
351
352 return TRUE;
353}
354
10fcf31a 355wxBitmap::~wxBitmap()
2bda0e17 356{
2bda0e17
KB
357}
358
5bd3a2da 359wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
2bda0e17 360{
4fe5383d 361 Init();
2bda0e17 362
04ef50df 363#ifndef __WXMICROWIN__
6d167489
VZ
364 wxBitmapRefData *refData = new wxBitmapRefData;
365 m_refData = refData;
2bda0e17 366
5bd3a2da
VZ
367 refData->m_width = width;
368 refData->m_height = height;
369 refData->m_depth = depth;
2bda0e17 370
5bd3a2da
VZ
371 char *data;
372 if ( depth == 1 )
373 {
374 // we assume that it is in XBM format which is not quite the same as
375 // the format CreateBitmap() wants because the order of bytes in the
376 // line is inversed!
4d24ece7
VZ
377 const size_t bytesPerLine = (width + 7) / 8;
378 const size_t padding = bytesPerLine % 2;
379 const size_t len = height * ( padding + bytesPerLine );
5bd3a2da
VZ
380 data = (char *)malloc(len);
381 const char *src = bits;
382 char *dst = data;
383
384 for ( int rows = 0; rows < height; rows++ )
385 {
0765adca 386 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
5bd3a2da 387 {
0765adca
VZ
388 unsigned char val = *src++;
389 unsigned char reversed = 0;
390
391 for ( int bits = 0; bits < 8; bits++)
392 {
393 reversed <<= 1;
394 reversed |= (val & 0x01);
395 val >>= 1;
396 }
397 *dst++ = reversed;
5bd3a2da
VZ
398 }
399
400 if ( padding )
401 *dst++ = 0;
5bd3a2da
VZ
402 }
403 }
404 else
405 {
406 // bits should already be in Windows standard format
407 data = (char *)bits; // const_cast is harmless
408 }
409
410 HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
6d167489
VZ
411 if ( !hbmp )
412 {
f6bcfd97 413 wxLogLastError(wxT("CreateBitmap"));
6d167489 414 }
2bda0e17 415
5bd3a2da
VZ
416 if ( data != bits )
417 {
418 free(data);
419 }
420
6d167489 421 SetHBITMAP((WXHBITMAP)hbmp);
04ef50df 422#endif
2bda0e17
KB
423}
424
2fd284a4 425// Create from XPM data
4b7f2165 426bool wxBitmap::CreateFromXpm(const char **data)
2fd284a4 427{
66e23ad2 428#if wxUSE_IMAGE && wxUSE_XPM
4fe5383d
VZ
429 Init();
430
66e23ad2 431 wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") )
4d24ece7 432
66e23ad2
VS
433 wxXPMDecoder decoder;
434 wxImage img = decoder.ReadData(data);
435 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
4d24ece7 436
c59b4b01 437 *this = wxBitmap(img);
66e23ad2
VS
438 return TRUE;
439#else
4d24ece7 440 return FALSE;
66e23ad2 441#endif
2fd284a4
JS
442}
443
debe6624 444wxBitmap::wxBitmap(int w, int h, int d)
2bda0e17 445{
4fe5383d 446 Init();
2bda0e17 447
4fe5383d 448 (void)Create(w, h, d);
2bda0e17
KB
449}
450
98ea6b7d
VZ
451wxBitmap::wxBitmap(int w, int h, const wxDC& dc)
452{
453 Init();
454
455 (void)Create(w, h, dc);
456}
457
debe6624 458wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
2bda0e17 459{
4fe5383d 460 Init();
2bda0e17 461
6d167489 462 (void)Create(data, type, width, height, depth);
2bda0e17
KB
463}
464
2aeec9ec 465wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
2bda0e17 466{
4fe5383d 467 Init();
2bda0e17 468
4fe5383d 469 LoadFile(filename, (int)type);
2bda0e17
KB
470}
471
2cf45d69
VZ
472bool wxBitmap::Create(int width, int height, int depth)
473{
474 return DoCreate(width, height, depth, 0);
475}
476
477bool wxBitmap::Create(int width, int height, const wxDC& dc)
478{
479 wxCHECK_MSG( dc.Ok(), FALSE, _T("invalid HDC in wxBitmap::Create()") );
480
481 return DoCreate(width, height, -1, dc.GetHDC());
482}
483
484bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
2bda0e17 485{
6d167489
VZ
486 UnRef();
487
488 m_refData = new wxBitmapRefData;
489
2cf45d69
VZ
490 GetBitmapData()->m_width = w;
491 GetBitmapData()->m_height = h;
492
493 HBITMAP hbmp;
494
495 if ( wxShouldCreateDIB(w, h, d, hdc) )
6d167489 496 {
2cf45d69
VZ
497 if ( d == -1 )
498 {
499 // create DIBs without alpha channel by default
500 d = 24;
501 }
502
503 wxDIB dib(w, h, d);
504 if ( !dib.IsOk() )
0becd470 505 return FALSE;
2cf45d69
VZ
506
507 // don't delete the DIB section in dib object dtor
508 hbmp = dib.Detach();
509
fa275e86 510 GetBitmapData()->m_isDIB = TRUE;
2cf45d69 511 GetBitmapData()->m_depth = d;
6d167489 512 }
2cf45d69 513 else // create a DDB
6d167489 514 {
2cf45d69
VZ
515 if ( d == -1 )
516 d = wxDisplayDepth();
517
0becd470
VZ
518 GetBitmapData()->m_depth = d;
519
0becd470
VZ
520#ifndef __WXMICROWIN__
521 if ( d > 0 )
6d167489 522 {
0becd470
VZ
523 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
524 if ( !hbmp )
525 {
526 wxLogLastError(wxT("CreateBitmap"));
527 }
6d167489 528 }
0becd470
VZ
529 else
530#endif // !__WXMICROWIN__
531 {
532 ScreenHDC dc;
533 hbmp = ::CreateCompatibleBitmap(dc, w, h);
534 if ( !hbmp )
535 {
536 wxLogLastError(wxT("CreateCompatibleBitmap"));
537 }
6d167489 538
0becd470
VZ
539 GetBitmapData()->m_depth = wxDisplayDepth();
540 }
2cf45d69 541 }
2bda0e17 542
2cf45d69 543 SetHBITMAP((WXHBITMAP)hbmp);
2bda0e17 544
6d167489 545#if WXWIN_COMPATIBILITY_2
2cf45d69 546 GetBitmapData()->m_ok = hbmp != 0;
6d167489 547#endif // WXWIN_COMPATIBILITY_2
0becd470 548
6d167489 549 return Ok();
2bda0e17
KB
550}
551
acf8e3d2 552#if wxUSE_IMAGE
0becd470 553
6d51f220 554// ----------------------------------------------------------------------------
acf8e3d2 555// wxImage to/from conversions for Microwin
6d51f220
VZ
556// ----------------------------------------------------------------------------
557
acf8e3d2
VZ
558// Microwin versions are so different from normal ones that it really doesn't
559// make sense to use #ifdefs inside the function bodies
560#ifdef __WXMICROWIN__
6d51f220 561
2cf45d69 562bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc)
fec19ea9 563{
acf8e3d2
VZ
564 // Set this to 1 to experiment with mask code,
565 // which currently doesn't work
566 #define USE_MASKS 0
62e1ba75 567
54a96d02 568 m_refData = new wxBitmapRefData();
e640f823
JS
569
570 // Initial attempt at a simple-minded implementation.
571 // The bitmap will always be created at the screen depth,
572 // so the 'depth' argument is ignored.
8e9ff815 573
e640f823
JS
574 HDC hScreenDC = ::GetDC(NULL);
575 int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
576
577 HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
62e1ba75
JS
578 HBITMAP hMaskBitmap = NULL;
579 HBITMAP hOldMaskBitmap = NULL;
580 HDC hMaskDC = NULL;
581 unsigned char maskR = 0;
582 unsigned char maskG = 0;
583 unsigned char maskB = 0;
584
54a96d02 585 // printf("Created bitmap %d\n", (int) hBitmap);
e640f823
JS
586 if (hBitmap == NULL)
587 {
588 ::ReleaseDC(NULL, hScreenDC);
8e9ff815 589 return FALSE;
e640f823
JS
590 }
591 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
e640f823
JS
592
593 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
62e1ba75
JS
594 ::ReleaseDC(NULL, hScreenDC);
595
596 // created an mono-bitmap for the possible mask
597 bool hasMask = image.HasMask();
598
599 if ( hasMask )
600 {
601#if USE_MASKS
602 // FIXME: we should be able to pass bpp = 1, but
603 // GdBlit can't handle a different depth
604#if 0
605 hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL );
606#else
607 hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight());
608#endif
609 maskR = image.GetMaskRed();
610 maskG = image.GetMaskGreen();
611 maskB = image.GetMaskBlue();
612
613 if (!hMaskBitmap)
614 {
615 hasMask = FALSE;
8e9ff815 616 }
62e1ba75
JS
617 else
618 {
619 hScreenDC = ::GetDC(NULL);
620 hMaskDC = ::CreateCompatibleDC(hScreenDC);
621 ::ReleaseDC(NULL, hScreenDC);
622
623 hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
8e9ff815 624 }
62e1ba75
JS
625#else
626 hasMask = FALSE;
627#endif
628 }
e640f823
JS
629
630 int i, j;
631 for (i = 0; i < image.GetWidth(); i++)
632 {
8e9ff815
VZ
633 for (j = 0; j < image.GetHeight(); j++)
634 {
635 unsigned char red = image.GetRed(i, j);
636 unsigned char green = image.GetGreen(i, j);
637 unsigned char blue = image.GetBlue(i, j);
e640f823 638
8e9ff815 639 ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
62e1ba75
JS
640
641 if (hasMask)
642 {
643 // scan the bitmap for the transparent colour and set the corresponding
644 // pixels in the mask to BLACK and the rest to WHITE
645 if (maskR == red && maskG == green && maskB == blue)
646 ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0));
647 else
648 ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255));
8e9ff815
VZ
649 }
650 }
e640f823
JS
651 }
652
653 ::SelectObject(hMemDC, hOldBitmap);
654 ::DeleteDC(hMemDC);
62e1ba75
JS
655 if (hasMask)
656 {
657 ::SelectObject(hMaskDC, hOldMaskBitmap);
8e9ff815 658 ::DeleteDC(hMaskDC);
62e1ba75 659
8bbbae21 660 ((wxBitmapRefData*)m_refData)->SetMask(hMaskBitmap);
62e1ba75 661 }
8e9ff815 662
e640f823
JS
663 SetWidth(image.GetWidth());
664 SetHeight(image.GetHeight());
665 SetDepth(screenDepth);
666 SetHBITMAP( (WXHBITMAP) hBitmap );
8e9ff815 667
e640f823
JS
668#if wxUSE_PALETTE
669 // Copy the palette from the source image
670 SetPalette(image.GetPalette());
671#endif // wxUSE_PALETTE
672
54a96d02
JS
673#if WXWIN_COMPATIBILITY_2
674 // check the wxBitmap object
675 GetBitmapData()->SetOk();
676#endif // WXWIN_COMPATIBILITY_2
677
e640f823 678 return TRUE;
fec19ea9
VS
679}
680
681wxImage wxBitmap::ConvertToImage() const
682{
e640f823
JS
683 // Initial attempt at a simple-minded implementation.
684 // The bitmap will always be created at the screen depth,
685 // so the 'depth' argument is ignored.
686 // TODO: transparency (create a mask image)
687
688 if (!Ok())
689 {
690 wxFAIL_MSG( wxT("bitmap is invalid") );
8e9ff815 691 return wxNullImage;
e640f823
JS
692 }
693
694 wxImage image;
695
696 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
697
698 // create an wxImage object
699 int width = GetWidth();
700 int height = GetHeight();
701 image.Create( width, height );
702 unsigned char *data = image.GetData();
703 if( !data )
704 {
705 wxFAIL_MSG( wxT("could not allocate data for image") );
706 return wxNullImage;
707 }
8e9ff815 708
e640f823
JS
709 HDC hScreenDC = ::GetDC(NULL);
710
711 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
712 ::ReleaseDC(NULL, hScreenDC);
713
714 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
8e9ff815 715
e640f823
JS
716 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
717
718 int i, j;
719 for (i = 0; i < GetWidth(); i++)
720 {
8e9ff815
VZ
721 for (j = 0; j < GetHeight(); j++)
722 {
723 COLORREF color = ::GetPixel(hMemDC, i, j);
724 unsigned char red = GetRValue(color);
725 unsigned char green = GetGValue(color);
726 unsigned char blue = GetBValue(color);
727
728 image.SetRGB(i, j, red, green, blue);
729 }
e640f823
JS
730 }
731
732 ::SelectObject(hMemDC, hOldBitmap);
733 ::DeleteDC(hMemDC);
8e9ff815 734
e640f823
JS
735#if wxUSE_PALETTE
736 // Copy the palette from the source image
737 if (GetPalette())
738 image.SetPalette(* GetPalette());
739#endif // wxUSE_PALETTE
740
741 return image;
acf8e3d2
VZ
742}
743
744#endif // __WXMICROWIN__
745
746// ----------------------------------------------------------------------------
747// wxImage to/from conversions
748// ----------------------------------------------------------------------------
749
2cf45d69
VZ
750bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
751{
752 return CreateFromImage(image, depth, 0);
753}
754
755bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc)
756{
757 wxCHECK_MSG( dc.Ok(), FALSE,
758 _T("invalid HDC in wxBitmap::CreateFromImage()") );
759
760 return CreateFromImage(image, -1, dc.GetHDC());
761}
762
763bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc )
acf8e3d2
VZ
764{
765 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
766
767 UnRef();
768
769 // first convert the image to DIB
770 const int h = image.GetHeight();
771 const int w = image.GetWidth();
772
773 wxDIB dib(image);
774 if ( !dib.IsOk() )
775 return FALSE;
776
777
778 // store the bitmap parameters
779 wxBitmapRefData *refData = new wxBitmapRefData;
780 refData->m_width = w;
781 refData->m_height = h;
acf8e3d2
VZ
782 refData->m_hasAlpha = image.HasAlpha();
783
784 m_refData = refData;
785
786
787 // next either store DIB as is or create a DDB from it
788 HBITMAP hbitmap;
789
2cf45d69
VZ
790 // are we going to use DIB?
791 if ( wxShouldCreateDIB(w, h, depth, hdc) )
792 {
793 // don't delete the DIB section in dib object dtor
794 hbitmap = dib.Detach();
795
fa275e86 796 refData->m_isDIB = TRUE;
2cf45d69
VZ
797 refData->m_depth = dib.GetDepth();
798 }
799 else // we need to convert DIB to DDB
acf8e3d2 800 {
b0ea5d96 801 hbitmap = dib.CreateDDB((HDC)hdc);
2cf45d69
VZ
802
803 refData->m_depth = depth == -1 ? wxDisplayDepth() : depth;
acf8e3d2
VZ
804 }
805
806 // validate this object
807 SetHBITMAP((WXHBITMAP)hbitmap);
808
809#if WXWIN_COMPATIBILITY_2
810 m_refData->m_ok = TRUE;
811#endif // WXWIN_COMPATIBILITY_2
812
813 // finally also set the mask if we have one
814 if ( image.HasMask() )
815 {
816 SetMask(new wxMask(*this, wxColour(image.GetMaskRed(),
817 image.GetMaskGreen(),
818 image.GetMaskBlue())));
819 }
820
821 return TRUE;
822}
823
824wxImage wxBitmap::ConvertToImage() const
825{
fec19ea9 826 wxImage image;
6d51f220 827
fec19ea9
VS
828 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
829
830 // create an wxImage object
831 int width = GetWidth();
832 int height = GetHeight();
833 image.Create( width, height );
834 unsigned char *data = image.GetData();
835 if( !data )
836 {
837 wxFAIL_MSG( wxT("could not allocate data for image") );
838 return wxNullImage;
839 }
840
841 // calc the number of bytes per scanline and padding in the DIB
842 int bytePerLine = width*3;
843 int sizeDWORD = sizeof( DWORD );
844 int lineBoundary = bytePerLine % sizeDWORD;
845 int padding = 0;
846 if( lineBoundary > 0 )
847 {
848 padding = sizeDWORD - lineBoundary;
849 bytePerLine += padding;
850 }
851
852 // create a DIB header
853 int headersize = sizeof(BITMAPINFOHEADER);
854 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
855 if( !lpDIBh )
856 {
857 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
858 free( data );
859 return wxNullImage;
860 }
861 // Fill in the DIB header
862 lpDIBh->bmiHeader.biSize = headersize;
863 lpDIBh->bmiHeader.biWidth = width;
864 lpDIBh->bmiHeader.biHeight = -height;
865 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
866 lpDIBh->bmiHeader.biPlanes = 1;
867 lpDIBh->bmiHeader.biBitCount = 24;
868 lpDIBh->bmiHeader.biCompression = BI_RGB;
869 lpDIBh->bmiHeader.biClrUsed = 0;
870 // These seem not really needed for our purpose here.
871 lpDIBh->bmiHeader.biClrImportant = 0;
872 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
873 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
874 // memory for DIB data
875 unsigned char *lpBits;
876 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
877 if( !lpBits )
878 {
879 wxFAIL_MSG( wxT("could not allocate data for DIB") );
880 free( data );
881 free( lpDIBh );
882 return wxNullImage;
883 }
884
885 // copy data from the device-dependent bitmap to the DIB
886 HDC hdc = ::GetDC(NULL);
887 HBITMAP hbitmap;
888 hbitmap = (HBITMAP) GetHBITMAP();
889 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
890
891 // copy DIB data into the wxImage object
892 int i, j;
893 unsigned char *ptdata = data;
894 unsigned char *ptbits = lpBits;
895 for( i=0; i<height; i++ )
896 {
897 for( j=0; j<width; j++ )
898 {
899 *(ptdata++) = *(ptbits+2);
900 *(ptdata++) = *(ptbits+1);
901 *(ptdata++) = *(ptbits );
902 ptbits += 3;
903 }
904 ptbits += padding;
905 }
906
907 // similarly, set data according to the possible mask bitmap
908 if( GetMask() && GetMask()->GetMaskBitmap() )
909 {
910 hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
911 // memory DC created, color set, data copied, and memory DC deleted
912 HDC memdc = ::CreateCompatibleDC( hdc );
913 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
914 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
915 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
916 ::DeleteDC( memdc );
917 // background color set to RGB(16,16,16) in consistent with wxGTK
918 unsigned char r=16, g=16, b=16;
919 ptdata = data;
920 ptbits = lpBits;
921 for( i=0; i<height; i++ )
922 {
923 for( j=0; j<width; j++ )
924 {
925 if( *ptbits != 0 )
926 ptdata += 3;
927 else
928 {
929 *(ptdata++) = r;
930 *(ptdata++) = g;
931 *(ptdata++) = b;
932 }
933 ptbits += 3;
934 }
935 ptbits += padding;
936 }
937 image.SetMaskColour( r, g, b );
938 image.SetMask( TRUE );
939 }
940 else
941 {
942 image.SetMask( FALSE );
943 }
944 // free allocated resources
945 ::ReleaseDC(NULL, hdc);
946 free(lpDIBh);
947 free(lpBits);
948
949 return image;
950}
951
6d51f220
VZ
952#endif // wxUSE_IMAGE
953
acf8e3d2
VZ
954// ----------------------------------------------------------------------------
955// loading and saving bitmaps
956// ----------------------------------------------------------------------------
957
debe6624 958bool wxBitmap::LoadFile(const wxString& filename, long type)
2bda0e17 959{
6d167489 960 UnRef();
2bda0e17 961
6d167489 962 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 963
6d167489
VZ
964 if ( handler )
965 {
966 m_refData = new wxBitmapRefData;
2bda0e17 967
6d167489
VZ
968 return handler->LoadFile(this, filename, type, -1, -1);
969 }
6d51f220 970#if wxUSE_IMAGE
6d167489 971 else
b75dd496 972 {
6d167489 973 wxImage image;
6d51f220
VZ
974 if ( image.LoadFile( filename, type ) && image.Ok() )
975 {
368d59f0 976 *this = wxBitmap(image);
6d167489 977
6d51f220
VZ
978 return TRUE;
979 }
b75dd496 980 }
6d51f220
VZ
981#endif // wxUSE_IMAGE
982
983 return FALSE;
2bda0e17
KB
984}
985
debe6624 986bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
2bda0e17 987{
6d167489 988 UnRef();
2bda0e17 989
6d167489 990 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 991
6d167489
VZ
992 if ( !handler )
993 {
9b601c24 994 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type);
2bda0e17 995
6d167489
VZ
996 return FALSE;
997 }
1d792928 998
6d167489 999 m_refData = new wxBitmapRefData;
1d792928 1000
6d167489 1001 return handler->Create(this, data, type, width, height, depth);
2bda0e17
KB
1002}
1003
d275c7eb
VZ
1004bool wxBitmap::SaveFile(const wxString& filename,
1005 int type,
1006 const wxPalette *palette)
2bda0e17 1007{
6d167489 1008 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 1009
6d167489
VZ
1010 if ( handler )
1011 {
1012 return handler->SaveFile(this, filename, type, palette);
1013 }
6d51f220 1014#if wxUSE_IMAGE
6d167489
VZ
1015 else
1016 {
1017 // FIXME what about palette? shouldn't we use it?
368d59f0 1018 wxImage image = ConvertToImage();
6d51f220
VZ
1019 if ( image.Ok() )
1020 {
1021 return image.SaveFile(filename, type);
1022 }
6d167489 1023 }
6d51f220
VZ
1024#endif // wxUSE_IMAGE
1025
1026 return FALSE;
2bda0e17
KB
1027}
1028
4b7f2165
VZ
1029// ----------------------------------------------------------------------------
1030// sub bitmap extraction
1031// ----------------------------------------------------------------------------
1032
1033wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1034{
1035 wxCHECK_MSG( Ok() &&
1036 (rect.x >= 0) && (rect.y >= 0) &&
1037 (rect.x+rect.width <= GetWidth()) &&
1038 (rect.y+rect.height <= GetHeight()),
1039 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
1040
acf8e3d2 1041 wxBitmap ret( rect.width, rect.height );
4b7f2165
VZ
1042 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1043
8e9ff815 1044#ifndef __WXMICROWIN__
acf8e3d2
VZ
1045 // TODO: copy alpha channel data if any
1046
4b7f2165 1047 // copy bitmap data
acf8e3d2
VZ
1048 MemoryHDC dcSrc,
1049 dcDst;
8e9ff815
VZ
1050
1051 {
1052 SelectInHDC selectSrc(dcSrc, GetHbitmap()),
1053 selectDst(dcDst, GetHbitmapOf(ret));
1054
acf8e3d2
VZ
1055 if ( !selectSrc || !selectDst )
1056 {
1057 wxLogLastError(_T("SelectObjct(hBitmap)"));
1058 }
1059
8e9ff815
VZ
1060 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1061 dcSrc, rect.x, rect.y, SRCCOPY) )
1062 {
1063 wxLogLastError(_T("BitBlt"));
1064 }
1065 }
4b7f2165
VZ
1066
1067 // copy mask if there is one
8e9ff815 1068 if ( GetMask() )
4b7f2165
VZ
1069 {
1070 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
1071
8e9ff815
VZ
1072 SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()),
1073 selectDst(dcDst, hbmpMask);
1074
1075 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1076 dcSrc, rect.x, rect.y, SRCCOPY) )
1077 {
1078 wxLogLastError(_T("BitBlt"));
1079 }
4b7f2165
VZ
1080
1081 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
1082 ret.SetMask(mask);
1083 }
8e9ff815 1084#endif // !__WXMICROWIN__
4b7f2165
VZ
1085
1086 return ret;
1087}
1088
6d167489
VZ
1089// ----------------------------------------------------------------------------
1090// wxBitmap accessors
1091// ----------------------------------------------------------------------------
2bda0e17 1092
8bbbae21 1093wxPalette* wxBitmap::GetPalette() const
2bda0e17 1094{
8bbbae21
VZ
1095 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1096 : (wxPalette *) NULL;
1097}
1098
1099wxMask *wxBitmap::GetMask() const
1100{
1101 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL;
1102}
2bda0e17 1103
3ca22d5e
MB
1104#ifdef __WXDEBUG__
1105
8bbbae21
VZ
1106wxDC *wxBitmap::GetSelectedInto() const
1107{
1108 return GetBitmapData() ? GetBitmapData()->m_selectedInto : (wxDC *) NULL;
2bda0e17
KB
1109}
1110
3ca22d5e
MB
1111#endif
1112
8bbbae21
VZ
1113#if WXWIN_COMPATIBILITY_2_4
1114
1115int wxBitmap::GetQuality() const
1116{
1117 return 0;
1118}
1119
1120#endif // WXWIN_COMPATIBILITY_2_4
1121
10b41b53
VZ
1122void wxBitmap::UseAlpha()
1123{
1124 if ( GetBitmapData() )
1125 GetBitmapData()->m_hasAlpha = true;
1126}
1127
acf8e3d2
VZ
1128bool wxBitmap::HasAlpha() const
1129{
1130 return GetBitmapData() && GetBitmapData()->m_hasAlpha;
1131}
1132
8bbbae21
VZ
1133// ----------------------------------------------------------------------------
1134// wxBitmap setters
1135// ----------------------------------------------------------------------------
1136
3ca22d5e
MB
1137#ifdef __WXDEBUG__
1138
8bbbae21
VZ
1139void wxBitmap::SetSelectedInto(wxDC *dc)
1140{
1141 if ( GetBitmapData() )
1142 GetBitmapData()->m_selectedInto = dc;
2bda0e17
KB
1143}
1144
3ca22d5e
MB
1145#endif
1146
d275c7eb
VZ
1147#if wxUSE_PALETTE
1148
2bda0e17
KB
1149void wxBitmap::SetPalette(const wxPalette& palette)
1150{
6d167489 1151 EnsureHasData();
2bda0e17 1152
6d167489 1153 GetBitmapData()->m_bitmapPalette = palette;
2bda0e17
KB
1154}
1155
d275c7eb
VZ
1156#endif // wxUSE_PALETTE
1157
2bda0e17
KB
1158void wxBitmap::SetMask(wxMask *mask)
1159{
6d167489 1160 EnsureHasData();
2bda0e17 1161
8bbbae21
VZ
1162 GetBitmapData()->SetMask(mask);
1163}
1164
1165#if WXWIN_COMPATIBILITY_2
1166
1167void wxBitmap::SetOk(bool isOk)
1168{
1169 EnsureHasData();
1170
1171 GetBitmapData()->m_ok = isOk;
1172}
1173
1174#endif // WXWIN_COMPATIBILITY_2
1175
1176#if WXWIN_COMPATIBILITY_2_4
1177
1178void wxBitmap::SetQuality(int WXUNUSED(quality))
1179{
2bda0e17
KB
1180}
1181
8bbbae21
VZ
1182#endif // WXWIN_COMPATIBILITY_2_4
1183
2cf45d69
VZ
1184// ----------------------------------------------------------------------------
1185// raw bitmap access support
1186// ----------------------------------------------------------------------------
1187
b9bcaf11 1188void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
2cf45d69 1189{
2cf45d69
VZ
1190 if ( !Ok() )
1191 {
1192 // no bitmap, no data (raw or otherwise)
b9bcaf11 1193 return NULL;
2cf45d69
VZ
1194 }
1195
fa275e86
VZ
1196 // if we're already a DIB we can access our data directly, but if not we
1197 // need to convert this DDB to a DIB section and use it for raw access and
1198 // then convert it back
1199 HBITMAP hDIB;
1200 if ( !GetBitmapData()->m_isDIB )
1201 {
1202 wxCHECK_MSG( !GetBitmapData()->m_dib, FALSE,
1203 _T("GetRawData() may be called only once") );
1204
1205 wxDIB *dib = new wxDIB(*this);
1206 if ( !dib->IsOk() )
1207 {
1208 delete dib;
1209
b9bcaf11 1210 return NULL;
fa275e86
VZ
1211 }
1212
1213 // we'll free it in UngetRawData()
1214 GetBitmapData()->m_dib = dib;
1215
1216 hDIB = dib->GetHandle();
1217 }
1218 else // we're a DIB
1219 {
1220 hDIB = GetHbitmap();
1221 }
1222
2cf45d69 1223 DIBSECTION ds;
fa275e86 1224 if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) )
2cf45d69 1225 {
fa275e86
VZ
1226 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1227
b9bcaf11
VZ
1228 return NULL;
1229 }
1230
1231 // check that the bitmap is in correct format
1232 if ( ds.dsBm.bmBitsPixel != bpp )
1233 {
1234 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1235
1236 return NULL;
2cf45d69
VZ
1237 }
1238
b9bcaf11 1239 // ok, store the relevant info in wxPixelDataBase
2cf45d69
VZ
1240 const LONG h = ds.dsBm.bmHeight;
1241
b9bcaf11
VZ
1242 data.m_width = ds.dsBm.bmWidth;
1243 data.m_height = h;
2cf45d69
VZ
1244
1245 // remember that DIBs are stored in top to bottom order!
1246 const LONG bytesPerRow = ds.dsBm.bmWidthBytes;
b9bcaf11
VZ
1247 data.m_stride = -bytesPerRow;
1248
1249 char *bits = (char *)ds.dsBm.bmBits;
2cf45d69
VZ
1250 if ( h > 1 )
1251 {
b9bcaf11 1252 bits += (h - 1)*bytesPerRow;
2cf45d69
VZ
1253 }
1254
b9bcaf11 1255 return bits;
2cf45d69
VZ
1256}
1257
b9bcaf11 1258void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
13c13ecb 1259{
fa275e86
VZ
1260 if ( !Ok() )
1261 return;
1262
b9bcaf11
VZ
1263 // the cast is ugly but we can't do without it and without making this
1264 // function template (and hence inline) unfortunately
1265 typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> PixelData;
1266 PixelData& data = (PixelData &)dataBase;
1267
1268 if ( !data )
7af64aac
VZ
1269 {
1270 // invalid data, don't crash -- but don't assert neither as we're
b9bcaf11 1271 // called automatically from wxPixelDataBase dtor and so there is no
7af64aac
VZ
1272 // way to prevent this from happening
1273 return;
1274 }
1275
b9bcaf11 1276 if ( GetBitmapData()->m_hasAlpha )
13c13ecb 1277 {
b9bcaf11
VZ
1278 // AlphaBlend() wants to have premultiplied source alpha but
1279 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1280 // them here now
1281 PixelData::Iterator p(data);
106c33bb 1282
b9bcaf11
VZ
1283 const int w = data.GetWidth();
1284 const int h = data.GetHeight();
1285
1286 for ( int y = 0; y < h; y++ )
13c13ecb 1287 {
b9bcaf11 1288 PixelData::Iterator rowStart = p;
106c33bb 1289
b9bcaf11
VZ
1290 for ( int x = 0; x < w; x++ )
1291 {
1292 const unsigned alpha = p.Alpha();
106c33bb 1293
b9bcaf11
VZ
1294 p.Red() = (p.Red() * alpha + 127) / 255;
1295 p.Blue() = (p.Blue() * alpha + 127) / 255;
1296 p.Green() = (p.Green() * alpha + 127) / 255;
106c33bb 1297
b9bcaf11
VZ
1298 ++p;
1299 }
fa275e86 1300
b9bcaf11
VZ
1301 p = rowStart;
1302 p.OffsetY(data, 1);
1303 }
1304
1305 // if we're a DDB we need to convert DIB back to DDB now to make the
1306 // changes made via raw bitmap access effective
1307 if ( !GetBitmapData()->m_isDIB )
1308 {
1309 wxDIB *dib = GetBitmapData()->m_dib;
1310 GetBitmapData()->m_dib = NULL;
fa275e86 1311
b9bcaf11 1312 // TODO: convert
fa275e86 1313
b9bcaf11
VZ
1314 delete dib;
1315 }
fa275e86 1316 }
13c13ecb
VZ
1317}
1318
6d167489
VZ
1319// ----------------------------------------------------------------------------
1320// wxMask
1321// ----------------------------------------------------------------------------
2bda0e17 1322
10fcf31a 1323wxMask::wxMask()
2bda0e17
KB
1324{
1325 m_maskBitmap = 0;
1326}
1327
1328// Construct a mask from a bitmap and a colour indicating
1329// the transparent area
1330wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1331{
1332 m_maskBitmap = 0;
6d167489 1333 Create(bitmap, colour);
2bda0e17
KB
1334}
1335
1336// Construct a mask from a bitmap and a palette index indicating
1337// the transparent area
debe6624 1338wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1339{
1340 m_maskBitmap = 0;
6d167489 1341 Create(bitmap, paletteIndex);
2bda0e17
KB
1342}
1343
1344// Construct a mask from a mono bitmap (copies the bitmap).
1345wxMask::wxMask(const wxBitmap& bitmap)
1346{
1347 m_maskBitmap = 0;
6d167489 1348 Create(bitmap);
2bda0e17
KB
1349}
1350
10fcf31a 1351wxMask::~wxMask()
2bda0e17
KB
1352{
1353 if ( m_maskBitmap )
1354 ::DeleteObject((HBITMAP) m_maskBitmap);
1355}
1356
1357// Create a mask from a mono bitmap (copies the bitmap).
1358bool wxMask::Create(const wxBitmap& bitmap)
1359{
04ef50df 1360#ifndef __WXMICROWIN__
a58a12e9
VZ
1361 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
1362 _T("can't create mask from invalid or not monochrome bitmap") );
1363
2bda0e17 1364 if ( m_maskBitmap )
6d167489
VZ
1365 {
1366 ::DeleteObject((HBITMAP) m_maskBitmap);
1367 m_maskBitmap = 0;
1368 }
a58a12e9 1369
6d167489
VZ
1370 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1371 bitmap.GetWidth(),
1372 bitmap.GetHeight(),
1373 1, 1, 0
1374 );
1375 HDC srcDC = CreateCompatibleDC(0);
1376 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1377 HDC destDC = CreateCompatibleDC(0);
1378 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1379 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1380 SelectObject(srcDC, 0);
1381 DeleteDC(srcDC);
1382 SelectObject(destDC, 0);
1383 DeleteDC(destDC);
1384 return TRUE;
04ef50df
JS
1385#else
1386 return FALSE;
1387#endif
2bda0e17
KB
1388}
1389
1390// Create a mask from a bitmap and a palette index indicating
1391// the transparent area
debe6624 1392bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1393{
1394 if ( m_maskBitmap )
1d792928 1395 {
6d167489
VZ
1396 ::DeleteObject((HBITMAP) m_maskBitmap);
1397 m_maskBitmap = 0;
1d792928 1398 }
d275c7eb
VZ
1399
1400#if wxUSE_PALETTE
6d167489
VZ
1401 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1402 {
1403 unsigned char red, green, blue;
1404 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1405 {
1406 wxColour transparentColour(red, green, blue);
1407 return Create(bitmap, transparentColour);
1408 }
1409 }
d275c7eb
VZ
1410#endif // wxUSE_PALETTE
1411
6d167489 1412 return FALSE;
2bda0e17
KB
1413}
1414
1415// Create a mask from a bitmap and a colour indicating
1416// the transparent area
1417bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1418{
04ef50df 1419#ifndef __WXMICROWIN__
4b7f2165
VZ
1420 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1421
2bda0e17 1422 if ( m_maskBitmap )
1d792928 1423 {
6d167489
VZ
1424 ::DeleteObject((HBITMAP) m_maskBitmap);
1425 m_maskBitmap = 0;
1426 }
4b7f2165
VZ
1427
1428 int width = bitmap.GetWidth(),
1429 height = bitmap.GetHeight();
1430
1431 // scan the bitmap for the transparent colour and set the corresponding
1432 // pixels in the mask to BLACK and the rest to WHITE
f423f4db 1433 COLORREF maskColour = wxColourToPalRGB(colour);
4b7f2165
VZ
1434 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1435
1436 HDC srcDC = ::CreateCompatibleDC(NULL);
1437 HDC destDC = ::CreateCompatibleDC(NULL);
1438 if ( !srcDC || !destDC )
1439 {
f6bcfd97 1440 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1441 }
1442
0bafad0c
VZ
1443 bool ok = TRUE;
1444
1e6feb95
VZ
1445 // SelectObject() will fail
1446 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1447 _T("bitmap can't be selected in another DC") );
1448
0bafad0c
VZ
1449 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1450 if ( !hbmpSrcOld )
6d167489 1451 {
f6bcfd97 1452 wxLogLastError(wxT("SelectObject"));
0bafad0c
VZ
1453
1454 ok = FALSE;
4b7f2165 1455 }
0bafad0c
VZ
1456
1457 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1458 if ( !hbmpDstOld )
4b7f2165 1459 {
f6bcfd97 1460 wxLogLastError(wxT("SelectObject"));
0bafad0c
VZ
1461
1462 ok = FALSE;
1d792928 1463 }
2bda0e17 1464
59ff46cb 1465 if ( ok )
2bda0e17 1466 {
59ff46cb
VZ
1467 // this will create a monochrome bitmap with 0 points for the pixels
1468 // which have the same value as the background colour and 1 for the
1469 // others
1470 ::SetBkColor(srcDC, maskColour);
1471 ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
2bda0e17 1472 }
4b7f2165 1473
0bafad0c 1474 ::SelectObject(srcDC, hbmpSrcOld);
6d167489 1475 ::DeleteDC(srcDC);
0bafad0c 1476 ::SelectObject(destDC, hbmpDstOld);
6d167489 1477 ::DeleteDC(destDC);
4b7f2165 1478
0bafad0c 1479 return ok;
59ff46cb 1480#else // __WXMICROWIN__
04ef50df 1481 return FALSE;
59ff46cb 1482#endif // __WXMICROWIN__/!__WXMICROWIN__
6d167489
VZ
1483}
1484
1485// ----------------------------------------------------------------------------
1486// wxBitmapHandler
1487// ----------------------------------------------------------------------------
1d792928 1488
6d167489
VZ
1489bool wxBitmapHandler::Create(wxGDIImage *image,
1490 void *data,
1491 long flags,
1492 int width, int height, int depth)
1493{
1494 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1d792928 1495
8c1375b9 1496 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
2bda0e17
KB
1497}
1498
6d167489
VZ
1499bool wxBitmapHandler::Load(wxGDIImage *image,
1500 const wxString& name,
1501 long flags,
1502 int width, int height)
2bda0e17 1503{
6d167489 1504 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
2bda0e17 1505
6d167489
VZ
1506 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1507}
2bda0e17 1508
6d167489
VZ
1509bool wxBitmapHandler::Save(wxGDIImage *image,
1510 const wxString& name,
1511 int type)
2bda0e17 1512{
6d167489
VZ
1513 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1514
1515 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
2bda0e17
KB
1516}
1517
6d167489
VZ
1518bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1519 void *WXUNUSED(data),
1520 long WXUNUSED(type),
1521 int WXUNUSED(width),
1522 int WXUNUSED(height),
1523 int WXUNUSED(depth))
2bda0e17 1524{
6d167489 1525 return FALSE;
2bda0e17
KB
1526}
1527
6d167489
VZ
1528bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1529 const wxString& WXUNUSED(name),
1530 long WXUNUSED(type),
1531 int WXUNUSED(desiredWidth),
1532 int WXUNUSED(desiredHeight))
2bda0e17 1533{
6d167489 1534 return FALSE;
2bda0e17
KB
1535}
1536
6d167489
VZ
1537bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1538 const wxString& WXUNUSED(name),
1539 int WXUNUSED(type),
1540 const wxPalette *WXUNUSED(palette))
2bda0e17 1541{
6d167489 1542 return FALSE;
7b46ecac
JS
1543}
1544
6d167489
VZ
1545// ----------------------------------------------------------------------------
1546// DIB functions
1547// ----------------------------------------------------------------------------
1548
04ef50df 1549#ifndef __WXMICROWIN__
6d167489
VZ
1550bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1551 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
7b46ecac
JS
1552{
1553 unsigned long i, headerSize;
1554 LPBITMAPINFO lpDIBheader = NULL;
1555 LPPALETTEENTRY lpPe = NULL;
1556
1557
1558 // Allocate space for a DIB header
1559 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1560 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1561 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1562
1563 GetPaletteEntries(hPal, 0, 256, lpPe);
1564
7b46ecac
JS
1565 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1566
7b46ecac
JS
1567 // Fill in the static parts of the DIB header
1568 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1569 lpDIBheader->bmiHeader.biWidth = xSize;
1570 lpDIBheader->bmiHeader.biHeight = ySize;
1571 lpDIBheader->bmiHeader.biPlanes = 1;
1572
1573 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1574 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1575 lpDIBheader->bmiHeader.biCompression = BI_RGB;
6d167489 1576 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
7b46ecac
JS
1577 lpDIBheader->bmiHeader.biClrUsed = 256;
1578
1579
1580 // Initialize the DIB palette
1581 for (i = 0; i < 256; i++) {
1582 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1583 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1584 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1585 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1586 }
1587
1588 *lpDIBHeader = lpDIBheader;
1589
6d167489 1590 return TRUE;
7b46ecac
JS
1591}
1592
6d167489 1593void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
7b46ecac 1594{
6d167489 1595 free(lpDIBHeader);
7b46ecac 1596}
04ef50df 1597#endif
7b46ecac 1598
4b7f2165 1599// ----------------------------------------------------------------------------
211b54b1 1600// global helper functions implemented here
4b7f2165
VZ
1601// ----------------------------------------------------------------------------
1602
211b54b1
VZ
1603// helper of wxBitmapToHICON/HCURSOR
1604static
1605HICON wxBitmapToIconOrCursor(const wxBitmap& bmp,
1606 bool iconWanted,
1607 int hotSpotX,
1608 int hotSpotY)
1609{
1610 if ( !bmp.Ok() )
1611 {
1612 // we can't create an icon/cursor form nothing
1613 return 0;
1614 }
1615
1616 wxMask *mask = bmp.GetMask();
1617 if ( !mask )
1618 {
1619 // we must have a mask for an icon, so even if it's probably incorrect,
1620 // do create it (grey is the "standard" transparent colour)
1621 mask = new wxMask(bmp, *wxLIGHT_GREY);
1622 }
1623
1624 ICONINFO iconInfo;
1625 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1626 if ( !iconWanted )
1627 {
1628 iconInfo.xHotspot = hotSpotX;
1629 iconInfo.yHotspot = hotSpotY;
1630 }
1631
1632 iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
1633 iconInfo.hbmColor = GetHbitmapOf(bmp);
1634
1635 // black out the transparent area to preserve background colour, because
1636 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1637 // the mask to the dest rect.
1638 {
1639 MemoryHDC dcSrc, dcDst;
1640 SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
1641 selectBitmap(dcDst, iconInfo.hbmColor);
1642
1643 if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
1644 dcSrc, 0, 0, SRCAND) )
1645 {
1646 wxLogLastError(_T("BitBlt"));
1647 }
1648 }
1649
1650 HICON hicon = ::CreateIconIndirect(&iconInfo);
1651
1652 if ( !bmp.GetMask() )
1653 {
1654 // we created the mask, now delete it
1655 delete mask;
1656 }
1657
1658 // delete the inverted mask bitmap we created as well
1659 ::DeleteObject(iconInfo.hbmMask);
1660
1661 return hicon;
1662}
1663
1664HICON wxBitmapToHICON(const wxBitmap& bmp)
1665{
1666 return wxBitmapToIconOrCursor(bmp, TRUE, 0, 0);
1667}
1668
1669HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY)
1670{
1671 return (HCURSOR)wxBitmapToIconOrCursor(bmp, FALSE, hotSpotX, hotSpotY);
1672}
1673
1674HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
4b7f2165 1675{
04ef50df 1676#ifndef __WXMICROWIN__
4b7f2165
VZ
1677 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1678
1679 // get width/height from the bitmap if not given
1680 if ( !w || !h )
1681 {
1682 BITMAP bm;
1683 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1684 w = bm.bmWidth;
1685 h = bm.bmHeight;
1686 }
1687
1688 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1689 HDC hdcDst = ::CreateCompatibleDC(NULL);
1690 if ( !hdcSrc || !hdcDst )
1691 {
f6bcfd97 1692 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1693 }
1694
1695 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1696 if ( !hbmpInvMask )
1697 {
f6bcfd97 1698 wxLogLastError(wxT("CreateBitmap"));
4b7f2165
VZ
1699 }
1700
1701 ::SelectObject(hdcSrc, hbmpMask);
1702 ::SelectObject(hdcDst, hbmpInvMask);
1703 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1704 hdcSrc, 0, 0,
1705 NOTSRCCOPY) )
1706 {
f6bcfd97 1707 wxLogLastError(wxT("BitBlt"));
4b7f2165 1708 }
7b46ecac 1709
4b7f2165
VZ
1710 ::DeleteDC(hdcSrc);
1711 ::DeleteDC(hdcDst);
1712
1713 return hbmpInvMask;
04ef50df
JS
1714#else
1715 return 0;
1716#endif
4b7f2165 1717}