]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bitmap.cpp
Swap client data pointers in wxRearrangeList too.
[wxWidgets.git] / src / gtk / bitmap.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
0b04c4e0 2// Name: src/gtk/bitmap.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
6f65e337 5// RCS-ID: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
559a723c
WS
13#include "wx/bitmap.h"
14
93763ad5 15#ifndef WX_PRECOMP
923d28da 16 #include "wx/icon.h"
155ecd4c 17 #include "wx/image.h"
5ff14574 18 #include "wx/colour.h"
93763ad5 19#endif
22bd9387 20
ce7c8a97 21#include "wx/rawbmp.h"
9e691f46 22
f3d74739
VZ
23#include "wx/gtk/private/object.h"
24
d76fe38b 25#include <gtk/gtk.h>
13111b2a 26
c2fa61e8 27extern GtkWidget *wxGetRootWindow();
c801d85f 28
06497cba
PC
29static void PixmapToPixbuf(GdkPixmap* pixmap, GdkPixbuf* pixbuf, int w, int h)
30{
31 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
32 if (gdk_drawable_get_depth(pixmap) == 1)
33 {
34 // invert to match XBM convention
35 guchar* p = gdk_pixbuf_get_pixels(pixbuf);
36 const int inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0);
37 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - w * inc;
38 for (int y = h; y; y--, p += rowpad)
39 for (int x = w; x; x--, p += inc)
40 {
41 // pixels are either (0,0,0) or (0xff,0xff,0xff)
42 p[0] = ~p[0];
43 p[1] = ~p[1];
44 p[2] = ~p[2];
45 }
46 }
47}
48
49static void MaskToAlpha(GdkPixmap* mask, GdkPixbuf* pixbuf, int w, int h)
50{
51 GdkPixbuf* mask_pixbuf = gdk_pixbuf_get_from_drawable(
52 NULL, mask, NULL, 0, 0, 0, 0, w, h);
53 guchar* p = gdk_pixbuf_get_pixels(pixbuf) + 3;
54 const guchar* mask_data = gdk_pixbuf_get_pixels(mask_pixbuf);
55 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - w * 4;
56 const int mask_rowpad = gdk_pixbuf_get_rowstride(mask_pixbuf) - w * 3;
57 for (int y = h; y; y--, p += rowpad, mask_data += mask_rowpad)
58 {
59 for (int x = w; x; x--, p += 4, mask_data += 3)
60 {
61 *p = 255;
62 // no need to test all 3 components,
63 // pixels are either (0,0,0) or (0xff,0xff,0xff)
64 if (mask_data[0] == 0)
65 *p = 0;
66 }
67 }
68 g_object_unref(mask_pixbuf);
69}
70
c801d85f
KB
71//-----------------------------------------------------------------------------
72// wxMask
73//-----------------------------------------------------------------------------
74
60a3d1c6 75IMPLEMENT_DYNAMIC_CLASS(wxMask, wxMaskBase)
c801d85f 76
8bbe427f 77wxMask::wxMask()
c801d85f 78{
d3b9f782 79 m_bitmap = NULL;
ff7b1510 80}
c801d85f 81
27297c82
VZ
82wxMask::wxMask(const wxMask& mask)
83{
84 if ( !mask.m_bitmap )
85 {
86 m_bitmap = NULL;
87 return;
88 }
89
90 // create a copy of an existing mask
91 gint w, h;
92 gdk_drawable_get_size(mask.m_bitmap, &w, &h);
93 m_bitmap = gdk_pixmap_new(mask.m_bitmap, w, h, 1);
94
95 wxGtkObject<GdkGC> gc(gdk_gc_new(m_bitmap));
96 gdk_draw_drawable(m_bitmap, gc, mask.m_bitmap, 0, 0, 0, 0, -1, -1);
97}
98
91b8de8d 99wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
c801d85f 100{
d3b9f782 101 m_bitmap = NULL;
60a3d1c6 102 InitFromColour(bitmap, colour);
ff7b1510 103}
c801d85f 104
0b04c4e0 105#if wxUSE_PALETTE
91b8de8d 106wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
c801d85f 107{
d3b9f782 108 m_bitmap = NULL;
91b8de8d 109 Create( bitmap, paletteIndex );
ff7b1510 110}
0b04c4e0 111#endif // wxUSE_PALETTE
c801d85f 112
91b8de8d 113wxMask::wxMask( const wxBitmap& bitmap )
c801d85f 114{
d3b9f782 115 m_bitmap = NULL;
60a3d1c6 116 InitFromMonoBitmap(bitmap);
ff7b1510 117}
c801d85f 118
8bbe427f 119wxMask::~wxMask()
c801d85f 120{
13111b2a 121 if (m_bitmap)
3fe39b0c 122 g_object_unref (m_bitmap);
ff7b1510 123}
c801d85f 124
60a3d1c6 125void wxMask::FreeData()
91b8de8d
RR
126{
127 if (m_bitmap)
284b4c88 128 {
3fe39b0c 129 g_object_unref (m_bitmap);
d3b9f782 130 m_bitmap = NULL;
91b8de8d 131 }
60a3d1c6 132}
13111b2a 133
60a3d1c6
PC
134bool wxMask::InitFromColour(const wxBitmap& bitmap, const wxColour& colour)
135{
13785a4b
PC
136 const int w = bitmap.GetWidth();
137 const int h = bitmap.GetHeight();
13111b2a 138
5ac1d44a 139 // create mask as XBM format bitmap
13111b2a 140
5ac1d44a
PC
141 // one bit per pixel, each row starts on a byte boundary
142 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
143 wxByte* out = new wxByte[out_size];
d0e79755 144 // set bits are unmasked
5ac1d44a
PC
145 memset(out, 0xff, out_size);
146 unsigned bit_index = 0;
13785a4b 147 if (bitmap.HasPixbuf())
1fb4de31 148 {
5ac1d44a
PC
149 const wxByte r_mask = colour.Red();
150 const wxByte g_mask = colour.Green();
151 const wxByte b_mask = colour.Blue();
13785a4b 152 GdkPixbuf* pixbuf = bitmap.GetPixbuf();
5ac1d44a
PC
153 const wxByte* in = gdk_pixbuf_get_pixels(pixbuf);
154 const int inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0);
155 const int rowpadding = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
156 for (int y = 0; y < h; y++, in += rowpadding)
157 {
158 for (int x = 0; x < w; x++, in += inc, bit_index++)
159 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
160 out[bit_index >> 3] ^= 1 << (bit_index & 7);
161 // move index to next byte boundary
162 bit_index = (bit_index + 7) & ~7u;
163 }
2eefae6e 164 }
13785a4b 165 else
1fb4de31 166 {
5ac1d44a 167 GdkImage* image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h);
13785a4b 168 GdkColormap* colormap = gdk_image_get_colormap(image);
a7cfe08a
PC
169 guint32 mask_pixel;
170 if (colormap == NULL)
171 // mono bitmap, white is pixel value 0
172 mask_pixel = guint32(colour.Red() != 255 || colour.Green() != 255 || colour.Blue() != 255);
173 else
13785a4b
PC
174 {
175 wxColor c(colour);
176 c.CalcPixel(colormap);
177 mask_pixel = c.GetPixel();
178 }
5ac1d44a 179 for (int y = 0; y < h; y++)
1fb4de31 180 {
5ac1d44a
PC
181 for (int x = 0; x < w; x++, bit_index++)
182 if (gdk_image_get_pixel(image, x, y) == mask_pixel)
183 out[bit_index >> 3] ^= 1 << (bit_index & 7);
184 bit_index = (bit_index + 7) & ~7u;
f9ee644e 185 }
13785a4b 186 g_object_unref(image);
5ac1d44a
PC
187 }
188 m_bitmap = gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h);
189 delete[] out;
902725ee 190 return true;
91b8de8d
RR
191}
192
60a3d1c6 193bool wxMask::InitFromMonoBitmap(const wxBitmap& bitmap)
91b8de8d 194{
3d37a968 195 if (!bitmap.IsOk()) return false;
284b4c88 196
b85229d1 197 wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
284b4c88 198
c2fa61e8 199 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
284b4c88 200
902725ee 201 if (!m_bitmap) return false;
284b4c88 202
f3d74739 203 wxGtkObject<GdkGC> gc(gdk_gc_new( m_bitmap ));
9be3f755
PC
204 gdk_gc_set_function(gc, GDK_COPY_INVERT);
205 gdk_draw_drawable(m_bitmap, gc, bitmap.GetPixmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight());
284b4c88 206
902725ee 207 return true;
91b8de8d
RR
208}
209
210GdkBitmap *wxMask::GetBitmap() const
c801d85f 211{
fd0eed64 212 return m_bitmap;
ff7b1510 213}
8bbe427f 214
c801d85f 215//-----------------------------------------------------------------------------
732d8c74 216// wxBitmapRefData
c801d85f
KB
217//-----------------------------------------------------------------------------
218
8f884a0d 219class wxBitmapRefData: public wxGDIRefData
c801d85f 220{
fd0eed64 221public:
06497cba 222 wxBitmapRefData(int width, int height, int depth);
d3c7fc99 223 virtual ~wxBitmapRefData();
f2593d0d 224
06497cba 225 virtual bool IsOk() const;
8f884a0d 226
f2593d0d 227 GdkPixmap *m_pixmap;
feac7937 228 GdkPixbuf *m_pixbuf;
f2593d0d
RR
229 wxMask *m_mask;
230 int m_width;
231 int m_height;
232 int m_bpp;
06497cba 233 bool m_alphaRequested;
c801d85f
KB
234};
235
06497cba 236wxBitmapRefData::wxBitmapRefData(int width, int height, int depth)
c801d85f 237{
d3b9f782
VZ
238 m_pixmap = NULL;
239 m_pixbuf = NULL;
240 m_mask = NULL;
06497cba
PC
241 m_width = width;
242 m_height = height;
243 m_bpp = depth;
244 if (m_bpp < 0)
245 m_bpp = gdk_drawable_get_depth(wxGetRootWindow()->window);
246 m_alphaRequested = depth == 32;
ff7b1510 247}
c801d85f 248
8bbe427f 249wxBitmapRefData::~wxBitmapRefData()
c801d85f 250{
3ebcd89d 251 if (m_pixmap)
3fe39b0c 252 g_object_unref (m_pixmap);
feac7937 253 if (m_pixbuf)
3fe39b0c 254 g_object_unref (m_pixbuf);
3ebcd89d 255 delete m_mask;
ff7b1510 256}
c801d85f 257
06497cba
PC
258bool wxBitmapRefData::IsOk() const
259{
260 return m_bpp != 0;
261}
732d8c74
FM
262
263//-----------------------------------------------------------------------------
264// wxBitmap
c801d85f
KB
265//-----------------------------------------------------------------------------
266
5c33522f 267#define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
c801d85f
KB
268
269IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
270
23656673
PC
271wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
272{
273 LoadFile(filename, type);
274}
275
276wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
c801d85f 277{
23656673
PC
278 wxASSERT(depth == 1);
279 if (width > 0 && height > 0 && depth == 1)
280 {
281 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, bits, width, height));
23656673 282 }
ff7b1510 283}
8bbe427f 284
452418c4
PC
285wxBitmap::wxBitmap(const char* const* bits)
286{
287 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
288
289 GdkBitmap* mask = NULL;
5c33522f 290 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, const_cast<char**>(bits)));
3d37a968
FM
291 if (!M_BMPDATA)
292 return;
452418c4
PC
293
294 if (M_BMPDATA->m_pixmap != NULL && mask != NULL)
295 {
296 M_BMPDATA->m_mask = new wxMask;
297 M_BMPDATA->m_mask->m_bitmap = mask;
298 }
299}
300
23656673 301wxBitmap::~wxBitmap()
c801d85f 302{
c826213d
RR
303}
304
305bool wxBitmap::Create( int width, int height, int depth )
306{
307 UnRef();
06497cba
PC
308 wxCHECK_MSG(width >= 0 && height >= 0, false, "invalid bitmap size");
309 m_refData = new wxBitmapRefData(width, height, depth);
310 return true;
ff7b1510 311}
b5f01ae0 312
c0521644
VZ
313#if wxUSE_IMAGE
314
feac7937 315bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
b5f01ae0 316{
a11672a4
RR
317 UnRef();
318
3d37a968 319 wxCHECK_MSG( image.IsOk(), false, wxT("invalid image") );
637b7e4f 320 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
b5f01ae0 321
feac7937
VS
322 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
323 return false;
902725ee 324
70029506
PC
325 // create pixbuf if image has alpha and requested depth is compatible
326 if (image.HasAlpha() && (depth == -1 || depth == 32))
5588ce92
PC
327 return CreateFromImageAsPixbuf(image);
328
70029506 329 // otherwise create pixmap, if alpha is present it will be converted to mask
5ac1d44a 330 return CreateFromImageAsPixmap(image, depth);
feac7937 331}
3ebcd89d 332
5ac1d44a 333bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth)
feac7937 334{
5ac1d44a
PC
335 const int w = image.GetWidth();
336 const int h = image.GetHeight();
337 if (depth == 1)
feac7937 338 {
5ac1d44a
PC
339 // create XBM format bitmap
340
341 // one bit per pixel, each row starts on a byte boundary
342 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
343 wxByte* out = new wxByte[out_size];
d0e79755 344 // set bits are black
5ac1d44a
PC
345 memset(out, 0xff, out_size);
346 const wxByte* in = image.GetData();
347 unsigned bit_index = 0;
348 for (int y = 0; y < h; y++)
349 {
350 for (int x = 0; x < w; x++, in += 3, bit_index++)
351 if (in[0] == 255 && in[1] == 255 && in[2] == 255)
352 out[bit_index >> 3] ^= 1 << (bit_index & 7);
353 // move index to next byte boundary
354 bit_index = (bit_index + 7) & ~7u;
355 }
356 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h));
357 delete[] out;
3d37a968
FM
358
359 if (!M_BMPDATA) // SetPixmap may have failed
360 return false;
5ac1d44a
PC
361 }
362 else
363 {
364 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth));
3d37a968
FM
365 if (!M_BMPDATA)
366 return false;
367
f3d74739 368 wxGtkObject<GdkGC> gc(gdk_gc_new(M_BMPDATA->m_pixmap));
5ac1d44a
PC
369 gdk_draw_rgb_image(
370 M_BMPDATA->m_pixmap, gc,
371 0, 0, w, h,
372 GDK_RGB_DITHER_NONE, image.GetData(), w * 3);
feac7937 373 }
b5f01ae0 374
5ac1d44a
PC
375 const wxByte* alpha = image.GetAlpha();
376 if (alpha != NULL || image.HasMask())
feac7937 377 {
5ac1d44a
PC
378 // create mask as XBM format bitmap
379
380 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
381 wxByte* out = new wxByte[out_size];
382 memset(out, 0xff, out_size);
383 unsigned bit_index = 0;
384 if (alpha != NULL)
b5f01ae0 385 {
5ac1d44a 386 for (int y = 0; y < h; y++)
b5f01ae0 387 {
5ac1d44a
PC
388 for (int x = 0; x < w; x++, bit_index++)
389 if (*alpha++ < wxIMAGE_ALPHA_THRESHOLD)
390 out[bit_index >> 3] ^= 1 << (bit_index & 7);
391 bit_index = (bit_index + 7) & ~7u;
feac7937 392 }
5ac1d44a
PC
393 }
394 else
b5f01ae0 395 {
5ac1d44a
PC
396 const wxByte r_mask = image.GetMaskRed();
397 const wxByte g_mask = image.GetMaskGreen();
398 const wxByte b_mask = image.GetMaskBlue();
399 const wxByte* in = image.GetData();
400 for (int y = 0; y < h; y++)
401 {
402 for (int x = 0; x < w; x++, in += 3, bit_index++)
403 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
404 out[bit_index >> 3] ^= 1 << (bit_index & 7);
405 bit_index = (bit_index + 7) & ~7u;
406 }
407 }
408 wxMask* mask = new wxMask;
409 mask->m_bitmap = gdk_bitmap_create_from_data(M_BMPDATA->m_pixmap, (char*)out, w, h);
410 SetMask(mask);
411 delete[] out;
412 }
3d37a968 413 return IsOk();
b5f01ae0
VS
414}
415
feac7937 416bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
b5f01ae0 417{
70029506
PC
418 wxASSERT(image.HasAlpha());
419
feac7937
VS
420 int width = image.GetWidth();
421 int height = image.GetHeight();
2eefae6e 422
23656673 423 Create(width, height, 32);
06497cba 424 GdkPixbuf* pixbuf = GetPixbuf();
feac7937
VS
425 if (!pixbuf)
426 return false;
c2fa61e8 427
feac7937 428 // Copy the data:
23656673 429 const unsigned char* in = image.GetData();
feac7937
VS
430 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
431 unsigned char *alpha = image.GetAlpha();
902725ee 432
23656673 433 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
b5f01ae0 434
23656673 435 for (int y = 0; y < height; y++, out += rowpad)
b5f01ae0 436 {
feac7937
VS
437 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
438 {
439 out[0] = in[0];
440 out[1] = in[1];
441 out[2] = in[2];
442 out[3] = *alpha;
443 }
b5f01ae0 444 }
902725ee 445
feac7937
VS
446 return true;
447}
b5f01ae0 448
feac7937
VS
449wxImage wxBitmap::ConvertToImage() const
450{
3d37a968 451 wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") );
902725ee 452
afbfbfdf
PC
453 const int w = GetWidth();
454 const int h = GetHeight();
23656673 455 wxImage image(w, h, false);
feac7937 456 unsigned char *data = image.GetData();
2eefae6e 457
5588ce92 458 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
b5f01ae0 459
70029506 460 // prefer pixbuf if available, it will preserve alpha and should be quicker
feac7937 461 if (HasPixbuf())
b5f01ae0 462 {
feac7937 463 GdkPixbuf *pixbuf = GetPixbuf();
70029506
PC
464 unsigned char* alpha = NULL;
465 if (gdk_pixbuf_get_has_alpha(pixbuf))
466 {
467 image.SetAlpha();
468 alpha = image.GetAlpha();
469 }
23656673 470 const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf);
feac7937 471 unsigned char *out = data;
70029506 472 const int inc = 3 + int(alpha != NULL);
23656673 473 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
b5f01ae0 474
23656673 475 for (int y = 0; y < h; y++, in += rowpad)
feac7937 476 {
70029506 477 for (int x = 0; x < w; x++, in += inc, out += 3)
feac7937
VS
478 {
479 out[0] = in[0];
480 out[1] = in[1];
481 out[2] = in[2];
70029506
PC
482 if (alpha != NULL)
483 *alpha++ = in[3];
feac7937
VS
484 }
485 }
b5f01ae0 486 }
feac7937 487 else
b5f01ae0 488 {
afbfbfdf
PC
489 GdkPixmap* pixmap = GetPixmap();
490 GdkPixmap* pixmap_invert = NULL;
491 if (GetDepth() == 1)
b5f01ae0 492 {
d0e79755 493 // mono bitmaps are inverted, i.e. 0 is white
afbfbfdf 494 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
f3d74739 495 wxGtkObject<GdkGC> gc(gdk_gc_new(pixmap_invert));
afbfbfdf
PC
496 gdk_gc_set_function(gc, GDK_COPY_INVERT);
497 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
afbfbfdf 498 pixmap = pixmap_invert;
feac7937 499 }
afbfbfdf
PC
500 // create a pixbuf which shares data with the wxImage
501 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(
502 data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL);
feac7937 503
afbfbfdf 504 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
feac7937 505
afbfbfdf
PC
506 g_object_unref(pixbuf);
507 if (pixmap_invert != NULL)
508 g_object_unref(pixmap_invert);
70029506
PC
509 }
510 // convert mask, unless there is already alpha
511 if (GetMask() && !image.HasAlpha())
512 {
23656673
PC
513 // we hard code the mask colour for now but we could also make an
514 // effort (and waste time) to choose a colour not present in the
515 // image already to avoid having to fudge the pixels below --
516 // whether it's worth to do it is unclear however
70029506
PC
517 const int MASK_RED = 1;
518 const int MASK_GREEN = 2;
519 const int MASK_BLUE = 3;
520 const int MASK_BLUE_REPLACEMENT = 2;
feac7937 521
70029506
PC
522 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
523 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
feac7937 524
70029506
PC
525 for (int y = 0; y < h; y++)
526 {
527 for (int x = 0; x < w; x++, data += 3)
8ab696e0 528 {
70029506 529 if (gdk_image_get_pixel(image_mask, x, y) == 0)
8ab696e0 530 {
70029506
PC
531 data[0] = MASK_RED;
532 data[1] = MASK_GREEN;
533 data[2] = MASK_BLUE;
534 }
535 else if (data[0] == MASK_RED && data[1] == MASK_GREEN && data[2] == MASK_BLUE)
536 {
23656673
PC
537 // we have to fudge the colour a bit to prevent
538 // this pixel from appearing transparent
70029506 539 data[2] = MASK_BLUE_REPLACEMENT;
feac7937 540 }
feac7937 541 }
b5f01ae0 542 }
70029506 543 g_object_unref(image_mask);
feac7937 544 }
b5f01ae0
VS
545
546 return image;
547}
548
c0521644
VZ
549#endif // wxUSE_IMAGE
550
91b8de8d 551int wxBitmap::GetHeight() const
c801d85f 552{
3d37a968 553 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
e55ad60e 554
fd0eed64 555 return M_BMPDATA->m_height;
ff7b1510 556}
c801d85f 557
91b8de8d 558int wxBitmap::GetWidth() const
c801d85f 559{
3d37a968 560 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
8bbe427f 561
fd0eed64 562 return M_BMPDATA->m_width;
ff7b1510 563}
c801d85f 564
91b8de8d 565int wxBitmap::GetDepth() const
c801d85f 566{
3d37a968 567 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
8bbe427f 568
fd0eed64 569 return M_BMPDATA->m_bpp;
ff7b1510 570}
c801d85f 571
91b8de8d 572wxMask *wxBitmap::GetMask() const
c801d85f 573{
d3b9f782 574 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
8bbe427f 575
fd0eed64 576 return M_BMPDATA->m_mask;
ff7b1510 577}
c801d85f
KB
578
579void wxBitmap::SetMask( wxMask *mask )
580{
3d37a968 581 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
8bbe427f 582
e3e89a93 583 AllocExclusive();
23656673 584 delete M_BMPDATA->m_mask;
fd0eed64 585 M_BMPDATA->m_mask = mask;
ff7b1510 586}
c801d85f 587
db0aec83
VS
588bool wxBitmap::CopyFromIcon(const wxIcon& icon)
589{
590 *this = icon;
3d37a968 591 return IsOk();
db0aec83
VS
592}
593
17bec151
RR
594wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
595{
5588ce92
PC
596 wxBitmap ret;
597
3d37a968 598 wxCHECK_MSG(IsOk(), ret, wxT("invalid bitmap"));
06497cba
PC
599
600 const int w = rect.width;
601 const int h = rect.height;
602 const wxBitmapRefData* bmpData = M_BMPDATA;
603
23656673 604 wxCHECK_MSG(rect.x >= 0 && rect.y >= 0 &&
06497cba
PC
605 rect.x + w <= bmpData->m_width &&
606 rect.y + h <= bmpData->m_height,
23656673 607 ret, wxT("invalid bitmap region"));
13111b2a 608
06497cba
PC
609 wxBitmapRefData* newRef = new wxBitmapRefData(*bmpData);
610 ret.m_refData = newRef;
611 newRef->m_width = w;
612 newRef->m_height = h;
613
614 if (bmpData->m_pixbuf)
17bec151 615 {
06497cba
PC
616 GdkPixbuf* pixbuf =
617 gdk_pixbuf_new_subpixbuf(bmpData->m_pixbuf, rect.x, rect.y, w, h);
618 newRef->m_pixbuf = gdk_pixbuf_copy(pixbuf);
619 g_object_unref(pixbuf);
17bec151 620 }
06497cba 621 if (bmpData->m_pixmap)
17bec151 622 {
06497cba
PC
623 newRef->m_pixmap = gdk_pixmap_new(bmpData->m_pixmap, w, h, -1);
624 GdkGC* gc = gdk_gc_new(newRef->m_pixmap);
625 gdk_draw_drawable(
626 newRef->m_pixmap, gc, bmpData->m_pixmap, rect.x, rect.y, 0, 0, w, h);
627 g_object_unref(gc);
17bec151 628 }
06497cba
PC
629 newRef->m_mask = NULL;
630 if (bmpData->m_mask && bmpData->m_mask->m_bitmap)
17bec151 631 {
06497cba
PC
632 GdkPixmap* sub_mask = gdk_pixmap_new(bmpData->m_mask->m_bitmap, w, h, 1);
633 newRef->m_mask = new wxMask;
634 newRef->m_mask->m_bitmap = sub_mask;
635 GdkGC* gc = gdk_gc_new(sub_mask);
636 gdk_draw_drawable(
637 sub_mask, gc, bmpData->m_mask->m_bitmap, rect.x, rect.y, 0, 0, w, h);
638 g_object_unref(gc);
17bec151 639 }
13111b2a 640
17bec151
RR
641 return ret;
642}
643
4611dd06 644bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
c801d85f 645{
3d37a968 646 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
8bbe427f 647
c0521644 648#if wxUSE_IMAGE
5588ce92 649 wxImage image = ConvertToImage();
8362e67c
PC
650 if (image.IsOk() && image.SaveFile(name, type))
651 return true;
652#endif
653 const char* type_name = NULL;
654 switch (type)
655 {
656 case wxBITMAP_TYPE_BMP: type_name = "bmp"; break;
657 case wxBITMAP_TYPE_ICO: type_name = "ico"; break;
658 case wxBITMAP_TYPE_JPEG: type_name = "jpeg"; break;
659 case wxBITMAP_TYPE_PNG: type_name = "png"; break;
660 default: break;
661 }
662 return type_name &&
663 gdk_pixbuf_save(GetPixbuf(), name.fn_str(), type_name, NULL, NULL);
ff7b1510 664}
c801d85f 665
4611dd06 666bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
c801d85f 667{
c0521644 668#if wxUSE_IMAGE
8362e67c
PC
669 wxImage image;
670 if (image.LoadFile(name, type) && image.IsOk())
671 *this = wxBitmap(image);
672 else
673#endif
fd0eed64 674 {
8362e67c
PC
675 UnRef();
676 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(name.fn_str(), NULL);
677 if (pixbuf)
678 SetPixbuf(pixbuf);
fd0eed64 679 }
8bbe427f 680
3d37a968 681 return IsOk();
ff7b1510 682}
8bbe427f 683
0b04c4e0 684#if wxUSE_PALETTE
91b8de8d 685wxPalette *wxBitmap::GetPalette() const
c801d85f 686{
b2bf3ac6 687 return NULL;
ff7b1510 688}
c801d85f 689
4611dd06
RR
690void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
691{
692 // TODO
693}
0b04c4e0 694#endif // wxUSE_PALETTE
4611dd06 695
4bc67cc5
RR
696void wxBitmap::SetHeight( int height )
697{
e3e89a93 698 AllocExclusive();
4bc67cc5
RR
699 M_BMPDATA->m_height = height;
700}
701
702void wxBitmap::SetWidth( int width )
703{
e3e89a93 704 AllocExclusive();
4bc67cc5
RR
705 M_BMPDATA->m_width = width;
706}
707
708void wxBitmap::SetDepth( int depth )
709{
e3e89a93 710 AllocExclusive();
4bc67cc5
RR
711 M_BMPDATA->m_bpp = depth;
712}
713
714void wxBitmap::SetPixmap( GdkPixmap *pixmap )
715{
06497cba
PC
716 UnRef();
717
3d37a968
FM
718 if (!pixmap)
719 return;
720
06497cba
PC
721 int w, h;
722 gdk_drawable_get_size(pixmap, &w, &h);
723 wxBitmapRefData* bmpData = new wxBitmapRefData(w, h, 0);
724 m_refData = bmpData;
725 bmpData->m_pixmap = pixmap;
726 bmpData->m_bpp = gdk_drawable_get_depth(pixmap);
4bc67cc5
RR
727}
728
91b8de8d 729GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 730{
d3b9f782 731 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
8bbe427f 732
06497cba
PC
733 wxBitmapRefData* bmpData = M_BMPDATA;
734 if (bmpData->m_pixmap)
735 return bmpData->m_pixmap;
736
737 if (bmpData->m_pixbuf)
feac7937 738 {
06497cba
PC
739 GdkPixmap** mask_pixmap = NULL;
740 if (gdk_pixbuf_get_has_alpha(bmpData->m_pixbuf))
70029506
PC
741 {
742 // make new mask from alpha
06497cba
PC
743 delete bmpData->m_mask;
744 bmpData->m_mask = new wxMask;
745 mask_pixmap = &bmpData->m_mask->m_bitmap;
70029506 746 }
06497cba
PC
747 gdk_pixbuf_render_pixmap_and_mask(
748 bmpData->m_pixbuf, &bmpData->m_pixmap, mask_pixmap, 128);
feac7937 749 }
06497cba
PC
750 else
751 {
752 bmpData->m_pixmap = gdk_pixmap_new(wxGetRootWindow()->window,
753 bmpData->m_width, bmpData->m_height, bmpData->m_bpp == 1 ? 1 : -1);
754 }
755 return bmpData->m_pixmap;
ff7b1510 756}
8bbe427f 757
feac7937
VS
758bool wxBitmap::HasPixmap() const
759{
3d37a968 760 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
feac7937
VS
761
762 return M_BMPDATA->m_pixmap != NULL;
763}
764
feac7937
VS
765GdkPixbuf *wxBitmap::GetPixbuf() const
766{
3d37a968 767 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
feac7937 768
06497cba
PC
769 wxBitmapRefData* bmpData = M_BMPDATA;
770 if (bmpData->m_pixbuf)
771 return bmpData->m_pixbuf;
6db34764 772
06497cba
PC
773 const int w = bmpData->m_width;
774 const int h = bmpData->m_height;
775 GdkPixmap* mask = NULL;
776 if (bmpData->m_mask)
777 mask = bmpData->m_mask->m_bitmap;
778 const bool useAlpha = bmpData->m_alphaRequested || mask;
779 bmpData->m_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, useAlpha, 8, w, h);
780 if (bmpData->m_pixmap)
781 PixmapToPixbuf(bmpData->m_pixmap, bmpData->m_pixbuf, w, h);
782 if (mask)
783 MaskToAlpha(mask, bmpData->m_pixbuf, w, h);
784 return bmpData->m_pixbuf;
feac7937
VS
785}
786
787bool wxBitmap::HasPixbuf() const
788{
3d37a968 789 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
feac7937
VS
790
791 return M_BMPDATA->m_pixbuf != NULL;
792}
793
06497cba 794void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf)
feac7937 795{
06497cba
PC
796 UnRef();
797
3d37a968
FM
798 if (!pixbuf)
799 return;
800
06497cba
PC
801 int depth = -1;
802 if (gdk_pixbuf_get_has_alpha(pixbuf))
803 depth = 32;
804 m_refData = new wxBitmapRefData(
805 gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), depth);
feac7937
VS
806
807 M_BMPDATA->m_pixbuf = pixbuf;
808}
809
810void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
811{
812 if (keep == Pixmap && HasPixbuf())
813 {
3fe39b0c 814 g_object_unref (M_BMPDATA->m_pixbuf);
feac7937
VS
815 M_BMPDATA->m_pixbuf = NULL;
816 }
817 if (keep == Pixbuf && HasPixmap())
818 {
3fe39b0c 819 g_object_unref (M_BMPDATA->m_pixmap);
feac7937
VS
820 M_BMPDATA->m_pixmap = NULL;
821 }
822}
823
ce7c8a97 824#ifdef wxHAS_RAW_BITMAP
284f2b59
RR
825void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
826{
70029506 827 void* bits = NULL;
284f2b59 828 GdkPixbuf *pixbuf = GetPixbuf();
70029506 829 const bool hasAlpha = HasAlpha();
3d37a968 830
70029506 831 // allow access if bpp is valid and matches existence of alpha
c74fc1e1 832 if ( pixbuf && ((bpp == 24 && !hasAlpha) || (bpp == 32 && hasAlpha)) )
70029506
PC
833 {
834 data.m_height = gdk_pixbuf_get_height( pixbuf );
835 data.m_width = gdk_pixbuf_get_width( pixbuf );
836 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
837 bits = gdk_pixbuf_get_pixels(pixbuf);
838 }
839 return bits;
284f2b59
RR
840}
841
17a1ebd1 842void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
284f2b59
RR
843{
844}
ce7c8a97 845#endif // wxHAS_RAW_BITMAP
284f2b59 846
902725ee 847bool wxBitmap::HasAlpha() const
0ff2a74d 848{
06497cba
PC
849 const wxBitmapRefData* bmpData = M_BMPDATA;
850 return bmpData && (bmpData->m_alphaRequested ||
851 (bmpData->m_pixbuf && gdk_pixbuf_get_has_alpha(bmpData->m_pixbuf)));
0ff2a74d
RR
852}
853
8f884a0d 854wxGDIRefData* wxBitmap::CreateGDIRefData() const
e3e89a93 855{
06497cba 856 return new wxBitmapRefData(0, 0, 0);
e3e89a93
PC
857}
858
8f884a0d 859wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const
e3e89a93 860{
5c33522f 861 const wxBitmapRefData* oldRef = static_cast<const wxBitmapRefData*>(data);
06497cba 862 wxBitmapRefData* newRef = new wxBitmapRefData(*oldRef);
e3e89a93
PC
863 if (oldRef->m_pixmap != NULL)
864 {
865 newRef->m_pixmap = gdk_pixmap_new(
866 oldRef->m_pixmap, oldRef->m_width, oldRef->m_height,
867 // use pixmap depth, m_bpp may not match
868 gdk_drawable_get_depth(oldRef->m_pixmap));
f3d74739 869 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_pixmap));
e3e89a93
PC
870 gdk_draw_drawable(
871 newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1);
e3e89a93
PC
872 }
873 if (oldRef->m_pixbuf != NULL)
874 {
875 newRef->m_pixbuf = gdk_pixbuf_copy(oldRef->m_pixbuf);
876 }
877 if (oldRef->m_mask != NULL)
878 {
27297c82 879 newRef->m_mask = new wxMask(*oldRef->m_mask);
e3e89a93 880 }
e3e89a93
PC
881
882 return newRef;
883}
884
4b61c88d
RR
885/* static */ void wxBitmap::InitStandardHandlers()
886{
887 // TODO: Insert handler based on GdkPixbufs handler later
888}