]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bitmap.cpp
Test the commit announcement bot some more by fixing more gtk signal callback handler...
[wxWidgets.git] / src / gtk / bitmap.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
0b04c4e0 2// Name: src/gtk/bitmap.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
6f65e337 5// RCS-ID: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
22bd9387
VZ
13#include "wx/defs.h"
14
c801d85f 15#include "wx/bitmap.h"
e1bf3ad3 16#include "wx/palette.h"
52cbfcf0 17#include "wx/icon.h"
fd0eed64 18#include "wx/filefn.h"
83624f79 19#include "wx/image.h"
f9ee644e 20#include "wx/dcmemory.h"
b5f01ae0 21#include "wx/app.h"
83624f79 22
5696ebb3 23#include "wx/rawbmp.h"
9e691f46
VZ
24 // need this to get gdk_image_new_bitmap()
25 #define GDK_ENABLE_BROKEN
9e691f46 26
20e05ffb 27#include <gdk/gdk.h>
d76fe38b 28#include <gtk/gtk.h>
b5f01ae0
VS
29#include <gdk/gdkx.h>
30
68567a96 31#include <gdk/gdkimage.h>
13111b2a 32
463c4d71 33#include "wx/math.h"
783da845 34
f6bcfd97
BP
35extern void gdk_wx_draw_bitmap (GdkDrawable *drawable,
36 GdkGC *gc,
37 GdkDrawable *src,
38 gint xsrc,
39 gint ysrc,
40 gint xdest,
41 gint ydest,
42 gint width,
43 gint height);
44
d76fe38b
RR
45//-----------------------------------------------------------------------------
46// data
47//-----------------------------------------------------------------------------
48
c2fa61e8 49extern GtkWidget *wxGetRootWindow();
c801d85f
KB
50
51//-----------------------------------------------------------------------------
52// wxMask
53//-----------------------------------------------------------------------------
54
55IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
56
8bbe427f 57wxMask::wxMask()
c801d85f 58{
fd0eed64 59 m_bitmap = (GdkBitmap *) NULL;
ff7b1510 60}
c801d85f 61
91b8de8d 62wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
c801d85f 63{
72a7edf0 64 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 65 Create( bitmap, colour );
ff7b1510 66}
c801d85f 67
0b04c4e0 68#if wxUSE_PALETTE
91b8de8d 69wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
c801d85f 70{
72a7edf0 71 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 72 Create( bitmap, paletteIndex );
ff7b1510 73}
0b04c4e0 74#endif // wxUSE_PALETTE
c801d85f 75
91b8de8d 76wxMask::wxMask( const wxBitmap& bitmap )
c801d85f 77{
72a7edf0 78 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 79 Create( bitmap );
ff7b1510 80}
c801d85f 81
8bbe427f 82wxMask::~wxMask()
c801d85f 83{
13111b2a 84 if (m_bitmap)
72a7edf0 85 gdk_bitmap_unref( m_bitmap );
ff7b1510 86}
c801d85f 87
1fb4de31
RR
88bool wxMask::Create( const wxBitmap& bitmap,
89 const wxColour& colour )
91b8de8d
RR
90{
91 if (m_bitmap)
284b4c88 92 {
91b8de8d 93 gdk_bitmap_unref( m_bitmap );
284b4c88 94 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 95 }
13111b2a 96
368d59f0 97 wxImage image = bitmap.ConvertToImage();
902725ee 98 if (!image.Ok()) return false;
13111b2a 99
c2fa61e8 100 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
f9ee644e 101 GdkGC *gc = gdk_gc_new( m_bitmap );
13111b2a 102
f9ee644e
RR
103 GdkColor color;
104 color.red = 65000;
105 color.green = 65000;
106 color.blue = 65000;
107 color.pixel = 1;
108 gdk_gc_set_foreground( gc, &color );
109 gdk_gc_set_fill( gc, GDK_SOLID );
110 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
13111b2a 111
1fb4de31
RR
112 unsigned char *data = image.GetData();
113 int index = 0;
13111b2a 114
1fb4de31
RR
115 unsigned char red = colour.Red();
116 unsigned char green = colour.Green();
117 unsigned char blue = colour.Blue();
13111b2a 118
005f5d18 119 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 120
1fb4de31 121 int bpp = visual->depth;
2eefae6e
VZ
122 if ((bpp == 16) && (visual->red_mask != 0xf800))
123 bpp = 15;
1fb4de31
RR
124 if (bpp == 15)
125 {
126 red = red & 0xf8;
1fb4de31 127 green = green & 0xf8;
f6bcfd97 128 blue = blue & 0xf8;
2eefae6e
VZ
129 }
130 else if (bpp == 16)
1fb4de31
RR
131 {
132 red = red & 0xf8;
f6bcfd97
BP
133 green = green & 0xfc;
134 blue = blue & 0xf8;
2eefae6e
VZ
135 }
136 else if (bpp == 12)
005f5d18
RR
137 {
138 red = red & 0xf0;
139 green = green & 0xf0;
140 blue = blue & 0xf0;
1fb4de31 141 }
13111b2a 142
f9ee644e
RR
143 color.red = 0;
144 color.green = 0;
145 color.blue = 0;
146 color.pixel = 0;
147 gdk_gc_set_foreground( gc, &color );
13111b2a 148
1fb4de31 149 for (int j = 0; j < image.GetHeight(); j++)
f9ee644e 150 {
f2593d0d
RR
151 int start_x = -1;
152 int i;
153 for (i = 0; i < image.GetWidth(); i++)
1fb4de31 154 {
13111b2a
VZ
155 if ((data[index] == red) &&
156 (data[index+1] == green) &&
157 (data[index+2] == blue))
158 {
159 if (start_x == -1)
160 start_x = i;
161 }
162 else
163 {
164 if (start_x != -1)
165 {
166 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
167 start_x = -1;
168 }
f9ee644e
RR
169 }
170 index += 3;
171 }
172 if (start_x != -1)
173 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
174 }
1fb4de31 175
f9ee644e 176 gdk_gc_unref( gc );
1fb4de31 177
902725ee 178 return true;
91b8de8d
RR
179}
180
0b04c4e0 181#if wxUSE_PALETTE
b5f01ae0 182bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
91b8de8d 183{
b5f01ae0
VS
184 unsigned char r,g,b;
185 wxPalette *pal = bitmap.GetPalette();
284b4c88 186
902725ee 187 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
c2fa61e8 188
b5f01ae0 189 pal->GetRGB(paletteIndex, &r, &g, &b);
284b4c88 190
b5f01ae0 191 return Create(bitmap, wxColour(r, g, b));
91b8de8d 192}
0b04c4e0 193#endif // wxUSE_PALETTE
91b8de8d
RR
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
902725ee 203 if (!bitmap.Ok()) return false;
284b4c88 204
902725ee 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
902725ee 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
902725ee 217 return true;
91b8de8d
RR
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;
feac7937 237 GdkPixbuf *m_pixbuf;
f2593d0d
RR
238 wxMask *m_mask;
239 int m_width;
240 int m_height;
241 int m_bpp;
242 wxPalette *m_palette;
c801d85f
KB
243};
244
8bbe427f 245wxBitmapRefData::wxBitmapRefData()
c801d85f 246{
fd0eed64
RR
247 m_pixmap = (GdkPixmap *) NULL;
248 m_bitmap = (GdkBitmap *) NULL;
feac7937 249 m_pixbuf = (GdkPixbuf *) NULL;
fd0eed64
RR
250 m_mask = (wxMask *) NULL;
251 m_width = 0;
252 m_height = 0;
253 m_bpp = 0;
254 m_palette = (wxPalette *) NULL;
ff7b1510 255}
c801d85f 256
8bbe427f 257wxBitmapRefData::~wxBitmapRefData()
c801d85f 258{
3ebcd89d
VZ
259 if (m_pixmap)
260 gdk_pixmap_unref( m_pixmap );
261 if (m_bitmap)
262 gdk_bitmap_unref( m_bitmap );
feac7937
VS
263 if (m_pixbuf)
264 gdk_pixbuf_unref( m_pixbuf );
3ebcd89d 265 delete m_mask;
0b04c4e0 266#if wxUSE_PALETTE
3ebcd89d 267 delete m_palette;
0b04c4e0 268#endif // wxUSE_PALETTE
ff7b1510 269}
c801d85f
KB
270
271//-----------------------------------------------------------------------------
272
273#define M_BMPDATA ((wxBitmapRefData *)m_refData)
274
275IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
276
8bbe427f 277wxBitmap::wxBitmap()
c801d85f 278{
ff7b1510 279}
8bbe427f 280
debe6624 281wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 282{
c826213d 283 Create( width, height, depth );
c826213d
RR
284}
285
286bool wxBitmap::Create( int width, int height, int depth )
287{
288 UnRef();
289
3ebcd89d
VZ
290 if ( width <= 0 || height <= 0 )
291 {
292 return false;
293 }
284b4c88 294
005f5d18 295 GdkVisual *visual = wxTheApp->GetGdkVisual();
284b4c88 296
3ebcd89d
VZ
297 if (depth == -1)
298 depth = visual->depth;
103aab26 299
902725ee 300 wxCHECK_MSG( (depth == visual->depth) || (depth == 1) || (depth == 32), false,
3ebcd89d 301 wxT("invalid bitmap depth") )
8bbe427f 302
eefa26be 303 m_refData = new wxBitmapRefData();
fd0eed64 304 M_BMPDATA->m_mask = (wxMask *) NULL;
fd0eed64
RR
305 M_BMPDATA->m_width = width;
306 M_BMPDATA->m_height = height;
eefa26be
RR
307 if (depth == 1)
308 {
c2fa61e8 309 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
eefa26be
RR
310 M_BMPDATA->m_bpp = 1;
311 }
284f2b59
RR
312 else if (depth == 32)
313 {
314 M_BMPDATA->m_pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, true,
315 8, width, height);
316 M_BMPDATA->m_bpp = 32;
317 }
eefa26be
RR
318 else
319 {
c2fa61e8 320 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth );
103aab26 321 M_BMPDATA->m_bpp = visual->depth;
eefa26be 322 }
8bbe427f 323
c826213d 324 return Ok();
ff7b1510 325}
b5f01ae0 326
e838cc14 327bool wxBitmap::CreateFromXpm( const char **bits )
e52f60e6 328{
a11672a4
RR
329 UnRef();
330
902725ee 331 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") )
8bbe427f 332
005f5d18 333 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 334
e52f60e6
RR
335 m_refData = new wxBitmapRefData();
336
337 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 338
c2fa61e8 339 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits );
8bbe427f 340
902725ee 341 wxCHECK_MSG( M_BMPDATA->m_pixmap, false, wxT("couldn't create pixmap") );
8bbe427f 342
fd0eed64
RR
343 if (mask)
344 {
345 M_BMPDATA->m_mask = new wxMask();
346 M_BMPDATA->m_mask->m_bitmap = mask;
347 }
8bbe427f 348
fd0eed64 349 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
2eefae6e 350
8cce8940 351 M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ?
c2fa61e8 352
902725ee 353 return true;
ff7b1510 354}
b5f01ae0 355
783da845
RR
356wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy )
357{
358 wxCHECK_MSG( Ok(), wxNullBitmap, wxT("invalid bitmap") );
359
360 if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height)
361 return *this;
902725ee 362
783da845
RR
363 int width = wxMax(newx, 1);
364 int height = wxMax(newy, 1);
365 width = wxMin(width, clipwidth);
366 height = wxMin(height, clipheight);
902725ee 367
feac7937 368 wxBitmap bmp;
783da845 369
feac7937 370 if (HasPixbuf())
783da845 371 {
feac7937
VS
372 bmp.SetWidth(width);
373 bmp.SetHeight(height);
374 bmp.SetDepth(GetDepth());
6db34764
VS
375 bmp.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB,
376 gdk_pixbuf_get_has_alpha(GetPixbuf()),
feac7937 377 8, width, height));
6db34764 378 gdk_pixbuf_scale(GetPixbuf(), bmp.GetPixbuf(),
feac7937 379 0, 0, width, height,
902725ee 380 clipx, clipy,
feac7937
VS
381 (double)newx/GetWidth(), (double)newy/GetHeight(),
382 GDK_INTERP_BILINEAR);
783da845 383 }
feac7937 384 else
783da845 385 {
feac7937
VS
386 GdkImage *img = (GdkImage*) NULL;
387 if (GetPixmap())
388 img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
389 else if (GetBitmap())
390 img = gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
391 else
392 wxFAIL_MSG( wxT("Ill-formed bitmap") );
393
394 wxCHECK_MSG( img, wxNullBitmap, wxT("couldn't create image") );
783da845 395
feac7937
VS
396 int bpp = -1;
397
902725ee 398
feac7937
VS
399 GdkGC *gc = NULL;
400 GdkPixmap *dstpix = NULL;
401 if (GetPixmap())
783da845 402 {
feac7937
VS
403 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
404 if (visual == NULL)
405 visual = wxTheApp->GetGdkVisual();
406
407 bpp = visual->depth;
408 bmp = wxBitmap(width,height,bpp);
409 dstpix = bmp.GetPixmap();
410 gc = gdk_gc_new( dstpix );
783da845 411 }
783da845 412
feac7937
VS
413 char *dst = NULL;
414 long dstbyteperline = 0;
902725ee 415
feac7937
VS
416 if (GetBitmap())
417 {
418 bpp = 1;
419 dstbyteperline = width/8*M_BMPDATA->m_bpp;
420 if (width*M_BMPDATA->m_bpp % 8 != 0)
421 dstbyteperline++;
422 dst = (char*) malloc(dstbyteperline*height);
423 }
902725ee 424
feac7937
VS
425 // be careful to use the right scaling factor
426 float scx = (float)M_BMPDATA->m_width/(float)newx;
427 float scy = (float)M_BMPDATA->m_height/(float)newy;
428 // prepare accel-tables
429 int *tablex = (int *)calloc(width,sizeof(int));
430 int *tabley = (int *)calloc(height,sizeof(int));
431
432 // accel table filled with clipped values
433 for (int x = 0; x < width; x++)
434 tablex[x] = (int) (scx * (x+clipx));
435 for (int y = 0; y < height; y++)
436 tabley[y] = (int) (scy * (y+clipy));
783da845 437
feac7937 438 // Main rescaling routine starts here
783da845
RR
439 for (int h = 0; h < height; h++)
440 {
441 char outbyte = 0;
f9ebac93 442 int old_x = -1;
b63b07a8 443 guint32 old_pixval = 0;
feac7937 444
f9ebac93 445 for (int w = 0; w < width; w++)
783da845 446 {
f9ebac93
RR
447 guint32 pixval;
448 int x = tablex[w];
449 if (x == old_x)
450 pixval = old_pixval;
451 else
452 {
453 pixval = gdk_image_get_pixel( img, x, tabley[h] );
454 old_pixval = pixval;
455 old_x = x;
456 }
902725ee 457
feac7937 458 if (bpp == 1)
783da845 459 {
feac7937
VS
460 if (!pixval)
461 {
462 char bit=1;
d0ee33f5 463 char shift = bit << (w % 8);
feac7937
VS
464 outbyte |= shift;
465 }
902725ee 466
feac7937
VS
467 if ((w+1)%8==0)
468 {
469 dst[h*dstbyteperline+w/8] = outbyte;
470 outbyte = 0;
471 }
783da845 472 }
feac7937 473 else
783da845 474 {
feac7937
VS
475 GdkColor col;
476 col.pixel = pixval;
477 gdk_gc_set_foreground( gc, &col );
478 gdk_draw_point( dstpix, gc, w, h);
783da845
RR
479 }
480 }
902725ee 481
783da845 482 // do not forget the last byte
feac7937 483 if ((bpp == 1) && (width % 8 != 0))
f9ebac93 484 dst[h*dstbyteperline+width/8] = outbyte;
783da845 485 }
902725ee 486
783da845 487 gdk_image_destroy( img );
feac7937
VS
488 if (gc) gdk_gc_unref( gc );
489
490 if (bpp == 1)
491 {
492 bmp = wxBitmap( (const char *)dst, width, height, 1 );
493 free( dst );
494 }
902725ee 495
feac7937
VS
496 if (GetMask())
497 {
498 dstbyteperline = width/8;
499 if (width % 8 != 0)
500 dstbyteperline++;
501 dst = (char*) malloc(dstbyteperline*height);
502 img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
783da845 503
feac7937
VS
504 for (int h = 0; h < height; h++)
505 {
506 char outbyte = 0;
507 int old_x = -1;
508 guint32 old_pixval = 0;
902725ee 509
feac7937
VS
510 for (int w = 0; w < width; w++)
511 {
512 guint32 pixval;
513 int x = tablex[w];
514 if (x == old_x)
515 pixval = old_pixval;
516 else
517 {
518 pixval = gdk_image_get_pixel( img, x, tabley[h] );
519 old_pixval = pixval;
520 old_x = x;
521 }
902725ee 522
feac7937
VS
523 if (pixval)
524 {
525 char bit=1;
d0ee33f5 526 char shift = bit << (w % 8);
feac7937
VS
527 outbyte |= shift;
528 }
902725ee 529
feac7937
VS
530 if ((w+1)%8 == 0)
531 {
532 dst[h*dstbyteperline+w/8] = outbyte;
533 outbyte = 0;
534 }
535 }
902725ee 536
feac7937
VS
537 // do not forget the last byte
538 if (width % 8 != 0)
539 dst[h*dstbyteperline+width/8] = outbyte;
540 }
541 wxMask* mask = new wxMask;
542 mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height );
543 bmp.SetMask(mask);
544
545 free( dst );
546 gdk_image_destroy( img );
547 }
548
549 free( tablex );
550 free( tabley );
551 }
902725ee
WS
552
553 return bmp;
783da845
RR
554}
555
feac7937 556bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
b5f01ae0 557{
a11672a4
RR
558 UnRef();
559
902725ee
WS
560 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") )
561 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") )
b5f01ae0 562
feac7937
VS
563 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
564 return false;
902725ee 565
feac7937 566 m_refData = new wxBitmapRefData();
3ebcd89d 567
feac7937 568 if (depth == 1)
3ebcd89d 569 {
feac7937 570 return CreateFromImageAsBitmap(image);
3ebcd89d 571 }
feac7937
VS
572 else
573 {
feac7937
VS
574 if (image.HasAlpha())
575 return CreateFromImageAsPixbuf(image);
68567a96 576
feac7937
VS
577 return CreateFromImageAsPixmap(image);
578 }
579}
3ebcd89d 580
feac7937
VS
581// conversion to mono bitmap:
582bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img)
583{
584 // convert alpha channel to mask, if it is present:
585 wxImage image(img);
586 image.ConvertAlphaToMask();
902725ee 587
feac7937
VS
588 int width = image.GetWidth();
589 int height = image.GetHeight();
c2fa61e8 590
3ebcd89d
VZ
591 SetHeight( height );
592 SetWidth( width );
593
feac7937 594 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
b5f01ae0 595
feac7937 596 SetDepth( 1 );
2eefae6e 597
feac7937 598 GdkVisual *visual = wxTheApp->GetGdkVisual();
b5f01ae0 599
feac7937 600 // Create picture image
b5f01ae0 601
feac7937 602 unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
b5f01ae0 603
feac7937
VS
604 GdkImage *data_image =
605 gdk_image_new_bitmap( visual, data_data, width, height );
b5f01ae0 606
feac7937 607 // Create mask image
b5f01ae0 608
feac7937 609 GdkImage *mask_image = (GdkImage*) NULL;
b5f01ae0 610
feac7937
VS
611 if (image.HasMask())
612 {
613 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
b5f01ae0 614
feac7937 615 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
b5f01ae0 616
feac7937
VS
617 wxMask *mask = new wxMask();
618 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
b5f01ae0 619
feac7937
VS
620 SetMask( mask );
621 }
b5f01ae0 622
feac7937
VS
623 int r_mask = image.GetMaskRed();
624 int g_mask = image.GetMaskGreen();
625 int b_mask = image.GetMaskBlue();
b5f01ae0 626
feac7937 627 unsigned char* data = image.GetData();
b5f01ae0 628
feac7937
VS
629 int index = 0;
630 for (int y = 0; y < height; y++)
631 {
632 for (int x = 0; x < width; x++)
b5f01ae0 633 {
feac7937
VS
634 int r = data[index];
635 index++;
636 int g = data[index];
637 index++;
638 int b = data[index];
639 index++;
640
641 if (image.HasMask())
b5f01ae0 642 {
feac7937
VS
643 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
644 gdk_image_put_pixel( mask_image, x, y, 1 );
b5f01ae0 645 else
feac7937
VS
646 gdk_image_put_pixel( mask_image, x, y, 0 );
647 }
b5f01ae0 648
feac7937
VS
649 if ((r == 255) && (b == 255) && (g == 255))
650 gdk_image_put_pixel( data_image, x, y, 1 );
651 else
652 gdk_image_put_pixel( data_image, x, y, 0 );
b5f01ae0 653
feac7937
VS
654 } // for
655 } // for
b5f01ae0 656
feac7937 657 // Blit picture
b5f01ae0 658
feac7937 659 GdkGC *data_gc = gdk_gc_new( GetBitmap() );
b5f01ae0 660
feac7937 661 gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
b5f01ae0 662
feac7937
VS
663 gdk_image_destroy( data_image );
664 gdk_gc_unref( data_gc );
b5f01ae0 665
feac7937 666 // Blit mask
b5f01ae0 667
feac7937
VS
668 if (image.HasMask())
669 {
670 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
b5f01ae0 671
feac7937
VS
672 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
673
674 gdk_image_destroy( mask_image );
675 gdk_gc_unref( mask_gc );
b5f01ae0 676 }
c2fa61e8 677
feac7937
VS
678 return true;
679}
902725ee 680
feac7937
VS
681// conversion to colour bitmap:
682bool wxBitmap::CreateFromImageAsPixmap(const wxImage& img)
683{
684 // convert alpha channel to mask, if it is present:
685 wxImage image(img);
686 image.ConvertAlphaToMask();
902725ee 687
feac7937
VS
688 int width = image.GetWidth();
689 int height = image.GetHeight();
690
691 SetHeight( height );
692 SetWidth( width );
693
694 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
b5f01ae0 695
feac7937 696 GdkVisual *visual = wxTheApp->GetGdkVisual();
b5f01ae0 697
feac7937 698 int bpp = visual->depth;
b5f01ae0 699
feac7937 700 SetDepth( bpp );
b5f01ae0 701
feac7937
VS
702 if ((bpp == 16) && (visual->red_mask != 0xf800))
703 bpp = 15;
704 else if (bpp < 8)
705 bpp = 8;
2eefae6e 706
feac7937
VS
707 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
708 // visuals are not supported by GDK so we do these ourselves, too.
709 // 15-bit and 16-bit should actually work and 24-bit certainly does.
9b72a74d 710#ifdef __sgi
feac7937 711 if (!image.HasMask() && (bpp > 16))
9b72a74d 712#else
feac7937 713 if (!image.HasMask() && (bpp > 12))
9b72a74d 714#endif
feac7937 715 {
902725ee 716 static bool s_hasInitialized = false;
b5f01ae0 717
feac7937
VS
718 if (!s_hasInitialized)
719 {
720 gdk_rgb_init();
902725ee 721 s_hasInitialized = true;
feac7937 722 }
b5f01ae0 723
feac7937 724 GdkGC *gc = gdk_gc_new( GetPixmap() );
b5f01ae0 725
feac7937
VS
726 gdk_draw_rgb_image( GetPixmap(),
727 gc,
728 0, 0,
729 width, height,
730 GDK_RGB_DITHER_NONE,
731 image.GetData(),
732 width*3 );
b5f01ae0 733
feac7937 734 gdk_gc_unref( gc );
902725ee 735 return true;
feac7937 736 }
b5f01ae0 737
feac7937 738 // Create picture image
b5f01ae0 739
feac7937
VS
740 GdkImage *data_image =
741 gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
b5f01ae0 742
feac7937 743 // Create mask image
b5f01ae0 744
feac7937 745 GdkImage *mask_image = (GdkImage*) NULL;
b5f01ae0 746
feac7937
VS
747 if (image.HasMask())
748 {
749 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
b5f01ae0 750
feac7937 751 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
b5f01ae0 752
feac7937
VS
753 wxMask *mask = new wxMask();
754 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
b5f01ae0 755
feac7937
VS
756 SetMask( mask );
757 }
b5f01ae0 758
feac7937 759 // Render
b5f01ae0 760
feac7937
VS
761 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
762 byte_order b_o = RGB;
b5f01ae0 763
feac7937
VS
764 if (bpp > 8)
765 {
766 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
767 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG;
768 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
769 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
770 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
771 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
772 }
b5f01ae0 773
feac7937
VS
774 int r_mask = image.GetMaskRed();
775 int g_mask = image.GetMaskGreen();
776 int b_mask = image.GetMaskBlue();
b5f01ae0 777
feac7937 778 unsigned char* data = image.GetData();
b5f01ae0 779
feac7937
VS
780 int index = 0;
781 for (int y = 0; y < height; y++)
782 {
783 for (int x = 0; x < width; x++)
b5f01ae0 784 {
feac7937
VS
785 int r = data[index];
786 index++;
787 int g = data[index];
788 index++;
789 int b = data[index];
790 index++;
791
792 if (image.HasMask())
b5f01ae0 793 {
feac7937
VS
794 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
795 gdk_image_put_pixel( mask_image, x, y, 1 );
796 else
797 gdk_image_put_pixel( mask_image, x, y, 0 );
798 }
b5f01ae0 799
feac7937
VS
800 switch (bpp)
801 {
802 case 8:
b5f01ae0 803 {
feac7937
VS
804 int pixel = -1;
805 if (wxTheApp->m_colorCube)
b5f01ae0 806 {
feac7937
VS
807 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
808 }
809 else
810 {
811 GdkColormap *cmap = gtk_widget_get_default_colormap();
812 GdkColor *colors = cmap->colors;
813 int max = 3 * (65536);
814
815 for (int i = 0; i < cmap->size; i++)
b5f01ae0 816 {
feac7937
VS
817 int rdiff = (r << 8) - colors[i].red;
818 int gdiff = (g << 8) - colors[i].green;
819 int bdiff = (b << 8) - colors[i].blue;
820 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
821 if (sum < max) { pixel = i; max = sum; }
b5f01ae0 822 }
feac7937 823 }
2eefae6e 824
feac7937 825 gdk_image_put_pixel( data_image, x, y, pixel );
2eefae6e 826
feac7937
VS
827 break;
828 }
829 case 12: // SGI only
830 {
831 guint32 pixel = 0;
832 switch (b_o)
b5f01ae0 833 {
feac7937
VS
834 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
835 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
836 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
837 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
838 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
839 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
b5f01ae0 840 }
feac7937
VS
841 gdk_image_put_pixel( data_image, x, y, pixel );
842 break;
843 }
844 case 15:
845 {
846 guint32 pixel = 0;
847 switch (b_o)
b5f01ae0 848 {
feac7937
VS
849 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
850 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
851 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
852 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
853 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
854 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
b5f01ae0 855 }
feac7937
VS
856 gdk_image_put_pixel( data_image, x, y, pixel );
857 break;
858 }
859 case 16:
860 {
861 // I actually don't know if for 16-bit displays, it is alway the green
862 // component or the second component which has 6 bits.
863 guint32 pixel = 0;
864 switch (b_o)
b5f01ae0 865 {
feac7937
VS
866 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
867 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
868 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
869 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
870 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
871 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
8ab696e0 872 }
feac7937
VS
873 gdk_image_put_pixel( data_image, x, y, pixel );
874 break;
875 }
876 case 32:
877 case 24:
878 {
879 guint32 pixel = 0;
880 switch (b_o)
8ab696e0 881 {
feac7937
VS
882 case RGB: pixel = (r << 16) | (g << 8) | b; break;
883 case RBG: pixel = (r << 16) | (b << 8) | g; break;
884 case BRG: pixel = (b << 16) | (r << 8) | g; break;
885 case BGR: pixel = (b << 16) | (g << 8) | r; break;
886 case GRB: pixel = (g << 16) | (r << 8) | b; break;
887 case GBR: pixel = (g << 16) | (b << 8) | r; break;
b5f01ae0 888 }
feac7937 889 gdk_image_put_pixel( data_image, x, y, pixel );
902725ee 890 break;
b5f01ae0 891 }
feac7937
VS
892 default: break;
893 }
894 } // for
895 } // for
b5f01ae0 896
feac7937 897 // Blit picture
b5f01ae0 898
feac7937 899 GdkGC *data_gc = gdk_gc_new( GetPixmap() );
b5f01ae0 900
feac7937 901 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
b5f01ae0 902
feac7937
VS
903 gdk_image_destroy( data_image );
904 gdk_gc_unref( data_gc );
b5f01ae0 905
feac7937 906 // Blit mask
b5f01ae0 907
feac7937
VS
908 if (image.HasMask())
909 {
910 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
b5f01ae0 911
feac7937 912 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
b5f01ae0 913
feac7937
VS
914 gdk_image_destroy( mask_image );
915 gdk_gc_unref( mask_gc );
b5f01ae0
VS
916 }
917
feac7937 918 return true;
b5f01ae0
VS
919}
920
feac7937 921bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
b5f01ae0 922{
feac7937
VS
923 int width = image.GetWidth();
924 int height = image.GetHeight();
2eefae6e 925
feac7937
VS
926 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
927 image.HasAlpha(),
928 8 /* bits per sample */,
929 width, height);
930 if (!pixbuf)
931 return false;
c2fa61e8 932
6db34764 933 wxASSERT( image.HasAlpha() ); // for now
feac7937
VS
934 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf) == 4 );
935 wxASSERT( gdk_pixbuf_get_width(pixbuf) == width );
936 wxASSERT( gdk_pixbuf_get_height(pixbuf) == height );
902725ee 937
feac7937
VS
938 M_BMPDATA->m_pixbuf = pixbuf;
939 SetHeight(height);
940 SetWidth(width);
941 SetDepth(wxTheApp->GetGdkVisual()->depth);
902725ee 942
feac7937
VS
943 // Copy the data:
944 unsigned char *in = image.GetData();
945 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
946 unsigned char *alpha = image.GetAlpha();
902725ee 947
feac7937 948 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
b5f01ae0 949
feac7937 950 for (int y = 0; y < height; y++, out += rowinc)
b5f01ae0 951 {
feac7937
VS
952 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
953 {
954 out[0] = in[0];
955 out[1] = in[1];
956 out[2] = in[2];
957 out[3] = *alpha;
958 }
b5f01ae0 959 }
902725ee 960
feac7937
VS
961 return true;
962}
b5f01ae0 963
feac7937
VS
964wxImage wxBitmap::ConvertToImage() const
965{
966 wxImage image;
c2fa61e8 967
feac7937 968 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
902725ee 969
feac7937
VS
970 image.Create(GetWidth(), GetHeight());
971 unsigned char *data = image.GetData();
2eefae6e 972
b5f01ae0
VS
973 if (!data)
974 {
b5f01ae0
VS
975 wxFAIL_MSG( wxT("couldn't create image") );
976 return wxNullImage;
977 }
978
feac7937 979 if (HasPixbuf())
b5f01ae0 980 {
feac7937
VS
981 GdkPixbuf *pixbuf = GetPixbuf();
982 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf) );
902725ee 983
feac7937
VS
984 int w = GetWidth();
985 int h = GetHeight();
902725ee 986
feac7937 987 image.SetAlpha();
b5f01ae0 988
feac7937
VS
989 unsigned char *alpha = image.GetAlpha();
990 unsigned char *in = gdk_pixbuf_get_pixels(pixbuf);
991 unsigned char *out = data;
992 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * w;
b5f01ae0 993
feac7937
VS
994 for (int y = 0; y < h; y++, in += rowinc)
995 {
996 for (int x = 0; x < w; x++, in += 4, out += 3, alpha++)
997 {
998 out[0] = in[0];
999 out[1] = in[1];
1000 out[2] = in[2];
1001 *alpha = in[3];
1002 }
1003 }
b5f01ae0 1004 }
feac7937 1005 else
b5f01ae0 1006 {
feac7937
VS
1007 // the colour used as transparent one in wxImage and the one it is
1008 // replaced with when it really occurs in the bitmap
1009 static const int MASK_RED = 1;
1010 static const int MASK_GREEN = 2;
1011 static const int MASK_BLUE = 3;
1012 static const int MASK_BLUE_REPLACEMENT = 2;
b5f01ae0 1013
feac7937 1014 GdkImage *gdk_image = (GdkImage*) NULL;
b5f01ae0 1015
feac7937
VS
1016 if (HasPixmap())
1017 {
1018 gdk_image = gdk_image_get( GetPixmap(),
1019 0, 0,
1020 GetWidth(), GetHeight() );
1021 }
1022 else if (GetBitmap())
1023 {
1024 gdk_image = gdk_image_get( GetBitmap(),
1025 0, 0,
1026 GetWidth(), GetHeight() );
1027 }
1028 else
1029 {
1030 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1031 }
b5f01ae0 1032
feac7937
VS
1033 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
1034
1035 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1036 if (GetMask())
b5f01ae0 1037 {
feac7937
VS
1038 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
1039 0, 0,
1040 GetWidth(), GetHeight() );
1041
1042 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1043 }
1044
1045 int bpp = -1;
1046 int red_shift_right = 0;
1047 int green_shift_right = 0;
1048 int blue_shift_right = 0;
1049 int red_shift_left = 0;
1050 int green_shift_left = 0;
1051 int blue_shift_left = 0;
902725ee 1052 bool use_shift = false;
feac7937
VS
1053
1054 if (GetPixmap())
1055 {
1056 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
1057 if (visual == NULL)
1058 visual = wxTheApp->GetGdkVisual();
1059
1060 bpp = visual->depth;
1061 if (bpp == 16)
1062 bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
1063 red_shift_right = visual->red_shift;
1064 red_shift_left = 8-visual->red_prec;
1065 green_shift_right = visual->green_shift;
1066 green_shift_left = 8-visual->green_prec;
1067 blue_shift_right = visual->blue_shift;
1068 blue_shift_left = 8-visual->blue_prec;
1069
1070 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
1071 }
1072 if (GetBitmap())
1073 {
1074 bpp = 1;
1075 }
1076
1077
1078 GdkColormap *cmap = gtk_widget_get_default_colormap();
1079
1080 long pos = 0;
1081 for (int j = 0; j < GetHeight(); j++)
1082 {
1083 for (int i = 0; i < GetWidth(); i++)
8ab696e0 1084 {
feac7937
VS
1085 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
1086 if (bpp == 1)
b5f01ae0 1087 {
feac7937
VS
1088 if (pixel == 0)
1089 {
1090 data[pos] = 0;
1091 data[pos+1] = 0;
1092 data[pos+2] = 0;
1093 }
1094 else
1095 {
1096 data[pos] = 255;
1097 data[pos+1] = 255;
1098 data[pos+2] = 255;
1099 }
8ab696e0 1100 }
feac7937 1101 else if (use_shift)
8ab696e0 1102 {
feac7937
VS
1103 data[pos] = (pixel >> red_shift_right) << red_shift_left;
1104 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
1105 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
8ab696e0 1106 }
feac7937 1107 else if (cmap->colors)
b5f01ae0 1108 {
feac7937
VS
1109 data[pos] = cmap->colors[pixel].red >> 8;
1110 data[pos+1] = cmap->colors[pixel].green >> 8;
1111 data[pos+2] = cmap->colors[pixel].blue >> 8;
2eefae6e 1112 }
feac7937 1113 else
2eefae6e 1114 {
feac7937 1115 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
b5f01ae0 1116 }
b5f01ae0 1117
feac7937
VS
1118 if (gdk_image_mask)
1119 {
1120 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1121 if (mask_pixel == 0)
1122 {
1123 data[pos] = MASK_RED;
1124 data[pos+1] = MASK_GREEN;
1125 data[pos+2] = MASK_BLUE;
1126 }
1127 else if ( data[pos] == MASK_RED &&
1128 data[pos+1] == MASK_GREEN &&
1129 data[pos+2] == MASK_BLUE )
1130 {
1131 data[pos+2] = MASK_BLUE_REPLACEMENT;
1132 }
1133 }
1134
1135 pos += 3;
1136 }
b5f01ae0 1137 }
b5f01ae0 1138
feac7937
VS
1139 gdk_image_destroy( gdk_image );
1140 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1141 }
b5f01ae0
VS
1142
1143 return image;
1144}
1145
4611dd06 1146wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
c801d85f 1147{
fd0eed64 1148 LoadFile( filename, type );
ff7b1510 1149}
c801d85f 1150
debe6624 1151wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 1152{
3ebcd89d
VZ
1153 if ( width > 0 && height > 0 )
1154 {
1155 m_refData = new wxBitmapRefData();
6f65e337 1156
3ebcd89d
VZ
1157 M_BMPDATA->m_mask = (wxMask *) NULL;
1158 M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data
1159 (
1160 wxGetRootWindow()->window,
1161 (gchar *) bits,
1162 width,
1163 height
1164 );
1165 M_BMPDATA->m_width = width;
1166 M_BMPDATA->m_height = height;
1167 M_BMPDATA->m_bpp = 1;
6f65e337 1168
3ebcd89d
VZ
1169 wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
1170 }
6f65e337 1171}
8bbe427f
VZ
1172
1173wxBitmap::~wxBitmap()
c801d85f 1174{
ff7b1510 1175}
8bbe427f 1176
f6bcfd97 1177bool wxBitmap::operator == ( const wxBitmap& bmp ) const
c801d85f 1178{
8bbe427f 1179 return m_refData == bmp.m_refData;
ff7b1510 1180}
8bbe427f 1181
f6bcfd97 1182bool wxBitmap::operator != ( const wxBitmap& bmp ) const
c801d85f 1183{
8bbe427f 1184 return m_refData != bmp.m_refData;
ff7b1510 1185}
8bbe427f 1186
91b8de8d 1187bool wxBitmap::Ok() const
c801d85f 1188{
902725ee 1189 return (m_refData != NULL) &&
feac7937 1190 (
feac7937 1191 M_BMPDATA->m_pixbuf ||
902725ee 1192 M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap
feac7937 1193 );
ff7b1510 1194}
8bbe427f 1195
91b8de8d 1196int wxBitmap::GetHeight() const
c801d85f 1197{
223d09f6 1198 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
e55ad60e 1199
fd0eed64 1200 return M_BMPDATA->m_height;
ff7b1510 1201}
c801d85f 1202
91b8de8d 1203int wxBitmap::GetWidth() const
c801d85f 1204{
223d09f6 1205 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 1206
fd0eed64 1207 return M_BMPDATA->m_width;
ff7b1510 1208}
c801d85f 1209
91b8de8d 1210int wxBitmap::GetDepth() const
c801d85f 1211{
223d09f6 1212 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 1213
fd0eed64 1214 return M_BMPDATA->m_bpp;
ff7b1510 1215}
c801d85f 1216
91b8de8d 1217wxMask *wxBitmap::GetMask() const
c801d85f 1218{
223d09f6 1219 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
8bbe427f 1220
fd0eed64 1221 return M_BMPDATA->m_mask;
ff7b1510 1222}
c801d85f
KB
1223
1224void wxBitmap::SetMask( wxMask *mask )
1225{
223d09f6 1226 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
8bbe427f 1227
fd0eed64 1228 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 1229
fd0eed64 1230 M_BMPDATA->m_mask = mask;
ff7b1510 1231}
c801d85f 1232
db0aec83
VS
1233bool wxBitmap::CopyFromIcon(const wxIcon& icon)
1234{
1235 *this = icon;
902725ee 1236 return true;
db0aec83
VS
1237}
1238
17bec151
RR
1239wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1240{
1241 wxCHECK_MSG( Ok() &&
13111b2a
VZ
1242 (rect.x >= 0) && (rect.y >= 0) &&
1243 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
17bec151 1244 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
13111b2a 1245
17bec151
RR
1246 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
1247 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
13111b2a 1248
feac7937 1249 if (HasPixbuf())
17bec151 1250 {
feac7937 1251 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
6db34764 1252 gdk_pixbuf_get_has_alpha(GetPixbuf()),
17f504ec 1253 8, rect.width, rect.height);
feac7937 1254 ret.SetPixbuf(pixbuf);
6db34764 1255 gdk_pixbuf_copy_area(GetPixbuf(),
feac7937
VS
1256 rect.x, rect.y, rect.width, rect.height,
1257 pixbuf, 0, 0);
17bec151
RR
1258 }
1259 else
1260 {
feac7937
VS
1261 if (ret.GetPixmap())
1262 {
1263 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
1264 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1265 gdk_gc_destroy( gc );
1266 }
1267 else
1268 {
1269 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
1270 GdkColor col;
1271 col.pixel = 0xFFFFFF;
1272 gdk_gc_set_foreground( gc, &col );
1273 col.pixel = 0;
1274 gdk_gc_set_background( gc, &col );
1275 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1276 gdk_gc_destroy( gc );
1277 }
17bec151 1278 }
13111b2a 1279
17bec151
RR
1280 if (GetMask())
1281 {
1282 wxMask *mask = new wxMask;
c2fa61e8 1283 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
13111b2a 1284
17bec151 1285 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
f9ebac93
RR
1286 GdkColor col;
1287 col.pixel = 0xFFFFFF;
1288 gdk_gc_set_foreground( gc, &col );
1289 col.pixel = 0;
1290 gdk_gc_set_background( gc, &col );
1291 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height );
13111b2a
VZ
1292 gdk_gc_destroy( gc );
1293
1294 ret.SetMask( mask );
17bec151 1295 }
13111b2a 1296
17bec151
RR
1297 return ret;
1298}
1299
4611dd06 1300bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
c801d85f 1301{
902725ee 1302 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
8bbe427f 1303
b75dd496 1304 // Try to save the bitmap via wxImage handlers:
fd0eed64 1305 {
368d59f0 1306 wxImage image = ConvertToImage();
284b4c88 1307 if (image.Ok()) return image.SaveFile( name, type );
fd0eed64 1308 }
8bbe427f 1309
902725ee 1310 return false;
ff7b1510 1311}
c801d85f 1312
4611dd06 1313bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
c801d85f 1314{
fd0eed64 1315 UnRef();
8bbe427f 1316
3ebcd89d 1317 if (!wxFileExists(name))
902725ee 1318 return false;
8bbe427f 1319
005f5d18 1320 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 1321
fd0eed64
RR
1322 if (type == wxBITMAP_TYPE_XPM)
1323 {
1324 m_refData = new wxBitmapRefData();
8bbe427f 1325
fd0eed64 1326 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 1327
3ebcd89d
VZ
1328 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm
1329 (
1330 wxGetRootWindow()->window,
1331 &mask,
1332 NULL,
1333 name.fn_str()
1334 );
8bbe427f 1335
fd0eed64
RR
1336 if (mask)
1337 {
1338 M_BMPDATA->m_mask = new wxMask();
1339 M_BMPDATA->m_mask->m_bitmap = mask;
1340 }
8bbe427f 1341
fd0eed64 1342 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
c2fa61e8 1343
103aab26 1344 M_BMPDATA->m_bpp = visual->depth;
fd0eed64 1345 }
b75dd496 1346 else // try if wxImage can load it
fd0eed64
RR
1347 {
1348 wxImage image;
3ebcd89d 1349 if ( !image.LoadFile( name, type ) || !image.Ok() )
902725ee 1350 return false;
3ebcd89d
VZ
1351
1352 *this = wxBitmap(image);
fd0eed64 1353 }
8bbe427f 1354
902725ee 1355 return true;
ff7b1510 1356}
8bbe427f 1357
0b04c4e0 1358#if wxUSE_PALETTE
91b8de8d 1359wxPalette *wxBitmap::GetPalette() const
c801d85f 1360{
3ebcd89d
VZ
1361 if (!Ok())
1362 return (wxPalette *) NULL;
8bbe427f 1363
fd0eed64 1364 return M_BMPDATA->m_palette;
ff7b1510 1365}
c801d85f 1366
4611dd06
RR
1367void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
1368{
1369 // TODO
1370}
0b04c4e0 1371#endif // wxUSE_PALETTE
4611dd06 1372
4bc67cc5
RR
1373void wxBitmap::SetHeight( int height )
1374{
3ebcd89d
VZ
1375 if (!m_refData)
1376 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1377
1378 M_BMPDATA->m_height = height;
1379}
1380
1381void wxBitmap::SetWidth( int width )
1382{
3ebcd89d
VZ
1383 if (!m_refData)
1384 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1385
1386 M_BMPDATA->m_width = width;
1387}
1388
1389void wxBitmap::SetDepth( int depth )
1390{
3ebcd89d
VZ
1391 if (!m_refData)
1392 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1393
1394 M_BMPDATA->m_bpp = depth;
1395}
1396
1397void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1398{
3ebcd89d
VZ
1399 if (!m_refData)
1400 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1401
1402 M_BMPDATA->m_pixmap = pixmap;
6db34764 1403 PurgeOtherRepresentations(Pixmap);
4bc67cc5
RR
1404}
1405
82ea63e6
RR
1406void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1407{
3ebcd89d
VZ
1408 if (!m_refData)
1409 m_refData = new wxBitmapRefData();
82ea63e6
RR
1410
1411 M_BMPDATA->m_bitmap = bitmap;
6db34764 1412 PurgeOtherRepresentations(Pixmap);
82ea63e6
RR
1413}
1414
91b8de8d 1415GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 1416{
223d09f6 1417 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1418
feac7937
VS
1419 // create the pixmap on the fly if we use Pixbuf representation:
1420 if (HasPixbuf() && !HasPixmap())
1421 {
70dcce79
VS
1422 delete M_BMPDATA->m_mask;
1423 M_BMPDATA->m_mask = new wxMask();
feac7937
VS
1424 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
1425 &M_BMPDATA->m_pixmap,
70dcce79 1426 &M_BMPDATA->m_mask->m_bitmap,
feac7937
VS
1427 128 /*threshold*/);
1428 }
feac7937 1429
fd0eed64 1430 return M_BMPDATA->m_pixmap;
ff7b1510 1431}
8bbe427f 1432
feac7937
VS
1433bool wxBitmap::HasPixmap() const
1434{
1435 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1436
1437 return M_BMPDATA->m_pixmap != NULL;
1438}
1439
91b8de8d 1440GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 1441{
223d09f6 1442 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1443
fd0eed64 1444 return M_BMPDATA->m_bitmap;
ff7b1510 1445}
feac7937 1446
feac7937
VS
1447GdkPixbuf *wxBitmap::GetPixbuf() const
1448{
1449 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
1450
6db34764
VS
1451 if (HasPixmap() && !HasPixbuf())
1452 {
1453 int width = GetWidth();
1454 int height = GetHeight();
902725ee 1455
6db34764
VS
1456 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
1457 GetMask() != NULL,
1458 8, width, height);
902725ee 1459 M_BMPDATA->m_pixbuf =
6db34764
VS
1460 gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
1461 0, 0, 0, 0, width, height);
902725ee 1462
6db34764
VS
1463 // apply the mask to created pixbuf:
1464 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
1465 {
902725ee 1466 GdkPixbuf *pmask =
6db34764
VS
1467 gdk_pixbuf_get_from_drawable(NULL,
1468 M_BMPDATA->m_mask->GetBitmap(),
1469 NULL,
1470 0, 0, 0, 0, width, height);
1471 if (pmask)
1472 {
1473 guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
1474 guchar *mask = gdk_pixbuf_get_pixels(pmask);
1475 int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
1476 int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
1477
1478 for (int y = 0; y < height;
1479 y++, bmp += bmprowinc, mask += maskrowinc)
1480 {
1481 for (int x = 0; x < width; x++, bmp += 4, mask += 3)
1482 {
1483 if (mask[0] == 0 /*black pixel*/)
1484 bmp[3] = 0;
1485 }
1486 }
902725ee 1487
6db34764
VS
1488 gdk_pixbuf_unref(pmask);
1489 }
1490 }
1491 }
1492
feac7937
VS
1493 return M_BMPDATA->m_pixbuf;
1494}
1495
1496bool wxBitmap::HasPixbuf() const
1497{
1498 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1499
1500 return M_BMPDATA->m_pixbuf != NULL;
1501}
1502
1503void wxBitmap::SetPixbuf( GdkPixbuf *pixbuf )
1504{
1505 if (!m_refData)
1506 m_refData = new wxBitmapRefData();
1507
1508 M_BMPDATA->m_pixbuf = pixbuf;
6db34764 1509 PurgeOtherRepresentations(Pixbuf);
feac7937
VS
1510}
1511
1512void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
1513{
1514 if (keep == Pixmap && HasPixbuf())
1515 {
1516 gdk_pixbuf_unref( M_BMPDATA->m_pixbuf );
1517 M_BMPDATA->m_pixbuf = NULL;
1518 }
1519 if (keep == Pixbuf && HasPixmap())
1520 {
1521 gdk_pixmap_unref( M_BMPDATA->m_pixmap );
1522 M_BMPDATA->m_pixmap = NULL;
1523 }
1524}
1525
284f2b59
RR
1526void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1527{
284f2b59
RR
1528 if (bpp != 32)
1529 return NULL;
902725ee 1530
284f2b59
RR
1531 GdkPixbuf *pixbuf = GetPixbuf();
1532 if (!pixbuf)
1533 return NULL;
1534
902725ee 1535#if 0
284f2b59
RR
1536 if (gdk_pixbuf_get_has_alpha( pixbuf ))
1537 wxPrintf( wxT("Has alpha\n") );
1538 else
1539 wxPrintf( wxT("No alpha.\n") );
1540#endif
1541
902725ee
WS
1542 data.m_height = gdk_pixbuf_get_height( pixbuf );
1543 data.m_width = gdk_pixbuf_get_width( pixbuf );
1544 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
1545
284f2b59 1546 return gdk_pixbuf_get_pixels( pixbuf );
284f2b59
RR
1547}
1548
17a1ebd1 1549void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
284f2b59
RR
1550{
1551}
1552
0ff2a74d 1553
902725ee 1554bool wxBitmap::HasAlpha() const
0ff2a74d 1555{
0ff2a74d 1556 return HasPixbuf();
0ff2a74d
RR
1557}
1558
1559void wxBitmap::UseAlpha()
902725ee 1560{
0ff2a74d 1561 GetPixbuf();
0ff2a74d
RR
1562}
1563
4b61c88d
RR
1564//-----------------------------------------------------------------------------
1565// wxBitmapHandler
1566//-----------------------------------------------------------------------------
1567
1568IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler,wxBitmapHandlerBase)
1569
1570wxBitmapHandler::~wxBitmapHandler()
1571{
1572}
1573
17a1ebd1
VZ
1574bool wxBitmapHandler::Create(wxBitmap * WXUNUSED(bitmap),
1575 void * WXUNUSED(data),
1576 long WXUNUSED(type),
1577 int WXUNUSED(width),
1578 int WXUNUSED(height),
1579 int WXUNUSED(depth))
4b61c88d 1580{
17a1ebd1
VZ
1581 wxFAIL_MSG( _T("not implemented") );
1582
902725ee 1583 return false;
4b61c88d
RR
1584}
1585
17a1ebd1
VZ
1586bool wxBitmapHandler::LoadFile(wxBitmap * WXUNUSED(bitmap),
1587 const wxString& WXUNUSED(name),
1588 long WXUNUSED(flags),
1589 int WXUNUSED(desiredWidth),
1590 int WXUNUSED(desiredHeight))
4b61c88d 1591{
17a1ebd1
VZ
1592 wxFAIL_MSG( _T("not implemented") );
1593
902725ee 1594 return false;
4b61c88d
RR
1595}
1596
17a1ebd1
VZ
1597bool wxBitmapHandler::SaveFile(const wxBitmap * WXUNUSED(bitmap),
1598 const wxString& WXUNUSED(name),
1599 int WXUNUSED(type),
1600 const wxPalette * WXUNUSED(palette))
4b61c88d 1601{
17a1ebd1
VZ
1602 wxFAIL_MSG( _T("not implemented") );
1603
902725ee 1604 return false;
4b61c88d
RR
1605}
1606
1607/* static */ void wxBitmap::InitStandardHandlers()
1608{
1609 // TODO: Insert handler based on GdkPixbufs handler later
1610}