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