]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
fixed compilation warnings with GTK+ 2.0
[wxWidgets.git] / src / gtk / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
11 #pragma implementation "bitmap.h"
12 #endif
13
14 #include "wx/defs.h"
15
16 #include "wx/palette.h"
17 #include "wx/bitmap.h"
18 #include "wx/icon.h"
19 #include "wx/filefn.h"
20 #include "wx/image.h"
21 #include "wx/dcmemory.h"
22 #include "wx/app.h"
23
24 #ifdef __WXGTK20__
25 // need this to get gdk_image_new_bitmap()
26 #define GDK_ENABLE_BROKEN
27 #endif
28
29 #include <gdk/gdk.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkx.h>
32
33 #ifdef __WXGTK20__
34 #include <gdk/gdkimage.h>
35 #else // GTK+ 1.2
36 // VZ: is this still needed? seems to compile fine without it...
37 #if (GTK_MINOR_VERSION > 0)
38 #include <gdk/gdkrgb.h>
39 #endif
40 #endif // GTK+ 2.0/1.2
41
42 extern void gdk_wx_draw_bitmap (GdkDrawable *drawable,
43 GdkGC *gc,
44 GdkDrawable *src,
45 gint xsrc,
46 gint ysrc,
47 gint xdest,
48 gint ydest,
49 gint width,
50 gint height);
51
52 //-----------------------------------------------------------------------------
53 // data
54 //-----------------------------------------------------------------------------
55
56 extern GtkWidget *wxGetRootWindow();
57
58 //-----------------------------------------------------------------------------
59 // wxMask
60 //-----------------------------------------------------------------------------
61
62 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
63
64 wxMask::wxMask()
65 {
66 m_bitmap = (GdkBitmap *) NULL;
67 }
68
69 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
70 {
71 m_bitmap = (GdkBitmap *) NULL;
72 Create( bitmap, colour );
73 }
74
75 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
76 {
77 m_bitmap = (GdkBitmap *) NULL;
78 Create( bitmap, paletteIndex );
79 }
80
81 wxMask::wxMask( const wxBitmap& bitmap )
82 {
83 m_bitmap = (GdkBitmap *) NULL;
84 Create( bitmap );
85 }
86
87 wxMask::~wxMask()
88 {
89 if (m_bitmap)
90 gdk_bitmap_unref( m_bitmap );
91 }
92
93 bool wxMask::Create( const wxBitmap& bitmap,
94 const wxColour& colour )
95 {
96 if (m_bitmap)
97 {
98 gdk_bitmap_unref( m_bitmap );
99 m_bitmap = (GdkBitmap*) NULL;
100 }
101
102 wxImage image = bitmap.ConvertToImage();
103 if (!image.Ok()) return FALSE;
104
105 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
106 GdkGC *gc = gdk_gc_new( m_bitmap );
107
108 GdkColor color;
109 color.red = 65000;
110 color.green = 65000;
111 color.blue = 65000;
112 color.pixel = 1;
113 gdk_gc_set_foreground( gc, &color );
114 gdk_gc_set_fill( gc, GDK_SOLID );
115 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
116
117 unsigned char *data = image.GetData();
118 int index = 0;
119
120 unsigned char red = colour.Red();
121 unsigned char green = colour.Green();
122 unsigned char blue = colour.Blue();
123
124 GdkVisual *visual = wxTheApp->GetGdkVisual();
125
126 int bpp = visual->depth;
127 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
128 if (bpp == 15)
129 {
130 red = red & 0xf8;
131 green = green & 0xf8;
132 blue = blue & 0xf8;
133 } else
134 if (bpp == 16)
135 {
136 red = red & 0xf8;
137 green = green & 0xfc;
138 blue = blue & 0xf8;
139 } else
140 if (bpp == 12)
141 {
142 red = red & 0xf0;
143 green = green & 0xf0;
144 blue = blue & 0xf0;
145 }
146
147 color.red = 0;
148 color.green = 0;
149 color.blue = 0;
150 color.pixel = 0;
151 gdk_gc_set_foreground( gc, &color );
152
153 for (int j = 0; j < image.GetHeight(); j++)
154 {
155 int start_x = -1;
156 int i;
157 for (i = 0; i < image.GetWidth(); i++)
158 {
159 if ((data[index] == red) &&
160 (data[index+1] == green) &&
161 (data[index+2] == blue))
162 {
163 if (start_x == -1)
164 start_x = i;
165 }
166 else
167 {
168 if (start_x != -1)
169 {
170 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
171 start_x = -1;
172 }
173 }
174 index += 3;
175 }
176 if (start_x != -1)
177 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
178 }
179
180 gdk_gc_unref( gc );
181
182 return TRUE;
183 }
184
185 bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
186 {
187 unsigned char r,g,b;
188 wxPalette *pal = bitmap.GetPalette();
189
190 wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") );
191
192 pal->GetRGB(paletteIndex, &r, &g, &b);
193
194 return Create(bitmap, wxColour(r, g, b));
195 }
196
197 bool wxMask::Create( const wxBitmap& bitmap )
198 {
199 if (m_bitmap)
200 {
201 gdk_bitmap_unref( m_bitmap );
202 m_bitmap = (GdkBitmap*) NULL;
203 }
204
205 if (!bitmap.Ok()) return FALSE;
206
207 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
208
209 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
210
211 if (!m_bitmap) return FALSE;
212
213 GdkGC *gc = gdk_gc_new( m_bitmap );
214
215 gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
216
217 gdk_gc_unref( gc );
218
219 return TRUE;
220 }
221
222 GdkBitmap *wxMask::GetBitmap() const
223 {
224 return m_bitmap;
225 }
226
227 //-----------------------------------------------------------------------------
228 // wxBitmap
229 //-----------------------------------------------------------------------------
230
231 class wxBitmapRefData: public wxObjectRefData
232 {
233 public:
234 wxBitmapRefData();
235 ~wxBitmapRefData();
236
237 GdkPixmap *m_pixmap;
238 GdkBitmap *m_bitmap;
239 wxMask *m_mask;
240 int m_width;
241 int m_height;
242 int m_bpp;
243 wxPalette *m_palette;
244 };
245
246 wxBitmapRefData::wxBitmapRefData()
247 {
248 m_pixmap = (GdkPixmap *) NULL;
249 m_bitmap = (GdkBitmap *) NULL;
250 m_mask = (wxMask *) NULL;
251 m_width = 0;
252 m_height = 0;
253 m_bpp = 0;
254 m_palette = (wxPalette *) NULL;
255 }
256
257 wxBitmapRefData::~wxBitmapRefData()
258 {
259 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
260 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
261 if (m_mask) delete m_mask;
262 if (m_palette) delete m_palette;
263 }
264
265 //-----------------------------------------------------------------------------
266
267 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
268
269 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
270
271 wxBitmap::wxBitmap()
272 {
273 }
274
275 wxBitmap::wxBitmap( int width, int height, int depth )
276 {
277 Create( width, height, depth );
278 }
279
280 bool wxBitmap::Create( int width, int height, int depth )
281 {
282 UnRef();
283
284 wxCHECK_MSG( (width > 0) && (height > 0), FALSE, wxT("invalid bitmap size") )
285
286 GdkVisual *visual = wxTheApp->GetGdkVisual();
287
288 if (depth == -1) depth = visual->depth;
289
290 wxCHECK_MSG( (depth == visual->depth) ||
291 (depth == 1), FALSE, wxT("invalid bitmap depth") )
292
293 m_refData = new wxBitmapRefData();
294 M_BMPDATA->m_mask = (wxMask *) NULL;
295 M_BMPDATA->m_width = width;
296 M_BMPDATA->m_height = height;
297 if (depth == 1)
298 {
299 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
300 M_BMPDATA->m_bpp = 1;
301 }
302 else
303 {
304 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth );
305 M_BMPDATA->m_bpp = visual->depth;
306 }
307
308 return Ok();
309 }
310
311 bool wxBitmap::CreateFromXpm( const char **bits )
312 {
313 UnRef();
314
315 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
316
317 GdkVisual *visual = wxTheApp->GetGdkVisual();
318
319 m_refData = new wxBitmapRefData();
320
321 GdkBitmap *mask = (GdkBitmap*) NULL;
322
323 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits );
324
325 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
326
327 if (mask)
328 {
329 M_BMPDATA->m_mask = new wxMask();
330 M_BMPDATA->m_mask->m_bitmap = mask;
331 }
332
333 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
334
335 M_BMPDATA->m_bpp = visual->depth; // ?
336
337 return TRUE;
338 }
339
340 bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
341 {
342 UnRef();
343
344 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
345 wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") )
346
347 m_refData = new wxBitmapRefData();
348
349 // ------
350 // convertion to mono bitmap:
351 // ------
352 if (depth == 1)
353 {
354 int width = image.GetWidth();
355 int height = image.GetHeight();
356
357 SetHeight( height );
358 SetWidth( width );
359
360 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
361
362 SetDepth( 1 );
363
364 GdkVisual *visual = wxTheApp->GetGdkVisual();
365
366 // Create picture image
367
368 unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
369
370 GdkImage *data_image =
371 gdk_image_new_bitmap( visual, data_data, width, height );
372
373 // Create mask image
374
375 GdkImage *mask_image = (GdkImage*) NULL;
376
377 if (image.HasMask())
378 {
379 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
380
381 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
382
383 wxMask *mask = new wxMask();
384 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
385
386 SetMask( mask );
387 }
388
389 int r_mask = image.GetMaskRed();
390 int g_mask = image.GetMaskGreen();
391 int b_mask = image.GetMaskBlue();
392
393 unsigned char* data = image.GetData();
394
395 int index = 0;
396 for (int y = 0; y < height; y++)
397 {
398 for (int x = 0; x < width; x++)
399 {
400 int r = data[index];
401 index++;
402 int g = data[index];
403 index++;
404 int b = data[index];
405 index++;
406
407 if (image.HasMask())
408 {
409 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
410 gdk_image_put_pixel( mask_image, x, y, 1 );
411 else
412 gdk_image_put_pixel( mask_image, x, y, 0 );
413 }
414
415 if ((r == 255) && (b == 255) && (g == 255))
416 gdk_image_put_pixel( data_image, x, y, 1 );
417 else
418 gdk_image_put_pixel( data_image, x, y, 0 );
419
420 } // for
421 } // for
422
423 // Blit picture
424
425 GdkGC *data_gc = gdk_gc_new( GetBitmap() );
426
427 gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
428
429 gdk_image_destroy( data_image );
430 gdk_gc_unref( data_gc );
431
432 // Blit mask
433
434 if (image.HasMask())
435 {
436 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
437
438 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
439
440 gdk_image_destroy( mask_image );
441 gdk_gc_unref( mask_gc );
442 }
443 }
444
445 // ------
446 // convertion to colour bitmap:
447 // ------
448 else
449 {
450 int width = image.GetWidth();
451 int height = image.GetHeight();
452
453 SetHeight( height );
454 SetWidth( width );
455
456 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
457
458 GdkVisual *visual = wxTheApp->GetGdkVisual();
459
460 int bpp = visual->depth;
461
462 SetDepth( bpp );
463
464 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
465 if (bpp < 8) bpp = 8;
466
467 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
468 // visuals are not supported by GDK so we do these ourselves, too.
469 // 15-bit and 16-bit should actually work and 24-bit certainly does.
470 #ifdef __sgi
471 if (!image.HasMask() && (bpp > 16))
472 #else
473 if (!image.HasMask() && (bpp > 12))
474 #endif
475 {
476 static bool s_hasInitialized = FALSE;
477
478 if (!s_hasInitialized)
479 {
480 gdk_rgb_init();
481 s_hasInitialized = TRUE;
482 }
483
484 GdkGC *gc = gdk_gc_new( GetPixmap() );
485
486 gdk_draw_rgb_image( GetPixmap(),
487 gc,
488 0, 0,
489 width, height,
490 GDK_RGB_DITHER_NONE,
491 image.GetData(),
492 width*3 );
493
494 gdk_gc_unref( gc );
495 return TRUE;
496 }
497
498 // Create picture image
499
500 GdkImage *data_image =
501 gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
502
503 // Create mask image
504
505 GdkImage *mask_image = (GdkImage*) NULL;
506
507 if (image.HasMask())
508 {
509 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
510
511 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
512
513 wxMask *mask = new wxMask();
514 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
515
516 SetMask( mask );
517 }
518
519 // Render
520
521 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
522 byte_order b_o = RGB;
523
524 if (bpp > 8)
525 {
526 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
527 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG;
528 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
529 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
530 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
531 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
532 }
533
534 int r_mask = image.GetMaskRed();
535 int g_mask = image.GetMaskGreen();
536 int b_mask = image.GetMaskBlue();
537
538 unsigned char* data = image.GetData();
539
540 int index = 0;
541 for (int y = 0; y < height; y++)
542 {
543 for (int x = 0; x < width; x++)
544 {
545 int r = data[index];
546 index++;
547 int g = data[index];
548 index++;
549 int b = data[index];
550 index++;
551
552 if (image.HasMask())
553 {
554 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
555 gdk_image_put_pixel( mask_image, x, y, 1 );
556 else
557 gdk_image_put_pixel( mask_image, x, y, 0 );
558 }
559
560 switch (bpp)
561 {
562 case 8:
563 {
564 int pixel = -1;
565 if (wxTheApp->m_colorCube)
566 {
567 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
568 }
569 else
570 {
571 GdkColormap *cmap = gtk_widget_get_default_colormap();
572 GdkColor *colors = cmap->colors;
573 int max = 3 * (65536);
574
575 for (int i = 0; i < cmap->size; i++)
576 {
577 int rdiff = (r << 8) - colors[i].red;
578 int gdiff = (g << 8) - colors[i].green;
579 int bdiff = (b << 8) - colors[i].blue;
580 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
581 if (sum < max) { pixel = i; max = sum; }
582 }
583 }
584
585 gdk_image_put_pixel( data_image, x, y, pixel );
586
587 break;
588 }
589 case 12: // SGI only
590 {
591 guint32 pixel = 0;
592 switch (b_o)
593 {
594 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
595 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
596 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
597 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
598 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
599 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
600 }
601 gdk_image_put_pixel( data_image, x, y, pixel );
602 break;
603 }
604 case 15:
605 {
606 guint32 pixel = 0;
607 switch (b_o)
608 {
609 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
610 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
611 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
612 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
613 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
614 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
615 }
616 gdk_image_put_pixel( data_image, x, y, pixel );
617 break;
618 }
619 case 16:
620 {
621 // I actually don't know if for 16-bit displays, it is alway the green
622 // component or the second component which has 6 bits.
623 guint32 pixel = 0;
624 switch (b_o)
625 {
626 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
627 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
628 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
629 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
630 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
631 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
632 }
633 gdk_image_put_pixel( data_image, x, y, pixel );
634 break;
635 }
636 case 32:
637 case 24:
638 {
639 guint32 pixel = 0;
640 switch (b_o)
641 {
642 case RGB: pixel = (r << 16) | (g << 8) | b; break;
643 case RBG: pixel = (r << 16) | (b << 8) | g; break;
644 case BRG: pixel = (b << 16) | (r << 8) | g; break;
645 case BGR: pixel = (b << 16) | (g << 8) | r; break;
646 case GRB: pixel = (g << 16) | (r << 8) | b; break;
647 case GBR: pixel = (g << 16) | (b << 8) | r; break;
648 }
649 gdk_image_put_pixel( data_image, x, y, pixel );
650 }
651 default: break;
652 }
653 } // for
654 } // for
655
656 // Blit picture
657
658 GdkGC *data_gc = gdk_gc_new( GetPixmap() );
659
660 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
661
662 gdk_image_destroy( data_image );
663 gdk_gc_unref( data_gc );
664
665 // Blit mask
666
667 if (image.HasMask())
668 {
669 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
670
671 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
672
673 gdk_image_destroy( mask_image );
674 gdk_gc_unref( mask_gc );
675 }
676 }
677
678 return TRUE;
679 }
680
681 wxImage wxBitmap::ConvertToImage() const
682 {
683 wxImage image;
684
685 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
686
687 GdkImage *gdk_image = (GdkImage*) NULL;
688 if (GetPixmap())
689 {
690 gdk_image = gdk_image_get( GetPixmap(),
691 0, 0,
692 GetWidth(), GetHeight() );
693 } else
694 if (GetBitmap())
695 {
696 gdk_image = gdk_image_get( GetBitmap(),
697 0, 0,
698 GetWidth(), GetHeight() );
699 } else
700 {
701 wxFAIL_MSG( wxT("Ill-formed bitmap") );
702 }
703
704 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
705
706 image.Create( GetWidth(), GetHeight() );
707 char unsigned *data = image.GetData();
708
709 if (!data)
710 {
711 gdk_image_destroy( gdk_image );
712 wxFAIL_MSG( wxT("couldn't create image") );
713 return wxNullImage;
714 }
715
716 GdkImage *gdk_image_mask = (GdkImage*) NULL;
717 if (GetMask())
718 {
719 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
720 0, 0,
721 GetWidth(), GetHeight() );
722
723 image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
724 }
725
726 int bpp = -1;
727 int red_shift_right = 0;
728 int green_shift_right = 0;
729 int blue_shift_right = 0;
730 int red_shift_left = 0;
731 int green_shift_left = 0;
732 int blue_shift_left = 0;
733 bool use_shift = FALSE;
734
735 if (GetPixmap())
736 {
737 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
738 if (visual == NULL)
739 visual = wxTheApp->GetGdkVisual();
740
741 bpp = visual->depth;
742 if (bpp == 16) bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
743 red_shift_right = visual->red_shift;
744 red_shift_left = 8-visual->red_prec;
745 green_shift_right = visual->green_shift;
746 green_shift_left = 8-visual->green_prec;
747 blue_shift_right = visual->blue_shift;
748 blue_shift_left = 8-visual->blue_prec;
749
750 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
751 }
752 if (GetBitmap())
753 {
754 bpp = 1;
755 }
756
757
758 GdkColormap *cmap = gtk_widget_get_default_colormap();
759
760 long pos = 0;
761 for (int j = 0; j < GetHeight(); j++)
762 {
763 for (int i = 0; i < GetWidth(); i++)
764 {
765 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
766 if (bpp == 1)
767 {
768 if (pixel == 0)
769 {
770 data[pos] = 0;
771 data[pos+1] = 0;
772 data[pos+2] = 0;
773 }
774 else
775 {
776 data[pos] = 255;
777 data[pos+1] = 255;
778 data[pos+2] = 255;
779 }
780 }
781 else if (use_shift)
782 {
783 data[pos] = (pixel >> red_shift_right) << red_shift_left;
784 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
785 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
786 }
787 else if (cmap->colors)
788 {
789 data[pos] = cmap->colors[pixel].red >> 8;
790 data[pos+1] = cmap->colors[pixel].green >> 8;
791 data[pos+2] = cmap->colors[pixel].blue >> 8;
792 }
793 else
794 {
795 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
796 }
797
798 if (gdk_image_mask)
799 {
800 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
801 if (mask_pixel == 0)
802 {
803 data[pos] = 16;
804 data[pos+1] = 16;
805 data[pos+2] = 16;
806 }
807 }
808
809 pos += 3;
810 }
811 }
812
813 gdk_image_destroy( gdk_image );
814 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
815
816 return image;
817 }
818
819 wxBitmap::wxBitmap( const wxBitmap& bmp )
820 {
821 Ref( bmp );
822 }
823
824 wxBitmap::wxBitmap( const wxString &filename, int type )
825 {
826 LoadFile( filename, type );
827 }
828
829 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
830 {
831 m_refData = new wxBitmapRefData();
832
833 M_BMPDATA->m_mask = (wxMask *) NULL;
834 M_BMPDATA->m_bitmap =
835 gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
836 M_BMPDATA->m_width = width;
837 M_BMPDATA->m_height = height;
838 M_BMPDATA->m_bpp = 1;
839
840 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
841 }
842
843 wxBitmap::~wxBitmap()
844 {
845 }
846
847 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
848 {
849 if ( m_refData != bmp.m_refData )
850 Ref( bmp );
851
852 return *this;
853 }
854
855 bool wxBitmap::operator == ( const wxBitmap& bmp ) const
856 {
857 return m_refData == bmp.m_refData;
858 }
859
860 bool wxBitmap::operator != ( const wxBitmap& bmp ) const
861 {
862 return m_refData != bmp.m_refData;
863 }
864
865 bool wxBitmap::Ok() const
866 {
867 return (m_refData != NULL);
868 }
869
870 int wxBitmap::GetHeight() const
871 {
872 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
873
874 return M_BMPDATA->m_height;
875 }
876
877 int wxBitmap::GetWidth() const
878 {
879 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
880
881 return M_BMPDATA->m_width;
882 }
883
884 int wxBitmap::GetDepth() const
885 {
886 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
887
888 return M_BMPDATA->m_bpp;
889 }
890
891 wxMask *wxBitmap::GetMask() const
892 {
893 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
894
895 return M_BMPDATA->m_mask;
896 }
897
898 void wxBitmap::SetMask( wxMask *mask )
899 {
900 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
901
902 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
903
904 M_BMPDATA->m_mask = mask;
905 }
906
907 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
908 {
909 *this = icon;
910 return TRUE;
911 }
912
913 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
914 {
915 wxCHECK_MSG( Ok() &&
916 (rect.x >= 0) && (rect.y >= 0) &&
917 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
918 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
919
920 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
921 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
922
923 if (ret.GetPixmap())
924 {
925 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
926 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
927 gdk_gc_destroy( gc );
928 }
929 else
930 {
931 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
932 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
933 gdk_gc_destroy( gc );
934 }
935
936 if (GetMask())
937 {
938 wxMask *mask = new wxMask;
939 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
940
941 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
942 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
943 gdk_gc_destroy( gc );
944
945 ret.SetMask( mask );
946 }
947
948 return ret;
949 }
950
951 bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
952 {
953 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
954
955 // Try to save the bitmap via wxImage handlers:
956 {
957 wxImage image = ConvertToImage();
958 if (image.Ok()) return image.SaveFile( name, type );
959 }
960
961 return FALSE;
962 }
963
964 bool wxBitmap::LoadFile( const wxString &name, int type )
965 {
966 UnRef();
967
968 if (!wxFileExists(name)) return FALSE;
969
970 GdkVisual *visual = wxTheApp->GetGdkVisual();
971
972 if (type == wxBITMAP_TYPE_XPM)
973 {
974 m_refData = new wxBitmapRefData();
975
976 GdkBitmap *mask = (GdkBitmap*) NULL;
977
978 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( wxGetRootWindow()->window, &mask, NULL, name.fn_str() );
979
980 if (mask)
981 {
982 M_BMPDATA->m_mask = new wxMask();
983 M_BMPDATA->m_mask->m_bitmap = mask;
984 }
985
986 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
987
988 M_BMPDATA->m_bpp = visual->depth;
989 }
990 else // try if wxImage can load it
991 {
992 wxImage image;
993 if (!image.LoadFile( name, type )) return FALSE;
994 if (image.Ok())
995 *this = wxBitmap(image);
996 else return FALSE;
997 }
998
999 return TRUE;
1000 }
1001
1002 wxPalette *wxBitmap::GetPalette() const
1003 {
1004 if (!Ok()) return (wxPalette *) NULL;
1005
1006 return M_BMPDATA->m_palette;
1007 }
1008
1009 void wxBitmap::SetHeight( int height )
1010 {
1011 if (!m_refData) m_refData = new wxBitmapRefData();
1012
1013 M_BMPDATA->m_height = height;
1014 }
1015
1016 void wxBitmap::SetWidth( int width )
1017 {
1018 if (!m_refData) m_refData = new wxBitmapRefData();
1019
1020 M_BMPDATA->m_width = width;
1021 }
1022
1023 void wxBitmap::SetDepth( int depth )
1024 {
1025 if (!m_refData) m_refData = new wxBitmapRefData();
1026
1027 M_BMPDATA->m_bpp = depth;
1028 }
1029
1030 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1031 {
1032 if (!m_refData) m_refData = new wxBitmapRefData();
1033
1034 M_BMPDATA->m_pixmap = pixmap;
1035 }
1036
1037 void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1038 {
1039 if (!m_refData) m_refData = new wxBitmapRefData();
1040
1041 M_BMPDATA->m_bitmap = bitmap;
1042 }
1043
1044 GdkPixmap *wxBitmap::GetPixmap() const
1045 {
1046 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
1047
1048 return M_BMPDATA->m_pixmap;
1049 }
1050
1051 GdkBitmap *wxBitmap::GetBitmap() const
1052 {
1053 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
1054
1055 return M_BMPDATA->m_bitmap;
1056 }