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