]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
fix wxStringOutputStream::Write() in Unicode build when the output overlaps a boundar...
[wxWidgets.git] / src / gtk / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bitmap.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/bitmap.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/app.h"
17 #include "wx/palette.h"
18 #include "wx/icon.h"
19 #include "wx/math.h"
20 #include "wx/image.h"
21 #include "wx/colour.h"
22 #endif
23
24 #include "wx/rawbmp.h"
25
26 #include <gtk/gtk.h>
27
28 //-----------------------------------------------------------------------------
29 // data
30 //-----------------------------------------------------------------------------
31
32 extern GtkWidget *wxGetRootWindow();
33
34 //-----------------------------------------------------------------------------
35 // wxMask
36 //-----------------------------------------------------------------------------
37
38 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
39
40 wxMask::wxMask()
41 {
42 m_bitmap = (GdkBitmap *) NULL;
43 }
44
45 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
46 {
47 m_bitmap = (GdkBitmap *) NULL;
48 Create( bitmap, colour );
49 }
50
51 #if wxUSE_PALETTE
52 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
53 {
54 m_bitmap = (GdkBitmap *) NULL;
55 Create( bitmap, paletteIndex );
56 }
57 #endif // wxUSE_PALETTE
58
59 wxMask::wxMask( const wxBitmap& bitmap )
60 {
61 m_bitmap = (GdkBitmap *) NULL;
62 Create( bitmap );
63 }
64
65 wxMask::~wxMask()
66 {
67 if (m_bitmap)
68 g_object_unref (m_bitmap);
69 }
70
71 bool wxMask::Create( const wxBitmap& bitmap,
72 const wxColour& colour )
73 {
74 if (m_bitmap)
75 {
76 g_object_unref (m_bitmap);
77 m_bitmap = (GdkBitmap*) NULL;
78 }
79
80 const int w = bitmap.GetWidth();
81 const int h = bitmap.GetHeight();
82
83 // create mask as XBM format bitmap
84
85 // one bit per pixel, each row starts on a byte boundary
86 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
87 wxByte* out = new wxByte[out_size];
88 // set bits are unmasked
89 memset(out, 0xff, out_size);
90 unsigned bit_index = 0;
91 if (bitmap.HasPixbuf())
92 {
93 const wxByte r_mask = colour.Red();
94 const wxByte g_mask = colour.Green();
95 const wxByte b_mask = colour.Blue();
96 GdkPixbuf* pixbuf = bitmap.GetPixbuf();
97 const wxByte* in = gdk_pixbuf_get_pixels(pixbuf);
98 const int inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0);
99 const int rowpadding = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
100 for (int y = 0; y < h; y++, in += rowpadding)
101 {
102 for (int x = 0; x < w; x++, in += inc, bit_index++)
103 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
104 out[bit_index >> 3] ^= 1 << (bit_index & 7);
105 // move index to next byte boundary
106 bit_index = (bit_index + 7) & ~7u;
107 }
108 }
109 else
110 {
111 GdkImage* image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h);
112 GdkColormap* colormap = gdk_image_get_colormap(image);
113 guint32 mask_pixel;
114 if (colormap == NULL)
115 // mono bitmap, white is pixel value 0
116 mask_pixel = guint32(colour.Red() != 255 || colour.Green() != 255 || colour.Blue() != 255);
117 else
118 {
119 wxColor c(colour);
120 c.CalcPixel(colormap);
121 mask_pixel = c.GetPixel();
122 }
123 for (int y = 0; y < h; y++)
124 {
125 for (int x = 0; x < w; x++, bit_index++)
126 if (gdk_image_get_pixel(image, x, y) == mask_pixel)
127 out[bit_index >> 3] ^= 1 << (bit_index & 7);
128 bit_index = (bit_index + 7) & ~7u;
129 }
130 g_object_unref(image);
131 }
132 m_bitmap = gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h);
133 delete[] out;
134 return true;
135 }
136
137 #if wxUSE_PALETTE
138 bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
139 {
140 unsigned char r,g,b;
141 wxPalette *pal = bitmap.GetPalette();
142
143 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
144
145 pal->GetRGB(paletteIndex, &r, &g, &b);
146
147 return Create(bitmap, wxColour(r, g, b));
148 }
149 #endif // wxUSE_PALETTE
150
151 bool wxMask::Create( const wxBitmap& bitmap )
152 {
153 if (m_bitmap)
154 {
155 g_object_unref (m_bitmap);
156 m_bitmap = (GdkBitmap*) NULL;
157 }
158
159 if (!bitmap.Ok()) return false;
160
161 wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
162
163 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
164
165 if (!m_bitmap) return false;
166
167 GdkGC *gc = gdk_gc_new( m_bitmap );
168 gdk_gc_set_function(gc, GDK_COPY_INVERT);
169 gdk_draw_drawable(m_bitmap, gc, bitmap.GetPixmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight());
170 g_object_unref (gc);
171
172 return true;
173 }
174
175 GdkBitmap *wxMask::GetBitmap() const
176 {
177 return m_bitmap;
178 }
179
180 //-----------------------------------------------------------------------------
181 // wxBitmap
182 //-----------------------------------------------------------------------------
183
184 class wxBitmapRefData: public wxObjectRefData
185 {
186 public:
187 wxBitmapRefData();
188 virtual ~wxBitmapRefData();
189
190 GdkPixmap *m_pixmap;
191 GdkPixbuf *m_pixbuf;
192 wxMask *m_mask;
193 int m_width;
194 int m_height;
195 int m_bpp;
196 #if wxUSE_PALETTE
197 wxPalette *m_palette;
198 #endif // wxUSE_PALETTE
199 };
200
201 wxBitmapRefData::wxBitmapRefData()
202 {
203 m_pixmap = (GdkPixmap *) NULL;
204 m_pixbuf = (GdkPixbuf *) NULL;
205 m_mask = (wxMask *) NULL;
206 m_width = 0;
207 m_height = 0;
208 m_bpp = 0;
209 #if wxUSE_PALETTE
210 m_palette = (wxPalette *) NULL;
211 #endif // wxUSE_PALETTE
212 }
213
214 wxBitmapRefData::~wxBitmapRefData()
215 {
216 if (m_pixmap)
217 g_object_unref (m_pixmap);
218 if (m_pixbuf)
219 g_object_unref (m_pixbuf);
220 delete m_mask;
221 #if wxUSE_PALETTE
222 delete m_palette;
223 #endif // wxUSE_PALETTE
224 }
225
226 //-----------------------------------------------------------------------------
227
228 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
229
230 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
231
232 wxBitmap::wxBitmap(int width, int height, int depth)
233 {
234 Create(width, height, depth);
235 }
236
237 wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
238 {
239 LoadFile(filename, type);
240 }
241
242 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
243 {
244 wxASSERT(depth == 1);
245 if (width > 0 && height > 0 && depth == 1)
246 {
247 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, bits, width, height));
248
249 wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("couldn't create bitmap") );
250 }
251 }
252
253 wxBitmap::wxBitmap(const char* const* bits)
254 {
255 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
256
257 GdkBitmap* mask = NULL;
258 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, wx_const_cast(char**, bits)));
259
260 if (M_BMPDATA->m_pixmap != NULL && mask != NULL)
261 {
262 M_BMPDATA->m_mask = new wxMask;
263 M_BMPDATA->m_mask->m_bitmap = mask;
264 }
265 }
266
267 wxBitmap::~wxBitmap()
268 {
269 }
270
271 bool wxBitmap::Create( int width, int height, int depth )
272 {
273 UnRef();
274
275 if ( width <= 0 || height <= 0 )
276 {
277 return false;
278 }
279
280 if (depth == 32)
281 {
282 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, width, height), 32);
283 }
284 else
285 {
286 if (depth != 1)
287 {
288 const GdkVisual* visual = wxTheApp->GetGdkVisual();
289 if (depth == -1)
290 depth = visual->depth;
291
292 wxCHECK_MSG(depth == visual->depth, false, wxT("invalid bitmap depth"));
293 }
294
295 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth));
296 }
297
298 return Ok();
299 }
300
301 wxBitmap wxBitmap::Rescale(int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy) const
302 {
303 wxBitmap bmp;
304
305 wxCHECK_MSG(Ok(), bmp, wxT("invalid bitmap"));
306
307 if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height)
308 return *this;
309
310 int width = wxMax(newx, 1);
311 int height = wxMax(newy, 1);
312 width = wxMin(width, clipwidth);
313 height = wxMin(height, clipheight);
314
315 // scale pixbuf if available and it has alpha or there is no mask
316 if (M_BMPDATA->m_pixbuf != NULL && (
317 M_BMPDATA->m_mask == NULL || gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf)))
318 {
319 bmp.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB,
320 gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf),
321 8, width, height), M_BMPDATA->m_bpp);
322 gdk_pixbuf_scale(M_BMPDATA->m_pixbuf, bmp.GetPixbuf(),
323 0, 0, width, height,
324 clipx, clipy,
325 (double)newx/GetWidth(), (double)newy/GetHeight(),
326 GDK_INTERP_BILINEAR);
327 }
328 else
329 {
330 GdkImage* img = gdk_drawable_get_image(
331 M_BMPDATA->m_pixmap, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height);
332
333 wxCHECK_MSG(img, bmp, wxT("couldn't create image"));
334
335 GdkGC *gc = NULL;
336 GdkPixmap *dstpix = NULL;
337 char *dst = NULL;
338 long dstbyteperline = 0;
339
340 if (GetDepth() != 1)
341 {
342 bmp.Create(width, height, gdk_drawable_get_depth(M_BMPDATA->m_pixmap));
343 dstpix = bmp.GetPixmap();
344 gc = gdk_gc_new( dstpix );
345 }
346 else
347 {
348 dstbyteperline = (width + 7) / 8;
349 dst = (char*) malloc(dstbyteperline*height);
350 }
351
352 // be careful to use the right scaling factor
353 float scx = (float)M_BMPDATA->m_width/(float)newx;
354 float scy = (float)M_BMPDATA->m_height/(float)newy;
355 // prepare accel-tables
356 int *tablex = (int *)calloc(width,sizeof(int));
357 int *tabley = (int *)calloc(height,sizeof(int));
358
359 // accel table filled with clipped values
360 for (int x = 0; x < width; x++)
361 tablex[x] = (int) (scx * (x+clipx));
362 for (int y = 0; y < height; y++)
363 tabley[y] = (int) (scy * (y+clipy));
364
365 // Main rescaling routine starts here
366 for (int h = 0; h < height; h++)
367 {
368 char outbyte = 0;
369 int old_x = -1;
370 guint32 old_pixval = 0;
371
372 for (int w = 0; w < width; w++)
373 {
374 guint32 pixval;
375 int x = tablex[w];
376 if (x == old_x)
377 pixval = old_pixval;
378 else
379 {
380 pixval = gdk_image_get_pixel( img, x, tabley[h] );
381 old_pixval = pixval;
382 old_x = x;
383 }
384
385 if ( dst )
386 {
387 if (pixval)
388 {
389 char bit=1;
390 char shift = bit << (w % 8);
391 outbyte |= shift;
392 }
393
394 if ((w+1)%8==0)
395 {
396 dst[h*dstbyteperline+w/8] = outbyte;
397 outbyte = 0;
398 }
399 }
400 else
401 {
402 GdkColor col;
403 col.pixel = pixval;
404 gdk_gc_set_foreground( gc, &col );
405 gdk_draw_point( dstpix, gc, w, h);
406 }
407 }
408
409 // do not forget the last byte
410 if ( dst && (width % 8 != 0) )
411 dst[h*dstbyteperline+width/8] = outbyte;
412 }
413
414 g_object_unref (img);
415 if (gc) g_object_unref (gc);
416
417 if ( dst )
418 {
419 bmp = wxBitmap(dst, width, height, 1);
420 free( dst );
421 }
422
423 if (GetMask())
424 {
425 dstbyteperline = (width + 7) / 8;
426 dst = (char*) malloc(dstbyteperline*height);
427 img = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
428
429 for (int h = 0; h < height; h++)
430 {
431 char outbyte = 0;
432 int old_x = -1;
433 guint32 old_pixval = 0;
434
435 for (int w = 0; w < width; w++)
436 {
437 guint32 pixval;
438 int x = tablex[w];
439 if (x == old_x)
440 pixval = old_pixval;
441 else
442 {
443 pixval = gdk_image_get_pixel( img, x, tabley[h] );
444 old_pixval = pixval;
445 old_x = x;
446 }
447
448 if (pixval)
449 {
450 char bit=1;
451 char shift = bit << (w % 8);
452 outbyte |= shift;
453 }
454
455 if ((w+1)%8 == 0)
456 {
457 dst[h*dstbyteperline+w/8] = outbyte;
458 outbyte = 0;
459 }
460 }
461
462 // do not forget the last byte
463 if (width % 8 != 0)
464 dst[h*dstbyteperline+width/8] = outbyte;
465 }
466 wxMask* mask = new wxMask;
467 mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height );
468 bmp.SetMask(mask);
469
470 free( dst );
471 g_object_unref (img);
472 }
473
474 free( tablex );
475 free( tabley );
476 }
477
478 return bmp;
479 }
480
481 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
482 {
483 UnRef();
484
485 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
486 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
487
488 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
489 return false;
490
491 // create pixbuf if image has alpha and requested depth is compatible
492 if (image.HasAlpha() && (depth == -1 || depth == 32))
493 return CreateFromImageAsPixbuf(image);
494
495 // otherwise create pixmap, if alpha is present it will be converted to mask
496 return CreateFromImageAsPixmap(image, depth);
497 }
498
499 bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth)
500 {
501 const int w = image.GetWidth();
502 const int h = image.GetHeight();
503 if (depth == 1)
504 {
505 // create XBM format bitmap
506
507 // one bit per pixel, each row starts on a byte boundary
508 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
509 wxByte* out = new wxByte[out_size];
510 // set bits are black
511 memset(out, 0xff, out_size);
512 const wxByte* in = image.GetData();
513 unsigned bit_index = 0;
514 for (int y = 0; y < h; y++)
515 {
516 for (int x = 0; x < w; x++, in += 3, bit_index++)
517 if (in[0] == 255 && in[1] == 255 && in[2] == 255)
518 out[bit_index >> 3] ^= 1 << (bit_index & 7);
519 // move index to next byte boundary
520 bit_index = (bit_index + 7) & ~7u;
521 }
522 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h));
523 delete[] out;
524 }
525 else
526 {
527 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth));
528 GdkGC* gc = gdk_gc_new(M_BMPDATA->m_pixmap);
529 gdk_draw_rgb_image(
530 M_BMPDATA->m_pixmap, gc,
531 0, 0, w, h,
532 GDK_RGB_DITHER_NONE, image.GetData(), w * 3);
533 g_object_unref(gc);
534 }
535
536 const wxByte* alpha = image.GetAlpha();
537 if (alpha != NULL || image.HasMask())
538 {
539 // create mask as XBM format bitmap
540
541 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
542 wxByte* out = new wxByte[out_size];
543 memset(out, 0xff, out_size);
544 unsigned bit_index = 0;
545 if (alpha != NULL)
546 {
547 for (int y = 0; y < h; y++)
548 {
549 for (int x = 0; x < w; x++, bit_index++)
550 if (*alpha++ < wxIMAGE_ALPHA_THRESHOLD)
551 out[bit_index >> 3] ^= 1 << (bit_index & 7);
552 bit_index = (bit_index + 7) & ~7u;
553 }
554 }
555 else
556 {
557 const wxByte r_mask = image.GetMaskRed();
558 const wxByte g_mask = image.GetMaskGreen();
559 const wxByte b_mask = image.GetMaskBlue();
560 const wxByte* in = image.GetData();
561 for (int y = 0; y < h; y++)
562 {
563 for (int x = 0; x < w; x++, in += 3, bit_index++)
564 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
565 out[bit_index >> 3] ^= 1 << (bit_index & 7);
566 bit_index = (bit_index + 7) & ~7u;
567 }
568 }
569 wxMask* mask = new wxMask;
570 mask->m_bitmap = gdk_bitmap_create_from_data(M_BMPDATA->m_pixmap, (char*)out, w, h);
571 SetMask(mask);
572 delete[] out;
573 }
574 return true;
575 }
576
577 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
578 {
579 wxASSERT(image.HasAlpha());
580
581 int width = image.GetWidth();
582 int height = image.GetHeight();
583
584 Create(width, height, 32);
585 GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf;
586 if (!pixbuf)
587 return false;
588
589 // Copy the data:
590 const unsigned char* in = image.GetData();
591 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
592 unsigned char *alpha = image.GetAlpha();
593
594 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
595
596 for (int y = 0; y < height; y++, out += rowpad)
597 {
598 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
599 {
600 out[0] = in[0];
601 out[1] = in[1];
602 out[2] = in[2];
603 out[3] = *alpha;
604 }
605 }
606
607 return true;
608 }
609
610 wxImage wxBitmap::ConvertToImage() const
611 {
612 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
613
614 const int w = GetWidth();
615 const int h = GetHeight();
616 wxImage image(w, h, false);
617 unsigned char *data = image.GetData();
618
619 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
620
621 // prefer pixbuf if available, it will preserve alpha and should be quicker
622 if (HasPixbuf())
623 {
624 GdkPixbuf *pixbuf = GetPixbuf();
625 unsigned char* alpha = NULL;
626 if (gdk_pixbuf_get_has_alpha(pixbuf))
627 {
628 image.SetAlpha();
629 alpha = image.GetAlpha();
630 }
631 const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf);
632 unsigned char *out = data;
633 const int inc = 3 + int(alpha != NULL);
634 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
635
636 for (int y = 0; y < h; y++, in += rowpad)
637 {
638 for (int x = 0; x < w; x++, in += inc, out += 3)
639 {
640 out[0] = in[0];
641 out[1] = in[1];
642 out[2] = in[2];
643 if (alpha != NULL)
644 *alpha++ = in[3];
645 }
646 }
647 }
648 else
649 {
650 GdkPixmap* pixmap = GetPixmap();
651 GdkPixmap* pixmap_invert = NULL;
652 if (GetDepth() == 1)
653 {
654 // mono bitmaps are inverted, i.e. 0 is white
655 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
656 GdkGC* gc = gdk_gc_new(pixmap_invert);
657 gdk_gc_set_function(gc, GDK_COPY_INVERT);
658 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
659 g_object_unref(gc);
660 pixmap = pixmap_invert;
661 }
662 // create a pixbuf which shares data with the wxImage
663 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(
664 data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL);
665
666 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
667
668 g_object_unref(pixbuf);
669 if (pixmap_invert != NULL)
670 g_object_unref(pixmap_invert);
671 }
672 // convert mask, unless there is already alpha
673 if (GetMask() && !image.HasAlpha())
674 {
675 // we hard code the mask colour for now but we could also make an
676 // effort (and waste time) to choose a colour not present in the
677 // image already to avoid having to fudge the pixels below --
678 // whether it's worth to do it is unclear however
679 const int MASK_RED = 1;
680 const int MASK_GREEN = 2;
681 const int MASK_BLUE = 3;
682 const int MASK_BLUE_REPLACEMENT = 2;
683
684 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
685 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
686
687 for (int y = 0; y < h; y++)
688 {
689 for (int x = 0; x < w; x++, data += 3)
690 {
691 if (gdk_image_get_pixel(image_mask, x, y) == 0)
692 {
693 data[0] = MASK_RED;
694 data[1] = MASK_GREEN;
695 data[2] = MASK_BLUE;
696 }
697 else if (data[0] == MASK_RED && data[1] == MASK_GREEN && data[2] == MASK_BLUE)
698 {
699 // we have to fudge the colour a bit to prevent
700 // this pixel from appearing transparent
701 data[2] = MASK_BLUE_REPLACEMENT;
702 }
703 }
704 }
705 g_object_unref(image_mask);
706 }
707
708 return image;
709 }
710
711 bool wxBitmap::IsOk() const
712 {
713 return (m_refData != NULL) &&
714 (
715 M_BMPDATA->m_pixbuf ||
716 M_BMPDATA->m_pixmap
717 );
718 }
719
720 int wxBitmap::GetHeight() const
721 {
722 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
723
724 return M_BMPDATA->m_height;
725 }
726
727 int wxBitmap::GetWidth() const
728 {
729 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
730
731 return M_BMPDATA->m_width;
732 }
733
734 int wxBitmap::GetDepth() const
735 {
736 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
737
738 return M_BMPDATA->m_bpp;
739 }
740
741 wxMask *wxBitmap::GetMask() const
742 {
743 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
744
745 return M_BMPDATA->m_mask;
746 }
747
748 void wxBitmap::SetMask( wxMask *mask )
749 {
750 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
751
752 AllocExclusive();
753 delete M_BMPDATA->m_mask;
754 M_BMPDATA->m_mask = mask;
755 }
756
757 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
758 {
759 *this = icon;
760 return Ok();
761 }
762
763 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
764 {
765 wxBitmap ret;
766
767 wxCHECK_MSG(Ok(), ret, wxT("invalid bitmap"));
768 wxCHECK_MSG(rect.x >= 0 && rect.y >= 0 &&
769 rect.x + rect.width <= M_BMPDATA->m_width &&
770 rect.y + rect.height <= M_BMPDATA->m_height,
771 ret, wxT("invalid bitmap region"));
772
773 if (HasPixbuf() || M_BMPDATA->m_bpp == 32)
774 {
775 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
776 gdk_pixbuf_get_has_alpha(GetPixbuf()),
777 8, rect.width, rect.height);
778 ret.SetPixbuf(pixbuf, M_BMPDATA->m_bpp);
779 gdk_pixbuf_copy_area(GetPixbuf(),
780 rect.x, rect.y, rect.width, rect.height,
781 pixbuf, 0, 0);
782 }
783 else
784 {
785 ret.Create(rect.width, rect.height, M_BMPDATA->m_bpp);
786 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
787 gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
788 g_object_unref (gc);
789 }
790 // make mask, unless there is already alpha
791 if (GetMask() && !HasAlpha())
792 {
793 wxMask *mask = new wxMask;
794 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
795
796 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
797 gdk_draw_drawable(mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height);
798 g_object_unref (gc);
799
800 ret.SetMask( mask );
801 }
802
803 return ret;
804 }
805
806 bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
807 {
808 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
809
810 // Try to save the bitmap via wxImage handlers:
811 wxImage image = ConvertToImage();
812 return image.Ok() && image.SaveFile(name, type);
813 }
814
815 bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
816 {
817 UnRef();
818
819 if (type == wxBITMAP_TYPE_XPM)
820 {
821 GdkBitmap *mask = (GdkBitmap*) NULL;
822 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str()));
823
824 if (mask)
825 {
826 M_BMPDATA->m_mask = new wxMask;
827 M_BMPDATA->m_mask->m_bitmap = mask;
828 }
829 }
830 else // try if wxImage can load it
831 {
832 wxImage image;
833 if (image.LoadFile(name, type) && image.Ok())
834 CreateFromImage(image, -1);
835 }
836
837 return Ok();
838 }
839
840 #if wxUSE_PALETTE
841 wxPalette *wxBitmap::GetPalette() const
842 {
843 wxCHECK_MSG(Ok(), NULL, wxT("invalid bitmap"));
844
845 return M_BMPDATA->m_palette;
846 }
847
848 void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
849 {
850 // TODO
851 }
852 #endif // wxUSE_PALETTE
853
854 void wxBitmap::SetHeight( int height )
855 {
856 AllocExclusive();
857 M_BMPDATA->m_height = height;
858 }
859
860 void wxBitmap::SetWidth( int width )
861 {
862 AllocExclusive();
863 M_BMPDATA->m_width = width;
864 }
865
866 void wxBitmap::SetDepth( int depth )
867 {
868 AllocExclusive();
869 M_BMPDATA->m_bpp = depth;
870 }
871
872 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
873 {
874 if (!m_refData)
875 m_refData = new wxBitmapRefData;
876
877 // AllocExclusive should not be needed for this internal function
878 wxASSERT(m_refData->GetRefCount() == 1);
879 wxASSERT(M_BMPDATA->m_pixmap == NULL);
880 M_BMPDATA->m_pixmap = pixmap;
881 gdk_drawable_get_size(pixmap, &M_BMPDATA->m_width, &M_BMPDATA->m_height);
882 M_BMPDATA->m_bpp = gdk_drawable_get_depth(pixmap);
883 PurgeOtherRepresentations(Pixmap);
884 }
885
886 GdkPixmap *wxBitmap::GetPixmap() const
887 {
888 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
889
890 // create the pixmap on the fly if we use Pixbuf representation:
891 if (M_BMPDATA->m_pixmap == NULL)
892 {
893 GdkPixmap** pmask = NULL;
894 if (gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf))
895 {
896 // make new mask from alpha
897 delete M_BMPDATA->m_mask;
898 M_BMPDATA->m_mask = new wxMask;
899 pmask = &M_BMPDATA->m_mask->m_bitmap;
900 }
901 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
902 &M_BMPDATA->m_pixmap,
903 pmask,
904 wxIMAGE_ALPHA_THRESHOLD);
905 }
906
907 return M_BMPDATA->m_pixmap;
908 }
909
910 bool wxBitmap::HasPixmap() const
911 {
912 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
913
914 return M_BMPDATA->m_pixmap != NULL;
915 }
916
917 GdkPixbuf *wxBitmap::GetPixbuf() const
918 {
919 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
920
921 if (M_BMPDATA->m_pixbuf == NULL)
922 {
923 int width = GetWidth();
924 int height = GetHeight();
925
926 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
927 GetMask() != NULL,
928 8, width, height);
929 M_BMPDATA->m_pixbuf = pixbuf;
930 gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
931 0, 0, 0, 0, width, height);
932
933 // apply the mask to created pixbuf:
934 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
935 {
936 GdkPixbuf *pmask =
937 gdk_pixbuf_get_from_drawable(NULL,
938 M_BMPDATA->m_mask->GetBitmap(),
939 NULL,
940 0, 0, 0, 0, width, height);
941 if (pmask)
942 {
943 guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
944 guchar *mask = gdk_pixbuf_get_pixels(pmask);
945 int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
946 int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
947
948 for (int y = 0; y < height;
949 y++, bmp += bmprowinc, mask += maskrowinc)
950 {
951 for (int x = 0; x < width; x++, bmp += 4, mask += 3)
952 {
953 if (mask[0] == 0 /*black pixel*/)
954 bmp[3] = 0;
955 }
956 }
957
958 g_object_unref (pmask);
959 }
960 }
961 }
962
963 return M_BMPDATA->m_pixbuf;
964 }
965
966 bool wxBitmap::HasPixbuf() const
967 {
968 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
969
970 return M_BMPDATA->m_pixbuf != NULL;
971 }
972
973 void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf, int depth)
974 {
975 if (!m_refData)
976 m_refData = new wxBitmapRefData;
977
978 // AllocExclusive should not be needed for this internal function
979 wxASSERT(m_refData->GetRefCount() == 1);
980 wxASSERT(M_BMPDATA->m_pixbuf == NULL);
981 M_BMPDATA->m_pixbuf = pixbuf;
982 M_BMPDATA->m_width = gdk_pixbuf_get_width(pixbuf);
983 M_BMPDATA->m_height = gdk_pixbuf_get_height(pixbuf);
984 // if depth specified
985 if (depth != 0)
986 M_BMPDATA->m_bpp = depth;
987 else if (M_BMPDATA->m_bpp == 0)
988 // use something reasonable
989 M_BMPDATA->m_bpp = wxTheApp->GetGdkVisual()->depth;
990 PurgeOtherRepresentations(Pixbuf);
991 }
992
993 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
994 {
995 if (keep == Pixmap && HasPixbuf())
996 {
997 g_object_unref (M_BMPDATA->m_pixbuf);
998 M_BMPDATA->m_pixbuf = NULL;
999 }
1000 if (keep == Pixbuf && HasPixmap())
1001 {
1002 g_object_unref (M_BMPDATA->m_pixmap);
1003 M_BMPDATA->m_pixmap = NULL;
1004 }
1005 }
1006
1007 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1008 {
1009 void* bits = NULL;
1010 GdkPixbuf *pixbuf = GetPixbuf();
1011 const bool hasAlpha = HasAlpha();
1012 // allow access if bpp is valid and matches existence of alpha
1013 if (pixbuf != NULL && (
1014 bpp == 24 && !hasAlpha ||
1015 bpp == 32 && hasAlpha))
1016 {
1017 data.m_height = gdk_pixbuf_get_height( pixbuf );
1018 data.m_width = gdk_pixbuf_get_width( pixbuf );
1019 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
1020 bits = gdk_pixbuf_get_pixels(pixbuf);
1021 }
1022 return bits;
1023 }
1024
1025 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
1026 {
1027 }
1028
1029 bool wxBitmap::HasAlpha() const
1030 {
1031 return m_refData != NULL && M_BMPDATA->m_pixbuf != NULL &&
1032 gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf);
1033 }
1034
1035 void wxBitmap::UseAlpha()
1036 {
1037 GdkPixbuf* pixbuf = GetPixbuf();
1038 // add alpha if necessary
1039 if (!gdk_pixbuf_get_has_alpha(pixbuf))
1040 {
1041 M_BMPDATA->m_pixbuf = NULL;
1042 AllocExclusive();
1043 M_BMPDATA->m_pixbuf = gdk_pixbuf_add_alpha(pixbuf, false, 0, 0, 0);
1044 g_object_unref(pixbuf);
1045 }
1046 }
1047
1048 wxObjectRefData* wxBitmap::CreateRefData() const
1049 {
1050 return new wxBitmapRefData;
1051 }
1052
1053 wxObjectRefData* wxBitmap::CloneRefData(const wxObjectRefData* data) const
1054 {
1055 const wxBitmapRefData* oldRef = wx_static_cast(const wxBitmapRefData*, data);
1056 wxBitmapRefData* newRef = new wxBitmapRefData;
1057 newRef->m_width = oldRef->m_width;
1058 newRef->m_height = oldRef->m_height;
1059 newRef->m_bpp = oldRef->m_bpp;
1060 if (oldRef->m_pixmap != NULL)
1061 {
1062 newRef->m_pixmap = gdk_pixmap_new(
1063 oldRef->m_pixmap, oldRef->m_width, oldRef->m_height,
1064 // use pixmap depth, m_bpp may not match
1065 gdk_drawable_get_depth(oldRef->m_pixmap));
1066 GdkGC* gc = gdk_gc_new(newRef->m_pixmap);
1067 gdk_draw_drawable(
1068 newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1);
1069 g_object_unref(gc);
1070 }
1071 if (oldRef->m_pixbuf != NULL)
1072 {
1073 newRef->m_pixbuf = gdk_pixbuf_copy(oldRef->m_pixbuf);
1074 }
1075 if (oldRef->m_mask != NULL)
1076 {
1077 newRef->m_mask = new wxMask;
1078 newRef->m_mask->m_bitmap = gdk_pixmap_new(
1079 oldRef->m_mask->m_bitmap, oldRef->m_width, oldRef->m_height, 1);
1080 GdkGC* gc = gdk_gc_new(newRef->m_mask->m_bitmap);
1081 gdk_draw_drawable(newRef->m_mask->m_bitmap,
1082 gc, oldRef->m_mask->m_bitmap, 0, 0, 0, 0, -1, -1);
1083 g_object_unref(gc);
1084 }
1085 #if wxUSE_PALETTE
1086 // implement this if SetPalette is ever implemented
1087 wxASSERT(oldRef->m_palette == NULL);
1088 #endif
1089
1090 return newRef;
1091 }
1092
1093 //-----------------------------------------------------------------------------
1094 // wxBitmapHandler
1095 //-----------------------------------------------------------------------------
1096
1097 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
1098
1099 /* static */ void wxBitmap::InitStandardHandlers()
1100 {
1101 // TODO: Insert handler based on GdkPixbufs handler later
1102 }