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