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