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