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