]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/bitmap.cpp
New default configuration is debug/static
[wxWidgets.git] / src / gtk1 / bitmap.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11#pragma implementation "bitmap.h"
12#endif
13
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#include "wx/defs.h"
18
19#include "wx/palette.h"
20#include "wx/bitmap.h"
21#include "wx/icon.h"
22#include "wx/filefn.h"
23#include "wx/image.h"
24#include "wx/dcmemory.h"
25#include "wx/app.h"
26
27#ifdef __WXGTK20__
28 // need this to get gdk_image_new_bitmap()
29 #define GDK_ENABLE_BROKEN
30#endif
31
32#include <gdk/gdk.h>
33#include <gtk/gtk.h>
34#include <gdk/gdkx.h>
35
36#ifdef __WXGTK20__
37 #include <gdk/gdkimage.h>
38#else // GTK+ 1.2
39 #include <gdk/gdkrgb.h>
40#endif // GTK+ 2.0/1.2
41
42extern void gdk_wx_draw_bitmap (GdkDrawable *drawable,
43 GdkGC *gc,
44 GdkDrawable *src,
45 gint xsrc,
46 gint ysrc,
47 gint xdest,
48 gint ydest,
49 gint width,
50 gint height);
51
52//-----------------------------------------------------------------------------
53// data
54//-----------------------------------------------------------------------------
55
56extern GtkWidget *wxGetRootWindow();
57
58//-----------------------------------------------------------------------------
59// wxMask
60//-----------------------------------------------------------------------------
61
62IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
63
64wxMask::wxMask()
65{
66 m_bitmap = (GdkBitmap *) NULL;
67}
68
69wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
70{
71 m_bitmap = (GdkBitmap *) NULL;
72 Create( bitmap, colour );
73}
74
75wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
76{
77 m_bitmap = (GdkBitmap *) NULL;
78 Create( bitmap, paletteIndex );
79}
80
81wxMask::wxMask( const wxBitmap& bitmap )
82{
83 m_bitmap = (GdkBitmap *) NULL;
84 Create( bitmap );
85}
86
87wxMask::~wxMask()
88{
89 if (m_bitmap)
90 gdk_bitmap_unref( m_bitmap );
91}
92
93bool wxMask::Create( const wxBitmap& bitmap,
94 const wxColour& colour )
95{
96 if (m_bitmap)
97 {
98 gdk_bitmap_unref( m_bitmap );
99 m_bitmap = (GdkBitmap*) NULL;
100 }
101
102 wxImage image = bitmap.ConvertToImage();
103 if (!image.Ok()) return FALSE;
104
105 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
106 GdkGC *gc = gdk_gc_new( m_bitmap );
107
108 GdkColor color;
109 color.red = 65000;
110 color.green = 65000;
111 color.blue = 65000;
112 color.pixel = 1;
113 gdk_gc_set_foreground( gc, &color );
114 gdk_gc_set_fill( gc, GDK_SOLID );
115 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
116
117 unsigned char *data = image.GetData();
118 int index = 0;
119
120 unsigned char red = colour.Red();
121 unsigned char green = colour.Green();
122 unsigned char blue = colour.Blue();
123
124 GdkVisual *visual = wxTheApp->GetGdkVisual();
125
126 int bpp = visual->depth;
127 if ((bpp == 16) && (visual->red_mask != 0xf800))
128 bpp = 15;
129 if (bpp == 15)
130 {
131 red = red & 0xf8;
132 green = green & 0xf8;
133 blue = blue & 0xf8;
134 }
135 else if (bpp == 16)
136 {
137 red = red & 0xf8;
138 green = green & 0xfc;
139 blue = blue & 0xf8;
140 }
141 else if (bpp == 12)
142 {
143 red = red & 0xf0;
144 green = green & 0xf0;
145 blue = blue & 0xf0;
146 }
147
148 color.red = 0;
149 color.green = 0;
150 color.blue = 0;
151 color.pixel = 0;
152 gdk_gc_set_foreground( gc, &color );
153
154 for (int j = 0; j < image.GetHeight(); j++)
155 {
156 int start_x = -1;
157 int i;
158 for (i = 0; i < image.GetWidth(); i++)
159 {
160 if ((data[index] == red) &&
161 (data[index+1] == green) &&
162 (data[index+2] == blue))
163 {
164 if (start_x == -1)
165 start_x = i;
166 }
167 else
168 {
169 if (start_x != -1)
170 {
171 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
172 start_x = -1;
173 }
174 }
175 index += 3;
176 }
177 if (start_x != -1)
178 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
179 }
180
181 gdk_gc_unref( gc );
182
183 return TRUE;
184}
185
186bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
187{
188 unsigned char r,g,b;
189 wxPalette *pal = bitmap.GetPalette();
190
191 wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") );
192
193 pal->GetRGB(paletteIndex, &r, &g, &b);
194
195 return Create(bitmap, wxColour(r, g, b));
196}
197
198bool wxMask::Create( const wxBitmap& bitmap )
199{
200 if (m_bitmap)
201 {
202 gdk_bitmap_unref( m_bitmap );
203 m_bitmap = (GdkBitmap*) NULL;
204 }
205
206 if (!bitmap.Ok()) return FALSE;
207
208 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
209
210 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
211
212 if (!m_bitmap) return FALSE;
213
214 GdkGC *gc = gdk_gc_new( m_bitmap );
215
216 gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
217
218 gdk_gc_unref( gc );
219
220 return TRUE;
221}
222
223GdkBitmap *wxMask::GetBitmap() const
224{
225 return m_bitmap;
226}
227
228//-----------------------------------------------------------------------------
229// wxBitmap
230//-----------------------------------------------------------------------------
231
232class wxBitmapRefData: public wxObjectRefData
233{
234public:
235 wxBitmapRefData();
236 ~wxBitmapRefData();
237
238 GdkPixmap *m_pixmap;
239 GdkBitmap *m_bitmap;
240 wxMask *m_mask;
241 int m_width;
242 int m_height;
243 int m_bpp;
244 wxPalette *m_palette;
245};
246
247wxBitmapRefData::wxBitmapRefData()
248{
249 m_pixmap = (GdkPixmap *) NULL;
250 m_bitmap = (GdkBitmap *) NULL;
251 m_mask = (wxMask *) NULL;
252 m_width = 0;
253 m_height = 0;
254 m_bpp = 0;
255 m_palette = (wxPalette *) NULL;
256}
257
258wxBitmapRefData::~wxBitmapRefData()
259{
260 if (m_pixmap)
261 gdk_pixmap_unref( m_pixmap );
262 if (m_bitmap)
263 gdk_bitmap_unref( m_bitmap );
264 delete m_mask;
265 delete m_palette;
266}
267
268//-----------------------------------------------------------------------------
269
270#define M_BMPDATA ((wxBitmapRefData *)m_refData)
271
272IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
273
274wxBitmap::wxBitmap()
275{
276}
277
278wxBitmap::wxBitmap( int width, int height, int depth )
279{
280 Create( width, height, depth );
281}
282
283bool wxBitmap::Create( int width, int height, int depth )
284{
285 UnRef();
286
287 if ( width <= 0 || height <= 0 )
288 {
289 return false;
290 }
291
292 GdkVisual *visual = wxTheApp->GetGdkVisual();
293
294 if (depth == -1)
295 depth = visual->depth;
296
297 wxCHECK_MSG( (depth == visual->depth) || (depth == 1), FALSE,
298 wxT("invalid bitmap depth") )
299
300 m_refData = new wxBitmapRefData();
301 M_BMPDATA->m_mask = (wxMask *) NULL;
302 M_BMPDATA->m_width = width;
303 M_BMPDATA->m_height = height;
304 if (depth == 1)
305 {
306 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
307 M_BMPDATA->m_bpp = 1;
308 }
309 else
310 {
311 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth );
312 M_BMPDATA->m_bpp = visual->depth;
313 }
314
315 return Ok();
316}
317
318bool wxBitmap::CreateFromXpm( const char **bits )
319{
320 UnRef();
321
322 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
323
324 GdkVisual *visual = wxTheApp->GetGdkVisual();
325
326 m_refData = new wxBitmapRefData();
327
328 GdkBitmap *mask = (GdkBitmap*) NULL;
329
330 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits );
331
332 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
333
334 if (mask)
335 {
336 M_BMPDATA->m_mask = new wxMask();
337 M_BMPDATA->m_mask->m_bitmap = mask;
338 }
339
340 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
341
342 M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ?
343
344 return TRUE;
345}
346
347bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
348{
349 UnRef();
350
351 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
352 wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") )
353
354 int width = image.GetWidth();
355 int height = image.GetHeight();
356
357 if ( width <= 0 || height <= 0 )
358 {
359 return false;
360 }
361
362 m_refData = new wxBitmapRefData();
363
364 SetHeight( height );
365 SetWidth( width );
366
367 // ------
368 // conversion to mono bitmap:
369 // ------
370 if (depth == 1)
371 {
372 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
373
374 SetDepth( 1 );
375
376 GdkVisual *visual = wxTheApp->GetGdkVisual();
377
378 // Create picture image
379
380 unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
381
382 GdkImage *data_image =
383 gdk_image_new_bitmap( visual, data_data, width, height );
384
385 // Create mask image
386
387 GdkImage *mask_image = (GdkImage*) NULL;
388
389 if (image.HasMask())
390 {
391 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
392
393 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
394
395 wxMask *mask = new wxMask();
396 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
397
398 SetMask( mask );
399 }
400
401 int r_mask = image.GetMaskRed();
402 int g_mask = image.GetMaskGreen();
403 int b_mask = image.GetMaskBlue();
404
405 unsigned char* data = image.GetData();
406
407 int index = 0;
408 for (int y = 0; y < height; y++)
409 {
410 for (int x = 0; x < width; x++)
411 {
412 int r = data[index];
413 index++;
414 int g = data[index];
415 index++;
416 int b = data[index];
417 index++;
418
419 if (image.HasMask())
420 {
421 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
422 gdk_image_put_pixel( mask_image, x, y, 1 );
423 else
424 gdk_image_put_pixel( mask_image, x, y, 0 );
425 }
426
427 if ((r == 255) && (b == 255) && (g == 255))
428 gdk_image_put_pixel( data_image, x, y, 1 );
429 else
430 gdk_image_put_pixel( data_image, x, y, 0 );
431
432 } // for
433 } // for
434
435 // Blit picture
436
437 GdkGC *data_gc = gdk_gc_new( GetBitmap() );
438
439 gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
440
441 gdk_image_destroy( data_image );
442 gdk_gc_unref( data_gc );
443
444 // Blit mask
445
446 if (image.HasMask())
447 {
448 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
449
450 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
451
452 gdk_image_destroy( mask_image );
453 gdk_gc_unref( mask_gc );
454 }
455 }
456
457 // ------
458 // conversion to colour bitmap:
459 // ------
460 else
461 {
462 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
463
464 GdkVisual *visual = wxTheApp->GetGdkVisual();
465
466 int bpp = visual->depth;
467
468 SetDepth( bpp );
469
470 if ((bpp == 16) && (visual->red_mask != 0xf800))
471 bpp = 15;
472 else if (bpp < 8)
473 bpp = 8;
474
475 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
476 // visuals are not supported by GDK so we do these ourselves, too.
477 // 15-bit and 16-bit should actually work and 24-bit certainly does.
478#ifdef __sgi
479 if (!image.HasMask() && (bpp > 16))
480#else
481 if (!image.HasMask() && (bpp > 12))
482#endif
483 {
484 static bool s_hasInitialized = FALSE;
485
486 if (!s_hasInitialized)
487 {
488 gdk_rgb_init();
489 s_hasInitialized = TRUE;
490 }
491
492 GdkGC *gc = gdk_gc_new( GetPixmap() );
493
494 gdk_draw_rgb_image( GetPixmap(),
495 gc,
496 0, 0,
497 width, height,
498 GDK_RGB_DITHER_NONE,
499 image.GetData(),
500 width*3 );
501
502 gdk_gc_unref( gc );
503 return TRUE;
504 }
505
506 // Create picture image
507
508 GdkImage *data_image =
509 gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
510
511 // Create mask image
512
513 GdkImage *mask_image = (GdkImage*) NULL;
514
515 if (image.HasMask())
516 {
517 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
518
519 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
520
521 wxMask *mask = new wxMask();
522 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
523
524 SetMask( mask );
525 }
526
527 // Render
528
529 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
530 byte_order b_o = RGB;
531
532 if (bpp > 8)
533 {
534 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
535 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG;
536 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
537 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
538 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
539 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
540 }
541
542 int r_mask = image.GetMaskRed();
543 int g_mask = image.GetMaskGreen();
544 int b_mask = image.GetMaskBlue();
545
546 unsigned char* data = image.GetData();
547
548 int index = 0;
549 for (int y = 0; y < height; y++)
550 {
551 for (int x = 0; x < width; x++)
552 {
553 int r = data[index];
554 index++;
555 int g = data[index];
556 index++;
557 int b = data[index];
558 index++;
559
560 if (image.HasMask())
561 {
562 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
563 gdk_image_put_pixel( mask_image, x, y, 1 );
564 else
565 gdk_image_put_pixel( mask_image, x, y, 0 );
566 }
567
568 switch (bpp)
569 {
570 case 8:
571 {
572 int pixel = -1;
573 if (wxTheApp->m_colorCube)
574 {
575 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
576 }
577 else
578 {
579 GdkColormap *cmap = gtk_widget_get_default_colormap();
580 GdkColor *colors = cmap->colors;
581 int max = 3 * (65536);
582
583 for (int i = 0; i < cmap->size; i++)
584 {
585 int rdiff = (r << 8) - colors[i].red;
586 int gdiff = (g << 8) - colors[i].green;
587 int bdiff = (b << 8) - colors[i].blue;
588 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
589 if (sum < max) { pixel = i; max = sum; }
590 }
591 }
592
593 gdk_image_put_pixel( data_image, x, y, pixel );
594
595 break;
596 }
597 case 12: // SGI only
598 {
599 guint32 pixel = 0;
600 switch (b_o)
601 {
602 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
603 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
604 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
605 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
606 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
607 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
608 }
609 gdk_image_put_pixel( data_image, x, y, pixel );
610 break;
611 }
612 case 15:
613 {
614 guint32 pixel = 0;
615 switch (b_o)
616 {
617 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
618 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
619 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
620 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
621 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
622 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
623 }
624 gdk_image_put_pixel( data_image, x, y, pixel );
625 break;
626 }
627 case 16:
628 {
629 // I actually don't know if for 16-bit displays, it is alway the green
630 // component or the second component which has 6 bits.
631 guint32 pixel = 0;
632 switch (b_o)
633 {
634 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
635 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
636 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
637 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
638 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
639 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
640 }
641 gdk_image_put_pixel( data_image, x, y, pixel );
642 break;
643 }
644 case 32:
645 case 24:
646 {
647 guint32 pixel = 0;
648 switch (b_o)
649 {
650 case RGB: pixel = (r << 16) | (g << 8) | b; break;
651 case RBG: pixel = (r << 16) | (b << 8) | g; break;
652 case BRG: pixel = (b << 16) | (r << 8) | g; break;
653 case BGR: pixel = (b << 16) | (g << 8) | r; break;
654 case GRB: pixel = (g << 16) | (r << 8) | b; break;
655 case GBR: pixel = (g << 16) | (b << 8) | r; break;
656 }
657 gdk_image_put_pixel( data_image, x, y, pixel );
658 }
659 default: break;
660 }
661 } // for
662 } // for
663
664 // Blit picture
665
666 GdkGC *data_gc = gdk_gc_new( GetPixmap() );
667
668 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
669
670 gdk_image_destroy( data_image );
671 gdk_gc_unref( data_gc );
672
673 // Blit mask
674
675 if (image.HasMask())
676 {
677 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
678
679 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
680
681 gdk_image_destroy( mask_image );
682 gdk_gc_unref( mask_gc );
683 }
684 }
685
686 return TRUE;
687}
688
689wxImage wxBitmap::ConvertToImage() const
690{
691 // the colour used as transparent one in wxImage and the one it is replaced
692 // with when it really occurs in the bitmap
693 static const int MASK_RED = 1;
694 static const int MASK_GREEN = 2;
695 static const int MASK_BLUE = 3;
696 static const int MASK_BLUE_REPLACEMENT = 2;
697
698 wxImage image;
699
700 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
701
702 GdkImage *gdk_image = (GdkImage*) NULL;
703 if (GetPixmap())
704 {
705 gdk_image = gdk_image_get( GetPixmap(),
706 0, 0,
707 GetWidth(), GetHeight() );
708 }
709 else if (GetBitmap())
710 {
711 gdk_image = gdk_image_get( GetBitmap(),
712 0, 0,
713 GetWidth(), GetHeight() );
714 }
715 else
716 {
717 wxFAIL_MSG( wxT("Ill-formed bitmap") );
718 }
719
720 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
721
722 image.Create( GetWidth(), GetHeight() );
723 char unsigned *data = image.GetData();
724
725 if (!data)
726 {
727 gdk_image_destroy( gdk_image );
728 wxFAIL_MSG( wxT("couldn't create image") );
729 return wxNullImage;
730 }
731
732 GdkImage *gdk_image_mask = (GdkImage*) NULL;
733 if (GetMask())
734 {
735 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
736 0, 0,
737 GetWidth(), GetHeight() );
738
739 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
740 }
741
742 int bpp = -1;
743 int red_shift_right = 0;
744 int green_shift_right = 0;
745 int blue_shift_right = 0;
746 int red_shift_left = 0;
747 int green_shift_left = 0;
748 int blue_shift_left = 0;
749 bool use_shift = FALSE;
750
751 if (GetPixmap())
752 {
753 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
754 if (visual == NULL)
755 visual = wxTheApp->GetGdkVisual();
756
757 bpp = visual->depth;
758 if (bpp == 16)
759 bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
760 red_shift_right = visual->red_shift;
761 red_shift_left = 8-visual->red_prec;
762 green_shift_right = visual->green_shift;
763 green_shift_left = 8-visual->green_prec;
764 blue_shift_right = visual->blue_shift;
765 blue_shift_left = 8-visual->blue_prec;
766
767 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
768 }
769 if (GetBitmap())
770 {
771 bpp = 1;
772 }
773
774
775 GdkColormap *cmap = gtk_widget_get_default_colormap();
776
777 long pos = 0;
778 for (int j = 0; j < GetHeight(); j++)
779 {
780 for (int i = 0; i < GetWidth(); i++)
781 {
782 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
783 if (bpp == 1)
784 {
785 if (pixel == 0)
786 {
787 data[pos] = 0;
788 data[pos+1] = 0;
789 data[pos+2] = 0;
790 }
791 else
792 {
793 data[pos] = 255;
794 data[pos+1] = 255;
795 data[pos+2] = 255;
796 }
797 }
798 else if (use_shift)
799 {
800 data[pos] = (pixel >> red_shift_right) << red_shift_left;
801 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
802 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
803 }
804 else if (cmap->colors)
805 {
806 data[pos] = cmap->colors[pixel].red >> 8;
807 data[pos+1] = cmap->colors[pixel].green >> 8;
808 data[pos+2] = cmap->colors[pixel].blue >> 8;
809 }
810 else
811 {
812 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
813 }
814
815 if (gdk_image_mask)
816 {
817 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
818 if (mask_pixel == 0)
819 {
820 data[pos] = MASK_RED;
821 data[pos+1] = MASK_GREEN;
822 data[pos+2] = MASK_BLUE;
823 }
824 else if ( data[pos] == MASK_RED &&
825 data[pos+1] == MASK_GREEN &&
826 data[pos+2] == MASK_BLUE )
827 {
828 data[pos+2] = MASK_BLUE_REPLACEMENT;
829 }
830 }
831
832 pos += 3;
833 }
834 }
835
836 gdk_image_destroy( gdk_image );
837 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
838
839 return image;
840}
841
842wxBitmap::wxBitmap( const wxBitmap& bmp )
843 : wxGDIObject()
844{
845 Ref( bmp );
846}
847
848wxBitmap::wxBitmap( const wxString &filename, int type )
849{
850 LoadFile( filename, type );
851}
852
853wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
854{
855 if ( width > 0 && height > 0 )
856 {
857 m_refData = new wxBitmapRefData();
858
859 M_BMPDATA->m_mask = (wxMask *) NULL;
860 M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data
861 (
862 wxGetRootWindow()->window,
863 (gchar *) bits,
864 width,
865 height
866 );
867 M_BMPDATA->m_width = width;
868 M_BMPDATA->m_height = height;
869 M_BMPDATA->m_bpp = 1;
870
871 wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
872 }
873}
874
875wxBitmap::~wxBitmap()
876{
877}
878
879wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
880{
881 if ( m_refData != bmp.m_refData )
882 Ref( bmp );
883
884 return *this;
885}
886
887bool wxBitmap::operator == ( const wxBitmap& bmp ) const
888{
889 return m_refData == bmp.m_refData;
890}
891
892bool wxBitmap::operator != ( const wxBitmap& bmp ) const
893{
894 return m_refData != bmp.m_refData;
895}
896
897bool wxBitmap::Ok() const
898{
899 return (m_refData != NULL);
900}
901
902int wxBitmap::GetHeight() const
903{
904 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
905
906 return M_BMPDATA->m_height;
907}
908
909int wxBitmap::GetWidth() const
910{
911 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
912
913 return M_BMPDATA->m_width;
914}
915
916int wxBitmap::GetDepth() const
917{
918 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
919
920 return M_BMPDATA->m_bpp;
921}
922
923wxMask *wxBitmap::GetMask() const
924{
925 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
926
927 return M_BMPDATA->m_mask;
928}
929
930void wxBitmap::SetMask( wxMask *mask )
931{
932 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
933
934 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
935
936 M_BMPDATA->m_mask = mask;
937}
938
939bool wxBitmap::CopyFromIcon(const wxIcon& icon)
940{
941 *this = icon;
942 return TRUE;
943}
944
945wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
946{
947 wxCHECK_MSG( Ok() &&
948 (rect.x >= 0) && (rect.y >= 0) &&
949 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
950 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
951
952 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
953 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
954
955 if (ret.GetPixmap())
956 {
957 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
958 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
959 gdk_gc_destroy( gc );
960 }
961 else
962 {
963 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
964 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
965 gdk_gc_destroy( gc );
966 }
967
968 if (GetMask())
969 {
970 wxMask *mask = new wxMask;
971 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
972
973 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
974 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
975 gdk_gc_destroy( gc );
976
977 ret.SetMask( mask );
978 }
979
980 return ret;
981}
982
983bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
984{
985 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
986
987 // Try to save the bitmap via wxImage handlers:
988 {
989 wxImage image = ConvertToImage();
990 if (image.Ok()) return image.SaveFile( name, type );
991 }
992
993 return FALSE;
994}
995
996bool wxBitmap::LoadFile( const wxString &name, int type )
997{
998 UnRef();
999
1000 if (!wxFileExists(name))
1001 return FALSE;
1002
1003 GdkVisual *visual = wxTheApp->GetGdkVisual();
1004
1005 if (type == wxBITMAP_TYPE_XPM)
1006 {
1007 m_refData = new wxBitmapRefData();
1008
1009 GdkBitmap *mask = (GdkBitmap*) NULL;
1010
1011 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm
1012 (
1013 wxGetRootWindow()->window,
1014 &mask,
1015 NULL,
1016 name.fn_str()
1017 );
1018
1019 if (mask)
1020 {
1021 M_BMPDATA->m_mask = new wxMask();
1022 M_BMPDATA->m_mask->m_bitmap = mask;
1023 }
1024
1025 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
1026
1027 M_BMPDATA->m_bpp = visual->depth;
1028 }
1029 else // try if wxImage can load it
1030 {
1031 wxImage image;
1032 if ( !image.LoadFile( name, type ) || !image.Ok() )
1033 return FALSE;
1034
1035 *this = wxBitmap(image);
1036 }
1037
1038 return TRUE;
1039}
1040
1041wxPalette *wxBitmap::GetPalette() const
1042{
1043 if (!Ok())
1044 return (wxPalette *) NULL;
1045
1046 return M_BMPDATA->m_palette;
1047}
1048
1049void wxBitmap::SetHeight( int height )
1050{
1051 if (!m_refData)
1052 m_refData = new wxBitmapRefData();
1053
1054 M_BMPDATA->m_height = height;
1055}
1056
1057void wxBitmap::SetWidth( int width )
1058{
1059 if (!m_refData)
1060 m_refData = new wxBitmapRefData();
1061
1062 M_BMPDATA->m_width = width;
1063}
1064
1065void wxBitmap::SetDepth( int depth )
1066{
1067 if (!m_refData)
1068 m_refData = new wxBitmapRefData();
1069
1070 M_BMPDATA->m_bpp = depth;
1071}
1072
1073void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1074{
1075 if (!m_refData)
1076 m_refData = new wxBitmapRefData();
1077
1078 M_BMPDATA->m_pixmap = pixmap;
1079}
1080
1081void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1082{
1083 if (!m_refData)
1084 m_refData = new wxBitmapRefData();
1085
1086 M_BMPDATA->m_bitmap = bitmap;
1087}
1088
1089GdkPixmap *wxBitmap::GetPixmap() const
1090{
1091 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
1092
1093 return M_BMPDATA->m_pixmap;
1094}
1095
1096GdkBitmap *wxBitmap::GetBitmap() const
1097{
1098 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
1099
1100 return M_BMPDATA->m_bitmap;
1101}