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