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