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