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