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