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