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