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