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