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