]>
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" |
5ff14574 | 21 | #include "wx/colour.h" |
93763ad5 | 22 | #endif |
22bd9387 | 23 | |
5696ebb3 | 24 | #include "wx/rawbmp.h" |
9e691f46 | 25 | |
d76fe38b | 26 | #include <gtk/gtk.h> |
13111b2a | 27 | |
d76fe38b RR |
28 | //----------------------------------------------------------------------------- |
29 | // data | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
c2fa61e8 | 32 | extern GtkWidget *wxGetRootWindow(); |
c801d85f KB |
33 | |
34 | //----------------------------------------------------------------------------- | |
35 | // wxMask | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
39 | ||
8bbe427f | 40 | wxMask::wxMask() |
c801d85f | 41 | { |
fd0eed64 | 42 | m_bitmap = (GdkBitmap *) NULL; |
ff7b1510 | 43 | } |
c801d85f | 44 | |
91b8de8d | 45 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) |
c801d85f | 46 | { |
72a7edf0 | 47 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 48 | Create( bitmap, colour ); |
ff7b1510 | 49 | } |
c801d85f | 50 | |
0b04c4e0 | 51 | #if wxUSE_PALETTE |
91b8de8d | 52 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) |
c801d85f | 53 | { |
72a7edf0 | 54 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 55 | Create( bitmap, paletteIndex ); |
ff7b1510 | 56 | } |
0b04c4e0 | 57 | #endif // wxUSE_PALETTE |
c801d85f | 58 | |
91b8de8d | 59 | wxMask::wxMask( const wxBitmap& bitmap ) |
c801d85f | 60 | { |
72a7edf0 | 61 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 62 | Create( bitmap ); |
ff7b1510 | 63 | } |
c801d85f | 64 | |
8bbe427f | 65 | wxMask::~wxMask() |
c801d85f | 66 | { |
13111b2a | 67 | if (m_bitmap) |
3fe39b0c | 68 | g_object_unref (m_bitmap); |
ff7b1510 | 69 | } |
c801d85f | 70 | |
1fb4de31 RR |
71 | bool wxMask::Create( const wxBitmap& bitmap, |
72 | const wxColour& colour ) | |
91b8de8d RR |
73 | { |
74 | if (m_bitmap) | |
284b4c88 | 75 | { |
3fe39b0c | 76 | g_object_unref (m_bitmap); |
284b4c88 | 77 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 78 | } |
13111b2a | 79 | |
13785a4b PC |
80 | const int w = bitmap.GetWidth(); |
81 | const int h = bitmap.GetHeight(); | |
13111b2a | 82 | |
5ac1d44a | 83 | // create mask as XBM format bitmap |
13111b2a | 84 | |
5ac1d44a PC |
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]; | |
d0e79755 | 88 | // set bits are unmasked |
5ac1d44a PC |
89 | memset(out, 0xff, out_size); |
90 | unsigned bit_index = 0; | |
13785a4b | 91 | if (bitmap.HasPixbuf()) |
1fb4de31 | 92 | { |
5ac1d44a PC |
93 | const wxByte r_mask = colour.Red(); |
94 | const wxByte g_mask = colour.Green(); | |
95 | const wxByte b_mask = colour.Blue(); | |
13785a4b | 96 | GdkPixbuf* pixbuf = bitmap.GetPixbuf(); |
5ac1d44a PC |
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 | } | |
2eefae6e | 108 | } |
13785a4b | 109 | else |
1fb4de31 | 110 | { |
5ac1d44a | 111 | GdkImage* image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h); |
13785a4b | 112 | GdkColormap* colormap = gdk_image_get_colormap(image); |
a7cfe08a PC |
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 | |
13785a4b PC |
118 | { |
119 | wxColor c(colour); | |
120 | c.CalcPixel(colormap); | |
121 | mask_pixel = c.GetPixel(); | |
122 | } | |
5ac1d44a | 123 | for (int y = 0; y < h; y++) |
1fb4de31 | 124 | { |
5ac1d44a PC |
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; | |
f9ee644e | 129 | } |
13785a4b | 130 | g_object_unref(image); |
5ac1d44a PC |
131 | } |
132 | m_bitmap = gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h); | |
133 | delete[] out; | |
902725ee | 134 | return true; |
91b8de8d RR |
135 | } |
136 | ||
0b04c4e0 | 137 | #if wxUSE_PALETTE |
b5f01ae0 | 138 | bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex ) |
91b8de8d | 139 | { |
b5f01ae0 VS |
140 | unsigned char r,g,b; |
141 | wxPalette *pal = bitmap.GetPalette(); | |
284b4c88 | 142 | |
902725ee | 143 | wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") ); |
c2fa61e8 | 144 | |
b5f01ae0 | 145 | pal->GetRGB(paletteIndex, &r, &g, &b); |
284b4c88 | 146 | |
b5f01ae0 | 147 | return Create(bitmap, wxColour(r, g, b)); |
91b8de8d | 148 | } |
0b04c4e0 | 149 | #endif // wxUSE_PALETTE |
91b8de8d RR |
150 | |
151 | bool wxMask::Create( const wxBitmap& bitmap ) | |
152 | { | |
153 | if (m_bitmap) | |
284b4c88 | 154 | { |
3fe39b0c | 155 | g_object_unref (m_bitmap); |
284b4c88 | 156 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 157 | } |
284b4c88 | 158 | |
902725ee | 159 | if (!bitmap.Ok()) return false; |
284b4c88 | 160 | |
b85229d1 | 161 | wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") ); |
284b4c88 | 162 | |
c2fa61e8 | 163 | m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); |
284b4c88 | 164 | |
902725ee | 165 | if (!m_bitmap) return false; |
284b4c88 | 166 | |
91b8de8d | 167 | GdkGC *gc = gdk_gc_new( m_bitmap ); |
9be3f755 PC |
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()); | |
3fe39b0c | 170 | g_object_unref (gc); |
284b4c88 | 171 | |
902725ee | 172 | return true; |
91b8de8d RR |
173 | } |
174 | ||
175 | GdkBitmap *wxMask::GetBitmap() const | |
c801d85f | 176 | { |
fd0eed64 | 177 | return m_bitmap; |
ff7b1510 | 178 | } |
8bbe427f | 179 | |
c801d85f KB |
180 | //----------------------------------------------------------------------------- |
181 | // wxBitmap | |
182 | //----------------------------------------------------------------------------- | |
183 | ||
184 | class wxBitmapRefData: public wxObjectRefData | |
185 | { | |
fd0eed64 | 186 | public: |
f2593d0d | 187 | wxBitmapRefData(); |
d3c7fc99 | 188 | virtual ~wxBitmapRefData(); |
f2593d0d RR |
189 | |
190 | GdkPixmap *m_pixmap; | |
feac7937 | 191 | GdkPixbuf *m_pixbuf; |
f2593d0d RR |
192 | wxMask *m_mask; |
193 | int m_width; | |
194 | int m_height; | |
195 | int m_bpp; | |
23656673 | 196 | #if wxUSE_PALETTE |
f2593d0d | 197 | wxPalette *m_palette; |
23656673 | 198 | #endif // wxUSE_PALETTE |
c801d85f KB |
199 | }; |
200 | ||
8bbe427f | 201 | wxBitmapRefData::wxBitmapRefData() |
c801d85f | 202 | { |
fd0eed64 | 203 | m_pixmap = (GdkPixmap *) NULL; |
feac7937 | 204 | m_pixbuf = (GdkPixbuf *) NULL; |
fd0eed64 RR |
205 | m_mask = (wxMask *) NULL; |
206 | m_width = 0; | |
207 | m_height = 0; | |
208 | m_bpp = 0; | |
23656673 | 209 | #if wxUSE_PALETTE |
fd0eed64 | 210 | m_palette = (wxPalette *) NULL; |
23656673 | 211 | #endif // wxUSE_PALETTE |
ff7b1510 | 212 | } |
c801d85f | 213 | |
8bbe427f | 214 | wxBitmapRefData::~wxBitmapRefData() |
c801d85f | 215 | { |
3ebcd89d | 216 | if (m_pixmap) |
3fe39b0c | 217 | g_object_unref (m_pixmap); |
feac7937 | 218 | if (m_pixbuf) |
3fe39b0c | 219 | g_object_unref (m_pixbuf); |
3ebcd89d | 220 | delete m_mask; |
0b04c4e0 | 221 | #if wxUSE_PALETTE |
3ebcd89d | 222 | delete m_palette; |
0b04c4e0 | 223 | #endif // wxUSE_PALETTE |
ff7b1510 | 224 | } |
c801d85f KB |
225 | |
226 | //----------------------------------------------------------------------------- | |
227 | ||
b85229d1 | 228 | #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData) |
c801d85f KB |
229 | |
230 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
231 | ||
23656673 PC |
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) | |
c801d85f | 243 | { |
23656673 PC |
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 | } | |
ff7b1510 | 251 | } |
8bbe427f | 252 | |
452418c4 PC |
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 | ||
23656673 | 267 | wxBitmap::~wxBitmap() |
c801d85f | 268 | { |
c826213d RR |
269 | } |
270 | ||
271 | bool wxBitmap::Create( int width, int height, int depth ) | |
272 | { | |
273 | UnRef(); | |
274 | ||
3ebcd89d VZ |
275 | if ( width <= 0 || height <= 0 ) |
276 | { | |
277 | return false; | |
278 | } | |
284b4c88 | 279 | |
b85229d1 | 280 | if (depth == 32) |
284f2b59 | 281 | { |
23656673 | 282 | SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, width, height), 32); |
284f2b59 | 283 | } |
eefa26be RR |
284 | else |
285 | { | |
5588ce92 PC |
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)); | |
eefa26be | 296 | } |
8bbe427f | 297 | |
c826213d | 298 | return Ok(); |
ff7b1510 | 299 | } |
b5f01ae0 | 300 | |
23656673 | 301 | wxBitmap wxBitmap::Rescale(int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy) const |
783da845 | 302 | { |
5588ce92 PC |
303 | wxBitmap bmp; |
304 | ||
305 | wxCHECK_MSG(Ok(), bmp, wxT("invalid bitmap")); | |
783da845 RR |
306 | |
307 | if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height) | |
308 | return *this; | |
902725ee | 309 | |
783da845 RR |
310 | int width = wxMax(newx, 1); |
311 | int height = wxMax(newy, 1); | |
312 | width = wxMin(width, clipwidth); | |
313 | height = wxMin(height, clipheight); | |
902725ee | 314 | |
70029506 PC |
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))) | |
783da845 | 318 | { |
6db34764 | 319 | bmp.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, |
23656673 PC |
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(), | |
feac7937 | 323 | 0, 0, width, height, |
902725ee | 324 | clipx, clipy, |
feac7937 VS |
325 | (double)newx/GetWidth(), (double)newy/GetHeight(), |
326 | GDK_INTERP_BILINEAR); | |
783da845 | 327 | } |
feac7937 | 328 | else |
783da845 | 329 | { |
23656673 PC |
330 | GdkImage* img = gdk_drawable_get_image( |
331 | M_BMPDATA->m_pixmap, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height); | |
feac7937 | 332 | |
5588ce92 | 333 | wxCHECK_MSG(img, bmp, wxT("couldn't create image")); |
902725ee | 334 | |
feac7937 VS |
335 | GdkGC *gc = NULL; |
336 | GdkPixmap *dstpix = NULL; | |
5588ce92 PC |
337 | char *dst = NULL; |
338 | long dstbyteperline = 0; | |
339 | ||
b85229d1 | 340 | if (GetDepth() != 1) |
783da845 | 341 | { |
23656673 | 342 | bmp.Create(width, height, gdk_drawable_get_depth(M_BMPDATA->m_pixmap)); |
feac7937 VS |
343 | dstpix = bmp.GetPixmap(); |
344 | gc = gdk_gc_new( dstpix ); | |
783da845 | 345 | } |
5588ce92 | 346 | else |
feac7937 | 347 | { |
5588ce92 | 348 | dstbyteperline = (width + 7) / 8; |
feac7937 VS |
349 | dst = (char*) malloc(dstbyteperline*height); |
350 | } | |
902725ee | 351 | |
feac7937 VS |
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)); | |
783da845 | 364 | |
feac7937 | 365 | // Main rescaling routine starts here |
783da845 RR |
366 | for (int h = 0; h < height; h++) |
367 | { | |
368 | char outbyte = 0; | |
f9ebac93 | 369 | int old_x = -1; |
b63b07a8 | 370 | guint32 old_pixval = 0; |
feac7937 | 371 | |
f9ebac93 | 372 | for (int w = 0; w < width; w++) |
783da845 | 373 | { |
f9ebac93 RR |
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 | } | |
902725ee | 384 | |
ce46daf9 | 385 | if ( dst ) |
783da845 | 386 | { |
5a67db48 | 387 | if (pixval) |
feac7937 VS |
388 | { |
389 | char bit=1; | |
d0ee33f5 | 390 | char shift = bit << (w % 8); |
feac7937 VS |
391 | outbyte |= shift; |
392 | } | |
902725ee | 393 | |
feac7937 VS |
394 | if ((w+1)%8==0) |
395 | { | |
396 | dst[h*dstbyteperline+w/8] = outbyte; | |
397 | outbyte = 0; | |
398 | } | |
783da845 | 399 | } |
feac7937 | 400 | else |
783da845 | 401 | { |
feac7937 VS |
402 | GdkColor col; |
403 | col.pixel = pixval; | |
404 | gdk_gc_set_foreground( gc, &col ); | |
405 | gdk_draw_point( dstpix, gc, w, h); | |
783da845 RR |
406 | } |
407 | } | |
902725ee | 408 | |
783da845 | 409 | // do not forget the last byte |
ce46daf9 | 410 | if ( dst && (width % 8 != 0) ) |
f9ebac93 | 411 | dst[h*dstbyteperline+width/8] = outbyte; |
783da845 | 412 | } |
902725ee | 413 | |
3fe39b0c MR |
414 | g_object_unref (img); |
415 | if (gc) g_object_unref (gc); | |
feac7937 | 416 | |
ce46daf9 | 417 | if ( dst ) |
feac7937 | 418 | { |
23656673 | 419 | bmp = wxBitmap(dst, width, height, 1); |
feac7937 VS |
420 | free( dst ); |
421 | } | |
902725ee | 422 | |
feac7937 VS |
423 | if (GetMask()) |
424 | { | |
5588ce92 | 425 | dstbyteperline = (width + 7) / 8; |
feac7937 | 426 | dst = (char*) malloc(dstbyteperline*height); |
4d1ebc86 | 427 | img = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight()); |
783da845 | 428 | |
feac7937 VS |
429 | for (int h = 0; h < height; h++) |
430 | { | |
431 | char outbyte = 0; | |
432 | int old_x = -1; | |
433 | guint32 old_pixval = 0; | |
902725ee | 434 | |
feac7937 VS |
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 | } | |
902725ee | 447 | |
feac7937 VS |
448 | if (pixval) |
449 | { | |
450 | char bit=1; | |
d0ee33f5 | 451 | char shift = bit << (w % 8); |
feac7937 VS |
452 | outbyte |= shift; |
453 | } | |
902725ee | 454 | |
feac7937 VS |
455 | if ((w+1)%8 == 0) |
456 | { | |
457 | dst[h*dstbyteperline+w/8] = outbyte; | |
458 | outbyte = 0; | |
459 | } | |
460 | } | |
902725ee | 461 | |
feac7937 VS |
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 ); | |
3fe39b0c | 471 | g_object_unref (img); |
feac7937 VS |
472 | } |
473 | ||
474 | free( tablex ); | |
475 | free( tabley ); | |
476 | } | |
902725ee WS |
477 | |
478 | return bmp; | |
783da845 RR |
479 | } |
480 | ||
feac7937 | 481 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth) |
b5f01ae0 | 482 | { |
a11672a4 RR |
483 | UnRef(); |
484 | ||
637b7e4f VZ |
485 | wxCHECK_MSG( image.Ok(), false, wxT("invalid image") ); |
486 | wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") ); | |
b5f01ae0 | 487 | |
feac7937 VS |
488 | if (image.GetWidth() <= 0 || image.GetHeight() <= 0) |
489 | return false; | |
902725ee | 490 | |
70029506 PC |
491 | // create pixbuf if image has alpha and requested depth is compatible |
492 | if (image.HasAlpha() && (depth == -1 || depth == 32)) | |
5588ce92 PC |
493 | return CreateFromImageAsPixbuf(image); |
494 | ||
70029506 | 495 | // otherwise create pixmap, if alpha is present it will be converted to mask |
5ac1d44a | 496 | return CreateFromImageAsPixmap(image, depth); |
feac7937 | 497 | } |
3ebcd89d | 498 | |
5ac1d44a | 499 | bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth) |
feac7937 | 500 | { |
5ac1d44a PC |
501 | const int w = image.GetWidth(); |
502 | const int h = image.GetHeight(); | |
503 | if (depth == 1) | |
feac7937 | 504 | { |
5ac1d44a PC |
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]; | |
d0e79755 | 510 | // set bits are black |
5ac1d44a PC |
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); | |
feac7937 | 534 | } |
b5f01ae0 | 535 | |
5ac1d44a PC |
536 | const wxByte* alpha = image.GetAlpha(); |
537 | if (alpha != NULL || image.HasMask()) | |
feac7937 | 538 | { |
5ac1d44a PC |
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) | |
b5f01ae0 | 546 | { |
5ac1d44a | 547 | for (int y = 0; y < h; y++) |
b5f01ae0 | 548 | { |
5ac1d44a PC |
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; | |
feac7937 | 553 | } |
5ac1d44a PC |
554 | } |
555 | else | |
b5f01ae0 | 556 | { |
5ac1d44a PC |
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 | } | |
feac7937 | 574 | return true; |
b5f01ae0 VS |
575 | } |
576 | ||
feac7937 | 577 | bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image) |
b5f01ae0 | 578 | { |
70029506 PC |
579 | wxASSERT(image.HasAlpha()); |
580 | ||
feac7937 VS |
581 | int width = image.GetWidth(); |
582 | int height = image.GetHeight(); | |
2eefae6e | 583 | |
23656673 PC |
584 | Create(width, height, 32); |
585 | GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf; | |
feac7937 VS |
586 | if (!pixbuf) |
587 | return false; | |
c2fa61e8 | 588 | |
feac7937 | 589 | // Copy the data: |
23656673 | 590 | const unsigned char* in = image.GetData(); |
feac7937 VS |
591 | unsigned char *out = gdk_pixbuf_get_pixels(pixbuf); |
592 | unsigned char *alpha = image.GetAlpha(); | |
902725ee | 593 | |
23656673 | 594 | int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width; |
b5f01ae0 | 595 | |
23656673 | 596 | for (int y = 0; y < height; y++, out += rowpad) |
b5f01ae0 | 597 | { |
feac7937 VS |
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 | } | |
b5f01ae0 | 605 | } |
902725ee | 606 | |
feac7937 VS |
607 | return true; |
608 | } | |
b5f01ae0 | 609 | |
feac7937 VS |
610 | wxImage wxBitmap::ConvertToImage() const |
611 | { | |
feac7937 | 612 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
902725ee | 613 | |
afbfbfdf PC |
614 | const int w = GetWidth(); |
615 | const int h = GetHeight(); | |
23656673 | 616 | wxImage image(w, h, false); |
feac7937 | 617 | unsigned char *data = image.GetData(); |
2eefae6e | 618 | |
5588ce92 | 619 | wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") ); |
b5f01ae0 | 620 | |
70029506 | 621 | // prefer pixbuf if available, it will preserve alpha and should be quicker |
feac7937 | 622 | if (HasPixbuf()) |
b5f01ae0 | 623 | { |
feac7937 | 624 | GdkPixbuf *pixbuf = GetPixbuf(); |
70029506 PC |
625 | unsigned char* alpha = NULL; |
626 | if (gdk_pixbuf_get_has_alpha(pixbuf)) | |
627 | { | |
628 | image.SetAlpha(); | |
629 | alpha = image.GetAlpha(); | |
630 | } | |
23656673 | 631 | const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf); |
feac7937 | 632 | unsigned char *out = data; |
70029506 | 633 | const int inc = 3 + int(alpha != NULL); |
23656673 | 634 | const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w; |
b5f01ae0 | 635 | |
23656673 | 636 | for (int y = 0; y < h; y++, in += rowpad) |
feac7937 | 637 | { |
70029506 | 638 | for (int x = 0; x < w; x++, in += inc, out += 3) |
feac7937 VS |
639 | { |
640 | out[0] = in[0]; | |
641 | out[1] = in[1]; | |
642 | out[2] = in[2]; | |
70029506 PC |
643 | if (alpha != NULL) |
644 | *alpha++ = in[3]; | |
feac7937 VS |
645 | } |
646 | } | |
b5f01ae0 | 647 | } |
feac7937 | 648 | else |
b5f01ae0 | 649 | { |
afbfbfdf PC |
650 | GdkPixmap* pixmap = GetPixmap(); |
651 | GdkPixmap* pixmap_invert = NULL; | |
652 | if (GetDepth() == 1) | |
b5f01ae0 | 653 | { |
d0e79755 | 654 | // mono bitmaps are inverted, i.e. 0 is white |
afbfbfdf PC |
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; | |
feac7937 | 661 | } |
afbfbfdf PC |
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); | |
feac7937 | 665 | |
afbfbfdf | 666 | gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h); |
feac7937 | 667 | |
afbfbfdf PC |
668 | g_object_unref(pixbuf); |
669 | if (pixmap_invert != NULL) | |
670 | g_object_unref(pixmap_invert); | |
70029506 PC |
671 | } |
672 | // convert mask, unless there is already alpha | |
673 | if (GetMask() && !image.HasAlpha()) | |
674 | { | |
23656673 PC |
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 | |
70029506 PC |
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; | |
feac7937 | 683 | |
70029506 PC |
684 | image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE); |
685 | GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h); | |
feac7937 | 686 | |
70029506 PC |
687 | for (int y = 0; y < h; y++) |
688 | { | |
689 | for (int x = 0; x < w; x++, data += 3) | |
8ab696e0 | 690 | { |
70029506 | 691 | if (gdk_image_get_pixel(image_mask, x, y) == 0) |
8ab696e0 | 692 | { |
70029506 PC |
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 | { | |
23656673 PC |
699 | // we have to fudge the colour a bit to prevent |
700 | // this pixel from appearing transparent | |
70029506 | 701 | data[2] = MASK_BLUE_REPLACEMENT; |
feac7937 | 702 | } |
feac7937 | 703 | } |
b5f01ae0 | 704 | } |
70029506 | 705 | g_object_unref(image_mask); |
feac7937 | 706 | } |
b5f01ae0 VS |
707 | |
708 | return image; | |
709 | } | |
710 | ||
f6bcfd97 | 711 | bool wxBitmap::operator == ( const wxBitmap& bmp ) const |
c801d85f | 712 | { |
8bbe427f | 713 | return m_refData == bmp.m_refData; |
ff7b1510 | 714 | } |
8bbe427f | 715 | |
b7cacb43 | 716 | bool wxBitmap::IsOk() const |
c801d85f | 717 | { |
902725ee | 718 | return (m_refData != NULL) && |
feac7937 | 719 | ( |
feac7937 | 720 | M_BMPDATA->m_pixbuf || |
b85229d1 | 721 | M_BMPDATA->m_pixmap |
feac7937 | 722 | ); |
ff7b1510 | 723 | } |
8bbe427f | 724 | |
91b8de8d | 725 | int wxBitmap::GetHeight() const |
c801d85f | 726 | { |
223d09f6 | 727 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
e55ad60e | 728 | |
fd0eed64 | 729 | return M_BMPDATA->m_height; |
ff7b1510 | 730 | } |
c801d85f | 731 | |
91b8de8d | 732 | int wxBitmap::GetWidth() const |
c801d85f | 733 | { |
223d09f6 | 734 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 735 | |
fd0eed64 | 736 | return M_BMPDATA->m_width; |
ff7b1510 | 737 | } |
c801d85f | 738 | |
91b8de8d | 739 | int wxBitmap::GetDepth() const |
c801d85f | 740 | { |
223d09f6 | 741 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 742 | |
fd0eed64 | 743 | return M_BMPDATA->m_bpp; |
ff7b1510 | 744 | } |
c801d85f | 745 | |
91b8de8d | 746 | wxMask *wxBitmap::GetMask() const |
c801d85f | 747 | { |
223d09f6 | 748 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 749 | |
fd0eed64 | 750 | return M_BMPDATA->m_mask; |
ff7b1510 | 751 | } |
c801d85f KB |
752 | |
753 | void wxBitmap::SetMask( wxMask *mask ) | |
754 | { | |
223d09f6 | 755 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); |
8bbe427f | 756 | |
e3e89a93 | 757 | AllocExclusive(); |
23656673 | 758 | delete M_BMPDATA->m_mask; |
fd0eed64 | 759 | M_BMPDATA->m_mask = mask; |
ff7b1510 | 760 | } |
c801d85f | 761 | |
db0aec83 VS |
762 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
763 | { | |
764 | *this = icon; | |
5588ce92 | 765 | return Ok(); |
db0aec83 VS |
766 | } |
767 | ||
17bec151 RR |
768 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const |
769 | { | |
5588ce92 PC |
770 | wxBitmap ret; |
771 | ||
23656673 PC |
772 | wxCHECK_MSG(Ok(), ret, wxT("invalid bitmap")); |
773 | wxCHECK_MSG(rect.x >= 0 && rect.y >= 0 && | |
774 | rect.x + rect.width <= M_BMPDATA->m_width && | |
775 | rect.y + rect.height <= M_BMPDATA->m_height, | |
776 | ret, wxT("invalid bitmap region")); | |
13111b2a | 777 | |
23656673 | 778 | if (HasPixbuf() || M_BMPDATA->m_bpp == 32) |
17bec151 | 779 | { |
feac7937 | 780 | GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, |
70029506 | 781 | gdk_pixbuf_get_has_alpha(GetPixbuf()), |
17f504ec | 782 | 8, rect.width, rect.height); |
23656673 | 783 | ret.SetPixbuf(pixbuf, M_BMPDATA->m_bpp); |
6db34764 | 784 | gdk_pixbuf_copy_area(GetPixbuf(), |
feac7937 VS |
785 | rect.x, rect.y, rect.width, rect.height, |
786 | pixbuf, 0, 0); | |
17bec151 RR |
787 | } |
788 | else | |
789 | { | |
23656673 | 790 | ret.Create(rect.width, rect.height, M_BMPDATA->m_bpp); |
9be3f755 PC |
791 | GdkGC *gc = gdk_gc_new( ret.GetPixmap() ); |
792 | gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
793 | g_object_unref (gc); | |
17bec151 | 794 | } |
70029506 PC |
795 | // make mask, unless there is already alpha |
796 | if (GetMask() && !HasAlpha()) | |
17bec151 RR |
797 | { |
798 | wxMask *mask = new wxMask; | |
c2fa61e8 | 799 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 ); |
13111b2a | 800 | |
17bec151 | 801 | GdkGC *gc = gdk_gc_new( mask->m_bitmap ); |
9be3f755 | 802 | gdk_draw_drawable(mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height); |
3fe39b0c | 803 | g_object_unref (gc); |
13111b2a VZ |
804 | |
805 | ret.SetMask( mask ); | |
17bec151 | 806 | } |
13111b2a | 807 | |
17bec151 RR |
808 | return ret; |
809 | } | |
810 | ||
4611dd06 | 811 | bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const |
c801d85f | 812 | { |
902725ee | 813 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); |
8bbe427f | 814 | |
b75dd496 | 815 | // Try to save the bitmap via wxImage handlers: |
5588ce92 PC |
816 | wxImage image = ConvertToImage(); |
817 | return image.Ok() && image.SaveFile(name, type); | |
ff7b1510 | 818 | } |
c801d85f | 819 | |
4611dd06 | 820 | bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type ) |
c801d85f | 821 | { |
fd0eed64 | 822 | UnRef(); |
8bbe427f | 823 | |
fd0eed64 RR |
824 | if (type == wxBITMAP_TYPE_XPM) |
825 | { | |
fd0eed64 | 826 | GdkBitmap *mask = (GdkBitmap*) NULL; |
5588ce92 | 827 | SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str())); |
8bbe427f | 828 | |
fd0eed64 RR |
829 | if (mask) |
830 | { | |
5588ce92 PC |
831 | M_BMPDATA->m_mask = new wxMask; |
832 | M_BMPDATA->m_mask->m_bitmap = mask; | |
fd0eed64 | 833 | } |
fd0eed64 | 834 | } |
b75dd496 | 835 | else // try if wxImage can load it |
fd0eed64 RR |
836 | { |
837 | wxImage image; | |
5588ce92 | 838 | if (image.LoadFile(name, type) && image.Ok()) |
23656673 | 839 | CreateFromImage(image, -1); |
fd0eed64 | 840 | } |
8bbe427f | 841 | |
5588ce92 | 842 | return Ok(); |
ff7b1510 | 843 | } |
8bbe427f | 844 | |
0b04c4e0 | 845 | #if wxUSE_PALETTE |
91b8de8d | 846 | wxPalette *wxBitmap::GetPalette() const |
c801d85f | 847 | { |
23656673 | 848 | wxCHECK_MSG(Ok(), NULL, wxT("invalid bitmap")); |
8bbe427f | 849 | |
fd0eed64 | 850 | return M_BMPDATA->m_palette; |
ff7b1510 | 851 | } |
c801d85f | 852 | |
4611dd06 RR |
853 | void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette)) |
854 | { | |
855 | // TODO | |
856 | } | |
0b04c4e0 | 857 | #endif // wxUSE_PALETTE |
4611dd06 | 858 | |
4bc67cc5 RR |
859 | void wxBitmap::SetHeight( int height ) |
860 | { | |
e3e89a93 | 861 | AllocExclusive(); |
4bc67cc5 RR |
862 | M_BMPDATA->m_height = height; |
863 | } | |
864 | ||
865 | void wxBitmap::SetWidth( int width ) | |
866 | { | |
e3e89a93 | 867 | AllocExclusive(); |
4bc67cc5 RR |
868 | M_BMPDATA->m_width = width; |
869 | } | |
870 | ||
871 | void wxBitmap::SetDepth( int depth ) | |
872 | { | |
e3e89a93 | 873 | AllocExclusive(); |
4bc67cc5 RR |
874 | M_BMPDATA->m_bpp = depth; |
875 | } | |
876 | ||
877 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
878 | { | |
3ebcd89d | 879 | if (!m_refData) |
5588ce92 | 880 | m_refData = new wxBitmapRefData; |
4bc67cc5 | 881 | |
e3e89a93 PC |
882 | // AllocExclusive should not be needed for this internal function |
883 | wxASSERT(m_refData->GetRefCount() == 1); | |
5588ce92 | 884 | wxASSERT(M_BMPDATA->m_pixmap == NULL); |
4bc67cc5 | 885 | M_BMPDATA->m_pixmap = pixmap; |
5588ce92 PC |
886 | gdk_drawable_get_size(pixmap, &M_BMPDATA->m_width, &M_BMPDATA->m_height); |
887 | M_BMPDATA->m_bpp = gdk_drawable_get_depth(pixmap); | |
6db34764 | 888 | PurgeOtherRepresentations(Pixmap); |
4bc67cc5 RR |
889 | } |
890 | ||
91b8de8d | 891 | GdkPixmap *wxBitmap::GetPixmap() const |
c801d85f | 892 | { |
223d09f6 | 893 | wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 894 | |
feac7937 | 895 | // create the pixmap on the fly if we use Pixbuf representation: |
5588ce92 | 896 | if (M_BMPDATA->m_pixmap == NULL) |
feac7937 | 897 | { |
70029506 PC |
898 | GdkPixmap** pmask = NULL; |
899 | if (gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf)) | |
900 | { | |
901 | // make new mask from alpha | |
902 | delete M_BMPDATA->m_mask; | |
903 | M_BMPDATA->m_mask = new wxMask; | |
904 | pmask = &M_BMPDATA->m_mask->m_bitmap; | |
905 | } | |
feac7937 VS |
906 | gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf, |
907 | &M_BMPDATA->m_pixmap, | |
70029506 | 908 | pmask, |
23656673 | 909 | wxIMAGE_ALPHA_THRESHOLD); |
feac7937 | 910 | } |
feac7937 | 911 | |
fd0eed64 | 912 | return M_BMPDATA->m_pixmap; |
ff7b1510 | 913 | } |
8bbe427f | 914 | |
feac7937 VS |
915 | bool wxBitmap::HasPixmap() const |
916 | { | |
917 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
918 | ||
919 | return M_BMPDATA->m_pixmap != NULL; | |
920 | } | |
921 | ||
feac7937 VS |
922 | GdkPixbuf *wxBitmap::GetPixbuf() const |
923 | { | |
924 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
925 | ||
5588ce92 | 926 | if (M_BMPDATA->m_pixbuf == NULL) |
6db34764 VS |
927 | { |
928 | int width = GetWidth(); | |
929 | int height = GetHeight(); | |
902725ee | 930 | |
6db34764 | 931 | GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, |
70029506 | 932 | GetMask() != NULL, |
6db34764 | 933 | 8, width, height); |
23656673 PC |
934 | M_BMPDATA->m_pixbuf = pixbuf; |
935 | gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL, | |
936 | 0, 0, 0, 0, width, height); | |
902725ee | 937 | |
6db34764 VS |
938 | // apply the mask to created pixbuf: |
939 | if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask) | |
940 | { | |
902725ee | 941 | GdkPixbuf *pmask = |
6db34764 VS |
942 | gdk_pixbuf_get_from_drawable(NULL, |
943 | M_BMPDATA->m_mask->GetBitmap(), | |
944 | NULL, | |
945 | 0, 0, 0, 0, width, height); | |
946 | if (pmask) | |
947 | { | |
948 | guchar *bmp = gdk_pixbuf_get_pixels(pixbuf); | |
949 | guchar *mask = gdk_pixbuf_get_pixels(pmask); | |
950 | int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width; | |
951 | int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width; | |
952 | ||
953 | for (int y = 0; y < height; | |
954 | y++, bmp += bmprowinc, mask += maskrowinc) | |
955 | { | |
956 | for (int x = 0; x < width; x++, bmp += 4, mask += 3) | |
957 | { | |
958 | if (mask[0] == 0 /*black pixel*/) | |
959 | bmp[3] = 0; | |
960 | } | |
961 | } | |
902725ee | 962 | |
3fe39b0c | 963 | g_object_unref (pmask); |
6db34764 VS |
964 | } |
965 | } | |
966 | } | |
967 | ||
feac7937 VS |
968 | return M_BMPDATA->m_pixbuf; |
969 | } | |
970 | ||
971 | bool wxBitmap::HasPixbuf() const | |
972 | { | |
973 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
974 | ||
975 | return M_BMPDATA->m_pixbuf != NULL; | |
976 | } | |
977 | ||
23656673 | 978 | void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf, int depth) |
feac7937 VS |
979 | { |
980 | if (!m_refData) | |
5588ce92 | 981 | m_refData = new wxBitmapRefData; |
feac7937 | 982 | |
e3e89a93 PC |
983 | // AllocExclusive should not be needed for this internal function |
984 | wxASSERT(m_refData->GetRefCount() == 1); | |
5588ce92 | 985 | wxASSERT(M_BMPDATA->m_pixbuf == NULL); |
feac7937 | 986 | M_BMPDATA->m_pixbuf = pixbuf; |
5588ce92 PC |
987 | M_BMPDATA->m_width = gdk_pixbuf_get_width(pixbuf); |
988 | M_BMPDATA->m_height = gdk_pixbuf_get_height(pixbuf); | |
23656673 PC |
989 | // if depth specified |
990 | if (depth != 0) | |
991 | M_BMPDATA->m_bpp = depth; | |
992 | else if (M_BMPDATA->m_bpp == 0) | |
993 | // use something reasonable | |
994 | M_BMPDATA->m_bpp = wxTheApp->GetGdkVisual()->depth; | |
6db34764 | 995 | PurgeOtherRepresentations(Pixbuf); |
feac7937 VS |
996 | } |
997 | ||
998 | void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep) | |
999 | { | |
1000 | if (keep == Pixmap && HasPixbuf()) | |
1001 | { | |
3fe39b0c | 1002 | g_object_unref (M_BMPDATA->m_pixbuf); |
feac7937 VS |
1003 | M_BMPDATA->m_pixbuf = NULL; |
1004 | } | |
1005 | if (keep == Pixbuf && HasPixmap()) | |
1006 | { | |
3fe39b0c | 1007 | g_object_unref (M_BMPDATA->m_pixmap); |
feac7937 VS |
1008 | M_BMPDATA->m_pixmap = NULL; |
1009 | } | |
1010 | } | |
1011 | ||
284f2b59 RR |
1012 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) |
1013 | { | |
70029506 | 1014 | void* bits = NULL; |
284f2b59 | 1015 | GdkPixbuf *pixbuf = GetPixbuf(); |
70029506 PC |
1016 | const bool hasAlpha = HasAlpha(); |
1017 | // allow access if bpp is valid and matches existence of alpha | |
1018 | if (pixbuf != NULL && ( | |
1019 | bpp == 24 && !hasAlpha || | |
1020 | bpp == 32 && hasAlpha)) | |
1021 | { | |
1022 | data.m_height = gdk_pixbuf_get_height( pixbuf ); | |
1023 | data.m_width = gdk_pixbuf_get_width( pixbuf ); | |
1024 | data.m_stride = gdk_pixbuf_get_rowstride( pixbuf ); | |
1025 | bits = gdk_pixbuf_get_pixels(pixbuf); | |
1026 | } | |
1027 | return bits; | |
284f2b59 RR |
1028 | } |
1029 | ||
17a1ebd1 | 1030 | void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data)) |
284f2b59 RR |
1031 | { |
1032 | } | |
1033 | ||
902725ee | 1034 | bool wxBitmap::HasAlpha() const |
0ff2a74d | 1035 | { |
70029506 PC |
1036 | return m_refData != NULL && M_BMPDATA->m_pixbuf != NULL && |
1037 | gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf); | |
0ff2a74d RR |
1038 | } |
1039 | ||
1040 | void wxBitmap::UseAlpha() | |
902725ee | 1041 | { |
70029506 PC |
1042 | GdkPixbuf* pixbuf = GetPixbuf(); |
1043 | // add alpha if necessary | |
1044 | if (!gdk_pixbuf_get_has_alpha(pixbuf)) | |
1045 | { | |
1046 | M_BMPDATA->m_pixbuf = NULL; | |
e3e89a93 PC |
1047 | AllocExclusive(); |
1048 | M_BMPDATA->m_pixbuf = gdk_pixbuf_add_alpha(pixbuf, false, 0, 0, 0); | |
70029506 PC |
1049 | g_object_unref(pixbuf); |
1050 | } | |
0ff2a74d RR |
1051 | } |
1052 | ||
e3e89a93 PC |
1053 | wxObjectRefData* wxBitmap::CreateRefData() const |
1054 | { | |
1055 | return new wxBitmapRefData; | |
1056 | } | |
1057 | ||
1058 | wxObjectRefData* wxBitmap::CloneRefData(const wxObjectRefData* data) const | |
1059 | { | |
1060 | const wxBitmapRefData* oldRef = wx_static_cast(const wxBitmapRefData*, data); | |
1061 | wxBitmapRefData* newRef = new wxBitmapRefData; | |
1062 | newRef->m_width = oldRef->m_width; | |
1063 | newRef->m_height = oldRef->m_height; | |
1064 | newRef->m_bpp = oldRef->m_bpp; | |
1065 | if (oldRef->m_pixmap != NULL) | |
1066 | { | |
1067 | newRef->m_pixmap = gdk_pixmap_new( | |
1068 | oldRef->m_pixmap, oldRef->m_width, oldRef->m_height, | |
1069 | // use pixmap depth, m_bpp may not match | |
1070 | gdk_drawable_get_depth(oldRef->m_pixmap)); | |
1071 | GdkGC* gc = gdk_gc_new(newRef->m_pixmap); | |
1072 | gdk_draw_drawable( | |
1073 | newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1); | |
1074 | g_object_unref(gc); | |
1075 | } | |
1076 | if (oldRef->m_pixbuf != NULL) | |
1077 | { | |
1078 | newRef->m_pixbuf = gdk_pixbuf_copy(oldRef->m_pixbuf); | |
1079 | } | |
1080 | if (oldRef->m_mask != NULL) | |
1081 | { | |
1082 | newRef->m_mask = new wxMask; | |
1083 | newRef->m_mask->m_bitmap = gdk_pixmap_new( | |
1084 | oldRef->m_mask->m_bitmap, oldRef->m_width, oldRef->m_height, 1); | |
1085 | GdkGC* gc = gdk_gc_new(newRef->m_mask->m_bitmap); | |
1086 | gdk_draw_drawable(newRef->m_mask->m_bitmap, | |
1087 | gc, oldRef->m_mask->m_bitmap, 0, 0, 0, 0, -1, -1); | |
1088 | g_object_unref(gc); | |
1089 | } | |
1090 | #if wxUSE_PALETTE | |
1091 | // implement this if SetPalette is ever implemented | |
1092 | wxASSERT(oldRef->m_palette == NULL); | |
1093 | #endif | |
1094 | ||
1095 | return newRef; | |
1096 | } | |
1097 | ||
4b61c88d RR |
1098 | //----------------------------------------------------------------------------- |
1099 | // wxBitmapHandler | |
1100 | //----------------------------------------------------------------------------- | |
1101 | ||
452418c4 | 1102 | IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase) |
4b61c88d RR |
1103 | |
1104 | /* static */ void wxBitmap::InitStandardHandlers() | |
1105 | { | |
1106 | // TODO: Insert handler based on GdkPixbufs handler later | |
1107 | } |