]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bitmap.cpp
(incomplete) compilation fix
[wxWidgets.git] / src / gtk1 / 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 #include <gdk/gdkrgb.h>
37 #endif // GTK+ 2.0/1.2
38
39 extern void gdk_wx_draw_bitmap (GdkDrawable *drawable,
40 GdkGC *gc,
41 GdkDrawable *src,
42 gint xsrc,
43 gint ysrc,
44 gint xdest,
45 gint ydest,
46 gint width,
47 gint height);
48
49 //-----------------------------------------------------------------------------
50 // data
51 //-----------------------------------------------------------------------------
52
53 extern GtkWidget *wxGetRootWindow();
54
55 //-----------------------------------------------------------------------------
56 // wxMask
57 //-----------------------------------------------------------------------------
58
59 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
60
61 wxMask::wxMask()
62 {
63 m_bitmap = (GdkBitmap *) NULL;
64 }
65
66 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
67 {
68 m_bitmap = (GdkBitmap *) NULL;
69 Create( bitmap, colour );
70 }
71
72 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
73 {
74 m_bitmap = (GdkBitmap *) NULL;
75 Create( bitmap, paletteIndex );
76 }
77
78 wxMask::wxMask( const wxBitmap& bitmap )
79 {
80 m_bitmap = (GdkBitmap *) NULL;
81 Create( bitmap );
82 }
83
84 wxMask::~wxMask()
85 {
86 if (m_bitmap)
87 gdk_bitmap_unref( m_bitmap );
88 }
89
90 bool wxMask::Create( const wxBitmap& bitmap,
91 const wxColour& colour )
92 {
93 if (m_bitmap)
94 {
95 gdk_bitmap_unref( m_bitmap );
96 m_bitmap = (GdkBitmap*) NULL;
97 }
98
99 wxImage image = bitmap.ConvertToImage();
100 if (!image.Ok()) return FALSE;
101
102 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
103 GdkGC *gc = gdk_gc_new( m_bitmap );
104
105 GdkColor color;
106 color.red = 65000;
107 color.green = 65000;
108 color.blue = 65000;
109 color.pixel = 1;
110 gdk_gc_set_foreground( gc, &color );
111 gdk_gc_set_fill( gc, GDK_SOLID );
112 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
113
114 unsigned char *data = image.GetData();
115 int index = 0;
116
117 unsigned char red = colour.Red();
118 unsigned char green = colour.Green();
119 unsigned char blue = colour.Blue();
120
121 GdkVisual *visual = wxTheApp->GetGdkVisual();
122
123 int bpp = visual->depth;
124 if ((bpp == 16) && (visual->red_mask != 0xf800))
125 bpp = 15;
126 if (bpp == 15)
127 {
128 red = red & 0xf8;
129 green = green & 0xf8;
130 blue = blue & 0xf8;
131 }
132 else if (bpp == 16)
133 {
134 red = red & 0xf8;
135 green = green & 0xfc;
136 blue = blue & 0xf8;
137 }
138 else if (bpp == 12)
139 {
140 red = red & 0xf0;
141 green = green & 0xf0;
142 blue = blue & 0xf0;
143 }
144
145 color.red = 0;
146 color.green = 0;
147 color.blue = 0;
148 color.pixel = 0;
149 gdk_gc_set_foreground( gc, &color );
150
151 for (int j = 0; j < image.GetHeight(); j++)
152 {
153 int start_x = -1;
154 int i;
155 for (i = 0; i < image.GetWidth(); i++)
156 {
157 if ((data[index] == red) &&
158 (data[index+1] == green) &&
159 (data[index+2] == blue))
160 {
161 if (start_x == -1)
162 start_x = i;
163 }
164 else
165 {
166 if (start_x != -1)
167 {
168 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
169 start_x = -1;
170 }
171 }
172 index += 3;
173 }
174 if (start_x != -1)
175 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
176 }
177
178 gdk_gc_unref( gc );
179
180 return TRUE;
181 }
182
183 bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
184 {
185 unsigned char r,g,b;
186 wxPalette *pal = bitmap.GetPalette();
187
188 wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") );
189
190 pal->GetRGB(paletteIndex, &r, &g, &b);
191
192 return Create(bitmap, wxColour(r, g, b));
193 }
194
195 bool wxMask::Create( const wxBitmap& bitmap )
196 {
197 if (m_bitmap)
198 {
199 gdk_bitmap_unref( m_bitmap );
200 m_bitmap = (GdkBitmap*) NULL;
201 }
202
203 if (!bitmap.Ok()) return FALSE;
204
205 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
206
207 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
208
209 if (!m_bitmap) return FALSE;
210
211 GdkGC *gc = gdk_gc_new( m_bitmap );
212
213 gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
214
215 gdk_gc_unref( gc );
216
217 return TRUE;
218 }
219
220 GdkBitmap *wxMask::GetBitmap() const
221 {
222 return m_bitmap;
223 }
224
225 //-----------------------------------------------------------------------------
226 // wxBitmap
227 //-----------------------------------------------------------------------------
228
229 class wxBitmapRefData: public wxObjectRefData
230 {
231 public:
232 wxBitmapRefData();
233 ~wxBitmapRefData();
234
235 GdkPixmap *m_pixmap;
236 GdkBitmap *m_bitmap;
237 wxMask *m_mask;
238 int m_width;
239 int m_height;
240 int m_bpp;
241 wxPalette *m_palette;
242 };
243
244 wxBitmapRefData::wxBitmapRefData()
245 {
246 m_pixmap = (GdkPixmap *) NULL;
247 m_bitmap = (GdkBitmap *) NULL;
248 m_mask = (wxMask *) NULL;
249 m_width = 0;
250 m_height = 0;
251 m_bpp = 0;
252 m_palette = (wxPalette *) NULL;
253 }
254
255 wxBitmapRefData::~wxBitmapRefData()
256 {
257 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
258 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
259 if (m_mask) delete m_mask;
260 if (m_palette) delete m_palette;
261 }
262
263 //-----------------------------------------------------------------------------
264
265 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
266
267 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
268
269 wxBitmap::wxBitmap()
270 {
271 }
272
273 wxBitmap::wxBitmap( int width, int height, int depth )
274 {
275 Create( width, height, depth );
276 }
277
278 bool wxBitmap::Create( int width, int height, int depth )
279 {
280 UnRef();
281
282 wxCHECK_MSG( (width > 0) && (height > 0), FALSE, wxT("invalid bitmap size") )
283
284 GdkVisual *visual = wxTheApp->GetGdkVisual();
285
286 if (depth == -1) depth = visual->depth;
287
288 wxCHECK_MSG( (depth == visual->depth) ||
289 (depth == 1), FALSE, wxT("invalid bitmap depth") )
290
291 m_refData = new wxBitmapRefData();
292 M_BMPDATA->m_mask = (wxMask *) NULL;
293 M_BMPDATA->m_width = width;
294 M_BMPDATA->m_height = height;
295 if (depth == 1)
296 {
297 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
298 M_BMPDATA->m_bpp = 1;
299 }
300 else
301 {
302 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth );
303 M_BMPDATA->m_bpp = visual->depth;
304 }
305
306 return Ok();
307 }
308
309 bool wxBitmap::CreateFromXpm( const char **bits )
310 {
311 UnRef();
312
313 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
314
315 GdkVisual *visual = wxTheApp->GetGdkVisual();
316
317 m_refData = new wxBitmapRefData();
318
319 GdkBitmap *mask = (GdkBitmap*) NULL;
320
321 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits );
322
323 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
324
325 if (mask)
326 {
327 M_BMPDATA->m_mask = new wxMask();
328 M_BMPDATA->m_mask->m_bitmap = mask;
329 }
330
331 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
332
333 M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ?
334
335 return TRUE;
336 }
337
338 bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
339 {
340 UnRef();
341
342 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
343 wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") )
344
345 m_refData = new wxBitmapRefData();
346
347 // ------
348 // conversion to mono bitmap:
349 // ------
350 if (depth == 1)
351 {
352 int width = image.GetWidth();
353 int height = image.GetHeight();
354
355 SetHeight( height );
356 SetWidth( width );
357
358 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
359
360 SetDepth( 1 );
361
362 GdkVisual *visual = wxTheApp->GetGdkVisual();
363
364 // Create picture image
365
366 unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
367
368 GdkImage *data_image =
369 gdk_image_new_bitmap( visual, data_data, width, height );
370
371 // Create mask image
372
373 GdkImage *mask_image = (GdkImage*) NULL;
374
375 if (image.HasMask())
376 {
377 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
378
379 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
380
381 wxMask *mask = new wxMask();
382 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
383
384 SetMask( mask );
385 }
386
387 int r_mask = image.GetMaskRed();
388 int g_mask = image.GetMaskGreen();
389 int b_mask = image.GetMaskBlue();
390
391 unsigned char* data = image.GetData();
392
393 int index = 0;
394 for (int y = 0; y < height; y++)
395 {
396 for (int x = 0; x < width; x++)
397 {
398 int r = data[index];
399 index++;
400 int g = data[index];
401 index++;
402 int b = data[index];
403 index++;
404
405 if (image.HasMask())
406 {
407 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
408 gdk_image_put_pixel( mask_image, x, y, 1 );
409 else
410 gdk_image_put_pixel( mask_image, x, y, 0 );
411 }
412
413 if ((r == 255) && (b == 255) && (g == 255))
414 gdk_image_put_pixel( data_image, x, y, 1 );
415 else
416 gdk_image_put_pixel( data_image, x, y, 0 );
417
418 } // for
419 } // for
420
421 // Blit picture
422
423 GdkGC *data_gc = gdk_gc_new( GetBitmap() );
424
425 gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
426
427 gdk_image_destroy( data_image );
428 gdk_gc_unref( data_gc );
429
430 // Blit mask
431
432 if (image.HasMask())
433 {
434 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
435
436 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
437
438 gdk_image_destroy( mask_image );
439 gdk_gc_unref( mask_gc );
440 }
441 }
442
443 // ------
444 // conversion to colour bitmap:
445 // ------
446 else
447 {
448 int width = image.GetWidth();
449 int height = image.GetHeight();
450
451 SetHeight( height );
452 SetWidth( width );
453
454 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
455
456 GdkVisual *visual = wxTheApp->GetGdkVisual();
457
458 int bpp = visual->depth;
459
460 SetDepth( bpp );
461
462 if ((bpp == 16) && (visual->red_mask != 0xf800))
463 bpp = 15;
464 else if (bpp < 8)
465 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 // the colour used as transparent one in wxImage and the one it is replaced
684 // with when it really occurs in the bitmap
685 static const int MASK_RED = 1;
686 static const int MASK_GREEN = 2;
687 static const int MASK_BLUE = 3;
688 static const int MASK_BLUE_REPLACEMENT = 2;
689
690 wxImage image;
691
692 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
693
694 GdkImage *gdk_image = (GdkImage*) NULL;
695 if (GetPixmap())
696 {
697 gdk_image = gdk_image_get( GetPixmap(),
698 0, 0,
699 GetWidth(), GetHeight() );
700 }
701 else if (GetBitmap())
702 {
703 gdk_image = gdk_image_get( GetBitmap(),
704 0, 0,
705 GetWidth(), GetHeight() );
706 }
707 else
708 {
709 wxFAIL_MSG( wxT("Ill-formed bitmap") );
710 }
711
712 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
713
714 image.Create( GetWidth(), GetHeight() );
715 char unsigned *data = image.GetData();
716
717 if (!data)
718 {
719 gdk_image_destroy( gdk_image );
720 wxFAIL_MSG( wxT("couldn't create image") );
721 return wxNullImage;
722 }
723
724 GdkImage *gdk_image_mask = (GdkImage*) NULL;
725 if (GetMask())
726 {
727 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
728 0, 0,
729 GetWidth(), GetHeight() );
730
731 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
732 }
733
734 int bpp = -1;
735 int red_shift_right = 0;
736 int green_shift_right = 0;
737 int blue_shift_right = 0;
738 int red_shift_left = 0;
739 int green_shift_left = 0;
740 int blue_shift_left = 0;
741 bool use_shift = FALSE;
742
743 if (GetPixmap())
744 {
745 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
746 if (visual == NULL)
747 visual = wxTheApp->GetGdkVisual();
748
749 bpp = visual->depth;
750 if (bpp == 16)
751 bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
752 red_shift_right = visual->red_shift;
753 red_shift_left = 8-visual->red_prec;
754 green_shift_right = visual->green_shift;
755 green_shift_left = 8-visual->green_prec;
756 blue_shift_right = visual->blue_shift;
757 blue_shift_left = 8-visual->blue_prec;
758
759 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
760 }
761 if (GetBitmap())
762 {
763 bpp = 1;
764 }
765
766
767 GdkColormap *cmap = gtk_widget_get_default_colormap();
768
769 long pos = 0;
770 for (int j = 0; j < GetHeight(); j++)
771 {
772 for (int i = 0; i < GetWidth(); i++)
773 {
774 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
775 if (bpp == 1)
776 {
777 if (pixel == 0)
778 {
779 data[pos] = 0;
780 data[pos+1] = 0;
781 data[pos+2] = 0;
782 }
783 else
784 {
785 data[pos] = 255;
786 data[pos+1] = 255;
787 data[pos+2] = 255;
788 }
789 }
790 else if (use_shift)
791 {
792 data[pos] = (pixel >> red_shift_right) << red_shift_left;
793 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
794 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
795 }
796 else if (cmap->colors)
797 {
798 data[pos] = cmap->colors[pixel].red >> 8;
799 data[pos+1] = cmap->colors[pixel].green >> 8;
800 data[pos+2] = cmap->colors[pixel].blue >> 8;
801 }
802 else
803 {
804 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
805 }
806
807 if (gdk_image_mask)
808 {
809 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
810 if (mask_pixel == 0)
811 {
812 data[pos] = MASK_RED;
813 data[pos+1] = MASK_GREEN;
814 data[pos+2] = MASK_BLUE;
815 }
816 else if ( data[pos] == MASK_RED &&
817 data[pos+1] == MASK_GREEN &&
818 data[pos+2] == MASK_BLUE )
819 {
820 data[pos+2] = MASK_BLUE_REPLACEMENT;
821 }
822 }
823
824 pos += 3;
825 }
826 }
827
828 gdk_image_destroy( gdk_image );
829 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
830
831 return image;
832 }
833
834 wxBitmap::wxBitmap( const wxBitmap& bmp )
835 : wxGDIObject()
836 {
837 Ref( bmp );
838 }
839
840 wxBitmap::wxBitmap( const wxString &filename, int type )
841 {
842 LoadFile( filename, type );
843 }
844
845 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
846 {
847 m_refData = new wxBitmapRefData();
848
849 M_BMPDATA->m_mask = (wxMask *) NULL;
850 M_BMPDATA->m_bitmap =
851 gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
852 M_BMPDATA->m_width = width;
853 M_BMPDATA->m_height = height;
854 M_BMPDATA->m_bpp = 1;
855
856 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
857 }
858
859 wxBitmap::~wxBitmap()
860 {
861 }
862
863 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
864 {
865 if ( m_refData != bmp.m_refData )
866 Ref( bmp );
867
868 return *this;
869 }
870
871 bool wxBitmap::operator == ( const wxBitmap& bmp ) const
872 {
873 return m_refData == bmp.m_refData;
874 }
875
876 bool wxBitmap::operator != ( const wxBitmap& bmp ) const
877 {
878 return m_refData != bmp.m_refData;
879 }
880
881 bool wxBitmap::Ok() const
882 {
883 return (m_refData != NULL);
884 }
885
886 int wxBitmap::GetHeight() const
887 {
888 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
889
890 return M_BMPDATA->m_height;
891 }
892
893 int wxBitmap::GetWidth() const
894 {
895 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
896
897 return M_BMPDATA->m_width;
898 }
899
900 int wxBitmap::GetDepth() const
901 {
902 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
903
904 return M_BMPDATA->m_bpp;
905 }
906
907 wxMask *wxBitmap::GetMask() const
908 {
909 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
910
911 return M_BMPDATA->m_mask;
912 }
913
914 void wxBitmap::SetMask( wxMask *mask )
915 {
916 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
917
918 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
919
920 M_BMPDATA->m_mask = mask;
921 }
922
923 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
924 {
925 *this = icon;
926 return TRUE;
927 }
928
929 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
930 {
931 wxCHECK_MSG( Ok() &&
932 (rect.x >= 0) && (rect.y >= 0) &&
933 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
934 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
935
936 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
937 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
938
939 if (ret.GetPixmap())
940 {
941 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
942 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
943 gdk_gc_destroy( gc );
944 }
945 else
946 {
947 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
948 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
949 gdk_gc_destroy( gc );
950 }
951
952 if (GetMask())
953 {
954 wxMask *mask = new wxMask;
955 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
956
957 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
958 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
959 gdk_gc_destroy( gc );
960
961 ret.SetMask( mask );
962 }
963
964 return ret;
965 }
966
967 bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
968 {
969 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
970
971 // Try to save the bitmap via wxImage handlers:
972 {
973 wxImage image = ConvertToImage();
974 if (image.Ok()) return image.SaveFile( name, type );
975 }
976
977 return FALSE;
978 }
979
980 bool wxBitmap::LoadFile( const wxString &name, int type )
981 {
982 UnRef();
983
984 if (!wxFileExists(name)) return FALSE;
985
986 GdkVisual *visual = wxTheApp->GetGdkVisual();
987
988 if (type == wxBITMAP_TYPE_XPM)
989 {
990 m_refData = new wxBitmapRefData();
991
992 GdkBitmap *mask = (GdkBitmap*) NULL;
993
994 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( wxGetRootWindow()->window, &mask, NULL, name.fn_str() );
995
996 if (mask)
997 {
998 M_BMPDATA->m_mask = new wxMask();
999 M_BMPDATA->m_mask->m_bitmap = mask;
1000 }
1001
1002 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
1003
1004 M_BMPDATA->m_bpp = visual->depth;
1005 }
1006 else // try if wxImage can load it
1007 {
1008 wxImage image;
1009 if (!image.LoadFile( name, type )) return FALSE;
1010 if (image.Ok())
1011 *this = wxBitmap(image);
1012 else return FALSE;
1013 }
1014
1015 return TRUE;
1016 }
1017
1018 wxPalette *wxBitmap::GetPalette() const
1019 {
1020 if (!Ok()) return (wxPalette *) NULL;
1021
1022 return M_BMPDATA->m_palette;
1023 }
1024
1025 void wxBitmap::SetHeight( int height )
1026 {
1027 if (!m_refData) m_refData = new wxBitmapRefData();
1028
1029 M_BMPDATA->m_height = height;
1030 }
1031
1032 void wxBitmap::SetWidth( int width )
1033 {
1034 if (!m_refData) m_refData = new wxBitmapRefData();
1035
1036 M_BMPDATA->m_width = width;
1037 }
1038
1039 void wxBitmap::SetDepth( int depth )
1040 {
1041 if (!m_refData) m_refData = new wxBitmapRefData();
1042
1043 M_BMPDATA->m_bpp = depth;
1044 }
1045
1046 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1047 {
1048 if (!m_refData) m_refData = new wxBitmapRefData();
1049
1050 M_BMPDATA->m_pixmap = pixmap;
1051 }
1052
1053 void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1054 {
1055 if (!m_refData) m_refData = new wxBitmapRefData();
1056
1057 M_BMPDATA->m_bitmap = bitmap;
1058 }
1059
1060 GdkPixmap *wxBitmap::GetPixmap() const
1061 {
1062 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
1063
1064 return M_BMPDATA->m_pixmap;
1065 }
1066
1067 GdkBitmap *wxBitmap::GetBitmap() const
1068 {
1069 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
1070
1071 return M_BMPDATA->m_bitmap;
1072 }