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