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