make pixbuf/alpha handling more coherent in gtk wxBitmap, allow pixbuf without alpha
[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 wxPalette *m_palette;
197 };
198
199 wxBitmapRefData::wxBitmapRefData()
200 {
201 m_pixmap = (GdkPixmap *) NULL;
202 m_pixbuf = (GdkPixbuf *) NULL;
203 m_mask = (wxMask *) NULL;
204 m_width = 0;
205 m_height = 0;
206 m_bpp = 0;
207 m_palette = (wxPalette *) NULL;
208 }
209
210 wxBitmapRefData::~wxBitmapRefData()
211 {
212 if (m_pixmap)
213 g_object_unref (m_pixmap);
214 if (m_pixbuf)
215 g_object_unref (m_pixbuf);
216 delete m_mask;
217 #if wxUSE_PALETTE
218 delete m_palette;
219 #endif // wxUSE_PALETTE
220 }
221
222 //-----------------------------------------------------------------------------
223
224 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
225
226 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
227
228 wxBitmap::wxBitmap()
229 {
230 }
231
232 wxBitmap::wxBitmap( int width, int height, int depth )
233 {
234 Create( width, height, depth );
235 }
236
237 bool wxBitmap::Create( int width, int height, int depth )
238 {
239 UnRef();
240
241 if ( width <= 0 || height <= 0 )
242 {
243 return false;
244 }
245
246 if (depth == 32)
247 {
248 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, width, height));
249 M_BMPDATA->m_bpp = 32;
250 }
251 else
252 {
253 if (depth != 1)
254 {
255 const GdkVisual* visual = wxTheApp->GetGdkVisual();
256 if (depth == -1)
257 depth = visual->depth;
258
259 wxCHECK_MSG(depth == visual->depth, false, wxT("invalid bitmap depth"));
260 }
261
262 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth));
263 }
264
265 return Ok();
266 }
267
268 bool wxBitmap::CreateFromXpm( const char **bits )
269 {
270 UnRef();
271
272 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
273
274 GdkBitmap *mask = (GdkBitmap*) NULL;
275 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, (gchar**)bits));
276
277 wxCHECK_MSG( M_BMPDATA->m_pixmap, false, wxT("couldn't create pixmap") );
278
279 if (mask)
280 {
281 M_BMPDATA->m_mask = new wxMask;
282 M_BMPDATA->m_mask->m_bitmap = mask;
283 }
284
285 return true;
286 }
287
288 wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy )
289 {
290 wxBitmap bmp;
291
292 wxCHECK_MSG(Ok(), bmp, wxT("invalid bitmap"));
293
294 if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height)
295 return *this;
296
297 int width = wxMax(newx, 1);
298 int height = wxMax(newy, 1);
299 width = wxMin(width, clipwidth);
300 height = wxMin(height, clipheight);
301
302 // scale pixbuf if available and it has alpha or there is no mask
303 if (M_BMPDATA->m_pixbuf != NULL && (
304 M_BMPDATA->m_mask == NULL || gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf)))
305 {
306 bmp.SetDepth(GetDepth());
307 bmp.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB,
308 gdk_pixbuf_get_has_alpha(GetPixbuf()),
309 8, width, height));
310 gdk_pixbuf_scale(GetPixbuf(), bmp.GetPixbuf(),
311 0, 0, width, height,
312 clipx, clipy,
313 (double)newx/GetWidth(), (double)newy/GetHeight(),
314 GDK_INTERP_BILINEAR);
315 }
316 else
317 {
318 GdkImage* img = gdk_drawable_get_image(GetPixmap(), 0, 0, GetWidth(), GetHeight());
319
320 wxCHECK_MSG(img, bmp, wxT("couldn't create image"));
321
322 GdkGC *gc = NULL;
323 GdkPixmap *dstpix = NULL;
324 char *dst = NULL;
325 long dstbyteperline = 0;
326
327 if (GetDepth() != 1)
328 {
329 GdkVisual *visual = gdk_drawable_get_visual( GetPixmap() );
330 if (visual == NULL)
331 visual = wxTheApp->GetGdkVisual();
332
333 bmp = wxBitmap(width, height, visual->depth);
334 dstpix = bmp.GetPixmap();
335 gc = gdk_gc_new( dstpix );
336 }
337 else
338 {
339 dstbyteperline = (width + 7) / 8;
340 dst = (char*) malloc(dstbyteperline*height);
341 }
342
343 // be careful to use the right scaling factor
344 float scx = (float)M_BMPDATA->m_width/(float)newx;
345 float scy = (float)M_BMPDATA->m_height/(float)newy;
346 // prepare accel-tables
347 int *tablex = (int *)calloc(width,sizeof(int));
348 int *tabley = (int *)calloc(height,sizeof(int));
349
350 // accel table filled with clipped values
351 for (int x = 0; x < width; x++)
352 tablex[x] = (int) (scx * (x+clipx));
353 for (int y = 0; y < height; y++)
354 tabley[y] = (int) (scy * (y+clipy));
355
356 // Main rescaling routine starts here
357 for (int h = 0; h < height; h++)
358 {
359 char outbyte = 0;
360 int old_x = -1;
361 guint32 old_pixval = 0;
362
363 for (int w = 0; w < width; w++)
364 {
365 guint32 pixval;
366 int x = tablex[w];
367 if (x == old_x)
368 pixval = old_pixval;
369 else
370 {
371 pixval = gdk_image_get_pixel( img, x, tabley[h] );
372 old_pixval = pixval;
373 old_x = x;
374 }
375
376 if ( dst )
377 {
378 if (pixval)
379 {
380 char bit=1;
381 char shift = bit << (w % 8);
382 outbyte |= shift;
383 }
384
385 if ((w+1)%8==0)
386 {
387 dst[h*dstbyteperline+w/8] = outbyte;
388 outbyte = 0;
389 }
390 }
391 else
392 {
393 GdkColor col;
394 col.pixel = pixval;
395 gdk_gc_set_foreground( gc, &col );
396 gdk_draw_point( dstpix, gc, w, h);
397 }
398 }
399
400 // do not forget the last byte
401 if ( dst && (width % 8 != 0) )
402 dst[h*dstbyteperline+width/8] = outbyte;
403 }
404
405 g_object_unref (img);
406 if (gc) g_object_unref (gc);
407
408 if ( dst )
409 {
410 bmp = wxBitmap( (const char *)dst, width, height, 1 );
411 free( dst );
412 }
413
414 if (GetMask())
415 {
416 dstbyteperline = (width + 7) / 8;
417 dst = (char*) malloc(dstbyteperline*height);
418 img = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
419
420 for (int h = 0; h < height; h++)
421 {
422 char outbyte = 0;
423 int old_x = -1;
424 guint32 old_pixval = 0;
425
426 for (int w = 0; w < width; w++)
427 {
428 guint32 pixval;
429 int x = tablex[w];
430 if (x == old_x)
431 pixval = old_pixval;
432 else
433 {
434 pixval = gdk_image_get_pixel( img, x, tabley[h] );
435 old_pixval = pixval;
436 old_x = x;
437 }
438
439 if (pixval)
440 {
441 char bit=1;
442 char shift = bit << (w % 8);
443 outbyte |= shift;
444 }
445
446 if ((w+1)%8 == 0)
447 {
448 dst[h*dstbyteperline+w/8] = outbyte;
449 outbyte = 0;
450 }
451 }
452
453 // do not forget the last byte
454 if (width % 8 != 0)
455 dst[h*dstbyteperline+width/8] = outbyte;
456 }
457 wxMask* mask = new wxMask;
458 mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height );
459 bmp.SetMask(mask);
460
461 free( dst );
462 g_object_unref (img);
463 }
464
465 free( tablex );
466 free( tabley );
467 }
468
469 return bmp;
470 }
471
472 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
473 {
474 UnRef();
475
476 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
477 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
478
479 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
480 return false;
481
482 // create pixbuf if image has alpha and requested depth is compatible
483 if (image.HasAlpha() && (depth == -1 || depth == 32))
484 return CreateFromImageAsPixbuf(image);
485
486 // otherwise create pixmap, if alpha is present it will be converted to mask
487 return CreateFromImageAsPixmap(image, depth);
488 }
489
490 bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth)
491 {
492 const int w = image.GetWidth();
493 const int h = image.GetHeight();
494 if (depth == 1)
495 {
496 // create XBM format bitmap
497
498 // one bit per pixel, each row starts on a byte boundary
499 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
500 wxByte* out = new wxByte[out_size];
501 // set bits are black
502 memset(out, 0xff, out_size);
503 const wxByte* in = image.GetData();
504 unsigned bit_index = 0;
505 for (int y = 0; y < h; y++)
506 {
507 for (int x = 0; x < w; x++, in += 3, bit_index++)
508 if (in[0] == 255 && in[1] == 255 && in[2] == 255)
509 out[bit_index >> 3] ^= 1 << (bit_index & 7);
510 // move index to next byte boundary
511 bit_index = (bit_index + 7) & ~7u;
512 }
513 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h));
514 delete[] out;
515 }
516 else
517 {
518 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth));
519 GdkGC* gc = gdk_gc_new(M_BMPDATA->m_pixmap);
520 gdk_draw_rgb_image(
521 M_BMPDATA->m_pixmap, gc,
522 0, 0, w, h,
523 GDK_RGB_DITHER_NONE, image.GetData(), w * 3);
524 g_object_unref(gc);
525 }
526
527 const wxByte* alpha = image.GetAlpha();
528 if (alpha != NULL || image.HasMask())
529 {
530 // create mask as XBM format bitmap
531
532 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
533 wxByte* out = new wxByte[out_size];
534 memset(out, 0xff, out_size);
535 unsigned bit_index = 0;
536 if (alpha != NULL)
537 {
538 for (int y = 0; y < h; y++)
539 {
540 for (int x = 0; x < w; x++, bit_index++)
541 if (*alpha++ < wxIMAGE_ALPHA_THRESHOLD)
542 out[bit_index >> 3] ^= 1 << (bit_index & 7);
543 bit_index = (bit_index + 7) & ~7u;
544 }
545 }
546 else
547 {
548 const wxByte r_mask = image.GetMaskRed();
549 const wxByte g_mask = image.GetMaskGreen();
550 const wxByte b_mask = image.GetMaskBlue();
551 const wxByte* in = image.GetData();
552 for (int y = 0; y < h; y++)
553 {
554 for (int x = 0; x < w; x++, in += 3, bit_index++)
555 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
556 out[bit_index >> 3] ^= 1 << (bit_index & 7);
557 bit_index = (bit_index + 7) & ~7u;
558 }
559 }
560 wxMask* mask = new wxMask;
561 mask->m_bitmap = gdk_bitmap_create_from_data(M_BMPDATA->m_pixmap, (char*)out, w, h);
562 SetMask(mask);
563 delete[] out;
564 }
565 return true;
566 }
567
568 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
569 {
570 wxASSERT(image.HasAlpha());
571
572 int width = image.GetWidth();
573 int height = image.GetHeight();
574
575 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
576 true,
577 8 /* bits per sample */,
578 width, height);
579 if (!pixbuf)
580 return false;
581
582 SetDepth(32);
583 SetPixbuf(pixbuf);
584
585 // Copy the data:
586 unsigned char *in = image.GetData();
587 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
588 unsigned char *alpha = image.GetAlpha();
589
590 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
591
592 for (int y = 0; y < height; y++, out += rowinc)
593 {
594 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
595 {
596 out[0] = in[0];
597 out[1] = in[1];
598 out[2] = in[2];
599 out[3] = *alpha;
600 }
601 }
602
603 return true;
604 }
605
606 wxImage wxBitmap::ConvertToImage() const
607 {
608 wxImage image;
609
610 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
611
612 const int w = GetWidth();
613 const int h = GetHeight();
614 image.Create(w, h);
615 unsigned char *data = image.GetData();
616
617 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
618
619 // prefer pixbuf if available, it will preserve alpha and should be quicker
620 if (HasPixbuf())
621 {
622 GdkPixbuf *pixbuf = GetPixbuf();
623 unsigned char* alpha = NULL;
624 if (gdk_pixbuf_get_has_alpha(pixbuf))
625 {
626 image.SetAlpha();
627 alpha = image.GetAlpha();
628 }
629 unsigned char *in = gdk_pixbuf_get_pixels(pixbuf);
630 unsigned char *out = data;
631 const int inc = 3 + int(alpha != NULL);
632 const int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
633
634 for (int y = 0; y < h; y++, in += rowinc)
635 {
636 for (int x = 0; x < w; x++, in += inc, out += 3)
637 {
638 out[0] = in[0];
639 out[1] = in[1];
640 out[2] = in[2];
641 if (alpha != NULL)
642 *alpha++ = in[3];
643 }
644 }
645 }
646 else
647 {
648 GdkPixmap* pixmap = GetPixmap();
649 GdkPixmap* pixmap_invert = NULL;
650 if (GetDepth() == 1)
651 {
652 // mono bitmaps are inverted, i.e. 0 is white
653 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
654 GdkGC* gc = gdk_gc_new(pixmap_invert);
655 gdk_gc_set_function(gc, GDK_COPY_INVERT);
656 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
657 g_object_unref(gc);
658 pixmap = pixmap_invert;
659 }
660 // create a pixbuf which shares data with the wxImage
661 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(
662 data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL);
663
664 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
665
666 g_object_unref(pixbuf);
667 if (pixmap_invert != NULL)
668 g_object_unref(pixmap_invert);
669 }
670 // convert mask, unless there is already alpha
671 if (GetMask() && !image.HasAlpha())
672 {
673 // the colour used as transparent one in wxImage and the one it is
674 // replaced with when it really occurs in the bitmap
675 const int MASK_RED = 1;
676 const int MASK_GREEN = 2;
677 const int MASK_BLUE = 3;
678 const int MASK_BLUE_REPLACEMENT = 2;
679
680 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
681 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
682
683 for (int y = 0; y < h; y++)
684 {
685 for (int x = 0; x < w; x++, data += 3)
686 {
687 if (gdk_image_get_pixel(image_mask, x, y) == 0)
688 {
689 data[0] = MASK_RED;
690 data[1] = MASK_GREEN;
691 data[2] = MASK_BLUE;
692 }
693 else if (data[0] == MASK_RED && data[1] == MASK_GREEN && data[2] == MASK_BLUE)
694 {
695 data[2] = MASK_BLUE_REPLACEMENT;
696 }
697 }
698 }
699 g_object_unref(image_mask);
700 }
701
702 return image;
703 }
704
705 wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
706 {
707 LoadFile( filename, type );
708 }
709
710 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
711 {
712 if ( width > 0 && height > 0 )
713 {
714 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, bits, width, height));
715
716 wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("couldn't create bitmap") );
717 }
718 }
719
720 wxBitmap::~wxBitmap()
721 {
722 }
723
724 bool wxBitmap::operator == ( const wxBitmap& bmp ) const
725 {
726 return m_refData == bmp.m_refData;
727 }
728
729 bool wxBitmap::operator != ( const wxBitmap& bmp ) const
730 {
731 return m_refData != bmp.m_refData;
732 }
733
734 bool wxBitmap::Ok() const
735 {
736 return (m_refData != NULL) &&
737 (
738 M_BMPDATA->m_pixbuf ||
739 M_BMPDATA->m_pixmap
740 );
741 }
742
743 int wxBitmap::GetHeight() const
744 {
745 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
746
747 return M_BMPDATA->m_height;
748 }
749
750 int wxBitmap::GetWidth() const
751 {
752 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
753
754 return M_BMPDATA->m_width;
755 }
756
757 int wxBitmap::GetDepth() const
758 {
759 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
760
761 return M_BMPDATA->m_bpp;
762 }
763
764 wxMask *wxBitmap::GetMask() const
765 {
766 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
767
768 return M_BMPDATA->m_mask;
769 }
770
771 void wxBitmap::SetMask( wxMask *mask )
772 {
773 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
774
775 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
776
777 M_BMPDATA->m_mask = mask;
778 }
779
780 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
781 {
782 *this = icon;
783 return Ok();
784 }
785
786 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
787 {
788 wxBitmap ret;
789
790 wxCHECK_MSG( Ok() &&
791 (rect.x >= 0) && (rect.y >= 0) &&
792 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
793 ret, wxT("invalid bitmap or bitmap region") );
794
795 if (HasPixbuf())
796 {
797 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
798 gdk_pixbuf_get_has_alpha(GetPixbuf()),
799 8, rect.width, rect.height);
800 ret.SetPixbuf(pixbuf);
801 ret.SetDepth(M_BMPDATA->m_bpp);
802 gdk_pixbuf_copy_area(GetPixbuf(),
803 rect.x, rect.y, rect.width, rect.height,
804 pixbuf, 0, 0);
805 }
806 else
807 {
808 ret = wxBitmap(rect.width, rect.height, M_BMPDATA->m_bpp);
809 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
810 gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
811 g_object_unref (gc);
812 }
813 // make mask, unless there is already alpha
814 if (GetMask() && !HasAlpha())
815 {
816 wxMask *mask = new wxMask;
817 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
818
819 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
820 gdk_draw_drawable(mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height);
821 g_object_unref (gc);
822
823 ret.SetMask( mask );
824 }
825
826 return ret;
827 }
828
829 bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
830 {
831 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
832
833 // Try to save the bitmap via wxImage handlers:
834 wxImage image = ConvertToImage();
835 return image.Ok() && image.SaveFile(name, type);
836 }
837
838 bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
839 {
840 UnRef();
841
842 if (type == wxBITMAP_TYPE_XPM)
843 {
844 GdkBitmap *mask = (GdkBitmap*) NULL;
845 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str()));
846
847 if (mask)
848 {
849 M_BMPDATA->m_mask = new wxMask;
850 M_BMPDATA->m_mask->m_bitmap = mask;
851 }
852 }
853 else // try if wxImage can load it
854 {
855 wxImage image;
856 if (image.LoadFile(name, type) && image.Ok())
857 *this = wxBitmap(image);
858 }
859
860 return Ok();
861 }
862
863 #if wxUSE_PALETTE
864 wxPalette *wxBitmap::GetPalette() const
865 {
866 if (!Ok())
867 return (wxPalette *) NULL;
868
869 return M_BMPDATA->m_palette;
870 }
871
872 void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
873 {
874 // TODO
875 }
876 #endif // wxUSE_PALETTE
877
878 void wxBitmap::SetHeight( int height )
879 {
880 if (!m_refData)
881 m_refData = new wxBitmapRefData;
882
883 M_BMPDATA->m_height = height;
884 }
885
886 void wxBitmap::SetWidth( int width )
887 {
888 if (!m_refData)
889 m_refData = new wxBitmapRefData;
890
891 M_BMPDATA->m_width = width;
892 }
893
894 void wxBitmap::SetDepth( int depth )
895 {
896 if (!m_refData)
897 m_refData = new wxBitmapRefData;
898
899 M_BMPDATA->m_bpp = depth;
900 }
901
902 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
903 {
904 if (!m_refData)
905 m_refData = new wxBitmapRefData;
906
907 wxASSERT(M_BMPDATA->m_pixmap == NULL);
908 M_BMPDATA->m_pixmap = pixmap;
909 gdk_drawable_get_size(pixmap, &M_BMPDATA->m_width, &M_BMPDATA->m_height);
910 M_BMPDATA->m_bpp = gdk_drawable_get_depth(pixmap);
911 PurgeOtherRepresentations(Pixmap);
912 }
913
914 GdkPixmap *wxBitmap::GetPixmap() const
915 {
916 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
917
918 // create the pixmap on the fly if we use Pixbuf representation:
919 if (M_BMPDATA->m_pixmap == NULL)
920 {
921 GdkPixmap** pmask = NULL;
922 if (gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf))
923 {
924 // make new mask from alpha
925 delete M_BMPDATA->m_mask;
926 M_BMPDATA->m_mask = new wxMask;
927 pmask = &M_BMPDATA->m_mask->m_bitmap;
928 }
929 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
930 &M_BMPDATA->m_pixmap,
931 pmask,
932 128 /*threshold*/);
933 }
934
935 return M_BMPDATA->m_pixmap;
936 }
937
938 bool wxBitmap::HasPixmap() const
939 {
940 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
941
942 return M_BMPDATA->m_pixmap != NULL;
943 }
944
945 GdkPixbuf *wxBitmap::GetPixbuf() const
946 {
947 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
948
949 if (M_BMPDATA->m_pixbuf == NULL)
950 {
951 int width = GetWidth();
952 int height = GetHeight();
953
954 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
955 GetMask() != NULL,
956 8, width, height);
957 M_BMPDATA->m_pixbuf =
958 gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
959 0, 0, 0, 0, width, height);
960
961 // apply the mask to created pixbuf:
962 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
963 {
964 GdkPixbuf *pmask =
965 gdk_pixbuf_get_from_drawable(NULL,
966 M_BMPDATA->m_mask->GetBitmap(),
967 NULL,
968 0, 0, 0, 0, width, height);
969 if (pmask)
970 {
971 guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
972 guchar *mask = gdk_pixbuf_get_pixels(pmask);
973 int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
974 int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
975
976 for (int y = 0; y < height;
977 y++, bmp += bmprowinc, mask += maskrowinc)
978 {
979 for (int x = 0; x < width; x++, bmp += 4, mask += 3)
980 {
981 if (mask[0] == 0 /*black pixel*/)
982 bmp[3] = 0;
983 }
984 }
985
986 g_object_unref (pmask);
987 }
988 }
989 }
990
991 return M_BMPDATA->m_pixbuf;
992 }
993
994 bool wxBitmap::HasPixbuf() const
995 {
996 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
997
998 return M_BMPDATA->m_pixbuf != NULL;
999 }
1000
1001 void wxBitmap::SetPixbuf( GdkPixbuf *pixbuf )
1002 {
1003 if (!m_refData)
1004 m_refData = new wxBitmapRefData;
1005
1006 wxASSERT(M_BMPDATA->m_pixbuf == NULL);
1007 M_BMPDATA->m_pixbuf = pixbuf;
1008 M_BMPDATA->m_width = gdk_pixbuf_get_width(pixbuf);
1009 M_BMPDATA->m_height = gdk_pixbuf_get_height(pixbuf);
1010 PurgeOtherRepresentations(Pixbuf);
1011 }
1012
1013 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
1014 {
1015 if (keep == Pixmap && HasPixbuf())
1016 {
1017 g_object_unref (M_BMPDATA->m_pixbuf);
1018 M_BMPDATA->m_pixbuf = NULL;
1019 }
1020 if (keep == Pixbuf && HasPixmap())
1021 {
1022 g_object_unref (M_BMPDATA->m_pixmap);
1023 M_BMPDATA->m_pixmap = NULL;
1024 }
1025 }
1026
1027 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1028 {
1029 void* bits = NULL;
1030 GdkPixbuf *pixbuf = GetPixbuf();
1031 const bool hasAlpha = HasAlpha();
1032 // allow access if bpp is valid and matches existence of alpha
1033 if (pixbuf != NULL && (
1034 bpp == 24 && !hasAlpha ||
1035 bpp == 32 && hasAlpha))
1036 {
1037 data.m_height = gdk_pixbuf_get_height( pixbuf );
1038 data.m_width = gdk_pixbuf_get_width( pixbuf );
1039 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
1040 bits = gdk_pixbuf_get_pixels(pixbuf);
1041 }
1042 return bits;
1043 }
1044
1045 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
1046 {
1047 }
1048
1049 bool wxBitmap::HasAlpha() const
1050 {
1051 return m_refData != NULL && M_BMPDATA->m_pixbuf != NULL &&
1052 gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf);
1053 }
1054
1055 void wxBitmap::UseAlpha()
1056 {
1057 GdkPixbuf* pixbuf = GetPixbuf();
1058 // add alpha if necessary
1059 if (!gdk_pixbuf_get_has_alpha(pixbuf))
1060 {
1061 M_BMPDATA->m_pixbuf = NULL;
1062 SetPixbuf(gdk_pixbuf_add_alpha(pixbuf, false, 0, 0, 0));
1063 g_object_unref(pixbuf);
1064 }
1065 }
1066
1067 //-----------------------------------------------------------------------------
1068 // wxBitmapHandler
1069 //-----------------------------------------------------------------------------
1070
1071 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler,wxBitmapHandlerBase)
1072
1073 wxBitmapHandler::~wxBitmapHandler()
1074 {
1075 }
1076
1077 bool wxBitmapHandler::Create(wxBitmap * WXUNUSED(bitmap),
1078 void * WXUNUSED(data),
1079 long WXUNUSED(type),
1080 int WXUNUSED(width),
1081 int WXUNUSED(height),
1082 int WXUNUSED(depth))
1083 {
1084 wxFAIL_MSG( _T("not implemented") );
1085
1086 return false;
1087 }
1088
1089 bool wxBitmapHandler::LoadFile(wxBitmap * WXUNUSED(bitmap),
1090 const wxString& WXUNUSED(name),
1091 long WXUNUSED(flags),
1092 int WXUNUSED(desiredWidth),
1093 int WXUNUSED(desiredHeight))
1094 {
1095 wxFAIL_MSG( _T("not implemented") );
1096
1097 return false;
1098 }
1099
1100 bool wxBitmapHandler::SaveFile(const wxBitmap * WXUNUSED(bitmap),
1101 const wxString& WXUNUSED(name),
1102 int WXUNUSED(type),
1103 const wxPalette * WXUNUSED(palette))
1104 {
1105 wxFAIL_MSG( _T("not implemented") );
1106
1107 return false;
1108 }
1109
1110 /* static */ void wxBitmap::InitStandardHandlers()
1111 {
1112 // TODO: Insert handler based on GdkPixbufs handler later
1113 }