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