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