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