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