]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bitmap.cpp
GTK2: gtk_draw_* -> gtk_paint_*
[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
c801d85f 1146wxBitmap::wxBitmap( const wxBitmap& bmp )
4611dd06 1147 : wxBitmapBase()
c801d85f 1148{
fd0eed64 1149 Ref( bmp );
ff7b1510 1150}
6f65e337 1151
4611dd06 1152wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
c801d85f 1153{
fd0eed64 1154 LoadFile( filename, type );
ff7b1510 1155}
c801d85f 1156
debe6624 1157wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 1158{
3ebcd89d
VZ
1159 if ( width > 0 && height > 0 )
1160 {
1161 m_refData = new wxBitmapRefData();
6f65e337 1162
3ebcd89d
VZ
1163 M_BMPDATA->m_mask = (wxMask *) NULL;
1164 M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data
1165 (
1166 wxGetRootWindow()->window,
1167 (gchar *) bits,
1168 width,
1169 height
1170 );
1171 M_BMPDATA->m_width = width;
1172 M_BMPDATA->m_height = height;
1173 M_BMPDATA->m_bpp = 1;
6f65e337 1174
3ebcd89d
VZ
1175 wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
1176 }
6f65e337 1177}
8bbe427f
VZ
1178
1179wxBitmap::~wxBitmap()
c801d85f 1180{
ff7b1510 1181}
8bbe427f 1182
c801d85f
KB
1183wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
1184{
7ecb8b06
VZ
1185 if ( m_refData != bmp.m_refData )
1186 Ref( bmp );
1187
8bbe427f 1188 return *this;
ff7b1510 1189}
8bbe427f 1190
f6bcfd97 1191bool wxBitmap::operator == ( const wxBitmap& bmp ) const
c801d85f 1192{
8bbe427f 1193 return m_refData == bmp.m_refData;
ff7b1510 1194}
8bbe427f 1195
f6bcfd97 1196bool wxBitmap::operator != ( const wxBitmap& bmp ) const
c801d85f 1197{
8bbe427f 1198 return m_refData != bmp.m_refData;
ff7b1510 1199}
8bbe427f 1200
91b8de8d 1201bool wxBitmap::Ok() const
c801d85f 1202{
902725ee 1203 return (m_refData != NULL) &&
feac7937 1204 (
feac7937 1205 M_BMPDATA->m_pixbuf ||
902725ee 1206 M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap
feac7937 1207 );
ff7b1510 1208}
8bbe427f 1209
91b8de8d 1210int wxBitmap::GetHeight() const
c801d85f 1211{
223d09f6 1212 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
e55ad60e 1213
fd0eed64 1214 return M_BMPDATA->m_height;
ff7b1510 1215}
c801d85f 1216
91b8de8d 1217int wxBitmap::GetWidth() const
c801d85f 1218{
223d09f6 1219 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 1220
fd0eed64 1221 return M_BMPDATA->m_width;
ff7b1510 1222}
c801d85f 1223
91b8de8d 1224int wxBitmap::GetDepth() const
c801d85f 1225{
223d09f6 1226 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 1227
fd0eed64 1228 return M_BMPDATA->m_bpp;
ff7b1510 1229}
c801d85f 1230
91b8de8d 1231wxMask *wxBitmap::GetMask() const
c801d85f 1232{
223d09f6 1233 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
8bbe427f 1234
fd0eed64 1235 return M_BMPDATA->m_mask;
ff7b1510 1236}
c801d85f
KB
1237
1238void wxBitmap::SetMask( wxMask *mask )
1239{
223d09f6 1240 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
8bbe427f 1241
fd0eed64 1242 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 1243
fd0eed64 1244 M_BMPDATA->m_mask = mask;
ff7b1510 1245}
c801d85f 1246
db0aec83
VS
1247bool wxBitmap::CopyFromIcon(const wxIcon& icon)
1248{
1249 *this = icon;
902725ee 1250 return true;
db0aec83
VS
1251}
1252
17bec151
RR
1253wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1254{
1255 wxCHECK_MSG( Ok() &&
13111b2a
VZ
1256 (rect.x >= 0) && (rect.y >= 0) &&
1257 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
17bec151 1258 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
13111b2a 1259
17bec151
RR
1260 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
1261 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
13111b2a 1262
feac7937 1263 if (HasPixbuf())
17bec151 1264 {
feac7937 1265 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
6db34764 1266 gdk_pixbuf_get_has_alpha(GetPixbuf()),
17f504ec 1267 8, rect.width, rect.height);
feac7937 1268 ret.SetPixbuf(pixbuf);
6db34764 1269 gdk_pixbuf_copy_area(GetPixbuf(),
feac7937
VS
1270 rect.x, rect.y, rect.width, rect.height,
1271 pixbuf, 0, 0);
17bec151
RR
1272 }
1273 else
1274 {
feac7937
VS
1275 if (ret.GetPixmap())
1276 {
1277 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
1278 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1279 gdk_gc_destroy( gc );
1280 }
1281 else
1282 {
1283 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
1284 GdkColor col;
1285 col.pixel = 0xFFFFFF;
1286 gdk_gc_set_foreground( gc, &col );
1287 col.pixel = 0;
1288 gdk_gc_set_background( gc, &col );
1289 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1290 gdk_gc_destroy( gc );
1291 }
17bec151 1292 }
13111b2a 1293
17bec151
RR
1294 if (GetMask())
1295 {
1296 wxMask *mask = new wxMask;
c2fa61e8 1297 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
13111b2a 1298
17bec151 1299 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
f9ebac93
RR
1300 GdkColor col;
1301 col.pixel = 0xFFFFFF;
1302 gdk_gc_set_foreground( gc, &col );
1303 col.pixel = 0;
1304 gdk_gc_set_background( gc, &col );
1305 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
1306 gdk_gc_destroy( gc );
1307
1308 ret.SetMask( mask );
17bec151 1309 }
13111b2a 1310
17bec151
RR
1311 return ret;
1312}
1313
4611dd06 1314bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
c801d85f 1315{
902725ee 1316 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
8bbe427f 1317
b75dd496 1318 // Try to save the bitmap via wxImage handlers:
fd0eed64 1319 {
368d59f0 1320 wxImage image = ConvertToImage();
284b4c88 1321 if (image.Ok()) return image.SaveFile( name, type );
fd0eed64 1322 }
8bbe427f 1323
902725ee 1324 return false;
ff7b1510 1325}
c801d85f 1326
4611dd06 1327bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
c801d85f 1328{
fd0eed64 1329 UnRef();
8bbe427f 1330
3ebcd89d 1331 if (!wxFileExists(name))
902725ee 1332 return false;
8bbe427f 1333
005f5d18 1334 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 1335
fd0eed64
RR
1336 if (type == wxBITMAP_TYPE_XPM)
1337 {
1338 m_refData = new wxBitmapRefData();
8bbe427f 1339
fd0eed64 1340 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 1341
3ebcd89d
VZ
1342 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm
1343 (
1344 wxGetRootWindow()->window,
1345 &mask,
1346 NULL,
1347 name.fn_str()
1348 );
8bbe427f 1349
fd0eed64
RR
1350 if (mask)
1351 {
1352 M_BMPDATA->m_mask = new wxMask();
1353 M_BMPDATA->m_mask->m_bitmap = mask;
1354 }
8bbe427f 1355
fd0eed64 1356 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
c2fa61e8 1357
103aab26 1358 M_BMPDATA->m_bpp = visual->depth;
fd0eed64 1359 }
b75dd496 1360 else // try if wxImage can load it
fd0eed64
RR
1361 {
1362 wxImage image;
3ebcd89d 1363 if ( !image.LoadFile( name, type ) || !image.Ok() )
902725ee 1364 return false;
3ebcd89d
VZ
1365
1366 *this = wxBitmap(image);
fd0eed64 1367 }
8bbe427f 1368
902725ee 1369 return true;
ff7b1510 1370}
8bbe427f 1371
0b04c4e0 1372#if wxUSE_PALETTE
91b8de8d 1373wxPalette *wxBitmap::GetPalette() const
c801d85f 1374{
3ebcd89d
VZ
1375 if (!Ok())
1376 return (wxPalette *) NULL;
8bbe427f 1377
fd0eed64 1378 return M_BMPDATA->m_palette;
ff7b1510 1379}
c801d85f 1380
4611dd06
RR
1381void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
1382{
1383 // TODO
1384}
0b04c4e0 1385#endif // wxUSE_PALETTE
4611dd06 1386
4bc67cc5
RR
1387void wxBitmap::SetHeight( int height )
1388{
3ebcd89d
VZ
1389 if (!m_refData)
1390 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1391
1392 M_BMPDATA->m_height = height;
1393}
1394
1395void wxBitmap::SetWidth( int width )
1396{
3ebcd89d
VZ
1397 if (!m_refData)
1398 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1399
1400 M_BMPDATA->m_width = width;
1401}
1402
1403void wxBitmap::SetDepth( int depth )
1404{
3ebcd89d
VZ
1405 if (!m_refData)
1406 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1407
1408 M_BMPDATA->m_bpp = depth;
1409}
1410
1411void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1412{
3ebcd89d
VZ
1413 if (!m_refData)
1414 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1415
1416 M_BMPDATA->m_pixmap = pixmap;
6db34764 1417 PurgeOtherRepresentations(Pixmap);
4bc67cc5
RR
1418}
1419
82ea63e6
RR
1420void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1421{
3ebcd89d
VZ
1422 if (!m_refData)
1423 m_refData = new wxBitmapRefData();
82ea63e6
RR
1424
1425 M_BMPDATA->m_bitmap = bitmap;
6db34764 1426 PurgeOtherRepresentations(Pixmap);
82ea63e6
RR
1427}
1428
91b8de8d 1429GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 1430{
223d09f6 1431 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1432
feac7937
VS
1433 // create the pixmap on the fly if we use Pixbuf representation:
1434 if (HasPixbuf() && !HasPixmap())
1435 {
70dcce79
VS
1436 delete M_BMPDATA->m_mask;
1437 M_BMPDATA->m_mask = new wxMask();
feac7937
VS
1438 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
1439 &M_BMPDATA->m_pixmap,
70dcce79 1440 &M_BMPDATA->m_mask->m_bitmap,
feac7937
VS
1441 128 /*threshold*/);
1442 }
feac7937 1443
fd0eed64 1444 return M_BMPDATA->m_pixmap;
ff7b1510 1445}
8bbe427f 1446
feac7937
VS
1447bool wxBitmap::HasPixmap() const
1448{
1449 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1450
1451 return M_BMPDATA->m_pixmap != NULL;
1452}
1453
91b8de8d 1454GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 1455{
223d09f6 1456 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1457
fd0eed64 1458 return M_BMPDATA->m_bitmap;
ff7b1510 1459}
feac7937 1460
feac7937
VS
1461GdkPixbuf *wxBitmap::GetPixbuf() const
1462{
1463 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
1464
6db34764
VS
1465 if (HasPixmap() && !HasPixbuf())
1466 {
1467 int width = GetWidth();
1468 int height = GetHeight();
902725ee 1469
6db34764
VS
1470 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
1471 GetMask() != NULL,
1472 8, width, height);
902725ee 1473 M_BMPDATA->m_pixbuf =
6db34764
VS
1474 gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
1475 0, 0, 0, 0, width, height);
902725ee 1476
6db34764
VS
1477 // apply the mask to created pixbuf:
1478 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
1479 {
902725ee 1480 GdkPixbuf *pmask =
6db34764
VS
1481 gdk_pixbuf_get_from_drawable(NULL,
1482 M_BMPDATA->m_mask->GetBitmap(),
1483 NULL,
1484 0, 0, 0, 0, width, height);
1485 if (pmask)
1486 {
1487 guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
1488 guchar *mask = gdk_pixbuf_get_pixels(pmask);
1489 int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
1490 int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
1491
1492 for (int y = 0; y < height;
1493 y++, bmp += bmprowinc, mask += maskrowinc)
1494 {
1495 for (int x = 0; x < width; x++, bmp += 4, mask += 3)
1496 {
1497 if (mask[0] == 0 /*black pixel*/)
1498 bmp[3] = 0;
1499 }
1500 }
902725ee 1501
6db34764
VS
1502 gdk_pixbuf_unref(pmask);
1503 }
1504 }
1505 }
1506
feac7937
VS
1507 return M_BMPDATA->m_pixbuf;
1508}
1509
1510bool wxBitmap::HasPixbuf() const
1511{
1512 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1513
1514 return M_BMPDATA->m_pixbuf != NULL;
1515}
1516
1517void wxBitmap::SetPixbuf( GdkPixbuf *pixbuf )
1518{
1519 if (!m_refData)
1520 m_refData = new wxBitmapRefData();
1521
1522 M_BMPDATA->m_pixbuf = pixbuf;
6db34764 1523 PurgeOtherRepresentations(Pixbuf);
feac7937
VS
1524}
1525
1526void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
1527{
1528 if (keep == Pixmap && HasPixbuf())
1529 {
1530 gdk_pixbuf_unref( M_BMPDATA->m_pixbuf );
1531 M_BMPDATA->m_pixbuf = NULL;
1532 }
1533 if (keep == Pixbuf && HasPixmap())
1534 {
1535 gdk_pixmap_unref( M_BMPDATA->m_pixmap );
1536 M_BMPDATA->m_pixmap = NULL;
1537 }
1538}
1539
284f2b59
RR
1540void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1541{
284f2b59
RR
1542 if (bpp != 32)
1543 return NULL;
902725ee 1544
284f2b59
RR
1545 GdkPixbuf *pixbuf = GetPixbuf();
1546 if (!pixbuf)
1547 return NULL;
1548
902725ee 1549#if 0
284f2b59
RR
1550 if (gdk_pixbuf_get_has_alpha( pixbuf ))
1551 wxPrintf( wxT("Has alpha\n") );
1552 else
1553 wxPrintf( wxT("No alpha.\n") );
1554#endif
1555
902725ee
WS
1556 data.m_height = gdk_pixbuf_get_height( pixbuf );
1557 data.m_width = gdk_pixbuf_get_width( pixbuf );
1558 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
1559
284f2b59 1560 return gdk_pixbuf_get_pixels( pixbuf );
284f2b59
RR
1561}
1562
17a1ebd1 1563void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
284f2b59
RR
1564{
1565}
1566
0ff2a74d 1567
902725ee 1568bool wxBitmap::HasAlpha() const
0ff2a74d 1569{
0ff2a74d 1570 return HasPixbuf();
0ff2a74d
RR
1571}
1572
1573void wxBitmap::UseAlpha()
902725ee 1574{
0ff2a74d 1575 GetPixbuf();
0ff2a74d
RR
1576}
1577
4b61c88d
RR
1578//-----------------------------------------------------------------------------
1579// wxBitmapHandler
1580//-----------------------------------------------------------------------------
1581
1582IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler,wxBitmapHandlerBase)
1583
1584wxBitmapHandler::~wxBitmapHandler()
1585{
1586}
1587
17a1ebd1
VZ
1588bool wxBitmapHandler::Create(wxBitmap * WXUNUSED(bitmap),
1589 void * WXUNUSED(data),
1590 long WXUNUSED(type),
1591 int WXUNUSED(width),
1592 int WXUNUSED(height),
1593 int WXUNUSED(depth))
4b61c88d 1594{
17a1ebd1
VZ
1595 wxFAIL_MSG( _T("not implemented") );
1596
902725ee 1597 return false;
4b61c88d
RR
1598}
1599
17a1ebd1
VZ
1600bool wxBitmapHandler::LoadFile(wxBitmap * WXUNUSED(bitmap),
1601 const wxString& WXUNUSED(name),
1602 long WXUNUSED(flags),
1603 int WXUNUSED(desiredWidth),
1604 int WXUNUSED(desiredHeight))
4b61c88d 1605{
17a1ebd1
VZ
1606 wxFAIL_MSG( _T("not implemented") );
1607
902725ee 1608 return false;
4b61c88d
RR
1609}
1610
17a1ebd1
VZ
1611bool wxBitmapHandler::SaveFile(const wxBitmap * WXUNUSED(bitmap),
1612 const wxString& WXUNUSED(name),
1613 int WXUNUSED(type),
1614 const wxPalette * WXUNUSED(palette))
4b61c88d 1615{
17a1ebd1
VZ
1616 wxFAIL_MSG( _T("not implemented") );
1617
902725ee 1618 return false;
4b61c88d
RR
1619}
1620
1621/* static */ void wxBitmap::InitStandardHandlers()
1622{
1623 // TODO: Insert handler based on GdkPixbufs handler later
1624}