avoid using deprecated gdk_image_new_bitmap()
[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/dcmemory.h"
18 #include "wx/palette.h"
19 #include "wx/icon.h"
20 #include "wx/math.h"
21 #include "wx/image.h"
22 #endif
23
24 #include "wx/filefn.h"
25
26 #include "wx/rawbmp.h"
27
28 #include <gdk/gdk.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkx.h>
31
32 #include <gdk/gdkimage.h>
33
34 extern void gdk_wx_draw_bitmap (GdkDrawable *drawable,
35 GdkGC *gc,
36 GdkDrawable *src,
37 gint xsrc,
38 gint ysrc,
39 gint xdest,
40 gint ydest,
41 gint width,
42 gint height);
43
44 //-----------------------------------------------------------------------------
45 // data
46 //-----------------------------------------------------------------------------
47
48 extern GtkWidget *wxGetRootWindow();
49
50 //-----------------------------------------------------------------------------
51 // wxMask
52 //-----------------------------------------------------------------------------
53
54 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
55
56 wxMask::wxMask()
57 {
58 m_bitmap = (GdkBitmap *) NULL;
59 }
60
61 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
62 {
63 m_bitmap = (GdkBitmap *) NULL;
64 Create( bitmap, colour );
65 }
66
67 #if wxUSE_PALETTE
68 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
69 {
70 m_bitmap = (GdkBitmap *) NULL;
71 Create( bitmap, paletteIndex );
72 }
73 #endif // wxUSE_PALETTE
74
75 wxMask::wxMask( const wxBitmap& bitmap )
76 {
77 m_bitmap = (GdkBitmap *) NULL;
78 Create( bitmap );
79 }
80
81 wxMask::~wxMask()
82 {
83 if (m_bitmap)
84 g_object_unref (m_bitmap);
85 }
86
87 bool wxMask::Create( const wxBitmap& bitmap,
88 const wxColour& colour )
89 {
90 if (m_bitmap)
91 {
92 g_object_unref (m_bitmap);
93 m_bitmap = (GdkBitmap*) NULL;
94 }
95
96 wxImage image = bitmap.ConvertToImage();
97 if (!image.Ok()) return false;
98
99 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
100 GdkGC *gc = gdk_gc_new( m_bitmap );
101
102 GdkColor color;
103 color.red = 65000;
104 color.green = 65000;
105 color.blue = 65000;
106 color.pixel = 1;
107 gdk_gc_set_foreground( gc, &color );
108 gdk_gc_set_fill( gc, GDK_SOLID );
109 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
110
111 unsigned char *data = image.GetData();
112 int index = 0;
113
114 unsigned char red = colour.Red();
115 unsigned char green = colour.Green();
116 unsigned char blue = colour.Blue();
117
118 GdkVisual *visual = wxTheApp->GetGdkVisual();
119
120 int bpp = visual->depth;
121 if ((bpp == 16) && (visual->red_mask != 0xf800))
122 bpp = 15;
123 if (bpp == 15)
124 {
125 red = red & 0xf8;
126 green = green & 0xf8;
127 blue = blue & 0xf8;
128 }
129 else if (bpp == 16)
130 {
131 red = red & 0xf8;
132 green = green & 0xfc;
133 blue = blue & 0xf8;
134 }
135 else if (bpp == 12)
136 {
137 red = red & 0xf0;
138 green = green & 0xf0;
139 blue = blue & 0xf0;
140 }
141
142 color.red = 0;
143 color.green = 0;
144 color.blue = 0;
145 color.pixel = 0;
146 gdk_gc_set_foreground( gc, &color );
147
148 for (int j = 0; j < image.GetHeight(); j++)
149 {
150 int start_x = -1;
151 int i;
152 for (i = 0; i < image.GetWidth(); i++)
153 {
154 if ((data[index] == red) &&
155 (data[index+1] == green) &&
156 (data[index+2] == blue))
157 {
158 if (start_x == -1)
159 start_x = i;
160 }
161 else
162 {
163 if (start_x != -1)
164 {
165 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
166 start_x = -1;
167 }
168 }
169 index += 3;
170 }
171 if (start_x != -1)
172 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
173 }
174
175 g_object_unref (gc);
176
177 return true;
178 }
179
180 #if wxUSE_PALETTE
181 bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
182 {
183 unsigned char r,g,b;
184 wxPalette *pal = bitmap.GetPalette();
185
186 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
187
188 pal->GetRGB(paletteIndex, &r, &g, &b);
189
190 return Create(bitmap, wxColour(r, g, b));
191 }
192 #endif // wxUSE_PALETTE
193
194 bool wxMask::Create( const wxBitmap& bitmap )
195 {
196 if (m_bitmap)
197 {
198 g_object_unref (m_bitmap);
199 m_bitmap = (GdkBitmap*) NULL;
200 }
201
202 if (!bitmap.Ok()) return false;
203
204 wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
205
206 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
207
208 if (!m_bitmap) return false;
209
210 GdkGC *gc = gdk_gc_new( m_bitmap );
211
212 gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetPixmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
213
214 g_object_unref (gc);
215
216 return true;
217 }
218
219 GdkBitmap *wxMask::GetBitmap() const
220 {
221 return m_bitmap;
222 }
223
224 //-----------------------------------------------------------------------------
225 // wxBitmap
226 //-----------------------------------------------------------------------------
227
228 class wxBitmapRefData: public wxObjectRefData
229 {
230 public:
231 wxBitmapRefData();
232 ~wxBitmapRefData();
233
234 GdkPixmap *m_pixmap;
235 GdkPixbuf *m_pixbuf;
236 wxMask *m_mask;
237 int m_width;
238 int m_height;
239 int m_bpp;
240 wxPalette *m_palette;
241 };
242
243 wxBitmapRefData::wxBitmapRefData()
244 {
245 m_pixmap = (GdkPixmap *) NULL;
246 m_pixbuf = (GdkPixbuf *) NULL;
247 m_mask = (wxMask *) NULL;
248 m_width = 0;
249 m_height = 0;
250 m_bpp = 0;
251 m_palette = (wxPalette *) NULL;
252 }
253
254 wxBitmapRefData::~wxBitmapRefData()
255 {
256 if (m_pixmap)
257 g_object_unref (m_pixmap);
258 if (m_pixbuf)
259 g_object_unref (m_pixbuf);
260 delete m_mask;
261 #if wxUSE_PALETTE
262 delete m_palette;
263 #endif // wxUSE_PALETTE
264 }
265
266 //-----------------------------------------------------------------------------
267
268 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
269
270 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
271
272 wxBitmap::wxBitmap()
273 {
274 }
275
276 wxBitmap::wxBitmap( int width, int height, int depth )
277 {
278 Create( width, height, depth );
279 }
280
281 bool wxBitmap::Create( int width, int height, int depth )
282 {
283 UnRef();
284
285 if ( width <= 0 || height <= 0 )
286 {
287 return false;
288 }
289
290 GdkVisual *visual = wxTheApp->GetGdkVisual();
291
292 if (depth == -1)
293 depth = visual->depth;
294
295 wxCHECK_MSG( (depth == visual->depth) || (depth == 1) || (depth == 32), false,
296 wxT("invalid bitmap depth") );
297
298 m_refData = new wxBitmapRefData();
299 M_BMPDATA->m_mask = (wxMask *) NULL;
300 M_BMPDATA->m_width = width;
301 M_BMPDATA->m_height = height;
302 M_BMPDATA->m_bpp = depth;
303 if (depth == 32)
304 {
305 M_BMPDATA->m_pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, true,
306 8, width, height);
307 }
308 else
309 {
310 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth );
311 }
312
313 return Ok();
314 }
315
316 bool wxBitmap::CreateFromXpm( const char **bits )
317 {
318 UnRef();
319
320 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
321
322 GdkVisual *visual = wxTheApp->GetGdkVisual();
323
324 m_refData = new wxBitmapRefData();
325
326 GdkBitmap *mask = (GdkBitmap*) NULL;
327
328 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits );
329
330 wxCHECK_MSG( M_BMPDATA->m_pixmap, false, wxT("couldn't create pixmap") );
331
332 if (mask)
333 {
334 M_BMPDATA->m_mask = new wxMask();
335 M_BMPDATA->m_mask->m_bitmap = mask;
336 }
337
338 gdk_drawable_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
339
340 M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ?
341
342 return true;
343 }
344
345 wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy )
346 {
347 wxCHECK_MSG( Ok(), wxNullBitmap, wxT("invalid bitmap") );
348
349 if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height)
350 return *this;
351
352 int width = wxMax(newx, 1);
353 int height = wxMax(newy, 1);
354 width = wxMin(width, clipwidth);
355 height = wxMin(height, clipheight);
356
357 wxBitmap bmp;
358
359 if (HasPixbuf())
360 {
361 bmp.SetWidth(width);
362 bmp.SetHeight(height);
363 bmp.SetDepth(GetDepth());
364 bmp.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB,
365 gdk_pixbuf_get_has_alpha(GetPixbuf()),
366 8, width, height));
367 gdk_pixbuf_scale(GetPixbuf(), bmp.GetPixbuf(),
368 0, 0, width, height,
369 clipx, clipy,
370 (double)newx/GetWidth(), (double)newy/GetHeight(),
371 GDK_INTERP_BILINEAR);
372 }
373 else
374 {
375 GdkImage *img = (GdkImage*) NULL;
376 if (GetPixmap())
377 img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
378 else
379 wxFAIL_MSG( wxT("Ill-formed bitmap") );
380
381 wxCHECK_MSG( img, wxNullBitmap, wxT("couldn't create image") );
382
383 int bpp = -1;
384
385
386 GdkGC *gc = NULL;
387 GdkPixmap *dstpix = NULL;
388 if (GetDepth() != 1)
389 {
390 GdkVisual *visual = gdk_drawable_get_visual( GetPixmap() );
391 if (visual == NULL)
392 visual = wxTheApp->GetGdkVisual();
393
394 bpp = visual->depth;
395 bmp = wxBitmap(width,height,bpp);
396 dstpix = bmp.GetPixmap();
397 gc = gdk_gc_new( dstpix );
398 }
399
400 char *dst = NULL;
401 long dstbyteperline = 0;
402
403 if (GetDepth() == 1)
404 {
405 bpp = 1;
406 dstbyteperline = width/8*M_BMPDATA->m_bpp;
407 if (width*M_BMPDATA->m_bpp % 8 != 0)
408 dstbyteperline++;
409 dst = (char*) malloc(dstbyteperline*height);
410 }
411
412 // be careful to use the right scaling factor
413 float scx = (float)M_BMPDATA->m_width/(float)newx;
414 float scy = (float)M_BMPDATA->m_height/(float)newy;
415 // prepare accel-tables
416 int *tablex = (int *)calloc(width,sizeof(int));
417 int *tabley = (int *)calloc(height,sizeof(int));
418
419 // accel table filled with clipped values
420 for (int x = 0; x < width; x++)
421 tablex[x] = (int) (scx * (x+clipx));
422 for (int y = 0; y < height; y++)
423 tabley[y] = (int) (scy * (y+clipy));
424
425 // Main rescaling routine starts here
426 for (int h = 0; h < height; h++)
427 {
428 char outbyte = 0;
429 int old_x = -1;
430 guint32 old_pixval = 0;
431
432 for (int w = 0; w < width; w++)
433 {
434 guint32 pixval;
435 int x = tablex[w];
436 if (x == old_x)
437 pixval = old_pixval;
438 else
439 {
440 pixval = gdk_image_get_pixel( img, x, tabley[h] );
441 old_pixval = pixval;
442 old_x = x;
443 }
444
445 if ( dst )
446 {
447 if (pixval)
448 {
449 char bit=1;
450 char shift = bit << (w % 8);
451 outbyte |= shift;
452 }
453
454 if ((w+1)%8==0)
455 {
456 dst[h*dstbyteperline+w/8] = outbyte;
457 outbyte = 0;
458 }
459 }
460 else
461 {
462 GdkColor col;
463 col.pixel = pixval;
464 gdk_gc_set_foreground( gc, &col );
465 gdk_draw_point( dstpix, gc, w, h);
466 }
467 }
468
469 // do not forget the last byte
470 if ( dst && (width % 8 != 0) )
471 dst[h*dstbyteperline+width/8] = outbyte;
472 }
473
474 g_object_unref (img);
475 if (gc) g_object_unref (gc);
476
477 if ( dst )
478 {
479 bmp = wxBitmap( (const char *)dst, width, height, 1 );
480 free( dst );
481 }
482
483 if (GetMask())
484 {
485 dstbyteperline = width/8;
486 if (width % 8 != 0)
487 dstbyteperline++;
488 dst = (char*) malloc(dstbyteperline*height);
489 img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
490
491 for (int h = 0; h < height; h++)
492 {
493 char outbyte = 0;
494 int old_x = -1;
495 guint32 old_pixval = 0;
496
497 for (int w = 0; w < width; w++)
498 {
499 guint32 pixval;
500 int x = tablex[w];
501 if (x == old_x)
502 pixval = old_pixval;
503 else
504 {
505 pixval = gdk_image_get_pixel( img, x, tabley[h] );
506 old_pixval = pixval;
507 old_x = x;
508 }
509
510 if (pixval)
511 {
512 char bit=1;
513 char shift = bit << (w % 8);
514 outbyte |= shift;
515 }
516
517 if ((w+1)%8 == 0)
518 {
519 dst[h*dstbyteperline+w/8] = outbyte;
520 outbyte = 0;
521 }
522 }
523
524 // do not forget the last byte
525 if (width % 8 != 0)
526 dst[h*dstbyteperline+width/8] = outbyte;
527 }
528 wxMask* mask = new wxMask;
529 mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height );
530 bmp.SetMask(mask);
531
532 free( dst );
533 g_object_unref (img);
534 }
535
536 free( tablex );
537 free( tabley );
538 }
539
540 return bmp;
541 }
542
543 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
544 {
545 UnRef();
546
547 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
548 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
549
550 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
551 return false;
552
553 m_refData = new wxBitmapRefData();
554
555 if (depth == 1)
556 {
557 return CreateFromImageAsBitmap(image);
558 }
559 else
560 {
561 if (image.HasAlpha())
562 return CreateFromImageAsPixbuf(image);
563
564 return CreateFromImageAsPixmap(image);
565 }
566 }
567
568 // conversion to mono bitmap:
569 bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img)
570 {
571 // convert alpha channel to mask, if it is present:
572 wxImage image(img);
573 image.ConvertAlphaToMask();
574
575 int width = image.GetWidth();
576 int height = image.GetHeight();
577
578 SetHeight( height );
579 SetWidth( width );
580
581 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
582
583 SetDepth( 1 );
584
585 // Create picture image
586
587 GdkGC* data_gc = gdk_gc_new(M_BMPDATA->m_pixmap);
588 GdkColor color;
589 color.pixel = 1;
590 gdk_gc_set_foreground(data_gc, &color);
591 gdk_draw_rectangle(M_BMPDATA->m_pixmap, data_gc, true, 0, 0, width, height);
592 GdkImage* data_image = gdk_drawable_get_image(M_BMPDATA->m_pixmap, 0, 0, width, height);
593
594 // Create mask image
595
596 GdkImage *mask_image = (GdkImage*) NULL;
597 GdkGC* mask_gc = NULL;
598
599 if (image.HasMask())
600 {
601 wxMask *mask = new wxMask();
602 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
603 mask_gc = gdk_gc_new(mask->m_bitmap);
604 gdk_gc_set_foreground(mask_gc, &color);
605 gdk_draw_rectangle(mask->m_bitmap, mask_gc, true, 0, 0, width, height);
606 mask_image = gdk_drawable_get_image(mask->m_bitmap, 0, 0, width, height);
607
608 SetMask( mask );
609 }
610
611 int r_mask = image.GetMaskRed();
612 int g_mask = image.GetMaskGreen();
613 int b_mask = image.GetMaskBlue();
614
615 unsigned char* data = image.GetData();
616
617 int index = 0;
618 for (int y = 0; y < height; y++)
619 {
620 for (int x = 0; x < width; x++)
621 {
622 int r = data[index];
623 index++;
624 int g = data[index];
625 index++;
626 int b = data[index];
627 index++;
628
629 if (mask_image != NULL)
630 {
631 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
632 gdk_image_put_pixel( mask_image, x, y, 0 );
633 }
634
635 if ((r == 255) && (b == 255) && (g == 255))
636 gdk_image_put_pixel( data_image, x, y, 0 );
637
638 } // for
639 } // for
640
641 // Blit picture
642
643 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
644
645 g_object_unref (data_image);
646 g_object_unref (data_gc);
647
648 // Blit mask
649
650 if (mask_image != NULL)
651 {
652 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
653
654 g_object_unref (mask_image);
655 g_object_unref (mask_gc);
656 }
657
658 return true;
659 }
660
661 // conversion to colour bitmap:
662 bool wxBitmap::CreateFromImageAsPixmap(const wxImage& img)
663 {
664 // convert alpha channel to mask, if it is present:
665 wxImage image(img);
666 image.ConvertAlphaToMask();
667
668 int width = image.GetWidth();
669 int height = image.GetHeight();
670
671 SetHeight( height );
672 SetWidth( width );
673
674 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
675
676 GdkVisual *visual = wxTheApp->GetGdkVisual();
677
678 int bpp = visual->depth;
679
680 SetDepth( bpp );
681
682 GdkGC *gc = gdk_gc_new( GetPixmap() );
683
684 gdk_draw_rgb_image( GetPixmap(),
685 gc,
686 0, 0,
687 width, height,
688 GDK_RGB_DITHER_NONE,
689 image.GetData(),
690 width*3 );
691
692 g_object_unref (gc);
693
694 // Create mask image
695
696 if (!image.HasMask())
697 return true;
698
699 wxMask *mask = new wxMask();
700 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
701 GdkGC* mask_gc = gdk_gc_new(mask->m_bitmap);
702 GdkColor color;
703 color.pixel = 1;
704 gdk_gc_set_foreground(mask_gc, &color);
705 gdk_draw_rectangle(mask->m_bitmap, mask_gc, true, 0, 0, width, height);
706 GdkImage* mask_image = gdk_drawable_get_image(mask->m_bitmap, 0, 0, width, height);
707
708 SetMask( mask );
709
710 int r_mask = image.GetMaskRed();
711 int g_mask = image.GetMaskGreen();
712 int b_mask = image.GetMaskBlue();
713
714 unsigned char* data = image.GetData();
715
716 int index = 0;
717 for (int y = 0; y < height; y++)
718 {
719 for (int x = 0; x < width; x++)
720 {
721 int r = data[index];
722 index++;
723 int g = data[index];
724 index++;
725 int b = data[index];
726 index++;
727
728 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
729 gdk_image_put_pixel( mask_image, x, y, 0 );
730 } // for
731 } // for
732
733 // Blit mask
734
735 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
736
737 g_object_unref (mask_image);
738 g_object_unref (mask_gc);
739
740 return true;
741 }
742
743 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
744 {
745 int width = image.GetWidth();
746 int height = image.GetHeight();
747
748 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
749 image.HasAlpha(),
750 8 /* bits per sample */,
751 width, height);
752 if (!pixbuf)
753 return false;
754
755 wxASSERT( image.HasAlpha() ); // for now
756 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf) == 4 );
757 wxASSERT( gdk_pixbuf_get_width(pixbuf) == width );
758 wxASSERT( gdk_pixbuf_get_height(pixbuf) == height );
759
760 M_BMPDATA->m_pixbuf = pixbuf;
761 SetHeight(height);
762 SetWidth(width);
763 SetDepth(wxTheApp->GetGdkVisual()->depth);
764
765 // Copy the data:
766 unsigned char *in = image.GetData();
767 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
768 unsigned char *alpha = image.GetAlpha();
769
770 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
771
772 for (int y = 0; y < height; y++, out += rowinc)
773 {
774 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
775 {
776 out[0] = in[0];
777 out[1] = in[1];
778 out[2] = in[2];
779 out[3] = *alpha;
780 }
781 }
782
783 return true;
784 }
785
786 wxImage wxBitmap::ConvertToImage() const
787 {
788 wxImage image;
789
790 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
791
792 image.Create(GetWidth(), GetHeight());
793 unsigned char *data = image.GetData();
794
795 if (!data)
796 {
797 wxFAIL_MSG( wxT("couldn't create image") );
798 return wxNullImage;
799 }
800
801 if (HasPixbuf())
802 {
803 GdkPixbuf *pixbuf = GetPixbuf();
804 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf) );
805
806 int w = GetWidth();
807 int h = GetHeight();
808
809 image.SetAlpha();
810
811 unsigned char *alpha = image.GetAlpha();
812 unsigned char *in = gdk_pixbuf_get_pixels(pixbuf);
813 unsigned char *out = data;
814 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * w;
815
816 for (int y = 0; y < h; y++, in += rowinc)
817 {
818 for (int x = 0; x < w; x++, in += 4, out += 3, alpha++)
819 {
820 out[0] = in[0];
821 out[1] = in[1];
822 out[2] = in[2];
823 *alpha = in[3];
824 }
825 }
826 }
827 else
828 {
829 // the colour used as transparent one in wxImage and the one it is
830 // replaced with when it really occurs in the bitmap
831 static const int MASK_RED = 1;
832 static const int MASK_GREEN = 2;
833 static const int MASK_BLUE = 3;
834 static const int MASK_BLUE_REPLACEMENT = 2;
835
836 GdkImage *gdk_image = (GdkImage*) NULL;
837
838 if (HasPixmap())
839 {
840 gdk_image = gdk_image_get( GetPixmap(),
841 0, 0,
842 GetWidth(), GetHeight() );
843 }
844 else
845 {
846 wxFAIL_MSG( wxT("Ill-formed bitmap") );
847 }
848
849 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
850
851 GdkImage *gdk_image_mask = (GdkImage*) NULL;
852 if (GetMask())
853 {
854 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
855 0, 0,
856 GetWidth(), GetHeight() );
857
858 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
859 }
860
861 int bpp = -1;
862 int red_shift_right = 0;
863 int green_shift_right = 0;
864 int blue_shift_right = 0;
865 int red_shift_left = 0;
866 int green_shift_left = 0;
867 int blue_shift_left = 0;
868 bool use_shift = false;
869
870 if (GetDepth() != 1)
871 {
872 GdkVisual *visual = gdk_drawable_get_visual( GetPixmap() );
873 if (visual == NULL)
874 visual = wxTheApp->GetGdkVisual();
875
876 bpp = visual->depth;
877 if (bpp == 16)
878 bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
879 red_shift_right = visual->red_shift;
880 red_shift_left = 8-visual->red_prec;
881 green_shift_right = visual->green_shift;
882 green_shift_left = 8-visual->green_prec;
883 blue_shift_right = visual->blue_shift;
884 blue_shift_left = 8-visual->blue_prec;
885
886 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
887 }
888 else
889 {
890 bpp = 1;
891 }
892
893
894 GdkColormap *cmap = gtk_widget_get_default_colormap();
895
896 long pos = 0;
897 for (int j = 0; j < GetHeight(); j++)
898 {
899 for (int i = 0; i < GetWidth(); i++)
900 {
901 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
902 if (bpp == 1)
903 {
904 if (pixel == 0)
905 {
906 data[pos] = 0;
907 data[pos+1] = 0;
908 data[pos+2] = 0;
909 }
910 else
911 {
912 data[pos] = 255;
913 data[pos+1] = 255;
914 data[pos+2] = 255;
915 }
916 }
917 else if (use_shift)
918 {
919 data[pos] = (pixel >> red_shift_right) << red_shift_left;
920 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
921 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
922 }
923 else if (cmap->colors)
924 {
925 data[pos] = cmap->colors[pixel].red >> 8;
926 data[pos+1] = cmap->colors[pixel].green >> 8;
927 data[pos+2] = cmap->colors[pixel].blue >> 8;
928 }
929 else
930 {
931 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
932 }
933
934 if (gdk_image_mask)
935 {
936 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
937 if (mask_pixel == 0)
938 {
939 data[pos] = MASK_RED;
940 data[pos+1] = MASK_GREEN;
941 data[pos+2] = MASK_BLUE;
942 }
943 else if ( data[pos] == MASK_RED &&
944 data[pos+1] == MASK_GREEN &&
945 data[pos+2] == MASK_BLUE )
946 {
947 data[pos+2] = MASK_BLUE_REPLACEMENT;
948 }
949 }
950
951 pos += 3;
952 }
953 }
954
955 g_object_unref (gdk_image);
956 if (gdk_image_mask) g_object_unref (gdk_image_mask);
957 }
958
959 return image;
960 }
961
962 wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
963 {
964 LoadFile( filename, type );
965 }
966
967 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
968 {
969 if ( width > 0 && height > 0 )
970 {
971 m_refData = new wxBitmapRefData();
972
973 M_BMPDATA->m_mask = (wxMask *) NULL;
974 M_BMPDATA->m_pixmap = gdk_bitmap_create_from_data
975 (
976 wxGetRootWindow()->window,
977 (gchar *) bits,
978 width,
979 height
980 );
981 M_BMPDATA->m_width = width;
982 M_BMPDATA->m_height = height;
983 M_BMPDATA->m_bpp = 1;
984
985 wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("couldn't create bitmap") );
986 }
987 }
988
989 wxBitmap::~wxBitmap()
990 {
991 }
992
993 bool wxBitmap::operator == ( const wxBitmap& bmp ) const
994 {
995 return m_refData == bmp.m_refData;
996 }
997
998 bool wxBitmap::operator != ( const wxBitmap& bmp ) const
999 {
1000 return m_refData != bmp.m_refData;
1001 }
1002
1003 bool wxBitmap::Ok() const
1004 {
1005 return (m_refData != NULL) &&
1006 (
1007 M_BMPDATA->m_pixbuf ||
1008 M_BMPDATA->m_pixmap
1009 );
1010 }
1011
1012 int wxBitmap::GetHeight() const
1013 {
1014 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1015
1016 return M_BMPDATA->m_height;
1017 }
1018
1019 int wxBitmap::GetWidth() const
1020 {
1021 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1022
1023 return M_BMPDATA->m_width;
1024 }
1025
1026 int wxBitmap::GetDepth() const
1027 {
1028 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1029
1030 return M_BMPDATA->m_bpp;
1031 }
1032
1033 wxMask *wxBitmap::GetMask() const
1034 {
1035 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1036
1037 return M_BMPDATA->m_mask;
1038 }
1039
1040 void wxBitmap::SetMask( wxMask *mask )
1041 {
1042 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1043
1044 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
1045
1046 M_BMPDATA->m_mask = mask;
1047 }
1048
1049 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
1050 {
1051 *this = icon;
1052 return true;
1053 }
1054
1055 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1056 {
1057 wxCHECK_MSG( Ok() &&
1058 (rect.x >= 0) && (rect.y >= 0) &&
1059 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
1060 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
1061
1062 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
1063 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1064
1065 if (HasPixbuf())
1066 {
1067 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
1068 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1069 8, rect.width, rect.height);
1070 ret.SetPixbuf(pixbuf);
1071 gdk_pixbuf_copy_area(GetPixbuf(),
1072 rect.x, rect.y, rect.width, rect.height,
1073 pixbuf, 0, 0);
1074 }
1075 else
1076 {
1077 if (ret.GetDepth() != 1)
1078 {
1079 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
1080 gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1081 g_object_unref (gc);
1082 }
1083 else
1084 {
1085 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
1086 GdkColor col;
1087 col.pixel = 0xFFFFFF;
1088 gdk_gc_set_foreground( gc, &col );
1089 col.pixel = 0;
1090 gdk_gc_set_background( gc, &col );
1091 gdk_wx_draw_bitmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1092 g_object_unref (gc);
1093 }
1094 }
1095
1096 if (GetMask())
1097 {
1098 wxMask *mask = new wxMask;
1099 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
1100
1101 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
1102 GdkColor col;
1103 col.pixel = 0xFFFFFF;
1104 gdk_gc_set_foreground( gc, &col );
1105 col.pixel = 0;
1106 gdk_gc_set_background( gc, &col );
1107 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height );
1108 g_object_unref (gc);
1109
1110 ret.SetMask( mask );
1111 }
1112
1113 return ret;
1114 }
1115
1116 bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
1117 {
1118 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1119
1120 // Try to save the bitmap via wxImage handlers:
1121 {
1122 wxImage image = ConvertToImage();
1123 if (image.Ok()) return image.SaveFile( name, type );
1124 }
1125
1126 return false;
1127 }
1128
1129 bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
1130 {
1131 UnRef();
1132
1133 if (!wxFileExists(name))
1134 return false;
1135
1136 GdkVisual *visual = wxTheApp->GetGdkVisual();
1137
1138 if (type == wxBITMAP_TYPE_XPM)
1139 {
1140 m_refData = new wxBitmapRefData();
1141
1142 GdkBitmap *mask = (GdkBitmap*) NULL;
1143
1144 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm
1145 (
1146 wxGetRootWindow()->window,
1147 &mask,
1148 NULL,
1149 name.fn_str()
1150 );
1151
1152 if (mask)
1153 {
1154 M_BMPDATA->m_mask = new wxMask();
1155 M_BMPDATA->m_mask->m_bitmap = mask;
1156 }
1157
1158 gdk_drawable_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
1159
1160 M_BMPDATA->m_bpp = visual->depth;
1161 }
1162 else // try if wxImage can load it
1163 {
1164 wxImage image;
1165 if ( !image.LoadFile( name, type ) || !image.Ok() )
1166 return false;
1167
1168 *this = wxBitmap(image);
1169 }
1170
1171 return true;
1172 }
1173
1174 #if wxUSE_PALETTE
1175 wxPalette *wxBitmap::GetPalette() const
1176 {
1177 if (!Ok())
1178 return (wxPalette *) NULL;
1179
1180 return M_BMPDATA->m_palette;
1181 }
1182
1183 void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
1184 {
1185 // TODO
1186 }
1187 #endif // wxUSE_PALETTE
1188
1189 void wxBitmap::SetHeight( int height )
1190 {
1191 if (!m_refData)
1192 m_refData = new wxBitmapRefData();
1193
1194 M_BMPDATA->m_height = height;
1195 }
1196
1197 void wxBitmap::SetWidth( int width )
1198 {
1199 if (!m_refData)
1200 m_refData = new wxBitmapRefData();
1201
1202 M_BMPDATA->m_width = width;
1203 }
1204
1205 void wxBitmap::SetDepth( int depth )
1206 {
1207 if (!m_refData)
1208 m_refData = new wxBitmapRefData();
1209
1210 M_BMPDATA->m_bpp = depth;
1211 }
1212
1213 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1214 {
1215 if (!m_refData)
1216 m_refData = new wxBitmapRefData();
1217
1218 M_BMPDATA->m_pixmap = pixmap;
1219 PurgeOtherRepresentations(Pixmap);
1220 }
1221
1222 GdkPixmap *wxBitmap::GetPixmap() const
1223 {
1224 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
1225
1226 // create the pixmap on the fly if we use Pixbuf representation:
1227 if (HasPixbuf() && !HasPixmap())
1228 {
1229 delete M_BMPDATA->m_mask;
1230 M_BMPDATA->m_mask = new wxMask();
1231 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
1232 &M_BMPDATA->m_pixmap,
1233 &M_BMPDATA->m_mask->m_bitmap,
1234 128 /*threshold*/);
1235 }
1236
1237 return M_BMPDATA->m_pixmap;
1238 }
1239
1240 bool wxBitmap::HasPixmap() const
1241 {
1242 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1243
1244 return M_BMPDATA->m_pixmap != NULL;
1245 }
1246
1247 GdkPixbuf *wxBitmap::GetPixbuf() const
1248 {
1249 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
1250
1251 if (HasPixmap() && !HasPixbuf())
1252 {
1253 int width = GetWidth();
1254 int height = GetHeight();
1255
1256 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
1257 GetMask() != NULL,
1258 8, width, height);
1259 M_BMPDATA->m_pixbuf =
1260 gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
1261 0, 0, 0, 0, width, height);
1262
1263 // apply the mask to created pixbuf:
1264 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
1265 {
1266 GdkPixbuf *pmask =
1267 gdk_pixbuf_get_from_drawable(NULL,
1268 M_BMPDATA->m_mask->GetBitmap(),
1269 NULL,
1270 0, 0, 0, 0, width, height);
1271 if (pmask)
1272 {
1273 guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
1274 guchar *mask = gdk_pixbuf_get_pixels(pmask);
1275 int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
1276 int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
1277
1278 for (int y = 0; y < height;
1279 y++, bmp += bmprowinc, mask += maskrowinc)
1280 {
1281 for (int x = 0; x < width; x++, bmp += 4, mask += 3)
1282 {
1283 if (mask[0] == 0 /*black pixel*/)
1284 bmp[3] = 0;
1285 }
1286 }
1287
1288 g_object_unref (pmask);
1289 }
1290 }
1291 }
1292
1293 return M_BMPDATA->m_pixbuf;
1294 }
1295
1296 bool wxBitmap::HasPixbuf() const
1297 {
1298 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1299
1300 return M_BMPDATA->m_pixbuf != NULL;
1301 }
1302
1303 void wxBitmap::SetPixbuf( GdkPixbuf *pixbuf )
1304 {
1305 if (!m_refData)
1306 m_refData = new wxBitmapRefData();
1307
1308 M_BMPDATA->m_pixbuf = pixbuf;
1309 PurgeOtherRepresentations(Pixbuf);
1310 }
1311
1312 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
1313 {
1314 if (keep == Pixmap && HasPixbuf())
1315 {
1316 g_object_unref (M_BMPDATA->m_pixbuf);
1317 M_BMPDATA->m_pixbuf = NULL;
1318 }
1319 if (keep == Pixbuf && HasPixmap())
1320 {
1321 g_object_unref (M_BMPDATA->m_pixmap);
1322 M_BMPDATA->m_pixmap = NULL;
1323 }
1324 }
1325
1326 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1327 {
1328 if (bpp != 32)
1329 return NULL;
1330
1331 GdkPixbuf *pixbuf = GetPixbuf();
1332 if (!pixbuf)
1333 return NULL;
1334
1335 #if 0
1336 if (gdk_pixbuf_get_has_alpha( pixbuf ))
1337 wxPrintf( wxT("Has alpha\n") );
1338 else
1339 wxPrintf( wxT("No alpha.\n") );
1340 #endif
1341
1342 data.m_height = gdk_pixbuf_get_height( pixbuf );
1343 data.m_width = gdk_pixbuf_get_width( pixbuf );
1344 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
1345
1346 return gdk_pixbuf_get_pixels( pixbuf );
1347 }
1348
1349 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
1350 {
1351 }
1352
1353
1354 bool wxBitmap::HasAlpha() const
1355 {
1356 return HasPixbuf();
1357 }
1358
1359 void wxBitmap::UseAlpha()
1360 {
1361 GetPixbuf();
1362 }
1363
1364 //-----------------------------------------------------------------------------
1365 // wxBitmapHandler
1366 //-----------------------------------------------------------------------------
1367
1368 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler,wxBitmapHandlerBase)
1369
1370 wxBitmapHandler::~wxBitmapHandler()
1371 {
1372 }
1373
1374 bool wxBitmapHandler::Create(wxBitmap * WXUNUSED(bitmap),
1375 void * WXUNUSED(data),
1376 long WXUNUSED(type),
1377 int WXUNUSED(width),
1378 int WXUNUSED(height),
1379 int WXUNUSED(depth))
1380 {
1381 wxFAIL_MSG( _T("not implemented") );
1382
1383 return false;
1384 }
1385
1386 bool wxBitmapHandler::LoadFile(wxBitmap * WXUNUSED(bitmap),
1387 const wxString& WXUNUSED(name),
1388 long WXUNUSED(flags),
1389 int WXUNUSED(desiredWidth),
1390 int WXUNUSED(desiredHeight))
1391 {
1392 wxFAIL_MSG( _T("not implemented") );
1393
1394 return false;
1395 }
1396
1397 bool wxBitmapHandler::SaveFile(const wxBitmap * WXUNUSED(bitmap),
1398 const wxString& WXUNUSED(name),
1399 int WXUNUSED(type),
1400 const wxPalette * WXUNUSED(palette))
1401 {
1402 wxFAIL_MSG( _T("not implemented") );
1403
1404 return false;
1405 }
1406
1407 /* static */ void wxBitmap::InitStandardHandlers()
1408 {
1409 // TODO: Insert handler based on GdkPixbufs handler later
1410 }