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