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