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