]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: bitmap.cpp | |
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 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c801d85f KB |
11 | #pragma implementation "bitmap.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
22bd9387 VZ |
17 | #include "wx/defs.h" |
18 | ||
c801d85f | 19 | #include "wx/bitmap.h" |
e1bf3ad3 | 20 | #include "wx/palette.h" |
52cbfcf0 | 21 | #include "wx/icon.h" |
fd0eed64 | 22 | #include "wx/filefn.h" |
83624f79 | 23 | #include "wx/image.h" |
f9ee644e | 24 | #include "wx/dcmemory.h" |
b5f01ae0 | 25 | #include "wx/app.h" |
83624f79 | 26 | |
9e691f46 VZ |
27 | #ifdef __WXGTK20__ |
28 | // need this to get gdk_image_new_bitmap() | |
29 | #define GDK_ENABLE_BROKEN | |
30 | #endif | |
31 | ||
20e05ffb | 32 | #include <gdk/gdk.h> |
d76fe38b | 33 | #include <gtk/gtk.h> |
b5f01ae0 VS |
34 | #include <gdk/gdkx.h> |
35 | ||
9e691f46 VZ |
36 | #ifdef __WXGTK20__ |
37 | #include <gdk/gdkimage.h> | |
38 | #else // GTK+ 1.2 | |
c7f62467 | 39 | #include <gdk/gdkrgb.h> |
9e691f46 | 40 | #endif // GTK+ 2.0/1.2 |
13111b2a | 41 | |
463c4d71 | 42 | #include "wx/math.h" |
783da845 | 43 | |
f6bcfd97 BP |
44 | extern void gdk_wx_draw_bitmap (GdkDrawable *drawable, |
45 | GdkGC *gc, | |
46 | GdkDrawable *src, | |
47 | gint xsrc, | |
48 | gint ysrc, | |
49 | gint xdest, | |
50 | gint ydest, | |
51 | gint width, | |
52 | gint height); | |
53 | ||
d76fe38b RR |
54 | //----------------------------------------------------------------------------- |
55 | // data | |
56 | //----------------------------------------------------------------------------- | |
57 | ||
c2fa61e8 | 58 | extern GtkWidget *wxGetRootWindow(); |
c801d85f KB |
59 | |
60 | //----------------------------------------------------------------------------- | |
61 | // wxMask | |
62 | //----------------------------------------------------------------------------- | |
63 | ||
64 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
65 | ||
8bbe427f | 66 | wxMask::wxMask() |
c801d85f | 67 | { |
fd0eed64 | 68 | m_bitmap = (GdkBitmap *) NULL; |
ff7b1510 | 69 | } |
c801d85f | 70 | |
91b8de8d | 71 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) |
c801d85f | 72 | { |
72a7edf0 | 73 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 74 | Create( bitmap, colour ); |
ff7b1510 | 75 | } |
c801d85f | 76 | |
91b8de8d | 77 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) |
c801d85f | 78 | { |
72a7edf0 | 79 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 80 | Create( bitmap, paletteIndex ); |
ff7b1510 | 81 | } |
c801d85f | 82 | |
91b8de8d | 83 | wxMask::wxMask( const wxBitmap& bitmap ) |
c801d85f | 84 | { |
72a7edf0 | 85 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 86 | Create( bitmap ); |
ff7b1510 | 87 | } |
c801d85f | 88 | |
8bbe427f | 89 | wxMask::~wxMask() |
c801d85f | 90 | { |
13111b2a | 91 | if (m_bitmap) |
72a7edf0 | 92 | gdk_bitmap_unref( m_bitmap ); |
ff7b1510 | 93 | } |
c801d85f | 94 | |
1fb4de31 RR |
95 | bool wxMask::Create( const wxBitmap& bitmap, |
96 | const wxColour& colour ) | |
91b8de8d RR |
97 | { |
98 | if (m_bitmap) | |
284b4c88 | 99 | { |
91b8de8d | 100 | gdk_bitmap_unref( m_bitmap ); |
284b4c88 | 101 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 102 | } |
13111b2a | 103 | |
368d59f0 | 104 | wxImage image = bitmap.ConvertToImage(); |
1fb4de31 | 105 | if (!image.Ok()) return FALSE; |
13111b2a | 106 | |
c2fa61e8 | 107 | m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 ); |
f9ee644e | 108 | GdkGC *gc = gdk_gc_new( m_bitmap ); |
13111b2a | 109 | |
f9ee644e RR |
110 | GdkColor color; |
111 | color.red = 65000; | |
112 | color.green = 65000; | |
113 | color.blue = 65000; | |
114 | color.pixel = 1; | |
115 | gdk_gc_set_foreground( gc, &color ); | |
116 | gdk_gc_set_fill( gc, GDK_SOLID ); | |
117 | gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() ); | |
13111b2a | 118 | |
1fb4de31 RR |
119 | unsigned char *data = image.GetData(); |
120 | int index = 0; | |
13111b2a | 121 | |
1fb4de31 RR |
122 | unsigned char red = colour.Red(); |
123 | unsigned char green = colour.Green(); | |
124 | unsigned char blue = colour.Blue(); | |
13111b2a | 125 | |
005f5d18 | 126 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 127 | |
1fb4de31 | 128 | int bpp = visual->depth; |
2eefae6e VZ |
129 | if ((bpp == 16) && (visual->red_mask != 0xf800)) |
130 | bpp = 15; | |
1fb4de31 RR |
131 | if (bpp == 15) |
132 | { | |
133 | red = red & 0xf8; | |
1fb4de31 | 134 | green = green & 0xf8; |
f6bcfd97 | 135 | blue = blue & 0xf8; |
2eefae6e VZ |
136 | } |
137 | else if (bpp == 16) | |
1fb4de31 RR |
138 | { |
139 | red = red & 0xf8; | |
f6bcfd97 BP |
140 | green = green & 0xfc; |
141 | blue = blue & 0xf8; | |
2eefae6e VZ |
142 | } |
143 | else if (bpp == 12) | |
005f5d18 RR |
144 | { |
145 | red = red & 0xf0; | |
146 | green = green & 0xf0; | |
147 | blue = blue & 0xf0; | |
1fb4de31 | 148 | } |
13111b2a | 149 | |
f9ee644e RR |
150 | color.red = 0; |
151 | color.green = 0; | |
152 | color.blue = 0; | |
153 | color.pixel = 0; | |
154 | gdk_gc_set_foreground( gc, &color ); | |
13111b2a | 155 | |
1fb4de31 | 156 | for (int j = 0; j < image.GetHeight(); j++) |
f9ee644e | 157 | { |
f2593d0d RR |
158 | int start_x = -1; |
159 | int i; | |
160 | for (i = 0; i < image.GetWidth(); i++) | |
1fb4de31 | 161 | { |
13111b2a VZ |
162 | if ((data[index] == red) && |
163 | (data[index+1] == green) && | |
164 | (data[index+2] == blue)) | |
165 | { | |
166 | if (start_x == -1) | |
167 | start_x = i; | |
168 | } | |
169 | else | |
170 | { | |
171 | if (start_x != -1) | |
172 | { | |
173 | gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j ); | |
174 | start_x = -1; | |
175 | } | |
f9ee644e RR |
176 | } |
177 | index += 3; | |
178 | } | |
179 | if (start_x != -1) | |
180 | gdk_draw_line( m_bitmap, gc, start_x, j, i, j ); | |
181 | } | |
1fb4de31 | 182 | |
f9ee644e | 183 | gdk_gc_unref( gc ); |
1fb4de31 | 184 | |
f9ee644e | 185 | return TRUE; |
91b8de8d RR |
186 | } |
187 | ||
b5f01ae0 | 188 | bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex ) |
91b8de8d | 189 | { |
b5f01ae0 VS |
190 | unsigned char r,g,b; |
191 | wxPalette *pal = bitmap.GetPalette(); | |
284b4c88 | 192 | |
b5f01ae0 | 193 | wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") ); |
c2fa61e8 | 194 | |
b5f01ae0 | 195 | pal->GetRGB(paletteIndex, &r, &g, &b); |
284b4c88 | 196 | |
b5f01ae0 | 197 | return Create(bitmap, wxColour(r, g, b)); |
91b8de8d RR |
198 | } |
199 | ||
200 | bool wxMask::Create( const wxBitmap& bitmap ) | |
201 | { | |
202 | if (m_bitmap) | |
284b4c88 | 203 | { |
91b8de8d | 204 | gdk_bitmap_unref( m_bitmap ); |
284b4c88 | 205 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 206 | } |
284b4c88 | 207 | |
91b8de8d | 208 | if (!bitmap.Ok()) return FALSE; |
284b4c88 | 209 | |
223d09f6 | 210 | wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") ); |
284b4c88 | 211 | |
c2fa61e8 | 212 | m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); |
284b4c88 | 213 | |
91b8de8d | 214 | if (!m_bitmap) return FALSE; |
284b4c88 | 215 | |
91b8de8d | 216 | GdkGC *gc = gdk_gc_new( m_bitmap ); |
284b4c88 | 217 | |
f6bcfd97 | 218 | gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() ); |
284b4c88 | 219 | |
91b8de8d | 220 | gdk_gc_unref( gc ); |
284b4c88 | 221 | |
91b8de8d RR |
222 | return TRUE; |
223 | } | |
224 | ||
225 | GdkBitmap *wxMask::GetBitmap() const | |
c801d85f | 226 | { |
fd0eed64 | 227 | return m_bitmap; |
ff7b1510 | 228 | } |
8bbe427f | 229 | |
c801d85f KB |
230 | //----------------------------------------------------------------------------- |
231 | // wxBitmap | |
232 | //----------------------------------------------------------------------------- | |
233 | ||
234 | class wxBitmapRefData: public wxObjectRefData | |
235 | { | |
fd0eed64 | 236 | public: |
f2593d0d RR |
237 | wxBitmapRefData(); |
238 | ~wxBitmapRefData(); | |
239 | ||
240 | GdkPixmap *m_pixmap; | |
241 | GdkBitmap *m_bitmap; | |
feac7937 VS |
242 | #ifdef __WXGTK20__ |
243 | GdkPixbuf *m_pixbuf; | |
244 | #endif | |
f2593d0d RR |
245 | wxMask *m_mask; |
246 | int m_width; | |
247 | int m_height; | |
248 | int m_bpp; | |
249 | wxPalette *m_palette; | |
c801d85f KB |
250 | }; |
251 | ||
8bbe427f | 252 | wxBitmapRefData::wxBitmapRefData() |
c801d85f | 253 | { |
fd0eed64 RR |
254 | m_pixmap = (GdkPixmap *) NULL; |
255 | m_bitmap = (GdkBitmap *) NULL; | |
feac7937 VS |
256 | #ifdef __WXGTK20__ |
257 | m_pixbuf = (GdkPixbuf *) NULL; | |
258 | #endif | |
fd0eed64 RR |
259 | m_mask = (wxMask *) NULL; |
260 | m_width = 0; | |
261 | m_height = 0; | |
262 | m_bpp = 0; | |
263 | m_palette = (wxPalette *) NULL; | |
ff7b1510 | 264 | } |
c801d85f | 265 | |
8bbe427f | 266 | wxBitmapRefData::~wxBitmapRefData() |
c801d85f | 267 | { |
3ebcd89d VZ |
268 | if (m_pixmap) |
269 | gdk_pixmap_unref( m_pixmap ); | |
270 | if (m_bitmap) | |
271 | gdk_bitmap_unref( m_bitmap ); | |
feac7937 VS |
272 | #ifdef __WXGTK20__ |
273 | if (m_pixbuf) | |
274 | gdk_pixbuf_unref( m_pixbuf ); | |
275 | #endif | |
3ebcd89d VZ |
276 | delete m_mask; |
277 | delete m_palette; | |
ff7b1510 | 278 | } |
c801d85f KB |
279 | |
280 | //----------------------------------------------------------------------------- | |
281 | ||
282 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) | |
283 | ||
284 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
285 | ||
8bbe427f | 286 | wxBitmap::wxBitmap() |
c801d85f | 287 | { |
ff7b1510 | 288 | } |
8bbe427f | 289 | |
debe6624 | 290 | wxBitmap::wxBitmap( int width, int height, int depth ) |
c801d85f | 291 | { |
c826213d | 292 | Create( width, height, depth ); |
c826213d RR |
293 | } |
294 | ||
295 | bool wxBitmap::Create( int width, int height, int depth ) | |
296 | { | |
297 | UnRef(); | |
298 | ||
3ebcd89d VZ |
299 | if ( width <= 0 || height <= 0 ) |
300 | { | |
301 | return false; | |
302 | } | |
284b4c88 | 303 | |
005f5d18 | 304 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
284b4c88 | 305 | |
3ebcd89d VZ |
306 | if (depth == -1) |
307 | depth = visual->depth; | |
103aab26 | 308 | |
3ebcd89d VZ |
309 | wxCHECK_MSG( (depth == visual->depth) || (depth == 1), FALSE, |
310 | wxT("invalid bitmap depth") ) | |
8bbe427f | 311 | |
eefa26be | 312 | m_refData = new wxBitmapRefData(); |
fd0eed64 | 313 | M_BMPDATA->m_mask = (wxMask *) NULL; |
fd0eed64 RR |
314 | M_BMPDATA->m_width = width; |
315 | M_BMPDATA->m_height = height; | |
eefa26be RR |
316 | if (depth == 1) |
317 | { | |
c2fa61e8 | 318 | M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); |
eefa26be RR |
319 | M_BMPDATA->m_bpp = 1; |
320 | } | |
321 | else | |
322 | { | |
c2fa61e8 | 323 | M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth ); |
103aab26 | 324 | M_BMPDATA->m_bpp = visual->depth; |
eefa26be | 325 | } |
8bbe427f | 326 | |
c826213d | 327 | return Ok(); |
ff7b1510 | 328 | } |
b5f01ae0 | 329 | |
e838cc14 | 330 | bool wxBitmap::CreateFromXpm( const char **bits ) |
e52f60e6 | 331 | { |
a11672a4 RR |
332 | UnRef(); |
333 | ||
e838cc14 | 334 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") ) |
8bbe427f | 335 | |
005f5d18 | 336 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 337 | |
e52f60e6 RR |
338 | m_refData = new wxBitmapRefData(); |
339 | ||
340 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
8bbe427f | 341 | |
c2fa61e8 | 342 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits ); |
8bbe427f | 343 | |
e838cc14 | 344 | wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") ); |
8bbe427f | 345 | |
fd0eed64 RR |
346 | if (mask) |
347 | { | |
348 | M_BMPDATA->m_mask = new wxMask(); | |
349 | M_BMPDATA->m_mask->m_bitmap = mask; | |
350 | } | |
8bbe427f | 351 | |
fd0eed64 | 352 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
2eefae6e | 353 | |
8cce8940 | 354 | M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ? |
c2fa61e8 | 355 | |
e838cc14 | 356 | return TRUE; |
ff7b1510 | 357 | } |
b5f01ae0 | 358 | |
783da845 RR |
359 | wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy ) |
360 | { | |
361 | wxCHECK_MSG( Ok(), wxNullBitmap, wxT("invalid bitmap") ); | |
362 | ||
363 | if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height) | |
364 | return *this; | |
feac7937 | 365 | |
783da845 RR |
366 | int width = wxMax(newx, 1); |
367 | int height = wxMax(newy, 1); | |
368 | width = wxMin(width, clipwidth); | |
369 | height = wxMin(height, clipheight); | |
feac7937 VS |
370 | |
371 | wxBitmap bmp; | |
783da845 | 372 | |
feac7937 VS |
373 | #ifdef __WXGTK20__ |
374 | if (HasPixbuf()) | |
783da845 | 375 | { |
feac7937 VS |
376 | bmp.SetWidth(width); |
377 | bmp.SetHeight(height); | |
378 | bmp.SetDepth(GetDepth()); | |
6db34764 VS |
379 | bmp.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, |
380 | gdk_pixbuf_get_has_alpha(GetPixbuf()), | |
feac7937 | 381 | 8, width, height)); |
6db34764 | 382 | gdk_pixbuf_scale(GetPixbuf(), bmp.GetPixbuf(), |
feac7937 VS |
383 | 0, 0, width, height, |
384 | clipx, clipy, | |
385 | (double)newx/GetWidth(), (double)newy/GetHeight(), | |
386 | GDK_INTERP_BILINEAR); | |
783da845 | 387 | } |
feac7937 VS |
388 | else |
389 | #endif // __WXGTK20__ | |
783da845 | 390 | { |
feac7937 VS |
391 | GdkImage *img = (GdkImage*) NULL; |
392 | if (GetPixmap()) | |
393 | img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() ); | |
394 | else if (GetBitmap()) | |
395 | img = gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() ); | |
396 | else | |
397 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); | |
398 | ||
399 | wxCHECK_MSG( img, wxNullBitmap, wxT("couldn't create image") ); | |
783da845 | 400 | |
feac7937 VS |
401 | int bpp = -1; |
402 | ||
403 | ||
404 | GdkGC *gc = NULL; | |
405 | GdkPixmap *dstpix = NULL; | |
406 | if (GetPixmap()) | |
783da845 | 407 | { |
feac7937 VS |
408 | GdkVisual *visual = gdk_window_get_visual( GetPixmap() ); |
409 | if (visual == NULL) | |
410 | visual = wxTheApp->GetGdkVisual(); | |
411 | ||
412 | bpp = visual->depth; | |
413 | bmp = wxBitmap(width,height,bpp); | |
414 | dstpix = bmp.GetPixmap(); | |
415 | gc = gdk_gc_new( dstpix ); | |
783da845 | 416 | } |
783da845 | 417 | |
feac7937 VS |
418 | char *dst = NULL; |
419 | long dstbyteperline = 0; | |
420 | ||
421 | if (GetBitmap()) | |
422 | { | |
423 | bpp = 1; | |
424 | dstbyteperline = width/8*M_BMPDATA->m_bpp; | |
425 | if (width*M_BMPDATA->m_bpp % 8 != 0) | |
426 | dstbyteperline++; | |
427 | dst = (char*) malloc(dstbyteperline*height); | |
428 | } | |
429 | ||
430 | // be careful to use the right scaling factor | |
431 | float scx = (float)M_BMPDATA->m_width/(float)newx; | |
432 | float scy = (float)M_BMPDATA->m_height/(float)newy; | |
433 | // prepare accel-tables | |
434 | int *tablex = (int *)calloc(width,sizeof(int)); | |
435 | int *tabley = (int *)calloc(height,sizeof(int)); | |
436 | ||
437 | // accel table filled with clipped values | |
438 | for (int x = 0; x < width; x++) | |
439 | tablex[x] = (int) (scx * (x+clipx)); | |
440 | for (int y = 0; y < height; y++) | |
441 | tabley[y] = (int) (scy * (y+clipy)); | |
783da845 | 442 | |
feac7937 | 443 | // Main rescaling routine starts here |
783da845 RR |
444 | for (int h = 0; h < height; h++) |
445 | { | |
446 | char outbyte = 0; | |
f9ebac93 | 447 | int old_x = -1; |
b63b07a8 | 448 | guint32 old_pixval = 0; |
feac7937 | 449 | |
f9ebac93 | 450 | for (int w = 0; w < width; w++) |
783da845 | 451 | { |
f9ebac93 RR |
452 | guint32 pixval; |
453 | int x = tablex[w]; | |
454 | if (x == old_x) | |
455 | pixval = old_pixval; | |
456 | else | |
457 | { | |
458 | pixval = gdk_image_get_pixel( img, x, tabley[h] ); | |
459 | old_pixval = pixval; | |
460 | old_x = x; | |
461 | } | |
feac7937 VS |
462 | |
463 | if (bpp == 1) | |
783da845 | 464 | { |
feac7937 VS |
465 | if (!pixval) |
466 | { | |
467 | char bit=1; | |
468 | char shift = bit << w % 8; | |
469 | outbyte |= shift; | |
470 | } | |
471 | ||
472 | if ((w+1)%8==0) | |
473 | { | |
474 | dst[h*dstbyteperline+w/8] = outbyte; | |
475 | outbyte = 0; | |
476 | } | |
783da845 | 477 | } |
feac7937 | 478 | else |
783da845 | 479 | { |
feac7937 VS |
480 | GdkColor col; |
481 | col.pixel = pixval; | |
482 | gdk_gc_set_foreground( gc, &col ); | |
483 | gdk_draw_point( dstpix, gc, w, h); | |
783da845 RR |
484 | } |
485 | } | |
486 | ||
487 | // do not forget the last byte | |
feac7937 | 488 | if ((bpp == 1) && (width % 8 != 0)) |
f9ebac93 | 489 | dst[h*dstbyteperline+width/8] = outbyte; |
783da845 | 490 | } |
feac7937 | 491 | |
783da845 | 492 | gdk_image_destroy( img ); |
feac7937 VS |
493 | if (gc) gdk_gc_unref( gc ); |
494 | ||
495 | if (bpp == 1) | |
496 | { | |
497 | bmp = wxBitmap( (const char *)dst, width, height, 1 ); | |
498 | free( dst ); | |
499 | } | |
500 | ||
501 | if (GetMask()) | |
502 | { | |
503 | dstbyteperline = width/8; | |
504 | if (width % 8 != 0) | |
505 | dstbyteperline++; | |
506 | dst = (char*) malloc(dstbyteperline*height); | |
507 | img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() ); | |
783da845 | 508 | |
feac7937 VS |
509 | for (int h = 0; h < height; h++) |
510 | { | |
511 | char outbyte = 0; | |
512 | int old_x = -1; | |
513 | guint32 old_pixval = 0; | |
514 | ||
515 | for (int w = 0; w < width; w++) | |
516 | { | |
517 | guint32 pixval; | |
518 | int x = tablex[w]; | |
519 | if (x == old_x) | |
520 | pixval = old_pixval; | |
521 | else | |
522 | { | |
523 | pixval = gdk_image_get_pixel( img, x, tabley[h] ); | |
524 | old_pixval = pixval; | |
525 | old_x = x; | |
526 | } | |
527 | ||
528 | if (pixval) | |
529 | { | |
530 | char bit=1; | |
531 | char shift = bit << w % 8; | |
532 | outbyte |= shift; | |
533 | } | |
534 | ||
535 | if ((w+1)%8 == 0) | |
536 | { | |
537 | dst[h*dstbyteperline+w/8] = outbyte; | |
538 | outbyte = 0; | |
539 | } | |
540 | } | |
541 | ||
542 | // do not forget the last byte | |
543 | if (width % 8 != 0) | |
544 | dst[h*dstbyteperline+width/8] = outbyte; | |
545 | } | |
546 | wxMask* mask = new wxMask; | |
547 | mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height ); | |
548 | bmp.SetMask(mask); | |
549 | ||
550 | free( dst ); | |
551 | gdk_image_destroy( img ); | |
552 | } | |
553 | ||
554 | free( tablex ); | |
555 | free( tabley ); | |
556 | } | |
783da845 RR |
557 | |
558 | return bmp; | |
559 | } | |
560 | ||
feac7937 | 561 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth) |
b5f01ae0 | 562 | { |
a11672a4 RR |
563 | UnRef(); |
564 | ||
feac7937 | 565 | wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ) |
b5f01ae0 VS |
566 | wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") ) |
567 | ||
feac7937 VS |
568 | if (image.GetWidth() <= 0 || image.GetHeight() <= 0) |
569 | return false; | |
570 | ||
571 | m_refData = new wxBitmapRefData(); | |
3ebcd89d | 572 | |
feac7937 | 573 | if (depth == 1) |
3ebcd89d | 574 | { |
feac7937 | 575 | return CreateFromImageAsBitmap(image); |
3ebcd89d | 576 | } |
feac7937 VS |
577 | else |
578 | { | |
579 | #ifdef __WXGTK20__ | |
580 | if (image.HasAlpha()) | |
581 | return CreateFromImageAsPixbuf(image); | |
582 | #endif | |
583 | return CreateFromImageAsPixmap(image); | |
584 | } | |
585 | } | |
3ebcd89d | 586 | |
feac7937 VS |
587 | // conversion to mono bitmap: |
588 | bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img) | |
589 | { | |
590 | // convert alpha channel to mask, if it is present: | |
591 | wxImage image(img); | |
592 | image.ConvertAlphaToMask(); | |
593 | ||
594 | int width = image.GetWidth(); | |
595 | int height = image.GetHeight(); | |
c2fa61e8 | 596 | |
3ebcd89d VZ |
597 | SetHeight( height ); |
598 | SetWidth( width ); | |
599 | ||
feac7937 | 600 | SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) ); |
b5f01ae0 | 601 | |
feac7937 | 602 | SetDepth( 1 ); |
2eefae6e | 603 | |
feac7937 | 604 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
b5f01ae0 | 605 | |
feac7937 | 606 | // Create picture image |
b5f01ae0 | 607 | |
feac7937 | 608 | unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); |
b5f01ae0 | 609 | |
feac7937 VS |
610 | GdkImage *data_image = |
611 | gdk_image_new_bitmap( visual, data_data, width, height ); | |
b5f01ae0 | 612 | |
feac7937 | 613 | // Create mask image |
b5f01ae0 | 614 | |
feac7937 | 615 | GdkImage *mask_image = (GdkImage*) NULL; |
b5f01ae0 | 616 | |
feac7937 VS |
617 | if (image.HasMask()) |
618 | { | |
619 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
b5f01ae0 | 620 | |
feac7937 | 621 | mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); |
b5f01ae0 | 622 | |
feac7937 VS |
623 | wxMask *mask = new wxMask(); |
624 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); | |
b5f01ae0 | 625 | |
feac7937 VS |
626 | SetMask( mask ); |
627 | } | |
b5f01ae0 | 628 | |
feac7937 VS |
629 | int r_mask = image.GetMaskRed(); |
630 | int g_mask = image.GetMaskGreen(); | |
631 | int b_mask = image.GetMaskBlue(); | |
b5f01ae0 | 632 | |
feac7937 | 633 | unsigned char* data = image.GetData(); |
b5f01ae0 | 634 | |
feac7937 VS |
635 | int index = 0; |
636 | for (int y = 0; y < height; y++) | |
637 | { | |
638 | for (int x = 0; x < width; x++) | |
b5f01ae0 | 639 | { |
feac7937 VS |
640 | int r = data[index]; |
641 | index++; | |
642 | int g = data[index]; | |
643 | index++; | |
644 | int b = data[index]; | |
645 | index++; | |
646 | ||
647 | if (image.HasMask()) | |
b5f01ae0 | 648 | { |
feac7937 VS |
649 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) |
650 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
b5f01ae0 | 651 | else |
feac7937 VS |
652 | gdk_image_put_pixel( mask_image, x, y, 0 ); |
653 | } | |
b5f01ae0 | 654 | |
feac7937 VS |
655 | if ((r == 255) && (b == 255) && (g == 255)) |
656 | gdk_image_put_pixel( data_image, x, y, 1 ); | |
657 | else | |
658 | gdk_image_put_pixel( data_image, x, y, 0 ); | |
b5f01ae0 | 659 | |
feac7937 VS |
660 | } // for |
661 | } // for | |
b5f01ae0 | 662 | |
feac7937 | 663 | // Blit picture |
b5f01ae0 | 664 | |
feac7937 | 665 | GdkGC *data_gc = gdk_gc_new( GetBitmap() ); |
b5f01ae0 | 666 | |
feac7937 | 667 | gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); |
b5f01ae0 | 668 | |
feac7937 VS |
669 | gdk_image_destroy( data_image ); |
670 | gdk_gc_unref( data_gc ); | |
b5f01ae0 | 671 | |
feac7937 | 672 | // Blit mask |
b5f01ae0 | 673 | |
feac7937 VS |
674 | if (image.HasMask()) |
675 | { | |
676 | GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() ); | |
b5f01ae0 | 677 | |
feac7937 VS |
678 | gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); |
679 | ||
680 | gdk_image_destroy( mask_image ); | |
681 | gdk_gc_unref( mask_gc ); | |
b5f01ae0 | 682 | } |
c2fa61e8 | 683 | |
feac7937 VS |
684 | return true; |
685 | } | |
686 | ||
687 | // conversion to colour bitmap: | |
688 | bool wxBitmap::CreateFromImageAsPixmap(const wxImage& img) | |
689 | { | |
690 | // convert alpha channel to mask, if it is present: | |
691 | wxImage image(img); | |
692 | image.ConvertAlphaToMask(); | |
693 | ||
694 | int width = image.GetWidth(); | |
695 | int height = image.GetHeight(); | |
696 | ||
697 | SetHeight( height ); | |
698 | SetWidth( width ); | |
699 | ||
700 | SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) ); | |
b5f01ae0 | 701 | |
feac7937 | 702 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
b5f01ae0 | 703 | |
feac7937 | 704 | int bpp = visual->depth; |
b5f01ae0 | 705 | |
feac7937 | 706 | SetDepth( bpp ); |
b5f01ae0 | 707 | |
feac7937 VS |
708 | if ((bpp == 16) && (visual->red_mask != 0xf800)) |
709 | bpp = 15; | |
710 | else if (bpp < 8) | |
711 | bpp = 8; | |
2eefae6e | 712 | |
feac7937 VS |
713 | // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit |
714 | // visuals are not supported by GDK so we do these ourselves, too. | |
715 | // 15-bit and 16-bit should actually work and 24-bit certainly does. | |
9b72a74d | 716 | #ifdef __sgi |
feac7937 | 717 | if (!image.HasMask() && (bpp > 16)) |
9b72a74d | 718 | #else |
feac7937 | 719 | if (!image.HasMask() && (bpp > 12)) |
9b72a74d | 720 | #endif |
feac7937 VS |
721 | { |
722 | static bool s_hasInitialized = FALSE; | |
b5f01ae0 | 723 | |
feac7937 VS |
724 | if (!s_hasInitialized) |
725 | { | |
726 | gdk_rgb_init(); | |
727 | s_hasInitialized = TRUE; | |
728 | } | |
b5f01ae0 | 729 | |
feac7937 | 730 | GdkGC *gc = gdk_gc_new( GetPixmap() ); |
b5f01ae0 | 731 | |
feac7937 VS |
732 | gdk_draw_rgb_image( GetPixmap(), |
733 | gc, | |
734 | 0, 0, | |
735 | width, height, | |
736 | GDK_RGB_DITHER_NONE, | |
737 | image.GetData(), | |
738 | width*3 ); | |
b5f01ae0 | 739 | |
feac7937 VS |
740 | gdk_gc_unref( gc ); |
741 | return TRUE; | |
742 | } | |
b5f01ae0 | 743 | |
feac7937 | 744 | // Create picture image |
b5f01ae0 | 745 | |
feac7937 VS |
746 | GdkImage *data_image = |
747 | gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height ); | |
b5f01ae0 | 748 | |
feac7937 | 749 | // Create mask image |
b5f01ae0 | 750 | |
feac7937 | 751 | GdkImage *mask_image = (GdkImage*) NULL; |
b5f01ae0 | 752 | |
feac7937 VS |
753 | if (image.HasMask()) |
754 | { | |
755 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
b5f01ae0 | 756 | |
feac7937 | 757 | mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); |
b5f01ae0 | 758 | |
feac7937 VS |
759 | wxMask *mask = new wxMask(); |
760 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); | |
b5f01ae0 | 761 | |
feac7937 VS |
762 | SetMask( mask ); |
763 | } | |
b5f01ae0 | 764 | |
feac7937 | 765 | // Render |
b5f01ae0 | 766 | |
feac7937 VS |
767 | enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR }; |
768 | byte_order b_o = RGB; | |
b5f01ae0 | 769 | |
feac7937 VS |
770 | if (bpp > 8) |
771 | { | |
772 | if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB; | |
773 | else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG; | |
774 | else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG; | |
775 | else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR; | |
776 | else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB; | |
777 | else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR; | |
778 | } | |
b5f01ae0 | 779 | |
feac7937 VS |
780 | int r_mask = image.GetMaskRed(); |
781 | int g_mask = image.GetMaskGreen(); | |
782 | int b_mask = image.GetMaskBlue(); | |
b5f01ae0 | 783 | |
feac7937 | 784 | unsigned char* data = image.GetData(); |
b5f01ae0 | 785 | |
feac7937 VS |
786 | int index = 0; |
787 | for (int y = 0; y < height; y++) | |
788 | { | |
789 | for (int x = 0; x < width; x++) | |
b5f01ae0 | 790 | { |
feac7937 VS |
791 | int r = data[index]; |
792 | index++; | |
793 | int g = data[index]; | |
794 | index++; | |
795 | int b = data[index]; | |
796 | index++; | |
797 | ||
798 | if (image.HasMask()) | |
b5f01ae0 | 799 | { |
feac7937 VS |
800 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) |
801 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
802 | else | |
803 | gdk_image_put_pixel( mask_image, x, y, 0 ); | |
804 | } | |
b5f01ae0 | 805 | |
feac7937 VS |
806 | switch (bpp) |
807 | { | |
808 | case 8: | |
b5f01ae0 | 809 | { |
feac7937 VS |
810 | int pixel = -1; |
811 | if (wxTheApp->m_colorCube) | |
b5f01ae0 | 812 | { |
feac7937 VS |
813 | pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; |
814 | } | |
815 | else | |
816 | { | |
817 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
818 | GdkColor *colors = cmap->colors; | |
819 | int max = 3 * (65536); | |
820 | ||
821 | for (int i = 0; i < cmap->size; i++) | |
b5f01ae0 | 822 | { |
feac7937 VS |
823 | int rdiff = (r << 8) - colors[i].red; |
824 | int gdiff = (g << 8) - colors[i].green; | |
825 | int bdiff = (b << 8) - colors[i].blue; | |
826 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
827 | if (sum < max) { pixel = i; max = sum; } | |
b5f01ae0 | 828 | } |
feac7937 | 829 | } |
2eefae6e | 830 | |
feac7937 | 831 | gdk_image_put_pixel( data_image, x, y, pixel ); |
2eefae6e | 832 | |
feac7937 VS |
833 | break; |
834 | } | |
835 | case 12: // SGI only | |
836 | { | |
837 | guint32 pixel = 0; | |
838 | switch (b_o) | |
b5f01ae0 | 839 | { |
feac7937 VS |
840 | case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break; |
841 | case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break; | |
842 | case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break; | |
843 | case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break; | |
844 | case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break; | |
845 | case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break; | |
b5f01ae0 | 846 | } |
feac7937 VS |
847 | gdk_image_put_pixel( data_image, x, y, pixel ); |
848 | break; | |
849 | } | |
850 | case 15: | |
851 | { | |
852 | guint32 pixel = 0; | |
853 | switch (b_o) | |
b5f01ae0 | 854 | { |
feac7937 VS |
855 | case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break; |
856 | case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
857 | case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
858 | case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
859 | case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
860 | case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
b5f01ae0 | 861 | } |
feac7937 VS |
862 | gdk_image_put_pixel( data_image, x, y, pixel ); |
863 | break; | |
864 | } | |
865 | case 16: | |
866 | { | |
867 | // I actually don't know if for 16-bit displays, it is alway the green | |
868 | // component or the second component which has 6 bits. | |
869 | guint32 pixel = 0; | |
870 | switch (b_o) | |
b5f01ae0 | 871 | { |
feac7937 VS |
872 | case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break; |
873 | case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
874 | case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break; | |
875 | case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
876 | case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
877 | case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
8ab696e0 | 878 | } |
feac7937 VS |
879 | gdk_image_put_pixel( data_image, x, y, pixel ); |
880 | break; | |
881 | } | |
882 | case 32: | |
883 | case 24: | |
884 | { | |
885 | guint32 pixel = 0; | |
886 | switch (b_o) | |
8ab696e0 | 887 | { |
feac7937 VS |
888 | case RGB: pixel = (r << 16) | (g << 8) | b; break; |
889 | case RBG: pixel = (r << 16) | (b << 8) | g; break; | |
890 | case BRG: pixel = (b << 16) | (r << 8) | g; break; | |
891 | case BGR: pixel = (b << 16) | (g << 8) | r; break; | |
892 | case GRB: pixel = (g << 16) | (r << 8) | b; break; | |
893 | case GBR: pixel = (g << 16) | (b << 8) | r; break; | |
b5f01ae0 | 894 | } |
feac7937 | 895 | gdk_image_put_pixel( data_image, x, y, pixel ); |
b5f01ae0 | 896 | } |
feac7937 VS |
897 | default: break; |
898 | } | |
899 | } // for | |
900 | } // for | |
b5f01ae0 | 901 | |
feac7937 | 902 | // Blit picture |
b5f01ae0 | 903 | |
feac7937 | 904 | GdkGC *data_gc = gdk_gc_new( GetPixmap() ); |
b5f01ae0 | 905 | |
feac7937 | 906 | gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); |
b5f01ae0 | 907 | |
feac7937 VS |
908 | gdk_image_destroy( data_image ); |
909 | gdk_gc_unref( data_gc ); | |
b5f01ae0 | 910 | |
feac7937 | 911 | // Blit mask |
b5f01ae0 | 912 | |
feac7937 VS |
913 | if (image.HasMask()) |
914 | { | |
915 | GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() ); | |
b5f01ae0 | 916 | |
feac7937 | 917 | gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); |
b5f01ae0 | 918 | |
feac7937 VS |
919 | gdk_image_destroy( mask_image ); |
920 | gdk_gc_unref( mask_gc ); | |
b5f01ae0 VS |
921 | } |
922 | ||
feac7937 | 923 | return true; |
b5f01ae0 VS |
924 | } |
925 | ||
feac7937 VS |
926 | #ifdef __WXGTK20__ |
927 | bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image) | |
b5f01ae0 | 928 | { |
feac7937 VS |
929 | int width = image.GetWidth(); |
930 | int height = image.GetHeight(); | |
2eefae6e | 931 | |
feac7937 VS |
932 | GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, |
933 | image.HasAlpha(), | |
934 | 8 /* bits per sample */, | |
935 | width, height); | |
936 | if (!pixbuf) | |
937 | return false; | |
c2fa61e8 | 938 | |
6db34764 | 939 | wxASSERT( image.HasAlpha() ); // for now |
feac7937 VS |
940 | wxASSERT( gdk_pixbuf_get_n_channels(pixbuf) == 4 ); |
941 | wxASSERT( gdk_pixbuf_get_width(pixbuf) == width ); | |
942 | wxASSERT( gdk_pixbuf_get_height(pixbuf) == height ); | |
943 | ||
944 | M_BMPDATA->m_pixbuf = pixbuf; | |
945 | SetHeight(height); | |
946 | SetWidth(width); | |
947 | SetDepth(wxTheApp->GetGdkVisual()->depth); | |
948 | ||
949 | // Copy the data: | |
950 | unsigned char *in = image.GetData(); | |
951 | unsigned char *out = gdk_pixbuf_get_pixels(pixbuf); | |
952 | unsigned char *alpha = image.GetAlpha(); | |
953 | ||
954 | int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width; | |
b5f01ae0 | 955 | |
feac7937 | 956 | for (int y = 0; y < height; y++, out += rowinc) |
b5f01ae0 | 957 | { |
feac7937 VS |
958 | for (int x = 0; x < width; x++, alpha++, out += 4, in += 3) |
959 | { | |
960 | out[0] = in[0]; | |
961 | out[1] = in[1]; | |
962 | out[2] = in[2]; | |
963 | out[3] = *alpha; | |
964 | } | |
b5f01ae0 | 965 | } |
feac7937 VS |
966 | |
967 | return true; | |
968 | } | |
969 | #endif // __WXGTK20__ | |
b5f01ae0 | 970 | |
feac7937 VS |
971 | wxImage wxBitmap::ConvertToImage() const |
972 | { | |
973 | wxImage image; | |
c2fa61e8 | 974 | |
feac7937 VS |
975 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
976 | ||
977 | image.Create(GetWidth(), GetHeight()); | |
978 | unsigned char *data = image.GetData(); | |
2eefae6e | 979 | |
b5f01ae0 VS |
980 | if (!data) |
981 | { | |
b5f01ae0 VS |
982 | wxFAIL_MSG( wxT("couldn't create image") ); |
983 | return wxNullImage; | |
984 | } | |
985 | ||
feac7937 VS |
986 | #ifdef __WXGTK20__ |
987 | if (HasPixbuf()) | |
b5f01ae0 | 988 | { |
feac7937 VS |
989 | GdkPixbuf *pixbuf = GetPixbuf(); |
990 | wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf) ); | |
991 | ||
992 | int w = GetWidth(); | |
993 | int h = GetHeight(); | |
994 | ||
995 | image.SetAlpha(); | |
b5f01ae0 | 996 | |
feac7937 VS |
997 | unsigned char *alpha = image.GetAlpha(); |
998 | unsigned char *in = gdk_pixbuf_get_pixels(pixbuf); | |
999 | unsigned char *out = data; | |
1000 | int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * w; | |
b5f01ae0 | 1001 | |
feac7937 VS |
1002 | for (int y = 0; y < h; y++, in += rowinc) |
1003 | { | |
1004 | for (int x = 0; x < w; x++, in += 4, out += 3, alpha++) | |
1005 | { | |
1006 | out[0] = in[0]; | |
1007 | out[1] = in[1]; | |
1008 | out[2] = in[2]; | |
1009 | *alpha = in[3]; | |
1010 | } | |
1011 | } | |
b5f01ae0 | 1012 | } |
feac7937 VS |
1013 | else |
1014 | #endif // __WXGTK20__ | |
b5f01ae0 | 1015 | { |
feac7937 VS |
1016 | // the colour used as transparent one in wxImage and the one it is |
1017 | // replaced with when it really occurs in the bitmap | |
1018 | static const int MASK_RED = 1; | |
1019 | static const int MASK_GREEN = 2; | |
1020 | static const int MASK_BLUE = 3; | |
1021 | static const int MASK_BLUE_REPLACEMENT = 2; | |
b5f01ae0 | 1022 | |
feac7937 | 1023 | GdkImage *gdk_image = (GdkImage*) NULL; |
b5f01ae0 | 1024 | |
feac7937 VS |
1025 | if (HasPixmap()) |
1026 | { | |
1027 | gdk_image = gdk_image_get( GetPixmap(), | |
1028 | 0, 0, | |
1029 | GetWidth(), GetHeight() ); | |
1030 | } | |
1031 | else if (GetBitmap()) | |
1032 | { | |
1033 | gdk_image = gdk_image_get( GetBitmap(), | |
1034 | 0, 0, | |
1035 | GetWidth(), GetHeight() ); | |
1036 | } | |
1037 | else | |
1038 | { | |
1039 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); | |
1040 | } | |
b5f01ae0 | 1041 | |
feac7937 VS |
1042 | wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") ); |
1043 | ||
1044 | GdkImage *gdk_image_mask = (GdkImage*) NULL; | |
1045 | if (GetMask()) | |
b5f01ae0 | 1046 | { |
feac7937 VS |
1047 | gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(), |
1048 | 0, 0, | |
1049 | GetWidth(), GetHeight() ); | |
1050 | ||
1051 | image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE ); | |
1052 | } | |
1053 | ||
1054 | int bpp = -1; | |
1055 | int red_shift_right = 0; | |
1056 | int green_shift_right = 0; | |
1057 | int blue_shift_right = 0; | |
1058 | int red_shift_left = 0; | |
1059 | int green_shift_left = 0; | |
1060 | int blue_shift_left = 0; | |
1061 | bool use_shift = FALSE; | |
1062 | ||
1063 | if (GetPixmap()) | |
1064 | { | |
1065 | GdkVisual *visual = gdk_window_get_visual( GetPixmap() ); | |
1066 | if (visual == NULL) | |
1067 | visual = wxTheApp->GetGdkVisual(); | |
1068 | ||
1069 | bpp = visual->depth; | |
1070 | if (bpp == 16) | |
1071 | bpp = visual->red_prec + visual->green_prec + visual->blue_prec; | |
1072 | red_shift_right = visual->red_shift; | |
1073 | red_shift_left = 8-visual->red_prec; | |
1074 | green_shift_right = visual->green_shift; | |
1075 | green_shift_left = 8-visual->green_prec; | |
1076 | blue_shift_right = visual->blue_shift; | |
1077 | blue_shift_left = 8-visual->blue_prec; | |
1078 | ||
1079 | use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR); | |
1080 | } | |
1081 | if (GetBitmap()) | |
1082 | { | |
1083 | bpp = 1; | |
1084 | } | |
1085 | ||
1086 | ||
1087 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
1088 | ||
1089 | long pos = 0; | |
1090 | for (int j = 0; j < GetHeight(); j++) | |
1091 | { | |
1092 | for (int i = 0; i < GetWidth(); i++) | |
8ab696e0 | 1093 | { |
feac7937 VS |
1094 | wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j ); |
1095 | if (bpp == 1) | |
b5f01ae0 | 1096 | { |
feac7937 VS |
1097 | if (pixel == 0) |
1098 | { | |
1099 | data[pos] = 0; | |
1100 | data[pos+1] = 0; | |
1101 | data[pos+2] = 0; | |
1102 | } | |
1103 | else | |
1104 | { | |
1105 | data[pos] = 255; | |
1106 | data[pos+1] = 255; | |
1107 | data[pos+2] = 255; | |
1108 | } | |
8ab696e0 | 1109 | } |
feac7937 | 1110 | else if (use_shift) |
8ab696e0 | 1111 | { |
feac7937 VS |
1112 | data[pos] = (pixel >> red_shift_right) << red_shift_left; |
1113 | data[pos+1] = (pixel >> green_shift_right) << green_shift_left; | |
1114 | data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left; | |
8ab696e0 | 1115 | } |
feac7937 | 1116 | else if (cmap->colors) |
b5f01ae0 | 1117 | { |
feac7937 VS |
1118 | data[pos] = cmap->colors[pixel].red >> 8; |
1119 | data[pos+1] = cmap->colors[pixel].green >> 8; | |
1120 | data[pos+2] = cmap->colors[pixel].blue >> 8; | |
2eefae6e | 1121 | } |
feac7937 | 1122 | else |
2eefae6e | 1123 | { |
feac7937 | 1124 | wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") ); |
b5f01ae0 | 1125 | } |
b5f01ae0 | 1126 | |
feac7937 VS |
1127 | if (gdk_image_mask) |
1128 | { | |
1129 | int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j ); | |
1130 | if (mask_pixel == 0) | |
1131 | { | |
1132 | data[pos] = MASK_RED; | |
1133 | data[pos+1] = MASK_GREEN; | |
1134 | data[pos+2] = MASK_BLUE; | |
1135 | } | |
1136 | else if ( data[pos] == MASK_RED && | |
1137 | data[pos+1] == MASK_GREEN && | |
1138 | data[pos+2] == MASK_BLUE ) | |
1139 | { | |
1140 | data[pos+2] = MASK_BLUE_REPLACEMENT; | |
1141 | } | |
1142 | } | |
1143 | ||
1144 | pos += 3; | |
1145 | } | |
b5f01ae0 | 1146 | } |
b5f01ae0 | 1147 | |
feac7937 VS |
1148 | gdk_image_destroy( gdk_image ); |
1149 | if (gdk_image_mask) gdk_image_destroy( gdk_image_mask ); | |
1150 | } | |
b5f01ae0 VS |
1151 | |
1152 | return image; | |
1153 | } | |
1154 | ||
c801d85f | 1155 | wxBitmap::wxBitmap( const wxBitmap& bmp ) |
3ebcd89d | 1156 | : wxGDIObject() |
c801d85f | 1157 | { |
fd0eed64 | 1158 | Ref( bmp ); |
ff7b1510 | 1159 | } |
6f65e337 | 1160 | |
debe6624 | 1161 | wxBitmap::wxBitmap( const wxString &filename, int type ) |
c801d85f | 1162 | { |
fd0eed64 | 1163 | LoadFile( filename, type ); |
ff7b1510 | 1164 | } |
c801d85f | 1165 | |
debe6624 | 1166 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth)) |
6f65e337 | 1167 | { |
3ebcd89d VZ |
1168 | if ( width > 0 && height > 0 ) |
1169 | { | |
1170 | m_refData = new wxBitmapRefData(); | |
6f65e337 | 1171 | |
3ebcd89d VZ |
1172 | M_BMPDATA->m_mask = (wxMask *) NULL; |
1173 | M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data | |
1174 | ( | |
1175 | wxGetRootWindow()->window, | |
1176 | (gchar *) bits, | |
1177 | width, | |
1178 | height | |
1179 | ); | |
1180 | M_BMPDATA->m_width = width; | |
1181 | M_BMPDATA->m_height = height; | |
1182 | M_BMPDATA->m_bpp = 1; | |
6f65e337 | 1183 | |
3ebcd89d VZ |
1184 | wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") ); |
1185 | } | |
6f65e337 | 1186 | } |
8bbe427f VZ |
1187 | |
1188 | wxBitmap::~wxBitmap() | |
c801d85f | 1189 | { |
ff7b1510 | 1190 | } |
8bbe427f | 1191 | |
c801d85f KB |
1192 | wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp ) |
1193 | { | |
7ecb8b06 VZ |
1194 | if ( m_refData != bmp.m_refData ) |
1195 | Ref( bmp ); | |
1196 | ||
8bbe427f | 1197 | return *this; |
ff7b1510 | 1198 | } |
8bbe427f | 1199 | |
f6bcfd97 | 1200 | bool wxBitmap::operator == ( const wxBitmap& bmp ) const |
c801d85f | 1201 | { |
8bbe427f | 1202 | return m_refData == bmp.m_refData; |
ff7b1510 | 1203 | } |
8bbe427f | 1204 | |
f6bcfd97 | 1205 | bool wxBitmap::operator != ( const wxBitmap& bmp ) const |
c801d85f | 1206 | { |
8bbe427f | 1207 | return m_refData != bmp.m_refData; |
ff7b1510 | 1208 | } |
8bbe427f | 1209 | |
91b8de8d | 1210 | bool wxBitmap::Ok() const |
c801d85f | 1211 | { |
feac7937 VS |
1212 | return (m_refData != NULL) && |
1213 | ( | |
1214 | #ifdef __WXGTK20__ | |
1215 | M_BMPDATA->m_pixbuf || | |
1216 | #endif | |
1217 | M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap | |
1218 | ); | |
ff7b1510 | 1219 | } |
8bbe427f | 1220 | |
91b8de8d | 1221 | int wxBitmap::GetHeight() const |
c801d85f | 1222 | { |
223d09f6 | 1223 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
e55ad60e | 1224 | |
fd0eed64 | 1225 | return M_BMPDATA->m_height; |
ff7b1510 | 1226 | } |
c801d85f | 1227 | |
91b8de8d | 1228 | int wxBitmap::GetWidth() const |
c801d85f | 1229 | { |
223d09f6 | 1230 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 1231 | |
fd0eed64 | 1232 | return M_BMPDATA->m_width; |
ff7b1510 | 1233 | } |
c801d85f | 1234 | |
91b8de8d | 1235 | int wxBitmap::GetDepth() const |
c801d85f | 1236 | { |
223d09f6 | 1237 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 1238 | |
fd0eed64 | 1239 | return M_BMPDATA->m_bpp; |
ff7b1510 | 1240 | } |
c801d85f | 1241 | |
91b8de8d | 1242 | wxMask *wxBitmap::GetMask() const |
c801d85f | 1243 | { |
223d09f6 | 1244 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1245 | |
fd0eed64 | 1246 | return M_BMPDATA->m_mask; |
ff7b1510 | 1247 | } |
c801d85f KB |
1248 | |
1249 | void wxBitmap::SetMask( wxMask *mask ) | |
1250 | { | |
223d09f6 | 1251 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); |
8bbe427f | 1252 | |
fd0eed64 | 1253 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; |
8bbe427f | 1254 | |
fd0eed64 | 1255 | M_BMPDATA->m_mask = mask; |
ff7b1510 | 1256 | } |
c801d85f | 1257 | |
db0aec83 VS |
1258 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
1259 | { | |
1260 | *this = icon; | |
1261 | return TRUE; | |
1262 | } | |
1263 | ||
17bec151 RR |
1264 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const |
1265 | { | |
1266 | wxCHECK_MSG( Ok() && | |
13111b2a VZ |
1267 | (rect.x >= 0) && (rect.y >= 0) && |
1268 | (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height), | |
17bec151 | 1269 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); |
13111b2a | 1270 | |
17bec151 RR |
1271 | wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp ); |
1272 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
13111b2a | 1273 | |
feac7937 VS |
1274 | #ifdef __WXGTK20__ |
1275 | if (HasPixbuf()) | |
17bec151 | 1276 | { |
feac7937 | 1277 | GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, |
6db34764 | 1278 | gdk_pixbuf_get_has_alpha(GetPixbuf()), |
feac7937 VS |
1279 | 8, GetWidth(), GetHeight()); |
1280 | ret.SetPixbuf(pixbuf); | |
6db34764 | 1281 | gdk_pixbuf_copy_area(GetPixbuf(), |
feac7937 VS |
1282 | rect.x, rect.y, rect.width, rect.height, |
1283 | pixbuf, 0, 0); | |
17bec151 RR |
1284 | } |
1285 | else | |
feac7937 | 1286 | #endif // __WXGTK20__ |
17bec151 | 1287 | { |
feac7937 VS |
1288 | if (ret.GetPixmap()) |
1289 | { | |
1290 | GdkGC *gc = gdk_gc_new( ret.GetPixmap() ); | |
1291 | gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
1292 | gdk_gc_destroy( gc ); | |
1293 | } | |
1294 | else | |
1295 | { | |
1296 | GdkGC *gc = gdk_gc_new( ret.GetBitmap() ); | |
1297 | GdkColor col; | |
1298 | col.pixel = 0xFFFFFF; | |
1299 | gdk_gc_set_foreground( gc, &col ); | |
1300 | col.pixel = 0; | |
1301 | gdk_gc_set_background( gc, &col ); | |
1302 | gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
1303 | gdk_gc_destroy( gc ); | |
1304 | } | |
17bec151 | 1305 | } |
13111b2a | 1306 | |
17bec151 RR |
1307 | if (GetMask()) |
1308 | { | |
1309 | wxMask *mask = new wxMask; | |
c2fa61e8 | 1310 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 ); |
13111b2a | 1311 | |
17bec151 | 1312 | GdkGC *gc = gdk_gc_new( mask->m_bitmap ); |
f9ebac93 RR |
1313 | GdkColor col; |
1314 | col.pixel = 0xFFFFFF; | |
1315 | gdk_gc_set_foreground( gc, &col ); | |
1316 | col.pixel = 0; | |
1317 | gdk_gc_set_background( gc, &col ); | |
1318 | gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
13111b2a VZ |
1319 | gdk_gc_destroy( gc ); |
1320 | ||
1321 | ret.SetMask( mask ); | |
17bec151 | 1322 | } |
13111b2a | 1323 | |
17bec151 RR |
1324 | return ret; |
1325 | } | |
1326 | ||
fd0eed64 | 1327 | bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) ) |
c801d85f | 1328 | { |
223d09f6 | 1329 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") ); |
8bbe427f | 1330 | |
b75dd496 | 1331 | // Try to save the bitmap via wxImage handlers: |
fd0eed64 | 1332 | { |
368d59f0 | 1333 | wxImage image = ConvertToImage(); |
284b4c88 | 1334 | if (image.Ok()) return image.SaveFile( name, type ); |
fd0eed64 | 1335 | } |
8bbe427f | 1336 | |
fd0eed64 | 1337 | return FALSE; |
ff7b1510 | 1338 | } |
c801d85f | 1339 | |
fd0eed64 | 1340 | bool wxBitmap::LoadFile( const wxString &name, int type ) |
c801d85f | 1341 | { |
fd0eed64 | 1342 | UnRef(); |
8bbe427f | 1343 | |
3ebcd89d VZ |
1344 | if (!wxFileExists(name)) |
1345 | return FALSE; | |
8bbe427f | 1346 | |
005f5d18 | 1347 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 1348 | |
fd0eed64 RR |
1349 | if (type == wxBITMAP_TYPE_XPM) |
1350 | { | |
1351 | m_refData = new wxBitmapRefData(); | |
8bbe427f | 1352 | |
fd0eed64 | 1353 | GdkBitmap *mask = (GdkBitmap*) NULL; |
8bbe427f | 1354 | |
3ebcd89d VZ |
1355 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm |
1356 | ( | |
1357 | wxGetRootWindow()->window, | |
1358 | &mask, | |
1359 | NULL, | |
1360 | name.fn_str() | |
1361 | ); | |
8bbe427f | 1362 | |
fd0eed64 RR |
1363 | if (mask) |
1364 | { | |
1365 | M_BMPDATA->m_mask = new wxMask(); | |
1366 | M_BMPDATA->m_mask->m_bitmap = mask; | |
1367 | } | |
8bbe427f | 1368 | |
fd0eed64 | 1369 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
c2fa61e8 | 1370 | |
103aab26 | 1371 | M_BMPDATA->m_bpp = visual->depth; |
fd0eed64 | 1372 | } |
b75dd496 | 1373 | else // try if wxImage can load it |
fd0eed64 RR |
1374 | { |
1375 | wxImage image; | |
3ebcd89d VZ |
1376 | if ( !image.LoadFile( name, type ) || !image.Ok() ) |
1377 | return FALSE; | |
1378 | ||
1379 | *this = wxBitmap(image); | |
fd0eed64 | 1380 | } |
8bbe427f | 1381 | |
fd0eed64 | 1382 | return TRUE; |
ff7b1510 | 1383 | } |
8bbe427f | 1384 | |
91b8de8d | 1385 | wxPalette *wxBitmap::GetPalette() const |
c801d85f | 1386 | { |
3ebcd89d VZ |
1387 | if (!Ok()) |
1388 | return (wxPalette *) NULL; | |
8bbe427f | 1389 | |
fd0eed64 | 1390 | return M_BMPDATA->m_palette; |
ff7b1510 | 1391 | } |
c801d85f | 1392 | |
4bc67cc5 RR |
1393 | void wxBitmap::SetHeight( int height ) |
1394 | { | |
3ebcd89d VZ |
1395 | if (!m_refData) |
1396 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1397 | |
1398 | M_BMPDATA->m_height = height; | |
1399 | } | |
1400 | ||
1401 | void wxBitmap::SetWidth( int width ) | |
1402 | { | |
3ebcd89d VZ |
1403 | if (!m_refData) |
1404 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1405 | |
1406 | M_BMPDATA->m_width = width; | |
1407 | } | |
1408 | ||
1409 | void wxBitmap::SetDepth( int depth ) | |
1410 | { | |
3ebcd89d VZ |
1411 | if (!m_refData) |
1412 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1413 | |
1414 | M_BMPDATA->m_bpp = depth; | |
1415 | } | |
1416 | ||
1417 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
1418 | { | |
3ebcd89d VZ |
1419 | if (!m_refData) |
1420 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1421 | |
1422 | M_BMPDATA->m_pixmap = pixmap; | |
6db34764 VS |
1423 | #ifdef __WXGTK20__ |
1424 | PurgeOtherRepresentations(Pixmap); | |
1425 | #endif | |
4bc67cc5 RR |
1426 | } |
1427 | ||
82ea63e6 RR |
1428 | void wxBitmap::SetBitmap( GdkPixmap *bitmap ) |
1429 | { | |
3ebcd89d VZ |
1430 | if (!m_refData) |
1431 | m_refData = new wxBitmapRefData(); | |
82ea63e6 RR |
1432 | |
1433 | M_BMPDATA->m_bitmap = bitmap; | |
6db34764 VS |
1434 | #ifdef __WXGTK20__ |
1435 | PurgeOtherRepresentations(Pixmap); | |
1436 | #endif | |
82ea63e6 RR |
1437 | } |
1438 | ||
91b8de8d | 1439 | GdkPixmap *wxBitmap::GetPixmap() const |
c801d85f | 1440 | { |
223d09f6 | 1441 | wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1442 | |
feac7937 VS |
1443 | #ifdef __WXGTK20__ |
1444 | // create the pixmap on the fly if we use Pixbuf representation: | |
1445 | if (HasPixbuf() && !HasPixmap()) | |
1446 | { | |
70dcce79 VS |
1447 | delete M_BMPDATA->m_mask; |
1448 | M_BMPDATA->m_mask = new wxMask(); | |
feac7937 VS |
1449 | gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf, |
1450 | &M_BMPDATA->m_pixmap, | |
70dcce79 | 1451 | &M_BMPDATA->m_mask->m_bitmap, |
feac7937 VS |
1452 | 128 /*threshold*/); |
1453 | } | |
1454 | #endif // __WXGTK20__ | |
1455 | ||
fd0eed64 | 1456 | return M_BMPDATA->m_pixmap; |
ff7b1510 | 1457 | } |
8bbe427f | 1458 | |
feac7937 VS |
1459 | bool wxBitmap::HasPixmap() const |
1460 | { | |
1461 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
1462 | ||
1463 | return M_BMPDATA->m_pixmap != NULL; | |
1464 | } | |
1465 | ||
91b8de8d | 1466 | GdkBitmap *wxBitmap::GetBitmap() const |
6f65e337 | 1467 | { |
223d09f6 | 1468 | wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1469 | |
fd0eed64 | 1470 | return M_BMPDATA->m_bitmap; |
ff7b1510 | 1471 | } |
feac7937 VS |
1472 | |
1473 | #ifdef __WXGTK20__ | |
1474 | GdkPixbuf *wxBitmap::GetPixbuf() const | |
1475 | { | |
1476 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
1477 | ||
6db34764 VS |
1478 | if (HasPixmap() && !HasPixbuf()) |
1479 | { | |
1480 | int width = GetWidth(); | |
1481 | int height = GetHeight(); | |
1482 | ||
1483 | GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, | |
1484 | GetMask() != NULL, | |
1485 | 8, width, height); | |
1486 | M_BMPDATA->m_pixbuf = | |
1487 | gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL, | |
1488 | 0, 0, 0, 0, width, height); | |
1489 | ||
1490 | // apply the mask to created pixbuf: | |
1491 | if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask) | |
1492 | { | |
1493 | GdkPixbuf *pmask = | |
1494 | gdk_pixbuf_get_from_drawable(NULL, | |
1495 | M_BMPDATA->m_mask->GetBitmap(), | |
1496 | NULL, | |
1497 | 0, 0, 0, 0, width, height); | |
1498 | if (pmask) | |
1499 | { | |
1500 | guchar *bmp = gdk_pixbuf_get_pixels(pixbuf); | |
1501 | guchar *mask = gdk_pixbuf_get_pixels(pmask); | |
1502 | int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width; | |
1503 | int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width; | |
1504 | ||
1505 | for (int y = 0; y < height; | |
1506 | y++, bmp += bmprowinc, mask += maskrowinc) | |
1507 | { | |
1508 | for (int x = 0; x < width; x++, bmp += 4, mask += 3) | |
1509 | { | |
1510 | if (mask[0] == 0 /*black pixel*/) | |
1511 | bmp[3] = 0; | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | gdk_pixbuf_unref(pmask); | |
1516 | } | |
1517 | } | |
1518 | } | |
1519 | ||
feac7937 VS |
1520 | return M_BMPDATA->m_pixbuf; |
1521 | } | |
1522 | ||
1523 | bool wxBitmap::HasPixbuf() const | |
1524 | { | |
1525 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
1526 | ||
1527 | return M_BMPDATA->m_pixbuf != NULL; | |
1528 | } | |
1529 | ||
1530 | void wxBitmap::SetPixbuf( GdkPixbuf *pixbuf ) | |
1531 | { | |
1532 | if (!m_refData) | |
1533 | m_refData = new wxBitmapRefData(); | |
1534 | ||
1535 | M_BMPDATA->m_pixbuf = pixbuf; | |
6db34764 | 1536 | PurgeOtherRepresentations(Pixbuf); |
feac7937 VS |
1537 | } |
1538 | ||
1539 | void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep) | |
1540 | { | |
1541 | if (keep == Pixmap && HasPixbuf()) | |
1542 | { | |
1543 | gdk_pixbuf_unref( M_BMPDATA->m_pixbuf ); | |
1544 | M_BMPDATA->m_pixbuf = NULL; | |
1545 | } | |
1546 | if (keep == Pixbuf && HasPixmap()) | |
1547 | { | |
1548 | gdk_pixmap_unref( M_BMPDATA->m_pixmap ); | |
1549 | M_BMPDATA->m_pixmap = NULL; | |
1550 | } | |
1551 | } | |
1552 | ||
1553 | #endif // __WXGTK20__ |