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