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