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