]>
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 | |
91b8de8d | 82 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) |
c801d85f | 83 | { |
d3b9f782 | 84 | m_bitmap = NULL; |
60a3d1c6 | 85 | InitFromColour(bitmap, colour); |
ff7b1510 | 86 | } |
c801d85f | 87 | |
0b04c4e0 | 88 | #if wxUSE_PALETTE |
91b8de8d | 89 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) |
c801d85f | 90 | { |
d3b9f782 | 91 | m_bitmap = NULL; |
91b8de8d | 92 | Create( bitmap, paletteIndex ); |
ff7b1510 | 93 | } |
0b04c4e0 | 94 | #endif // wxUSE_PALETTE |
c801d85f | 95 | |
91b8de8d | 96 | wxMask::wxMask( const wxBitmap& bitmap ) |
c801d85f | 97 | { |
d3b9f782 | 98 | m_bitmap = NULL; |
60a3d1c6 | 99 | InitFromMonoBitmap(bitmap); |
ff7b1510 | 100 | } |
c801d85f | 101 | |
8bbe427f | 102 | wxMask::~wxMask() |
c801d85f | 103 | { |
13111b2a | 104 | if (m_bitmap) |
3fe39b0c | 105 | g_object_unref (m_bitmap); |
ff7b1510 | 106 | } |
c801d85f | 107 | |
60a3d1c6 | 108 | void wxMask::FreeData() |
91b8de8d RR |
109 | { |
110 | if (m_bitmap) | |
284b4c88 | 111 | { |
3fe39b0c | 112 | g_object_unref (m_bitmap); |
d3b9f782 | 113 | m_bitmap = NULL; |
91b8de8d | 114 | } |
60a3d1c6 | 115 | } |
13111b2a | 116 | |
60a3d1c6 PC |
117 | bool wxMask::InitFromColour(const wxBitmap& bitmap, const wxColour& colour) |
118 | { | |
13785a4b PC |
119 | const int w = bitmap.GetWidth(); |
120 | const int h = bitmap.GetHeight(); | |
13111b2a | 121 | |
5ac1d44a | 122 | // create mask as XBM format bitmap |
13111b2a | 123 | |
5ac1d44a PC |
124 | // one bit per pixel, each row starts on a byte boundary |
125 | const size_t out_size = size_t((w + 7) / 8) * unsigned(h); | |
126 | wxByte* out = new wxByte[out_size]; | |
d0e79755 | 127 | // set bits are unmasked |
5ac1d44a PC |
128 | memset(out, 0xff, out_size); |
129 | unsigned bit_index = 0; | |
13785a4b | 130 | if (bitmap.HasPixbuf()) |
1fb4de31 | 131 | { |
5ac1d44a PC |
132 | const wxByte r_mask = colour.Red(); |
133 | const wxByte g_mask = colour.Green(); | |
134 | const wxByte b_mask = colour.Blue(); | |
13785a4b | 135 | GdkPixbuf* pixbuf = bitmap.GetPixbuf(); |
5ac1d44a PC |
136 | const wxByte* in = gdk_pixbuf_get_pixels(pixbuf); |
137 | const int inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0); | |
138 | const int rowpadding = gdk_pixbuf_get_rowstride(pixbuf) - inc * w; | |
139 | for (int y = 0; y < h; y++, in += rowpadding) | |
140 | { | |
141 | for (int x = 0; x < w; x++, in += inc, bit_index++) | |
142 | if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask) | |
143 | out[bit_index >> 3] ^= 1 << (bit_index & 7); | |
144 | // move index to next byte boundary | |
145 | bit_index = (bit_index + 7) & ~7u; | |
146 | } | |
2eefae6e | 147 | } |
13785a4b | 148 | else |
1fb4de31 | 149 | { |
5ac1d44a | 150 | GdkImage* image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h); |
13785a4b | 151 | GdkColormap* colormap = gdk_image_get_colormap(image); |
a7cfe08a PC |
152 | guint32 mask_pixel; |
153 | if (colormap == NULL) | |
154 | // mono bitmap, white is pixel value 0 | |
155 | mask_pixel = guint32(colour.Red() != 255 || colour.Green() != 255 || colour.Blue() != 255); | |
156 | else | |
13785a4b PC |
157 | { |
158 | wxColor c(colour); | |
159 | c.CalcPixel(colormap); | |
160 | mask_pixel = c.GetPixel(); | |
161 | } | |
5ac1d44a | 162 | for (int y = 0; y < h; y++) |
1fb4de31 | 163 | { |
5ac1d44a PC |
164 | for (int x = 0; x < w; x++, bit_index++) |
165 | if (gdk_image_get_pixel(image, x, y) == mask_pixel) | |
166 | out[bit_index >> 3] ^= 1 << (bit_index & 7); | |
167 | bit_index = (bit_index + 7) & ~7u; | |
f9ee644e | 168 | } |
13785a4b | 169 | g_object_unref(image); |
5ac1d44a PC |
170 | } |
171 | m_bitmap = gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h); | |
172 | delete[] out; | |
902725ee | 173 | return true; |
91b8de8d RR |
174 | } |
175 | ||
60a3d1c6 | 176 | bool wxMask::InitFromMonoBitmap(const wxBitmap& bitmap) |
91b8de8d | 177 | { |
3d37a968 | 178 | if (!bitmap.IsOk()) return false; |
284b4c88 | 179 | |
b85229d1 | 180 | wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") ); |
284b4c88 | 181 | |
c2fa61e8 | 182 | m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); |
284b4c88 | 183 | |
902725ee | 184 | if (!m_bitmap) return false; |
284b4c88 | 185 | |
f3d74739 | 186 | wxGtkObject<GdkGC> gc(gdk_gc_new( m_bitmap )); |
9be3f755 PC |
187 | gdk_gc_set_function(gc, GDK_COPY_INVERT); |
188 | gdk_draw_drawable(m_bitmap, gc, bitmap.GetPixmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight()); | |
284b4c88 | 189 | |
902725ee | 190 | return true; |
91b8de8d RR |
191 | } |
192 | ||
193 | GdkBitmap *wxMask::GetBitmap() const | |
c801d85f | 194 | { |
fd0eed64 | 195 | return m_bitmap; |
ff7b1510 | 196 | } |
8bbe427f | 197 | |
c801d85f | 198 | //----------------------------------------------------------------------------- |
732d8c74 | 199 | // wxBitmapRefData |
c801d85f KB |
200 | //----------------------------------------------------------------------------- |
201 | ||
8f884a0d | 202 | class wxBitmapRefData: public wxGDIRefData |
c801d85f | 203 | { |
fd0eed64 | 204 | public: |
06497cba | 205 | wxBitmapRefData(int width, int height, int depth); |
d3c7fc99 | 206 | virtual ~wxBitmapRefData(); |
f2593d0d | 207 | |
06497cba | 208 | virtual bool IsOk() const; |
8f884a0d | 209 | |
f2593d0d | 210 | GdkPixmap *m_pixmap; |
feac7937 | 211 | GdkPixbuf *m_pixbuf; |
f2593d0d RR |
212 | wxMask *m_mask; |
213 | int m_width; | |
214 | int m_height; | |
215 | int m_bpp; | |
06497cba | 216 | bool m_alphaRequested; |
c801d85f KB |
217 | }; |
218 | ||
06497cba | 219 | wxBitmapRefData::wxBitmapRefData(int width, int height, int depth) |
c801d85f | 220 | { |
d3b9f782 VZ |
221 | m_pixmap = NULL; |
222 | m_pixbuf = NULL; | |
223 | m_mask = NULL; | |
06497cba PC |
224 | m_width = width; |
225 | m_height = height; | |
226 | m_bpp = depth; | |
227 | if (m_bpp < 0) | |
228 | m_bpp = gdk_drawable_get_depth(wxGetRootWindow()->window); | |
229 | m_alphaRequested = depth == 32; | |
ff7b1510 | 230 | } |
c801d85f | 231 | |
8bbe427f | 232 | wxBitmapRefData::~wxBitmapRefData() |
c801d85f | 233 | { |
3ebcd89d | 234 | if (m_pixmap) |
3fe39b0c | 235 | g_object_unref (m_pixmap); |
feac7937 | 236 | if (m_pixbuf) |
3fe39b0c | 237 | g_object_unref (m_pixbuf); |
3ebcd89d | 238 | delete m_mask; |
ff7b1510 | 239 | } |
c801d85f | 240 | |
06497cba PC |
241 | bool wxBitmapRefData::IsOk() const |
242 | { | |
243 | return m_bpp != 0; | |
244 | } | |
732d8c74 FM |
245 | |
246 | //----------------------------------------------------------------------------- | |
247 | // wxBitmap | |
c801d85f KB |
248 | //----------------------------------------------------------------------------- |
249 | ||
5c33522f | 250 | #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData) |
c801d85f KB |
251 | |
252 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
253 | ||
23656673 PC |
254 | wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type) |
255 | { | |
256 | LoadFile(filename, type); | |
257 | } | |
258 | ||
259 | wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) | |
c801d85f | 260 | { |
23656673 PC |
261 | wxASSERT(depth == 1); |
262 | if (width > 0 && height > 0 && depth == 1) | |
263 | { | |
264 | SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, bits, width, height)); | |
23656673 | 265 | } |
ff7b1510 | 266 | } |
8bbe427f | 267 | |
452418c4 PC |
268 | wxBitmap::wxBitmap(const char* const* bits) |
269 | { | |
270 | wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data")); | |
271 | ||
272 | GdkBitmap* mask = NULL; | |
5c33522f | 273 | SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, const_cast<char**>(bits))); |
3d37a968 FM |
274 | if (!M_BMPDATA) |
275 | return; | |
452418c4 PC |
276 | |
277 | if (M_BMPDATA->m_pixmap != NULL && mask != NULL) | |
278 | { | |
279 | M_BMPDATA->m_mask = new wxMask; | |
280 | M_BMPDATA->m_mask->m_bitmap = mask; | |
281 | } | |
282 | } | |
283 | ||
23656673 | 284 | wxBitmap::~wxBitmap() |
c801d85f | 285 | { |
c826213d RR |
286 | } |
287 | ||
288 | bool wxBitmap::Create( int width, int height, int depth ) | |
289 | { | |
290 | UnRef(); | |
06497cba PC |
291 | wxCHECK_MSG(width >= 0 && height >= 0, false, "invalid bitmap size"); |
292 | m_refData = new wxBitmapRefData(width, height, depth); | |
293 | return true; | |
ff7b1510 | 294 | } |
b5f01ae0 | 295 | |
c0521644 VZ |
296 | #if wxUSE_IMAGE |
297 | ||
feac7937 | 298 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth) |
b5f01ae0 | 299 | { |
a11672a4 RR |
300 | UnRef(); |
301 | ||
3d37a968 | 302 | wxCHECK_MSG( image.IsOk(), false, wxT("invalid image") ); |
637b7e4f | 303 | wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") ); |
b5f01ae0 | 304 | |
feac7937 VS |
305 | if (image.GetWidth() <= 0 || image.GetHeight() <= 0) |
306 | return false; | |
902725ee | 307 | |
70029506 PC |
308 | // create pixbuf if image has alpha and requested depth is compatible |
309 | if (image.HasAlpha() && (depth == -1 || depth == 32)) | |
5588ce92 PC |
310 | return CreateFromImageAsPixbuf(image); |
311 | ||
70029506 | 312 | // otherwise create pixmap, if alpha is present it will be converted to mask |
5ac1d44a | 313 | return CreateFromImageAsPixmap(image, depth); |
feac7937 | 314 | } |
3ebcd89d | 315 | |
5ac1d44a | 316 | bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth) |
feac7937 | 317 | { |
5ac1d44a PC |
318 | const int w = image.GetWidth(); |
319 | const int h = image.GetHeight(); | |
320 | if (depth == 1) | |
feac7937 | 321 | { |
5ac1d44a PC |
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]; | |
d0e79755 | 327 | // set bits are black |
5ac1d44a PC |
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; | |
3d37a968 FM |
341 | |
342 | if (!M_BMPDATA) // SetPixmap may have failed | |
343 | return false; | |
5ac1d44a PC |
344 | } |
345 | else | |
346 | { | |
347 | SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth)); | |
3d37a968 FM |
348 | if (!M_BMPDATA) |
349 | return false; | |
350 | ||
f3d74739 | 351 | wxGtkObject<GdkGC> gc(gdk_gc_new(M_BMPDATA->m_pixmap)); |
5ac1d44a PC |
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); | |
feac7937 | 356 | } |
b5f01ae0 | 357 | |
5ac1d44a PC |
358 | const wxByte* alpha = image.GetAlpha(); |
359 | if (alpha != NULL || image.HasMask()) | |
feac7937 | 360 | { |
5ac1d44a PC |
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) | |
b5f01ae0 | 368 | { |
5ac1d44a | 369 | for (int y = 0; y < h; y++) |
b5f01ae0 | 370 | { |
5ac1d44a PC |
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; | |
feac7937 | 375 | } |
5ac1d44a PC |
376 | } |
377 | else | |
b5f01ae0 | 378 | { |
5ac1d44a PC |
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 | } | |
3d37a968 | 396 | return IsOk(); |
b5f01ae0 VS |
397 | } |
398 | ||
feac7937 | 399 | bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image) |
b5f01ae0 | 400 | { |
70029506 PC |
401 | wxASSERT(image.HasAlpha()); |
402 | ||
feac7937 VS |
403 | int width = image.GetWidth(); |
404 | int height = image.GetHeight(); | |
2eefae6e | 405 | |
23656673 | 406 | Create(width, height, 32); |
06497cba | 407 | GdkPixbuf* pixbuf = GetPixbuf(); |
feac7937 VS |
408 | if (!pixbuf) |
409 | return false; | |
c2fa61e8 | 410 | |
feac7937 | 411 | // Copy the data: |
23656673 | 412 | const unsigned char* in = image.GetData(); |
feac7937 VS |
413 | unsigned char *out = gdk_pixbuf_get_pixels(pixbuf); |
414 | unsigned char *alpha = image.GetAlpha(); | |
902725ee | 415 | |
23656673 | 416 | int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width; |
b5f01ae0 | 417 | |
23656673 | 418 | for (int y = 0; y < height; y++, out += rowpad) |
b5f01ae0 | 419 | { |
feac7937 VS |
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 | } | |
b5f01ae0 | 427 | } |
902725ee | 428 | |
feac7937 VS |
429 | return true; |
430 | } | |
b5f01ae0 | 431 | |
feac7937 VS |
432 | wxImage wxBitmap::ConvertToImage() const |
433 | { | |
3d37a968 | 434 | wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") ); |
902725ee | 435 | |
afbfbfdf PC |
436 | const int w = GetWidth(); |
437 | const int h = GetHeight(); | |
23656673 | 438 | wxImage image(w, h, false); |
feac7937 | 439 | unsigned char *data = image.GetData(); |
2eefae6e | 440 | |
5588ce92 | 441 | wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") ); |
b5f01ae0 | 442 | |
70029506 | 443 | // prefer pixbuf if available, it will preserve alpha and should be quicker |
feac7937 | 444 | if (HasPixbuf()) |
b5f01ae0 | 445 | { |
feac7937 | 446 | GdkPixbuf *pixbuf = GetPixbuf(); |
70029506 PC |
447 | unsigned char* alpha = NULL; |
448 | if (gdk_pixbuf_get_has_alpha(pixbuf)) | |
449 | { | |
450 | image.SetAlpha(); | |
451 | alpha = image.GetAlpha(); | |
452 | } | |
23656673 | 453 | const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf); |
feac7937 | 454 | unsigned char *out = data; |
70029506 | 455 | const int inc = 3 + int(alpha != NULL); |
23656673 | 456 | const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w; |
b5f01ae0 | 457 | |
23656673 | 458 | for (int y = 0; y < h; y++, in += rowpad) |
feac7937 | 459 | { |
70029506 | 460 | for (int x = 0; x < w; x++, in += inc, out += 3) |
feac7937 VS |
461 | { |
462 | out[0] = in[0]; | |
463 | out[1] = in[1]; | |
464 | out[2] = in[2]; | |
70029506 PC |
465 | if (alpha != NULL) |
466 | *alpha++ = in[3]; | |
feac7937 VS |
467 | } |
468 | } | |
b5f01ae0 | 469 | } |
feac7937 | 470 | else |
b5f01ae0 | 471 | { |
afbfbfdf PC |
472 | GdkPixmap* pixmap = GetPixmap(); |
473 | GdkPixmap* pixmap_invert = NULL; | |
474 | if (GetDepth() == 1) | |
b5f01ae0 | 475 | { |
d0e79755 | 476 | // mono bitmaps are inverted, i.e. 0 is white |
afbfbfdf | 477 | pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1); |
f3d74739 | 478 | wxGtkObject<GdkGC> gc(gdk_gc_new(pixmap_invert)); |
afbfbfdf PC |
479 | gdk_gc_set_function(gc, GDK_COPY_INVERT); |
480 | gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h); | |
afbfbfdf | 481 | pixmap = pixmap_invert; |
feac7937 | 482 | } |
afbfbfdf PC |
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); | |
feac7937 | 486 | |
afbfbfdf | 487 | gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h); |
feac7937 | 488 | |
afbfbfdf PC |
489 | g_object_unref(pixbuf); |
490 | if (pixmap_invert != NULL) | |
491 | g_object_unref(pixmap_invert); | |
70029506 PC |
492 | } |
493 | // convert mask, unless there is already alpha | |
494 | if (GetMask() && !image.HasAlpha()) | |
495 | { | |
23656673 PC |
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 | |
70029506 PC |
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; | |
feac7937 | 504 | |
70029506 PC |
505 | image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE); |
506 | GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h); | |
feac7937 | 507 | |
70029506 PC |
508 | for (int y = 0; y < h; y++) |
509 | { | |
510 | for (int x = 0; x < w; x++, data += 3) | |
8ab696e0 | 511 | { |
70029506 | 512 | if (gdk_image_get_pixel(image_mask, x, y) == 0) |
8ab696e0 | 513 | { |
70029506 PC |
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 | { | |
23656673 PC |
520 | // we have to fudge the colour a bit to prevent |
521 | // this pixel from appearing transparent | |
70029506 | 522 | data[2] = MASK_BLUE_REPLACEMENT; |
feac7937 | 523 | } |
feac7937 | 524 | } |
b5f01ae0 | 525 | } |
70029506 | 526 | g_object_unref(image_mask); |
feac7937 | 527 | } |
b5f01ae0 VS |
528 | |
529 | return image; | |
530 | } | |
531 | ||
c0521644 VZ |
532 | #endif // wxUSE_IMAGE |
533 | ||
91b8de8d | 534 | int wxBitmap::GetHeight() const |
c801d85f | 535 | { |
3d37a968 | 536 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
e55ad60e | 537 | |
fd0eed64 | 538 | return M_BMPDATA->m_height; |
ff7b1510 | 539 | } |
c801d85f | 540 | |
91b8de8d | 541 | int wxBitmap::GetWidth() const |
c801d85f | 542 | { |
3d37a968 | 543 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
8bbe427f | 544 | |
fd0eed64 | 545 | return M_BMPDATA->m_width; |
ff7b1510 | 546 | } |
c801d85f | 547 | |
91b8de8d | 548 | int wxBitmap::GetDepth() const |
c801d85f | 549 | { |
3d37a968 | 550 | wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); |
8bbe427f | 551 | |
fd0eed64 | 552 | return M_BMPDATA->m_bpp; |
ff7b1510 | 553 | } |
c801d85f | 554 | |
91b8de8d | 555 | wxMask *wxBitmap::GetMask() const |
c801d85f | 556 | { |
d3b9f782 | 557 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ); |
8bbe427f | 558 | |
fd0eed64 | 559 | return M_BMPDATA->m_mask; |
ff7b1510 | 560 | } |
c801d85f KB |
561 | |
562 | void wxBitmap::SetMask( wxMask *mask ) | |
563 | { | |
3d37a968 | 564 | wxCHECK_RET( IsOk(), wxT("invalid bitmap") ); |
8bbe427f | 565 | |
e3e89a93 | 566 | AllocExclusive(); |
23656673 | 567 | delete M_BMPDATA->m_mask; |
fd0eed64 | 568 | M_BMPDATA->m_mask = mask; |
ff7b1510 | 569 | } |
c801d85f | 570 | |
db0aec83 VS |
571 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
572 | { | |
573 | *this = icon; | |
3d37a968 | 574 | return IsOk(); |
db0aec83 VS |
575 | } |
576 | ||
17bec151 RR |
577 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const |
578 | { | |
5588ce92 PC |
579 | wxBitmap ret; |
580 | ||
3d37a968 | 581 | wxCHECK_MSG(IsOk(), ret, wxT("invalid bitmap")); |
06497cba PC |
582 | |
583 | const int w = rect.width; | |
584 | const int h = rect.height; | |
585 | const wxBitmapRefData* bmpData = M_BMPDATA; | |
586 | ||
23656673 | 587 | wxCHECK_MSG(rect.x >= 0 && rect.y >= 0 && |
06497cba PC |
588 | rect.x + w <= bmpData->m_width && |
589 | rect.y + h <= bmpData->m_height, | |
23656673 | 590 | ret, wxT("invalid bitmap region")); |
13111b2a | 591 | |
06497cba PC |
592 | wxBitmapRefData* newRef = new wxBitmapRefData(*bmpData); |
593 | ret.m_refData = newRef; | |
594 | newRef->m_width = w; | |
595 | newRef->m_height = h; | |
596 | ||
597 | if (bmpData->m_pixbuf) | |
17bec151 | 598 | { |
06497cba PC |
599 | GdkPixbuf* pixbuf = |
600 | gdk_pixbuf_new_subpixbuf(bmpData->m_pixbuf, rect.x, rect.y, w, h); | |
601 | newRef->m_pixbuf = gdk_pixbuf_copy(pixbuf); | |
602 | g_object_unref(pixbuf); | |
17bec151 | 603 | } |
06497cba | 604 | if (bmpData->m_pixmap) |
17bec151 | 605 | { |
06497cba PC |
606 | newRef->m_pixmap = gdk_pixmap_new(bmpData->m_pixmap, w, h, -1); |
607 | GdkGC* gc = gdk_gc_new(newRef->m_pixmap); | |
608 | gdk_draw_drawable( | |
609 | newRef->m_pixmap, gc, bmpData->m_pixmap, rect.x, rect.y, 0, 0, w, h); | |
610 | g_object_unref(gc); | |
17bec151 | 611 | } |
06497cba PC |
612 | newRef->m_mask = NULL; |
613 | if (bmpData->m_mask && bmpData->m_mask->m_bitmap) | |
17bec151 | 614 | { |
06497cba PC |
615 | GdkPixmap* sub_mask = gdk_pixmap_new(bmpData->m_mask->m_bitmap, w, h, 1); |
616 | newRef->m_mask = new wxMask; | |
617 | newRef->m_mask->m_bitmap = sub_mask; | |
618 | GdkGC* gc = gdk_gc_new(sub_mask); | |
619 | gdk_draw_drawable( | |
620 | sub_mask, gc, bmpData->m_mask->m_bitmap, rect.x, rect.y, 0, 0, w, h); | |
621 | g_object_unref(gc); | |
17bec151 | 622 | } |
13111b2a | 623 | |
17bec151 RR |
624 | return ret; |
625 | } | |
626 | ||
4611dd06 | 627 | bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const |
c801d85f | 628 | { |
3d37a968 | 629 | wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") ); |
8bbe427f | 630 | |
c0521644 | 631 | #if wxUSE_IMAGE |
5588ce92 | 632 | wxImage image = ConvertToImage(); |
8362e67c PC |
633 | if (image.IsOk() && image.SaveFile(name, type)) |
634 | return true; | |
635 | #endif | |
636 | const char* type_name = NULL; | |
637 | switch (type) | |
638 | { | |
639 | case wxBITMAP_TYPE_BMP: type_name = "bmp"; break; | |
640 | case wxBITMAP_TYPE_ICO: type_name = "ico"; break; | |
641 | case wxBITMAP_TYPE_JPEG: type_name = "jpeg"; break; | |
642 | case wxBITMAP_TYPE_PNG: type_name = "png"; break; | |
643 | default: break; | |
644 | } | |
645 | return type_name && | |
646 | gdk_pixbuf_save(GetPixbuf(), name.fn_str(), type_name, NULL, NULL); | |
ff7b1510 | 647 | } |
c801d85f | 648 | |
4611dd06 | 649 | bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type ) |
c801d85f | 650 | { |
c0521644 | 651 | #if wxUSE_IMAGE |
8362e67c PC |
652 | wxImage image; |
653 | if (image.LoadFile(name, type) && image.IsOk()) | |
654 | *this = wxBitmap(image); | |
655 | else | |
656 | #endif | |
fd0eed64 | 657 | { |
8362e67c PC |
658 | UnRef(); |
659 | GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(name.fn_str(), NULL); | |
660 | if (pixbuf) | |
661 | SetPixbuf(pixbuf); | |
fd0eed64 | 662 | } |
8bbe427f | 663 | |
3d37a968 | 664 | return IsOk(); |
ff7b1510 | 665 | } |
8bbe427f | 666 | |
0b04c4e0 | 667 | #if wxUSE_PALETTE |
91b8de8d | 668 | wxPalette *wxBitmap::GetPalette() const |
c801d85f | 669 | { |
b2bf3ac6 | 670 | return NULL; |
ff7b1510 | 671 | } |
c801d85f | 672 | |
4611dd06 RR |
673 | void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette)) |
674 | { | |
675 | // TODO | |
676 | } | |
0b04c4e0 | 677 | #endif // wxUSE_PALETTE |
4611dd06 | 678 | |
4bc67cc5 RR |
679 | void wxBitmap::SetHeight( int height ) |
680 | { | |
e3e89a93 | 681 | AllocExclusive(); |
4bc67cc5 RR |
682 | M_BMPDATA->m_height = height; |
683 | } | |
684 | ||
685 | void wxBitmap::SetWidth( int width ) | |
686 | { | |
e3e89a93 | 687 | AllocExclusive(); |
4bc67cc5 RR |
688 | M_BMPDATA->m_width = width; |
689 | } | |
690 | ||
691 | void wxBitmap::SetDepth( int depth ) | |
692 | { | |
e3e89a93 | 693 | AllocExclusive(); |
4bc67cc5 RR |
694 | M_BMPDATA->m_bpp = depth; |
695 | } | |
696 | ||
697 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
698 | { | |
06497cba PC |
699 | UnRef(); |
700 | ||
3d37a968 FM |
701 | if (!pixmap) |
702 | return; | |
703 | ||
06497cba PC |
704 | int w, h; |
705 | gdk_drawable_get_size(pixmap, &w, &h); | |
706 | wxBitmapRefData* bmpData = new wxBitmapRefData(w, h, 0); | |
707 | m_refData = bmpData; | |
708 | bmpData->m_pixmap = pixmap; | |
709 | bmpData->m_bpp = gdk_drawable_get_depth(pixmap); | |
4bc67cc5 RR |
710 | } |
711 | ||
91b8de8d | 712 | GdkPixmap *wxBitmap::GetPixmap() const |
c801d85f | 713 | { |
d3b9f782 | 714 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ); |
8bbe427f | 715 | |
06497cba PC |
716 | wxBitmapRefData* bmpData = M_BMPDATA; |
717 | if (bmpData->m_pixmap) | |
718 | return bmpData->m_pixmap; | |
719 | ||
720 | if (bmpData->m_pixbuf) | |
feac7937 | 721 | { |
06497cba PC |
722 | GdkPixmap** mask_pixmap = NULL; |
723 | if (gdk_pixbuf_get_has_alpha(bmpData->m_pixbuf)) | |
70029506 PC |
724 | { |
725 | // make new mask from alpha | |
06497cba PC |
726 | delete bmpData->m_mask; |
727 | bmpData->m_mask = new wxMask; | |
728 | mask_pixmap = &bmpData->m_mask->m_bitmap; | |
70029506 | 729 | } |
06497cba PC |
730 | gdk_pixbuf_render_pixmap_and_mask( |
731 | bmpData->m_pixbuf, &bmpData->m_pixmap, mask_pixmap, 128); | |
feac7937 | 732 | } |
06497cba PC |
733 | else |
734 | { | |
735 | bmpData->m_pixmap = gdk_pixmap_new(wxGetRootWindow()->window, | |
736 | bmpData->m_width, bmpData->m_height, bmpData->m_bpp == 1 ? 1 : -1); | |
737 | } | |
738 | return bmpData->m_pixmap; | |
ff7b1510 | 739 | } |
8bbe427f | 740 | |
feac7937 VS |
741 | bool wxBitmap::HasPixmap() const |
742 | { | |
3d37a968 | 743 | wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") ); |
feac7937 VS |
744 | |
745 | return M_BMPDATA->m_pixmap != NULL; | |
746 | } | |
747 | ||
feac7937 VS |
748 | GdkPixbuf *wxBitmap::GetPixbuf() const |
749 | { | |
3d37a968 | 750 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ); |
feac7937 | 751 | |
06497cba PC |
752 | wxBitmapRefData* bmpData = M_BMPDATA; |
753 | if (bmpData->m_pixbuf) | |
754 | return bmpData->m_pixbuf; | |
6db34764 | 755 | |
06497cba PC |
756 | const int w = bmpData->m_width; |
757 | const int h = bmpData->m_height; | |
758 | GdkPixmap* mask = NULL; | |
759 | if (bmpData->m_mask) | |
760 | mask = bmpData->m_mask->m_bitmap; | |
761 | const bool useAlpha = bmpData->m_alphaRequested || mask; | |
762 | bmpData->m_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, useAlpha, 8, w, h); | |
763 | if (bmpData->m_pixmap) | |
764 | PixmapToPixbuf(bmpData->m_pixmap, bmpData->m_pixbuf, w, h); | |
765 | if (mask) | |
766 | MaskToAlpha(mask, bmpData->m_pixbuf, w, h); | |
767 | return bmpData->m_pixbuf; | |
feac7937 VS |
768 | } |
769 | ||
770 | bool wxBitmap::HasPixbuf() const | |
771 | { | |
3d37a968 | 772 | wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") ); |
feac7937 VS |
773 | |
774 | return M_BMPDATA->m_pixbuf != NULL; | |
775 | } | |
776 | ||
06497cba | 777 | void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf) |
feac7937 | 778 | { |
06497cba PC |
779 | UnRef(); |
780 | ||
3d37a968 FM |
781 | if (!pixbuf) |
782 | return; | |
783 | ||
06497cba PC |
784 | int depth = -1; |
785 | if (gdk_pixbuf_get_has_alpha(pixbuf)) | |
786 | depth = 32; | |
787 | m_refData = new wxBitmapRefData( | |
788 | gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), depth); | |
feac7937 VS |
789 | |
790 | M_BMPDATA->m_pixbuf = pixbuf; | |
791 | } | |
792 | ||
793 | void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep) | |
794 | { | |
795 | if (keep == Pixmap && HasPixbuf()) | |
796 | { | |
3fe39b0c | 797 | g_object_unref (M_BMPDATA->m_pixbuf); |
feac7937 VS |
798 | M_BMPDATA->m_pixbuf = NULL; |
799 | } | |
800 | if (keep == Pixbuf && HasPixmap()) | |
801 | { | |
3fe39b0c | 802 | g_object_unref (M_BMPDATA->m_pixmap); |
feac7937 VS |
803 | M_BMPDATA->m_pixmap = NULL; |
804 | } | |
805 | } | |
806 | ||
ce7c8a97 | 807 | #ifdef wxHAS_RAW_BITMAP |
284f2b59 RR |
808 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) |
809 | { | |
70029506 | 810 | void* bits = NULL; |
284f2b59 | 811 | GdkPixbuf *pixbuf = GetPixbuf(); |
70029506 | 812 | const bool hasAlpha = HasAlpha(); |
3d37a968 | 813 | |
70029506 | 814 | // allow access if bpp is valid and matches existence of alpha |
c74fc1e1 | 815 | if ( pixbuf && ((bpp == 24 && !hasAlpha) || (bpp == 32 && hasAlpha)) ) |
70029506 PC |
816 | { |
817 | data.m_height = gdk_pixbuf_get_height( pixbuf ); | |
818 | data.m_width = gdk_pixbuf_get_width( pixbuf ); | |
819 | data.m_stride = gdk_pixbuf_get_rowstride( pixbuf ); | |
820 | bits = gdk_pixbuf_get_pixels(pixbuf); | |
821 | } | |
822 | return bits; | |
284f2b59 RR |
823 | } |
824 | ||
17a1ebd1 | 825 | void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data)) |
284f2b59 RR |
826 | { |
827 | } | |
ce7c8a97 | 828 | #endif // wxHAS_RAW_BITMAP |
284f2b59 | 829 | |
902725ee | 830 | bool wxBitmap::HasAlpha() const |
0ff2a74d | 831 | { |
06497cba PC |
832 | const wxBitmapRefData* bmpData = M_BMPDATA; |
833 | return bmpData && (bmpData->m_alphaRequested || | |
834 | (bmpData->m_pixbuf && gdk_pixbuf_get_has_alpha(bmpData->m_pixbuf))); | |
0ff2a74d RR |
835 | } |
836 | ||
8f884a0d | 837 | wxGDIRefData* wxBitmap::CreateGDIRefData() const |
e3e89a93 | 838 | { |
06497cba | 839 | return new wxBitmapRefData(0, 0, 0); |
e3e89a93 PC |
840 | } |
841 | ||
8f884a0d | 842 | wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const |
e3e89a93 | 843 | { |
5c33522f | 844 | const wxBitmapRefData* oldRef = static_cast<const wxBitmapRefData*>(data); |
06497cba | 845 | wxBitmapRefData* newRef = new wxBitmapRefData(*oldRef); |
e3e89a93 PC |
846 | if (oldRef->m_pixmap != NULL) |
847 | { | |
848 | newRef->m_pixmap = gdk_pixmap_new( | |
849 | oldRef->m_pixmap, oldRef->m_width, oldRef->m_height, | |
850 | // use pixmap depth, m_bpp may not match | |
851 | gdk_drawable_get_depth(oldRef->m_pixmap)); | |
f3d74739 | 852 | wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_pixmap)); |
e3e89a93 PC |
853 | gdk_draw_drawable( |
854 | newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1); | |
e3e89a93 PC |
855 | } |
856 | if (oldRef->m_pixbuf != NULL) | |
857 | { | |
858 | newRef->m_pixbuf = gdk_pixbuf_copy(oldRef->m_pixbuf); | |
859 | } | |
860 | if (oldRef->m_mask != NULL) | |
861 | { | |
862 | newRef->m_mask = new wxMask; | |
863 | newRef->m_mask->m_bitmap = gdk_pixmap_new( | |
864 | oldRef->m_mask->m_bitmap, oldRef->m_width, oldRef->m_height, 1); | |
f3d74739 | 865 | wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_mask->m_bitmap)); |
e3e89a93 PC |
866 | gdk_draw_drawable(newRef->m_mask->m_bitmap, |
867 | gc, oldRef->m_mask->m_bitmap, 0, 0, 0, 0, -1, -1); | |
e3e89a93 | 868 | } |
e3e89a93 PC |
869 | |
870 | return newRef; | |
871 | } | |
872 | ||
4b61c88d RR |
873 | /* static */ void wxBitmap::InitStandardHandlers() |
874 | { | |
875 | // TODO: Insert handler based on GdkPixbufs handler later | |
876 | } |