]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/bitmap.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #include "wx/bitmap.h" | |
14 | ||
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/app.h" | |
17 | #include "wx/palette.h" | |
18 | #include "wx/icon.h" | |
19 | #include "wx/math.h" | |
20 | #include "wx/image.h" | |
21 | #include "wx/colour.h" | |
22 | #endif | |
23 | ||
24 | #include "wx/rawbmp.h" | |
25 | ||
26 | #include <gtk/gtk.h> | |
27 | ||
28 | //----------------------------------------------------------------------------- | |
29 | // data | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
32 | extern GtkWidget *wxGetRootWindow(); | |
33 | ||
34 | //----------------------------------------------------------------------------- | |
35 | // wxMask | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
39 | ||
40 | wxMask::wxMask() | |
41 | { | |
42 | m_bitmap = (GdkBitmap *) NULL; | |
43 | } | |
44 | ||
45 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) | |
46 | { | |
47 | m_bitmap = (GdkBitmap *) NULL; | |
48 | Create( bitmap, colour ); | |
49 | } | |
50 | ||
51 | #if wxUSE_PALETTE | |
52 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) | |
53 | { | |
54 | m_bitmap = (GdkBitmap *) NULL; | |
55 | Create( bitmap, paletteIndex ); | |
56 | } | |
57 | #endif // wxUSE_PALETTE | |
58 | ||
59 | wxMask::wxMask( const wxBitmap& bitmap ) | |
60 | { | |
61 | m_bitmap = (GdkBitmap *) NULL; | |
62 | Create( bitmap ); | |
63 | } | |
64 | ||
65 | wxMask::~wxMask() | |
66 | { | |
67 | if (m_bitmap) | |
68 | g_object_unref (m_bitmap); | |
69 | } | |
70 | ||
71 | bool wxMask::Create( const wxBitmap& bitmap, | |
72 | const wxColour& colour ) | |
73 | { | |
74 | if (m_bitmap) | |
75 | { | |
76 | g_object_unref (m_bitmap); | |
77 | m_bitmap = (GdkBitmap*) NULL; | |
78 | } | |
79 | ||
80 | const int w = bitmap.GetWidth(); | |
81 | const int h = bitmap.GetHeight(); | |
82 | ||
83 | // create mask as XBM format bitmap | |
84 | ||
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]; | |
88 | // set bits are unmasked | |
89 | memset(out, 0xff, out_size); | |
90 | unsigned bit_index = 0; | |
91 | if (bitmap.HasPixbuf()) | |
92 | { | |
93 | const wxByte r_mask = colour.Red(); | |
94 | const wxByte g_mask = colour.Green(); | |
95 | const wxByte b_mask = colour.Blue(); | |
96 | GdkPixbuf* pixbuf = bitmap.GetPixbuf(); | |
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 | } | |
108 | } | |
109 | else | |
110 | { | |
111 | GdkImage* image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h); | |
112 | GdkColormap* colormap = gdk_image_get_colormap(image); | |
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 | |
118 | { | |
119 | wxColor c(colour); | |
120 | c.CalcPixel(colormap); | |
121 | mask_pixel = c.GetPixel(); | |
122 | } | |
123 | for (int y = 0; y < h; y++) | |
124 | { | |
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; | |
129 | } | |
130 | g_object_unref(image); | |
131 | } | |
132 | m_bitmap = gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h); | |
133 | delete[] out; | |
134 | return true; | |
135 | } | |
136 | ||
137 | #if wxUSE_PALETTE | |
138 | bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex ) | |
139 | { | |
140 | unsigned char r,g,b; | |
141 | wxPalette *pal = bitmap.GetPalette(); | |
142 | ||
143 | wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") ); | |
144 | ||
145 | pal->GetRGB(paletteIndex, &r, &g, &b); | |
146 | ||
147 | return Create(bitmap, wxColour(r, g, b)); | |
148 | } | |
149 | #endif // wxUSE_PALETTE | |
150 | ||
151 | bool wxMask::Create( const wxBitmap& bitmap ) | |
152 | { | |
153 | if (m_bitmap) | |
154 | { | |
155 | g_object_unref (m_bitmap); | |
156 | m_bitmap = (GdkBitmap*) NULL; | |
157 | } | |
158 | ||
159 | if (!bitmap.Ok()) return false; | |
160 | ||
161 | wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") ); | |
162 | ||
163 | m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); | |
164 | ||
165 | if (!m_bitmap) return false; | |
166 | ||
167 | GdkGC *gc = gdk_gc_new( m_bitmap ); | |
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()); | |
170 | g_object_unref (gc); | |
171 | ||
172 | return true; | |
173 | } | |
174 | ||
175 | GdkBitmap *wxMask::GetBitmap() const | |
176 | { | |
177 | return m_bitmap; | |
178 | } | |
179 | ||
180 | //----------------------------------------------------------------------------- | |
181 | // wxBitmap | |
182 | //----------------------------------------------------------------------------- | |
183 | ||
184 | class wxBitmapRefData: public wxObjectRefData | |
185 | { | |
186 | public: | |
187 | wxBitmapRefData(); | |
188 | virtual ~wxBitmapRefData(); | |
189 | ||
190 | GdkPixmap *m_pixmap; | |
191 | GdkPixbuf *m_pixbuf; | |
192 | wxMask *m_mask; | |
193 | int m_width; | |
194 | int m_height; | |
195 | int m_bpp; | |
196 | #if wxUSE_PALETTE | |
197 | wxPalette *m_palette; | |
198 | #endif // wxUSE_PALETTE | |
199 | }; | |
200 | ||
201 | wxBitmapRefData::wxBitmapRefData() | |
202 | { | |
203 | m_pixmap = (GdkPixmap *) NULL; | |
204 | m_pixbuf = (GdkPixbuf *) NULL; | |
205 | m_mask = (wxMask *) NULL; | |
206 | m_width = 0; | |
207 | m_height = 0; | |
208 | m_bpp = 0; | |
209 | #if wxUSE_PALETTE | |
210 | m_palette = (wxPalette *) NULL; | |
211 | #endif // wxUSE_PALETTE | |
212 | } | |
213 | ||
214 | wxBitmapRefData::~wxBitmapRefData() | |
215 | { | |
216 | if (m_pixmap) | |
217 | g_object_unref (m_pixmap); | |
218 | if (m_pixbuf) | |
219 | g_object_unref (m_pixbuf); | |
220 | delete m_mask; | |
221 | #if wxUSE_PALETTE | |
222 | delete m_palette; | |
223 | #endif // wxUSE_PALETTE | |
224 | } | |
225 | ||
226 | //----------------------------------------------------------------------------- | |
227 | ||
228 | #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData) | |
229 | ||
230 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
231 | ||
232 | wxBitmap::wxBitmap(int width, int height, int depth) | |
233 | { | |
234 | Create(width, height, depth); | |
235 | } | |
236 | ||
237 | wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type) | |
238 | { | |
239 | LoadFile(filename, type); | |
240 | } | |
241 | ||
242 | wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) | |
243 | { | |
244 | wxASSERT(depth == 1); | |
245 | if (width > 0 && height > 0 && depth == 1) | |
246 | { | |
247 | SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, bits, width, height)); | |
248 | ||
249 | wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("couldn't create bitmap") ); | |
250 | } | |
251 | } | |
252 | ||
253 | wxBitmap::wxBitmap(const char* const* bits) | |
254 | { | |
255 | wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data")); | |
256 | ||
257 | GdkBitmap* mask = NULL; | |
258 | SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, wx_const_cast(char**, bits))); | |
259 | ||
260 | if (M_BMPDATA->m_pixmap != NULL && mask != NULL) | |
261 | { | |
262 | M_BMPDATA->m_mask = new wxMask; | |
263 | M_BMPDATA->m_mask->m_bitmap = mask; | |
264 | } | |
265 | } | |
266 | ||
267 | wxBitmap::~wxBitmap() | |
268 | { | |
269 | } | |
270 | ||
271 | bool wxBitmap::Create( int width, int height, int depth ) | |
272 | { | |
273 | UnRef(); | |
274 | ||
275 | if ( width <= 0 || height <= 0 ) | |
276 | { | |
277 | return false; | |
278 | } | |
279 | ||
280 | if (depth == 32) | |
281 | { | |
282 | SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, width, height), 32); | |
283 | } | |
284 | else | |
285 | { | |
286 | if (depth != 1) | |
287 | { | |
288 | const GdkVisual* visual = wxTheApp->GetGdkVisual(); | |
289 | if (depth == -1) | |
290 | depth = visual->depth; | |
291 | ||
292 | wxCHECK_MSG(depth == visual->depth, false, wxT("invalid bitmap depth")); | |
293 | } | |
294 | ||
295 | SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth)); | |
296 | } | |
297 | ||
298 | return Ok(); | |
299 | } | |
300 | ||
301 | wxBitmap wxBitmap::Rescale(int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy) const | |
302 | { | |
303 | wxBitmap bmp; | |
304 | ||
305 | wxCHECK_MSG(Ok(), bmp, wxT("invalid bitmap")); | |
306 | ||
307 | int width = wxMax(newx, 1); | |
308 | int height = wxMax(newy, 1); | |
309 | width = wxMin(width, clipwidth); | |
310 | height = wxMin(height, clipheight); | |
311 | ||
312 | const double scale_x = double(newx) / M_BMPDATA->m_width; | |
313 | const double scale_y = double(newy) / M_BMPDATA->m_height; | |
314 | ||
315 | // Converting to pixbuf, scaling with gdk_pixbuf_scale, and converting | |
316 | // back, is faster than scaling pixmap ourselves. | |
317 | ||
318 | // use pixbuf if already available, | |
319 | // otherwise create temporary pixbuf from pixmap | |
320 | GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf; | |
321 | if (pixbuf) | |
322 | g_object_ref(pixbuf); | |
323 | else | |
324 | pixbuf = gdk_pixbuf_get_from_drawable( | |
325 | NULL, M_BMPDATA->m_pixmap, NULL, | |
326 | 0, 0, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height); | |
327 | ||
328 | // new pixbuf for scaled wxBitmap | |
329 | GdkPixbuf* pixbuf_scaled = gdk_pixbuf_new( | |
330 | GDK_COLORSPACE_RGB, gdk_pixbuf_get_has_alpha(pixbuf), 8, width, height); | |
331 | ||
332 | // GDK_INTERP_NEAREST is the lowest-quality method for continuous-tone | |
333 | // images, but the only one which preserves sharp edges | |
334 | gdk_pixbuf_scale( | |
335 | pixbuf, pixbuf_scaled, | |
336 | 0, 0, width, height, -clipx, -clipy, scale_x, scale_y, | |
337 | GDK_INTERP_NEAREST); | |
338 | ||
339 | g_object_unref(pixbuf); | |
340 | bmp.SetPixbuf(pixbuf_scaled, M_BMPDATA->m_bpp); | |
341 | ||
342 | if (M_BMPDATA->m_mask) | |
343 | { | |
344 | pixbuf = gdk_pixbuf_get_from_drawable( | |
345 | NULL, M_BMPDATA->m_mask->m_bitmap, NULL, | |
346 | 0, 0, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height); | |
347 | ||
348 | pixbuf_scaled = gdk_pixbuf_new( | |
349 | GDK_COLORSPACE_RGB, false, 8, width, height); | |
350 | ||
351 | gdk_pixbuf_scale( | |
352 | pixbuf, pixbuf_scaled, | |
353 | 0, 0, width, height, -clipx, -clipy, scale_x, scale_y, | |
354 | GDK_INTERP_NEAREST); | |
355 | ||
356 | g_object_unref(pixbuf); | |
357 | ||
358 | // use existing functionality to create mask from scaled pixbuf | |
359 | wxBitmap maskbmp; | |
360 | maskbmp.SetPixbuf(pixbuf_scaled); | |
361 | bmp.SetMask(new wxMask(maskbmp, *wxBLACK)); | |
362 | } | |
363 | return bmp; | |
364 | } | |
365 | ||
366 | #if wxUSE_IMAGE | |
367 | ||
368 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth) | |
369 | { | |
370 | UnRef(); | |
371 | ||
372 | wxCHECK_MSG( image.Ok(), false, wxT("invalid image") ); | |
373 | wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") ); | |
374 | ||
375 | if (image.GetWidth() <= 0 || image.GetHeight() <= 0) | |
376 | return false; | |
377 | ||
378 | // create pixbuf if image has alpha and requested depth is compatible | |
379 | if (image.HasAlpha() && (depth == -1 || depth == 32)) | |
380 | return CreateFromImageAsPixbuf(image); | |
381 | ||
382 | // otherwise create pixmap, if alpha is present it will be converted to mask | |
383 | return CreateFromImageAsPixmap(image, depth); | |
384 | } | |
385 | ||
386 | bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth) | |
387 | { | |
388 | const int w = image.GetWidth(); | |
389 | const int h = image.GetHeight(); | |
390 | if (depth == 1) | |
391 | { | |
392 | // create XBM format bitmap | |
393 | ||
394 | // one bit per pixel, each row starts on a byte boundary | |
395 | const size_t out_size = size_t((w + 7) / 8) * unsigned(h); | |
396 | wxByte* out = new wxByte[out_size]; | |
397 | // set bits are black | |
398 | memset(out, 0xff, out_size); | |
399 | const wxByte* in = image.GetData(); | |
400 | unsigned bit_index = 0; | |
401 | for (int y = 0; y < h; y++) | |
402 | { | |
403 | for (int x = 0; x < w; x++, in += 3, bit_index++) | |
404 | if (in[0] == 255 && in[1] == 255 && in[2] == 255) | |
405 | out[bit_index >> 3] ^= 1 << (bit_index & 7); | |
406 | // move index to next byte boundary | |
407 | bit_index = (bit_index + 7) & ~7u; | |
408 | } | |
409 | SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h)); | |
410 | delete[] out; | |
411 | } | |
412 | else | |
413 | { | |
414 | SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth)); | |
415 | GdkGC* gc = gdk_gc_new(M_BMPDATA->m_pixmap); | |
416 | gdk_draw_rgb_image( | |
417 | M_BMPDATA->m_pixmap, gc, | |
418 | 0, 0, w, h, | |
419 | GDK_RGB_DITHER_NONE, image.GetData(), w * 3); | |
420 | g_object_unref(gc); | |
421 | } | |
422 | ||
423 | const wxByte* alpha = image.GetAlpha(); | |
424 | if (alpha != NULL || image.HasMask()) | |
425 | { | |
426 | // create mask as XBM format bitmap | |
427 | ||
428 | const size_t out_size = size_t((w + 7) / 8) * unsigned(h); | |
429 | wxByte* out = new wxByte[out_size]; | |
430 | memset(out, 0xff, out_size); | |
431 | unsigned bit_index = 0; | |
432 | if (alpha != NULL) | |
433 | { | |
434 | for (int y = 0; y < h; y++) | |
435 | { | |
436 | for (int x = 0; x < w; x++, bit_index++) | |
437 | if (*alpha++ < wxIMAGE_ALPHA_THRESHOLD) | |
438 | out[bit_index >> 3] ^= 1 << (bit_index & 7); | |
439 | bit_index = (bit_index + 7) & ~7u; | |
440 | } | |
441 | } | |
442 | else | |
443 | { | |
444 | const wxByte r_mask = image.GetMaskRed(); | |
445 | const wxByte g_mask = image.GetMaskGreen(); | |
446 | const wxByte b_mask = image.GetMaskBlue(); | |
447 | const wxByte* in = image.GetData(); | |
448 | for (int y = 0; y < h; y++) | |
449 | { | |
450 | for (int x = 0; x < w; x++, in += 3, bit_index++) | |
451 | if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask) | |
452 | out[bit_index >> 3] ^= 1 << (bit_index & 7); | |
453 | bit_index = (bit_index + 7) & ~7u; | |
454 | } | |
455 | } | |
456 | wxMask* mask = new wxMask; | |
457 | mask->m_bitmap = gdk_bitmap_create_from_data(M_BMPDATA->m_pixmap, (char*)out, w, h); | |
458 | SetMask(mask); | |
459 | delete[] out; | |
460 | } | |
461 | return true; | |
462 | } | |
463 | ||
464 | bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image) | |
465 | { | |
466 | wxASSERT(image.HasAlpha()); | |
467 | ||
468 | int width = image.GetWidth(); | |
469 | int height = image.GetHeight(); | |
470 | ||
471 | Create(width, height, 32); | |
472 | GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf; | |
473 | if (!pixbuf) | |
474 | return false; | |
475 | ||
476 | // Copy the data: | |
477 | const unsigned char* in = image.GetData(); | |
478 | unsigned char *out = gdk_pixbuf_get_pixels(pixbuf); | |
479 | unsigned char *alpha = image.GetAlpha(); | |
480 | ||
481 | int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width; | |
482 | ||
483 | for (int y = 0; y < height; y++, out += rowpad) | |
484 | { | |
485 | for (int x = 0; x < width; x++, alpha++, out += 4, in += 3) | |
486 | { | |
487 | out[0] = in[0]; | |
488 | out[1] = in[1]; | |
489 | out[2] = in[2]; | |
490 | out[3] = *alpha; | |
491 | } | |
492 | } | |
493 | ||
494 | return true; | |
495 | } | |
496 | ||
497 | wxImage wxBitmap::ConvertToImage() const | |
498 | { | |
499 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); | |
500 | ||
501 | const int w = GetWidth(); | |
502 | const int h = GetHeight(); | |
503 | wxImage image(w, h, false); | |
504 | unsigned char *data = image.GetData(); | |
505 | ||
506 | wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") ); | |
507 | ||
508 | // prefer pixbuf if available, it will preserve alpha and should be quicker | |
509 | if (HasPixbuf()) | |
510 | { | |
511 | GdkPixbuf *pixbuf = GetPixbuf(); | |
512 | unsigned char* alpha = NULL; | |
513 | if (gdk_pixbuf_get_has_alpha(pixbuf)) | |
514 | { | |
515 | image.SetAlpha(); | |
516 | alpha = image.GetAlpha(); | |
517 | } | |
518 | const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf); | |
519 | unsigned char *out = data; | |
520 | const int inc = 3 + int(alpha != NULL); | |
521 | const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w; | |
522 | ||
523 | for (int y = 0; y < h; y++, in += rowpad) | |
524 | { | |
525 | for (int x = 0; x < w; x++, in += inc, out += 3) | |
526 | { | |
527 | out[0] = in[0]; | |
528 | out[1] = in[1]; | |
529 | out[2] = in[2]; | |
530 | if (alpha != NULL) | |
531 | *alpha++ = in[3]; | |
532 | } | |
533 | } | |
534 | } | |
535 | else | |
536 | { | |
537 | GdkPixmap* pixmap = GetPixmap(); | |
538 | GdkPixmap* pixmap_invert = NULL; | |
539 | if (GetDepth() == 1) | |
540 | { | |
541 | // mono bitmaps are inverted, i.e. 0 is white | |
542 | pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1); | |
543 | GdkGC* gc = gdk_gc_new(pixmap_invert); | |
544 | gdk_gc_set_function(gc, GDK_COPY_INVERT); | |
545 | gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h); | |
546 | g_object_unref(gc); | |
547 | pixmap = pixmap_invert; | |
548 | } | |
549 | // create a pixbuf which shares data with the wxImage | |
550 | GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data( | |
551 | data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL); | |
552 | ||
553 | gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h); | |
554 | ||
555 | g_object_unref(pixbuf); | |
556 | if (pixmap_invert != NULL) | |
557 | g_object_unref(pixmap_invert); | |
558 | } | |
559 | // convert mask, unless there is already alpha | |
560 | if (GetMask() && !image.HasAlpha()) | |
561 | { | |
562 | // we hard code the mask colour for now but we could also make an | |
563 | // effort (and waste time) to choose a colour not present in the | |
564 | // image already to avoid having to fudge the pixels below -- | |
565 | // whether it's worth to do it is unclear however | |
566 | const int MASK_RED = 1; | |
567 | const int MASK_GREEN = 2; | |
568 | const int MASK_BLUE = 3; | |
569 | const int MASK_BLUE_REPLACEMENT = 2; | |
570 | ||
571 | image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE); | |
572 | GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h); | |
573 | ||
574 | for (int y = 0; y < h; y++) | |
575 | { | |
576 | for (int x = 0; x < w; x++, data += 3) | |
577 | { | |
578 | if (gdk_image_get_pixel(image_mask, x, y) == 0) | |
579 | { | |
580 | data[0] = MASK_RED; | |
581 | data[1] = MASK_GREEN; | |
582 | data[2] = MASK_BLUE; | |
583 | } | |
584 | else if (data[0] == MASK_RED && data[1] == MASK_GREEN && data[2] == MASK_BLUE) | |
585 | { | |
586 | // we have to fudge the colour a bit to prevent | |
587 | // this pixel from appearing transparent | |
588 | data[2] = MASK_BLUE_REPLACEMENT; | |
589 | } | |
590 | } | |
591 | } | |
592 | g_object_unref(image_mask); | |
593 | } | |
594 | ||
595 | return image; | |
596 | } | |
597 | ||
598 | #endif // wxUSE_IMAGE | |
599 | ||
600 | bool wxBitmap::IsOk() const | |
601 | { | |
602 | return (m_refData != NULL) && | |
603 | ( | |
604 | M_BMPDATA->m_pixbuf || | |
605 | M_BMPDATA->m_pixmap | |
606 | ); | |
607 | } | |
608 | ||
609 | int wxBitmap::GetHeight() const | |
610 | { | |
611 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
612 | ||
613 | return M_BMPDATA->m_height; | |
614 | } | |
615 | ||
616 | int wxBitmap::GetWidth() const | |
617 | { | |
618 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
619 | ||
620 | return M_BMPDATA->m_width; | |
621 | } | |
622 | ||
623 | int wxBitmap::GetDepth() const | |
624 | { | |
625 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
626 | ||
627 | return M_BMPDATA->m_bpp; | |
628 | } | |
629 | ||
630 | wxMask *wxBitmap::GetMask() const | |
631 | { | |
632 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); | |
633 | ||
634 | return M_BMPDATA->m_mask; | |
635 | } | |
636 | ||
637 | void wxBitmap::SetMask( wxMask *mask ) | |
638 | { | |
639 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
640 | ||
641 | AllocExclusive(); | |
642 | delete M_BMPDATA->m_mask; | |
643 | M_BMPDATA->m_mask = mask; | |
644 | } | |
645 | ||
646 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
647 | { | |
648 | *this = icon; | |
649 | return Ok(); | |
650 | } | |
651 | ||
652 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const | |
653 | { | |
654 | wxBitmap ret; | |
655 | ||
656 | wxCHECK_MSG(Ok(), ret, wxT("invalid bitmap")); | |
657 | wxCHECK_MSG(rect.x >= 0 && rect.y >= 0 && | |
658 | rect.x + rect.width <= M_BMPDATA->m_width && | |
659 | rect.y + rect.height <= M_BMPDATA->m_height, | |
660 | ret, wxT("invalid bitmap region")); | |
661 | ||
662 | if (HasPixbuf() || M_BMPDATA->m_bpp == 32) | |
663 | { | |
664 | GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, | |
665 | gdk_pixbuf_get_has_alpha(GetPixbuf()), | |
666 | 8, rect.width, rect.height); | |
667 | ret.SetPixbuf(pixbuf, M_BMPDATA->m_bpp); | |
668 | gdk_pixbuf_copy_area(GetPixbuf(), | |
669 | rect.x, rect.y, rect.width, rect.height, | |
670 | pixbuf, 0, 0); | |
671 | } | |
672 | else | |
673 | { | |
674 | ret.Create(rect.width, rect.height, M_BMPDATA->m_bpp); | |
675 | GdkGC *gc = gdk_gc_new( ret.GetPixmap() ); | |
676 | gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
677 | g_object_unref (gc); | |
678 | } | |
679 | // make mask, unless there is already alpha | |
680 | if (GetMask() && !HasAlpha()) | |
681 | { | |
682 | wxMask *mask = new wxMask; | |
683 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 ); | |
684 | ||
685 | GdkGC *gc = gdk_gc_new( mask->m_bitmap ); | |
686 | gdk_draw_drawable(mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height); | |
687 | g_object_unref (gc); | |
688 | ||
689 | ret.SetMask( mask ); | |
690 | } | |
691 | ||
692 | return ret; | |
693 | } | |
694 | ||
695 | bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const | |
696 | { | |
697 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
698 | ||
699 | #if wxUSE_IMAGE | |
700 | // Try to save the bitmap via wxImage handlers: | |
701 | wxImage image = ConvertToImage(); | |
702 | return image.Ok() && image.SaveFile(name, type); | |
703 | #else // !wxUSE_IMAGE | |
704 | return false; | |
705 | #endif // wxUSE_IMAGE | |
706 | } | |
707 | ||
708 | bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type ) | |
709 | { | |
710 | UnRef(); | |
711 | ||
712 | if (type == wxBITMAP_TYPE_XPM) | |
713 | { | |
714 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
715 | SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str())); | |
716 | ||
717 | if (mask) | |
718 | { | |
719 | M_BMPDATA->m_mask = new wxMask; | |
720 | M_BMPDATA->m_mask->m_bitmap = mask; | |
721 | } | |
722 | } | |
723 | #if wxUSE_IMAGE | |
724 | else // try if wxImage can load it | |
725 | { | |
726 | wxImage image; | |
727 | if (image.LoadFile(name, type) && image.Ok()) | |
728 | CreateFromImage(image, -1); | |
729 | } | |
730 | #endif // wxUSE_IMAGE | |
731 | ||
732 | return Ok(); | |
733 | } | |
734 | ||
735 | #if wxUSE_PALETTE | |
736 | wxPalette *wxBitmap::GetPalette() const | |
737 | { | |
738 | wxCHECK_MSG(Ok(), NULL, wxT("invalid bitmap")); | |
739 | ||
740 | return M_BMPDATA->m_palette; | |
741 | } | |
742 | ||
743 | void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette)) | |
744 | { | |
745 | // TODO | |
746 | } | |
747 | #endif // wxUSE_PALETTE | |
748 | ||
749 | void wxBitmap::SetHeight( int height ) | |
750 | { | |
751 | AllocExclusive(); | |
752 | M_BMPDATA->m_height = height; | |
753 | } | |
754 | ||
755 | void wxBitmap::SetWidth( int width ) | |
756 | { | |
757 | AllocExclusive(); | |
758 | M_BMPDATA->m_width = width; | |
759 | } | |
760 | ||
761 | void wxBitmap::SetDepth( int depth ) | |
762 | { | |
763 | AllocExclusive(); | |
764 | M_BMPDATA->m_bpp = depth; | |
765 | } | |
766 | ||
767 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
768 | { | |
769 | if (!m_refData) | |
770 | m_refData = new wxBitmapRefData; | |
771 | ||
772 | // AllocExclusive should not be needed for this internal function | |
773 | wxASSERT(m_refData->GetRefCount() == 1); | |
774 | wxASSERT(M_BMPDATA->m_pixmap == NULL); | |
775 | M_BMPDATA->m_pixmap = pixmap; | |
776 | gdk_drawable_get_size(pixmap, &M_BMPDATA->m_width, &M_BMPDATA->m_height); | |
777 | M_BMPDATA->m_bpp = gdk_drawable_get_depth(pixmap); | |
778 | PurgeOtherRepresentations(Pixmap); | |
779 | } | |
780 | ||
781 | GdkPixmap *wxBitmap::GetPixmap() const | |
782 | { | |
783 | wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") ); | |
784 | ||
785 | // create the pixmap on the fly if we use Pixbuf representation: | |
786 | if (M_BMPDATA->m_pixmap == NULL) | |
787 | { | |
788 | GdkPixmap** pmask = NULL; | |
789 | if (gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf)) | |
790 | { | |
791 | // make new mask from alpha | |
792 | delete M_BMPDATA->m_mask; | |
793 | M_BMPDATA->m_mask = new wxMask; | |
794 | pmask = &M_BMPDATA->m_mask->m_bitmap; | |
795 | } | |
796 | gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf, | |
797 | &M_BMPDATA->m_pixmap, | |
798 | pmask, | |
799 | 0x80 /* alpha threshold */); | |
800 | } | |
801 | ||
802 | return M_BMPDATA->m_pixmap; | |
803 | } | |
804 | ||
805 | bool wxBitmap::HasPixmap() const | |
806 | { | |
807 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
808 | ||
809 | return M_BMPDATA->m_pixmap != NULL; | |
810 | } | |
811 | ||
812 | GdkPixbuf *wxBitmap::GetPixbuf() const | |
813 | { | |
814 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
815 | ||
816 | if (M_BMPDATA->m_pixbuf == NULL) | |
817 | { | |
818 | int width = GetWidth(); | |
819 | int height = GetHeight(); | |
820 | ||
821 | GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, | |
822 | GetMask() != NULL, | |
823 | 8, width, height); | |
824 | M_BMPDATA->m_pixbuf = pixbuf; | |
825 | gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL, | |
826 | 0, 0, 0, 0, width, height); | |
827 | ||
828 | // apply the mask to created pixbuf: | |
829 | if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask) | |
830 | { | |
831 | GdkPixbuf *pmask = | |
832 | gdk_pixbuf_get_from_drawable(NULL, | |
833 | M_BMPDATA->m_mask->GetBitmap(), | |
834 | NULL, | |
835 | 0, 0, 0, 0, width, height); | |
836 | if (pmask) | |
837 | { | |
838 | guchar *bmp = gdk_pixbuf_get_pixels(pixbuf); | |
839 | guchar *mask = gdk_pixbuf_get_pixels(pmask); | |
840 | int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width; | |
841 | int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width; | |
842 | ||
843 | for (int y = 0; y < height; | |
844 | y++, bmp += bmprowinc, mask += maskrowinc) | |
845 | { | |
846 | for (int x = 0; x < width; x++, bmp += 4, mask += 3) | |
847 | { | |
848 | if (mask[0] == 0 /*black pixel*/) | |
849 | bmp[3] = 0; | |
850 | } | |
851 | } | |
852 | ||
853 | g_object_unref (pmask); | |
854 | } | |
855 | } | |
856 | } | |
857 | ||
858 | return M_BMPDATA->m_pixbuf; | |
859 | } | |
860 | ||
861 | bool wxBitmap::HasPixbuf() const | |
862 | { | |
863 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
864 | ||
865 | return M_BMPDATA->m_pixbuf != NULL; | |
866 | } | |
867 | ||
868 | void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf, int depth) | |
869 | { | |
870 | if (!m_refData) | |
871 | m_refData = new wxBitmapRefData; | |
872 | ||
873 | // AllocExclusive should not be needed for this internal function | |
874 | wxASSERT(m_refData->GetRefCount() == 1); | |
875 | wxASSERT(M_BMPDATA->m_pixbuf == NULL); | |
876 | M_BMPDATA->m_pixbuf = pixbuf; | |
877 | M_BMPDATA->m_width = gdk_pixbuf_get_width(pixbuf); | |
878 | M_BMPDATA->m_height = gdk_pixbuf_get_height(pixbuf); | |
879 | // if depth specified | |
880 | if (depth != 0) | |
881 | M_BMPDATA->m_bpp = depth; | |
882 | else if (M_BMPDATA->m_bpp == 0) | |
883 | // use something reasonable | |
884 | M_BMPDATA->m_bpp = wxTheApp->GetGdkVisual()->depth; | |
885 | PurgeOtherRepresentations(Pixbuf); | |
886 | } | |
887 | ||
888 | void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep) | |
889 | { | |
890 | if (keep == Pixmap && HasPixbuf()) | |
891 | { | |
892 | g_object_unref (M_BMPDATA->m_pixbuf); | |
893 | M_BMPDATA->m_pixbuf = NULL; | |
894 | } | |
895 | if (keep == Pixbuf && HasPixmap()) | |
896 | { | |
897 | g_object_unref (M_BMPDATA->m_pixmap); | |
898 | M_BMPDATA->m_pixmap = NULL; | |
899 | } | |
900 | } | |
901 | ||
902 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) | |
903 | { | |
904 | void* bits = NULL; | |
905 | GdkPixbuf *pixbuf = GetPixbuf(); | |
906 | const bool hasAlpha = HasAlpha(); | |
907 | // allow access if bpp is valid and matches existence of alpha | |
908 | if (pixbuf != NULL && ( | |
909 | bpp == 24 && !hasAlpha || | |
910 | bpp == 32 && hasAlpha)) | |
911 | { | |
912 | data.m_height = gdk_pixbuf_get_height( pixbuf ); | |
913 | data.m_width = gdk_pixbuf_get_width( pixbuf ); | |
914 | data.m_stride = gdk_pixbuf_get_rowstride( pixbuf ); | |
915 | bits = gdk_pixbuf_get_pixels(pixbuf); | |
916 | } | |
917 | return bits; | |
918 | } | |
919 | ||
920 | void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data)) | |
921 | { | |
922 | } | |
923 | ||
924 | bool wxBitmap::HasAlpha() const | |
925 | { | |
926 | return m_refData != NULL && M_BMPDATA->m_pixbuf != NULL && | |
927 | gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf); | |
928 | } | |
929 | ||
930 | wxObjectRefData* wxBitmap::CreateRefData() const | |
931 | { | |
932 | return new wxBitmapRefData; | |
933 | } | |
934 | ||
935 | wxObjectRefData* wxBitmap::CloneRefData(const wxObjectRefData* data) const | |
936 | { | |
937 | const wxBitmapRefData* oldRef = wx_static_cast(const wxBitmapRefData*, data); | |
938 | wxBitmapRefData* newRef = new wxBitmapRefData; | |
939 | newRef->m_width = oldRef->m_width; | |
940 | newRef->m_height = oldRef->m_height; | |
941 | newRef->m_bpp = oldRef->m_bpp; | |
942 | if (oldRef->m_pixmap != NULL) | |
943 | { | |
944 | newRef->m_pixmap = gdk_pixmap_new( | |
945 | oldRef->m_pixmap, oldRef->m_width, oldRef->m_height, | |
946 | // use pixmap depth, m_bpp may not match | |
947 | gdk_drawable_get_depth(oldRef->m_pixmap)); | |
948 | GdkGC* gc = gdk_gc_new(newRef->m_pixmap); | |
949 | gdk_draw_drawable( | |
950 | newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1); | |
951 | g_object_unref(gc); | |
952 | } | |
953 | if (oldRef->m_pixbuf != NULL) | |
954 | { | |
955 | newRef->m_pixbuf = gdk_pixbuf_copy(oldRef->m_pixbuf); | |
956 | } | |
957 | if (oldRef->m_mask != NULL) | |
958 | { | |
959 | newRef->m_mask = new wxMask; | |
960 | newRef->m_mask->m_bitmap = gdk_pixmap_new( | |
961 | oldRef->m_mask->m_bitmap, oldRef->m_width, oldRef->m_height, 1); | |
962 | GdkGC* gc = gdk_gc_new(newRef->m_mask->m_bitmap); | |
963 | gdk_draw_drawable(newRef->m_mask->m_bitmap, | |
964 | gc, oldRef->m_mask->m_bitmap, 0, 0, 0, 0, -1, -1); | |
965 | g_object_unref(gc); | |
966 | } | |
967 | #if wxUSE_PALETTE | |
968 | // implement this if SetPalette is ever implemented | |
969 | wxASSERT(oldRef->m_palette == NULL); | |
970 | #endif | |
971 | ||
972 | return newRef; | |
973 | } | |
974 | ||
975 | //----------------------------------------------------------------------------- | |
976 | // wxBitmapHandler | |
977 | //----------------------------------------------------------------------------- | |
978 | ||
979 | IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase) | |
980 | ||
981 | /* static */ void wxBitmap::InitStandardHandlers() | |
982 | { | |
983 | // TODO: Insert handler based on GdkPixbufs handler later | |
984 | } |