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