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