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