]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/bitmap.cpp
fix memory leak (coverity checker CID 53)
[wxWidgets.git] / src / gtk1 / bitmap.cpp
... / ...
CommitLineData
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
31extern
32void 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
46extern GtkWidget *wxGetRootWindow();
47
48//-----------------------------------------------------------------------------
49// wxMask
50//-----------------------------------------------------------------------------
51
52IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
53
54wxMask::wxMask()
55{
56 m_bitmap = (GdkBitmap *) NULL;
57}
58
59wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
60{
61 m_bitmap = (GdkBitmap *) NULL;
62 Create( bitmap, colour );
63}
64
65#if wxUSE_PALETTE
66wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
67{
68 m_bitmap = (GdkBitmap *) NULL;
69 Create( bitmap, paletteIndex );
70}
71#endif // wxUSE_PALETTE
72
73wxMask::wxMask( const wxBitmap& bitmap )
74{
75 m_bitmap = (GdkBitmap *) NULL;
76 Create( bitmap );
77}
78
79wxMask::~wxMask()
80{
81 if (m_bitmap)
82 gdk_bitmap_unref( m_bitmap );
83}
84
85bool 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
179bool 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
192bool 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
217GdkBitmap *wxMask::GetBitmap() const
218{
219 return m_bitmap;
220}
221
222//-----------------------------------------------------------------------------
223// wxBitmap
224//-----------------------------------------------------------------------------
225
226class wxBitmapRefData: public wxObjectRefData
227{
228public:
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
241wxBitmapRefData::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
252wxBitmapRefData::~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
268IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
269
270wxBitmap::wxBitmap()
271{
272}
273
274wxBitmap::wxBitmap( int width, int height, int depth )
275{
276 Create( width, height, depth );
277}
278
279bool 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
314bool 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
343wxBitmap 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
526bool 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:
549bool 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:
649bool 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
888wxImage 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
1041wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
1042{
1043 LoadFile( filename, type );
1044}
1045
1046wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
1047{
1048 if ( width > 0 && height > 0 )
1049 {
1050 m_refData = new wxBitmapRefData();
1051
1052 M_BMPDATA->m_mask = (wxMask *) NULL;
1053 M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data
1054 (
1055 wxGetRootWindow()->window,
1056 (gchar *) bits,
1057 width,
1058 height
1059 );
1060 M_BMPDATA->m_width = width;
1061 M_BMPDATA->m_height = height;
1062 M_BMPDATA->m_bpp = 1;
1063
1064 wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
1065 }
1066}
1067
1068wxBitmap::~wxBitmap()
1069{
1070}
1071
1072bool wxBitmap::operator == ( const wxBitmap& bmp ) const
1073{
1074 return m_refData == bmp.m_refData;
1075}
1076
1077bool wxBitmap::operator != ( const wxBitmap& bmp ) const
1078{
1079 return m_refData != bmp.m_refData;
1080}
1081
1082bool wxBitmap::Ok() const
1083{
1084 return (m_refData != NULL) &&
1085 (M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap);
1086}
1087
1088int wxBitmap::GetHeight() const
1089{
1090 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1091
1092 return M_BMPDATA->m_height;
1093}
1094
1095int wxBitmap::GetWidth() const
1096{
1097 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1098
1099 return M_BMPDATA->m_width;
1100}
1101
1102int wxBitmap::GetDepth() const
1103{
1104 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1105
1106 return M_BMPDATA->m_bpp;
1107}
1108
1109wxMask *wxBitmap::GetMask() const
1110{
1111 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1112
1113 return M_BMPDATA->m_mask;
1114}
1115
1116void wxBitmap::SetMask( wxMask *mask )
1117{
1118 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1119
1120 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
1121
1122 M_BMPDATA->m_mask = mask;
1123}
1124
1125bool wxBitmap::CopyFromIcon(const wxIcon& icon)
1126{
1127 *this = icon;
1128 return true;
1129}
1130
1131wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1132{
1133 wxCHECK_MSG( Ok() &&
1134 (rect.x >= 0) && (rect.y >= 0) &&
1135 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
1136 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
1137
1138 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
1139 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1140
1141 if (ret.GetPixmap())
1142 {
1143 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
1144 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1145 gdk_gc_destroy( gc );
1146 }
1147 else
1148 {
1149 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
1150 GdkColor col;
1151 col.pixel = 0xFFFFFF;
1152 gdk_gc_set_foreground( gc, &col );
1153 col.pixel = 0;
1154 gdk_gc_set_background( gc, &col );
1155 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1156 gdk_gc_destroy( gc );
1157 }
1158
1159 if (GetMask())
1160 {
1161 wxMask *mask = new wxMask;
1162 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
1163
1164 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
1165 GdkColor col;
1166 col.pixel = 0xFFFFFF;
1167 gdk_gc_set_foreground( gc, &col );
1168 col.pixel = 0;
1169 gdk_gc_set_background( gc, &col );
1170 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height );
1171 gdk_gc_destroy( gc );
1172
1173 ret.SetMask( mask );
1174 }
1175
1176 return ret;
1177}
1178
1179bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
1180{
1181 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1182
1183 // Try to save the bitmap via wxImage handlers:
1184 {
1185 wxImage image = ConvertToImage();
1186 if (image.Ok()) return image.SaveFile( name, type );
1187 }
1188
1189 return false;
1190}
1191
1192bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
1193{
1194 UnRef();
1195
1196 if (!wxFileExists(name))
1197 return false;
1198
1199 GdkVisual *visual = wxTheApp->GetGdkVisual();
1200
1201 if (type == wxBITMAP_TYPE_XPM)
1202 {
1203 m_refData = new wxBitmapRefData();
1204
1205 GdkBitmap *mask = (GdkBitmap*) NULL;
1206
1207 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm
1208 (
1209 wxGetRootWindow()->window,
1210 &mask,
1211 NULL,
1212 name.fn_str()
1213 );
1214
1215 if (mask)
1216 {
1217 M_BMPDATA->m_mask = new wxMask();
1218 M_BMPDATA->m_mask->m_bitmap = mask;
1219 }
1220
1221 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
1222
1223 M_BMPDATA->m_bpp = visual->depth;
1224 }
1225 else // try if wxImage can load it
1226 {
1227 wxImage image;
1228 if ( !image.LoadFile( name, type ) || !image.Ok() )
1229 return false;
1230
1231 *this = wxBitmap(image);
1232 }
1233
1234 return true;
1235}
1236
1237#if wxUSE_PALETTE
1238wxPalette *wxBitmap::GetPalette() const
1239{
1240 if (!Ok())
1241 return (wxPalette *) NULL;
1242
1243 return M_BMPDATA->m_palette;
1244}
1245
1246void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
1247{
1248 // TODO
1249}
1250#endif // wxUSE_PALETTE
1251
1252void wxBitmap::SetHeight( int height )
1253{
1254 if (!m_refData)
1255 m_refData = new wxBitmapRefData();
1256
1257 M_BMPDATA->m_height = height;
1258}
1259
1260void wxBitmap::SetWidth( int width )
1261{
1262 if (!m_refData)
1263 m_refData = new wxBitmapRefData();
1264
1265 M_BMPDATA->m_width = width;
1266}
1267
1268void wxBitmap::SetDepth( int depth )
1269{
1270 if (!m_refData)
1271 m_refData = new wxBitmapRefData();
1272
1273 M_BMPDATA->m_bpp = depth;
1274}
1275
1276void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1277{
1278 if (!m_refData)
1279 m_refData = new wxBitmapRefData();
1280
1281 M_BMPDATA->m_pixmap = pixmap;
1282}
1283
1284void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1285{
1286 if (!m_refData)
1287 m_refData = new wxBitmapRefData();
1288
1289 M_BMPDATA->m_bitmap = bitmap;
1290}
1291
1292GdkPixmap *wxBitmap::GetPixmap() const
1293{
1294 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
1295
1296 return M_BMPDATA->m_pixmap;
1297}
1298
1299bool wxBitmap::HasPixmap() const
1300{
1301 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1302
1303 return M_BMPDATA->m_pixmap != NULL;
1304}
1305
1306GdkBitmap *wxBitmap::GetBitmap() const
1307{
1308 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
1309
1310 return M_BMPDATA->m_bitmap;
1311}
1312
1313void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1314{
1315 return NULL;
1316}
1317
1318void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
1319{
1320}
1321
1322
1323bool wxBitmap::HasAlpha() const
1324{
1325 return false;
1326}
1327
1328void wxBitmap::UseAlpha()
1329{
1330}
1331
1332//-----------------------------------------------------------------------------
1333// wxBitmapHandler
1334//-----------------------------------------------------------------------------
1335
1336IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler,wxBitmapHandlerBase)
1337
1338wxBitmapHandler::~wxBitmapHandler()
1339{
1340}
1341
1342bool wxBitmapHandler::Create(wxBitmap * WXUNUSED(bitmap),
1343 void * WXUNUSED(data),
1344 long WXUNUSED(type),
1345 int WXUNUSED(width),
1346 int WXUNUSED(height),
1347 int WXUNUSED(depth))
1348{
1349 wxFAIL_MSG( _T("not implemented") );
1350
1351 return false;
1352}
1353
1354bool wxBitmapHandler::LoadFile(wxBitmap * WXUNUSED(bitmap),
1355 const wxString& WXUNUSED(name),
1356 long WXUNUSED(flags),
1357 int WXUNUSED(desiredWidth),
1358 int WXUNUSED(desiredHeight))
1359{
1360 wxFAIL_MSG( _T("not implemented") );
1361
1362 return false;
1363}
1364
1365bool wxBitmapHandler::SaveFile(const wxBitmap * WXUNUSED(bitmap),
1366 const wxString& WXUNUSED(name),
1367 int WXUNUSED(type),
1368 const wxPalette * WXUNUSED(palette))
1369{
1370 wxFAIL_MSG( _T("not implemented") );
1371
1372 return false;
1373}
1374
1375/* static */ void wxBitmap::InitStandardHandlers()
1376{
1377 // TODO: Insert handler based on GdkPixbufs handler later
1378}