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