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