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