]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: bitmap.cpp | |
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 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c801d85f KB |
11 | #pragma implementation "bitmap.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
22bd9387 VZ |
17 | #include "wx/defs.h" |
18 | ||
19 | #include "wx/palette.h" | |
c801d85f | 20 | #include "wx/bitmap.h" |
52cbfcf0 | 21 | #include "wx/icon.h" |
fd0eed64 | 22 | #include "wx/filefn.h" |
83624f79 | 23 | #include "wx/image.h" |
f9ee644e | 24 | #include "wx/dcmemory.h" |
b5f01ae0 | 25 | #include "wx/app.h" |
83624f79 | 26 | |
9e691f46 VZ |
27 | #ifdef __WXGTK20__ |
28 | // need this to get gdk_image_new_bitmap() | |
29 | #define GDK_ENABLE_BROKEN | |
30 | #endif | |
31 | ||
20e05ffb | 32 | #include <gdk/gdk.h> |
d76fe38b | 33 | #include <gtk/gtk.h> |
b5f01ae0 VS |
34 | #include <gdk/gdkx.h> |
35 | ||
9e691f46 VZ |
36 | #ifdef __WXGTK20__ |
37 | #include <gdk/gdkimage.h> | |
38 | #else // GTK+ 1.2 | |
c7f62467 | 39 | #include <gdk/gdkrgb.h> |
9e691f46 | 40 | #endif // GTK+ 2.0/1.2 |
13111b2a | 41 | |
783da845 RR |
42 | #include <math.h> |
43 | ||
f6bcfd97 BP |
44 | extern void gdk_wx_draw_bitmap (GdkDrawable *drawable, |
45 | GdkGC *gc, | |
46 | GdkDrawable *src, | |
47 | gint xsrc, | |
48 | gint ysrc, | |
49 | gint xdest, | |
50 | gint ydest, | |
51 | gint width, | |
52 | gint height); | |
53 | ||
d76fe38b RR |
54 | //----------------------------------------------------------------------------- |
55 | // data | |
56 | //----------------------------------------------------------------------------- | |
57 | ||
c2fa61e8 | 58 | extern GtkWidget *wxGetRootWindow(); |
c801d85f KB |
59 | |
60 | //----------------------------------------------------------------------------- | |
61 | // wxMask | |
62 | //----------------------------------------------------------------------------- | |
63 | ||
64 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
65 | ||
8bbe427f | 66 | wxMask::wxMask() |
c801d85f | 67 | { |
fd0eed64 | 68 | m_bitmap = (GdkBitmap *) NULL; |
ff7b1510 | 69 | } |
c801d85f | 70 | |
91b8de8d | 71 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) |
c801d85f | 72 | { |
72a7edf0 | 73 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 74 | Create( bitmap, colour ); |
ff7b1510 | 75 | } |
c801d85f | 76 | |
91b8de8d | 77 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) |
c801d85f | 78 | { |
72a7edf0 | 79 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 80 | Create( bitmap, paletteIndex ); |
ff7b1510 | 81 | } |
c801d85f | 82 | |
91b8de8d | 83 | wxMask::wxMask( const wxBitmap& bitmap ) |
c801d85f | 84 | { |
72a7edf0 | 85 | m_bitmap = (GdkBitmap *) NULL; |
91b8de8d | 86 | Create( bitmap ); |
ff7b1510 | 87 | } |
c801d85f | 88 | |
8bbe427f | 89 | wxMask::~wxMask() |
c801d85f | 90 | { |
13111b2a | 91 | if (m_bitmap) |
72a7edf0 | 92 | gdk_bitmap_unref( m_bitmap ); |
ff7b1510 | 93 | } |
c801d85f | 94 | |
1fb4de31 RR |
95 | bool wxMask::Create( const wxBitmap& bitmap, |
96 | const wxColour& colour ) | |
91b8de8d RR |
97 | { |
98 | if (m_bitmap) | |
284b4c88 | 99 | { |
91b8de8d | 100 | gdk_bitmap_unref( m_bitmap ); |
284b4c88 | 101 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 102 | } |
13111b2a | 103 | |
368d59f0 | 104 | wxImage image = bitmap.ConvertToImage(); |
1fb4de31 | 105 | if (!image.Ok()) return FALSE; |
13111b2a | 106 | |
c2fa61e8 | 107 | m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 ); |
f9ee644e | 108 | GdkGC *gc = gdk_gc_new( m_bitmap ); |
13111b2a | 109 | |
f9ee644e RR |
110 | GdkColor color; |
111 | color.red = 65000; | |
112 | color.green = 65000; | |
113 | color.blue = 65000; | |
114 | color.pixel = 1; | |
115 | gdk_gc_set_foreground( gc, &color ); | |
116 | gdk_gc_set_fill( gc, GDK_SOLID ); | |
117 | gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() ); | |
13111b2a | 118 | |
1fb4de31 RR |
119 | unsigned char *data = image.GetData(); |
120 | int index = 0; | |
13111b2a | 121 | |
1fb4de31 RR |
122 | unsigned char red = colour.Red(); |
123 | unsigned char green = colour.Green(); | |
124 | unsigned char blue = colour.Blue(); | |
13111b2a | 125 | |
005f5d18 | 126 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 127 | |
1fb4de31 | 128 | int bpp = visual->depth; |
2eefae6e VZ |
129 | if ((bpp == 16) && (visual->red_mask != 0xf800)) |
130 | bpp = 15; | |
1fb4de31 RR |
131 | if (bpp == 15) |
132 | { | |
133 | red = red & 0xf8; | |
1fb4de31 | 134 | green = green & 0xf8; |
f6bcfd97 | 135 | blue = blue & 0xf8; |
2eefae6e VZ |
136 | } |
137 | else if (bpp == 16) | |
1fb4de31 RR |
138 | { |
139 | red = red & 0xf8; | |
f6bcfd97 BP |
140 | green = green & 0xfc; |
141 | blue = blue & 0xf8; | |
2eefae6e VZ |
142 | } |
143 | else if (bpp == 12) | |
005f5d18 RR |
144 | { |
145 | red = red & 0xf0; | |
146 | green = green & 0xf0; | |
147 | blue = blue & 0xf0; | |
1fb4de31 | 148 | } |
13111b2a | 149 | |
f9ee644e RR |
150 | color.red = 0; |
151 | color.green = 0; | |
152 | color.blue = 0; | |
153 | color.pixel = 0; | |
154 | gdk_gc_set_foreground( gc, &color ); | |
13111b2a | 155 | |
1fb4de31 | 156 | for (int j = 0; j < image.GetHeight(); j++) |
f9ee644e | 157 | { |
f2593d0d RR |
158 | int start_x = -1; |
159 | int i; | |
160 | for (i = 0; i < image.GetWidth(); i++) | |
1fb4de31 | 161 | { |
13111b2a VZ |
162 | if ((data[index] == red) && |
163 | (data[index+1] == green) && | |
164 | (data[index+2] == blue)) | |
165 | { | |
166 | if (start_x == -1) | |
167 | start_x = i; | |
168 | } | |
169 | else | |
170 | { | |
171 | if (start_x != -1) | |
172 | { | |
173 | gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j ); | |
174 | start_x = -1; | |
175 | } | |
f9ee644e RR |
176 | } |
177 | index += 3; | |
178 | } | |
179 | if (start_x != -1) | |
180 | gdk_draw_line( m_bitmap, gc, start_x, j, i, j ); | |
181 | } | |
1fb4de31 | 182 | |
f9ee644e | 183 | gdk_gc_unref( gc ); |
1fb4de31 | 184 | |
f9ee644e | 185 | return TRUE; |
91b8de8d RR |
186 | } |
187 | ||
b5f01ae0 | 188 | bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex ) |
91b8de8d | 189 | { |
b5f01ae0 VS |
190 | unsigned char r,g,b; |
191 | wxPalette *pal = bitmap.GetPalette(); | |
284b4c88 | 192 | |
b5f01ae0 | 193 | wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") ); |
c2fa61e8 | 194 | |
b5f01ae0 | 195 | pal->GetRGB(paletteIndex, &r, &g, &b); |
284b4c88 | 196 | |
b5f01ae0 | 197 | return Create(bitmap, wxColour(r, g, b)); |
91b8de8d RR |
198 | } |
199 | ||
200 | bool wxMask::Create( const wxBitmap& bitmap ) | |
201 | { | |
202 | if (m_bitmap) | |
284b4c88 | 203 | { |
91b8de8d | 204 | gdk_bitmap_unref( m_bitmap ); |
284b4c88 | 205 | m_bitmap = (GdkBitmap*) NULL; |
91b8de8d | 206 | } |
284b4c88 | 207 | |
91b8de8d | 208 | if (!bitmap.Ok()) return FALSE; |
284b4c88 | 209 | |
223d09f6 | 210 | wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") ); |
284b4c88 | 211 | |
c2fa61e8 | 212 | m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); |
284b4c88 | 213 | |
91b8de8d | 214 | if (!m_bitmap) return FALSE; |
284b4c88 | 215 | |
91b8de8d | 216 | GdkGC *gc = gdk_gc_new( m_bitmap ); |
284b4c88 | 217 | |
f6bcfd97 | 218 | gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() ); |
284b4c88 | 219 | |
91b8de8d | 220 | gdk_gc_unref( gc ); |
284b4c88 | 221 | |
91b8de8d RR |
222 | return TRUE; |
223 | } | |
224 | ||
225 | GdkBitmap *wxMask::GetBitmap() const | |
c801d85f | 226 | { |
fd0eed64 | 227 | return m_bitmap; |
ff7b1510 | 228 | } |
8bbe427f | 229 | |
c801d85f KB |
230 | //----------------------------------------------------------------------------- |
231 | // wxBitmap | |
232 | //----------------------------------------------------------------------------- | |
233 | ||
234 | class wxBitmapRefData: public wxObjectRefData | |
235 | { | |
fd0eed64 | 236 | public: |
f2593d0d RR |
237 | wxBitmapRefData(); |
238 | ~wxBitmapRefData(); | |
239 | ||
240 | GdkPixmap *m_pixmap; | |
241 | GdkBitmap *m_bitmap; | |
242 | wxMask *m_mask; | |
243 | int m_width; | |
244 | int m_height; | |
245 | int m_bpp; | |
246 | wxPalette *m_palette; | |
c801d85f KB |
247 | }; |
248 | ||
8bbe427f | 249 | wxBitmapRefData::wxBitmapRefData() |
c801d85f | 250 | { |
fd0eed64 RR |
251 | m_pixmap = (GdkPixmap *) NULL; |
252 | m_bitmap = (GdkBitmap *) NULL; | |
253 | m_mask = (wxMask *) NULL; | |
254 | m_width = 0; | |
255 | m_height = 0; | |
256 | m_bpp = 0; | |
257 | m_palette = (wxPalette *) NULL; | |
ff7b1510 | 258 | } |
c801d85f | 259 | |
8bbe427f | 260 | wxBitmapRefData::~wxBitmapRefData() |
c801d85f | 261 | { |
3ebcd89d VZ |
262 | if (m_pixmap) |
263 | gdk_pixmap_unref( m_pixmap ); | |
264 | if (m_bitmap) | |
265 | gdk_bitmap_unref( m_bitmap ); | |
266 | delete m_mask; | |
267 | delete m_palette; | |
ff7b1510 | 268 | } |
c801d85f KB |
269 | |
270 | //----------------------------------------------------------------------------- | |
271 | ||
272 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) | |
273 | ||
274 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
275 | ||
8bbe427f | 276 | wxBitmap::wxBitmap() |
c801d85f | 277 | { |
ff7b1510 | 278 | } |
8bbe427f | 279 | |
debe6624 | 280 | wxBitmap::wxBitmap( int width, int height, int depth ) |
c801d85f | 281 | { |
c826213d | 282 | Create( width, height, depth ); |
c826213d RR |
283 | } |
284 | ||
285 | bool wxBitmap::Create( int width, int height, int depth ) | |
286 | { | |
287 | UnRef(); | |
288 | ||
3ebcd89d VZ |
289 | if ( width <= 0 || height <= 0 ) |
290 | { | |
291 | return false; | |
292 | } | |
284b4c88 | 293 | |
005f5d18 | 294 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
284b4c88 | 295 | |
3ebcd89d VZ |
296 | if (depth == -1) |
297 | depth = visual->depth; | |
103aab26 | 298 | |
3ebcd89d VZ |
299 | wxCHECK_MSG( (depth == visual->depth) || (depth == 1), FALSE, |
300 | wxT("invalid bitmap depth") ) | |
8bbe427f | 301 | |
eefa26be | 302 | m_refData = new wxBitmapRefData(); |
fd0eed64 | 303 | M_BMPDATA->m_mask = (wxMask *) NULL; |
fd0eed64 RR |
304 | M_BMPDATA->m_width = width; |
305 | M_BMPDATA->m_height = height; | |
eefa26be RR |
306 | if (depth == 1) |
307 | { | |
c2fa61e8 | 308 | M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); |
eefa26be RR |
309 | M_BMPDATA->m_bpp = 1; |
310 | } | |
311 | else | |
312 | { | |
c2fa61e8 | 313 | M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth ); |
103aab26 | 314 | M_BMPDATA->m_bpp = visual->depth; |
eefa26be | 315 | } |
8bbe427f | 316 | |
c826213d | 317 | return Ok(); |
ff7b1510 | 318 | } |
b5f01ae0 | 319 | |
e838cc14 | 320 | bool wxBitmap::CreateFromXpm( const char **bits ) |
e52f60e6 | 321 | { |
a11672a4 RR |
322 | UnRef(); |
323 | ||
e838cc14 | 324 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") ) |
8bbe427f | 325 | |
005f5d18 | 326 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 327 | |
e52f60e6 RR |
328 | m_refData = new wxBitmapRefData(); |
329 | ||
330 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
8bbe427f | 331 | |
c2fa61e8 | 332 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits ); |
8bbe427f | 333 | |
e838cc14 | 334 | wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") ); |
8bbe427f | 335 | |
fd0eed64 RR |
336 | if (mask) |
337 | { | |
338 | M_BMPDATA->m_mask = new wxMask(); | |
339 | M_BMPDATA->m_mask->m_bitmap = mask; | |
340 | } | |
8bbe427f | 341 | |
fd0eed64 | 342 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
2eefae6e | 343 | |
8cce8940 | 344 | M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ? |
c2fa61e8 | 345 | |
e838cc14 | 346 | return TRUE; |
ff7b1510 | 347 | } |
b5f01ae0 | 348 | |
783da845 RR |
349 | wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy ) |
350 | { | |
351 | wxCHECK_MSG( Ok(), wxNullBitmap, wxT("invalid bitmap") ); | |
352 | ||
353 | if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height) | |
354 | return *this; | |
355 | ||
356 | GdkImage *img = (GdkImage*) NULL; | |
357 | if (GetPixmap()) | |
358 | img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() ); | |
359 | else if (GetBitmap()) | |
360 | img = gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() ); | |
361 | else | |
362 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); | |
363 | ||
364 | wxCHECK_MSG( img, wxNullBitmap, wxT("couldn't create image") ); | |
365 | ||
366 | wxBitmap bmp; | |
367 | int bpp = -1; | |
368 | ||
369 | int width = wxMax(newx, 1); | |
370 | int height = wxMax(newy, 1); | |
371 | width = wxMin(width, clipwidth); | |
372 | height = wxMin(height, clipheight); | |
373 | ||
374 | GdkGC *gc = NULL; | |
375 | GdkPixmap *dstpix = NULL; | |
376 | if (GetPixmap()) | |
377 | { | |
378 | GdkVisual *visual = gdk_window_get_visual( GetPixmap() ); | |
379 | if (visual == NULL) | |
380 | visual = wxTheApp->GetGdkVisual(); | |
381 | ||
382 | bpp = visual->depth; | |
383 | bmp = wxBitmap(width,height,bpp); | |
384 | dstpix = bmp.GetPixmap(); | |
385 | gc = gdk_gc_new( dstpix ); | |
386 | } | |
387 | ||
388 | char *dst = NULL; | |
389 | long dstbyteperline = 0; | |
390 | ||
391 | if (GetBitmap()) | |
392 | { | |
393 | bpp = 1; | |
394 | dstbyteperline = width/8*M_BMPDATA->m_bpp; | |
395 | if (width*M_BMPDATA->m_bpp % 8 != 0) | |
396 | dstbyteperline++; | |
397 | dst = (char*) malloc(dstbyteperline*height); | |
398 | } | |
399 | ||
400 | // be careful to use the right scaling factor | |
401 | float scx = (float)M_BMPDATA->m_width/(float)newx; | |
402 | float scy = (float)M_BMPDATA->m_height/(float)newy; | |
403 | // prepare accel-tables | |
404 | int *tablex = (int *)calloc(width,sizeof(int)); | |
405 | int *tabley = (int *)calloc(height,sizeof(int)); | |
406 | ||
407 | // accel table filled with clipped values | |
408 | for (int x = 0; x < width; x++) | |
409 | tablex[x] = (int) (scx * (x+clipx)); | |
410 | for (int y = 0; y < height; y++) | |
411 | tabley[y] = (int) (scy * (y+clipy)); | |
412 | ||
413 | // Main rescaling routine starts here | |
414 | for (int h = 0; h < height; h++) | |
415 | { | |
416 | char outbyte = 0; | |
551b5391 | 417 | int old_x = -1; |
b63b07a8 | 418 | guint32 old_pixval = 0; |
783da845 | 419 | |
f9ebac93 | 420 | for (int w = 0; w < width; w++) |
783da845 | 421 | { |
551b5391 RR |
422 | guint32 pixval; |
423 | int x = tablex[w]; | |
424 | if (x == old_x) | |
425 | pixval = old_pixval; | |
426 | else | |
f9ebac93 | 427 | { |
551b5391 | 428 | pixval = gdk_image_get_pixel( img, x, tabley[h] ); |
f9ebac93 RR |
429 | old_pixval = pixval; |
430 | old_x = x; | |
431 | } | |
551b5391 | 432 | |
f9ebac93 | 433 | if (bpp == 1) |
783da845 RR |
434 | { |
435 | if (!pixval) | |
436 | { | |
437 | char bit=1; | |
438 | char shift = bit << w % 8; | |
439 | outbyte |= shift; | |
440 | } | |
441 | ||
442 | if ((w+1)%8==0) | |
443 | { | |
f9ebac93 | 444 | dst[h*dstbyteperline+w/8] = outbyte; |
783da845 RR |
445 | outbyte = 0; |
446 | } | |
447 | } | |
448 | else | |
449 | { | |
450 | GdkColor col; | |
451 | col.pixel = pixval; | |
452 | gdk_gc_set_foreground( gc, &col ); | |
453 | gdk_draw_point( dstpix, gc, w, h); | |
454 | } | |
455 | } | |
456 | ||
457 | // do not forget the last byte | |
f9ebac93 RR |
458 | if ((bpp == 1) && (width % 8 != 0)) |
459 | dst[h*dstbyteperline+width/8] = outbyte; | |
783da845 RR |
460 | } |
461 | ||
462 | gdk_image_destroy( img ); | |
463 | if (gc) gdk_gc_unref( gc ); | |
464 | ||
465 | if (bpp == 1) | |
466 | { | |
467 | bmp = wxBitmap( (const char *)dst, width, height, 1 ); | |
468 | free( dst ); | |
469 | } | |
470 | ||
471 | if (GetMask()) | |
472 | { | |
f9ebac93 RR |
473 | dstbyteperline = width/8; |
474 | if (width % 8 != 0) | |
783da845 RR |
475 | dstbyteperline++; |
476 | dst = (char*) malloc(dstbyteperline*height); | |
477 | img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() ); | |
478 | ||
479 | for (int h = 0; h < height; h++) | |
480 | { | |
481 | char outbyte = 0; | |
f9ebac93 | 482 | int old_x = -1; |
b63b07a8 | 483 | guint32 old_pixval = 0; |
783da845 | 484 | |
f9ebac93 | 485 | for (int w = 0; w < width; w++) |
783da845 | 486 | { |
f9ebac93 RR |
487 | guint32 pixval; |
488 | int x = tablex[w]; | |
489 | if (x == old_x) | |
490 | pixval = old_pixval; | |
491 | else | |
492 | { | |
493 | pixval = gdk_image_get_pixel( img, x, tabley[h] ); | |
494 | old_pixval = pixval; | |
495 | old_x = x; | |
496 | } | |
497 | ||
783da845 RR |
498 | if (pixval) |
499 | { | |
500 | char bit=1; | |
501 | char shift = bit << w % 8; | |
502 | outbyte |= shift; | |
503 | } | |
504 | ||
f9ebac93 | 505 | if ((w+1)%8 == 0) |
783da845 | 506 | { |
f9ebac93 | 507 | dst[h*dstbyteperline+w/8] = outbyte; |
783da845 RR |
508 | outbyte = 0; |
509 | } | |
510 | } | |
511 | ||
512 | // do not forget the last byte | |
f9ebac93 RR |
513 | if (width % 8 != 0) |
514 | dst[h*dstbyteperline+width/8] = outbyte; | |
783da845 | 515 | } |
f9ebac93 RR |
516 | wxMask* mask = new wxMask; |
517 | mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height ); | |
783da845 | 518 | bmp.SetMask(mask); |
f9ebac93 | 519 | |
783da845 RR |
520 | free( dst ); |
521 | gdk_image_destroy( img ); | |
522 | } | |
523 | ||
524 | free( tablex ); | |
525 | free( tabley ); | |
526 | ||
527 | return bmp; | |
528 | } | |
529 | ||
a29b55cd | 530 | bool wxBitmap::CreateFromImage( const wxImage& img, int depth ) |
b5f01ae0 | 531 | { |
a11672a4 RR |
532 | UnRef(); |
533 | ||
a29b55cd | 534 | wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid image") ) |
b5f01ae0 VS |
535 | wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") ) |
536 | ||
a29b55cd VS |
537 | // NB: wxGTK doesn't yet support alpha channel in bitmaps. The best we can |
538 | // do is to convert alpha channel to mask, if it is present: | |
539 | wxImage image(img); | |
540 | image.ConvertAlphaToMask(); | |
541 | ||
3ebcd89d VZ |
542 | int width = image.GetWidth(); |
543 | int height = image.GetHeight(); | |
544 | ||
545 | if ( width <= 0 || height <= 0 ) | |
546 | { | |
547 | return false; | |
548 | } | |
549 | ||
b5f01ae0 | 550 | m_refData = new wxBitmapRefData(); |
c2fa61e8 | 551 | |
3ebcd89d VZ |
552 | SetHeight( height ); |
553 | SetWidth( width ); | |
554 | ||
b5f01ae0 | 555 | // ------ |
2b5f62a0 | 556 | // conversion to mono bitmap: |
b5f01ae0 VS |
557 | // ------ |
558 | if (depth == 1) | |
559 | { | |
c2fa61e8 | 560 | SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) ); |
b5f01ae0 VS |
561 | |
562 | SetDepth( 1 ); | |
2eefae6e | 563 | |
005f5d18 | 564 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
b5f01ae0 VS |
565 | |
566 | // Create picture image | |
567 | ||
568 | unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
569 | ||
570 | GdkImage *data_image = | |
571 | gdk_image_new_bitmap( visual, data_data, width, height ); | |
572 | ||
573 | // Create mask image | |
574 | ||
575 | GdkImage *mask_image = (GdkImage*) NULL; | |
576 | ||
577 | if (image.HasMask()) | |
578 | { | |
579 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
580 | ||
581 | mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); | |
582 | ||
583 | wxMask *mask = new wxMask(); | |
c2fa61e8 | 584 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); |
b5f01ae0 VS |
585 | |
586 | SetMask( mask ); | |
587 | } | |
588 | ||
589 | int r_mask = image.GetMaskRed(); | |
590 | int g_mask = image.GetMaskGreen(); | |
591 | int b_mask = image.GetMaskBlue(); | |
592 | ||
593 | unsigned char* data = image.GetData(); | |
594 | ||
595 | int index = 0; | |
596 | for (int y = 0; y < height; y++) | |
597 | { | |
598 | for (int x = 0; x < width; x++) | |
599 | { | |
600 | int r = data[index]; | |
601 | index++; | |
602 | int g = data[index]; | |
603 | index++; | |
604 | int b = data[index]; | |
605 | index++; | |
606 | ||
607 | if (image.HasMask()) | |
608 | { | |
609 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) | |
610 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
611 | else | |
612 | gdk_image_put_pixel( mask_image, x, y, 0 ); | |
613 | } | |
614 | ||
615 | if ((r == 255) && (b == 255) && (g == 255)) | |
616 | gdk_image_put_pixel( data_image, x, y, 1 ); | |
617 | else | |
618 | gdk_image_put_pixel( data_image, x, y, 0 ); | |
619 | ||
620 | } // for | |
621 | } // for | |
622 | ||
623 | // Blit picture | |
624 | ||
625 | GdkGC *data_gc = gdk_gc_new( GetBitmap() ); | |
626 | ||
627 | gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); | |
628 | ||
629 | gdk_image_destroy( data_image ); | |
630 | gdk_gc_unref( data_gc ); | |
631 | ||
632 | // Blit mask | |
633 | ||
634 | if (image.HasMask()) | |
635 | { | |
636 | GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() ); | |
637 | ||
638 | gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); | |
639 | ||
640 | gdk_image_destroy( mask_image ); | |
641 | gdk_gc_unref( mask_gc ); | |
642 | } | |
643 | } | |
c2fa61e8 | 644 | |
b5f01ae0 | 645 | // ------ |
2b5f62a0 | 646 | // conversion to colour bitmap: |
b5f01ae0 VS |
647 | // ------ |
648 | else | |
649 | { | |
c2fa61e8 | 650 | SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) ); |
b5f01ae0 | 651 | |
005f5d18 | 652 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
b5f01ae0 VS |
653 | |
654 | int bpp = visual->depth; | |
655 | ||
656 | SetDepth( bpp ); | |
b5f01ae0 | 657 | |
2eefae6e VZ |
658 | if ((bpp == 16) && (visual->red_mask != 0xf800)) |
659 | bpp = 15; | |
660 | else if (bpp < 8) | |
661 | bpp = 8; | |
662 | ||
663 | // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit | |
8ab696e0 RR |
664 | // visuals are not supported by GDK so we do these ourselves, too. |
665 | // 15-bit and 16-bit should actually work and 24-bit certainly does. | |
9b72a74d RR |
666 | #ifdef __sgi |
667 | if (!image.HasMask() && (bpp > 16)) | |
668 | #else | |
8ab696e0 | 669 | if (!image.HasMask() && (bpp > 12)) |
9b72a74d | 670 | #endif |
b5f01ae0 VS |
671 | { |
672 | static bool s_hasInitialized = FALSE; | |
673 | ||
674 | if (!s_hasInitialized) | |
675 | { | |
676 | gdk_rgb_init(); | |
677 | s_hasInitialized = TRUE; | |
678 | } | |
679 | ||
680 | GdkGC *gc = gdk_gc_new( GetPixmap() ); | |
681 | ||
682 | gdk_draw_rgb_image( GetPixmap(), | |
683 | gc, | |
684 | 0, 0, | |
685 | width, height, | |
686 | GDK_RGB_DITHER_NONE, | |
687 | image.GetData(), | |
688 | width*3 ); | |
689 | ||
690 | gdk_gc_unref( gc ); | |
691 | return TRUE; | |
692 | } | |
693 | ||
b5f01ae0 VS |
694 | // Create picture image |
695 | ||
696 | GdkImage *data_image = | |
697 | gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height ); | |
698 | ||
699 | // Create mask image | |
700 | ||
701 | GdkImage *mask_image = (GdkImage*) NULL; | |
702 | ||
703 | if (image.HasMask()) | |
704 | { | |
705 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
706 | ||
707 | mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); | |
708 | ||
709 | wxMask *mask = new wxMask(); | |
c2fa61e8 | 710 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); |
b5f01ae0 VS |
711 | |
712 | SetMask( mask ); | |
713 | } | |
714 | ||
715 | // Render | |
716 | ||
717 | enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR }; | |
718 | byte_order b_o = RGB; | |
719 | ||
8ab696e0 | 720 | if (bpp > 8) |
b5f01ae0 VS |
721 | { |
722 | if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB; | |
a11672a4 | 723 | else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG; |
b5f01ae0 VS |
724 | else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG; |
725 | else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR; | |
726 | else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB; | |
727 | else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR; | |
728 | } | |
729 | ||
730 | int r_mask = image.GetMaskRed(); | |
731 | int g_mask = image.GetMaskGreen(); | |
732 | int b_mask = image.GetMaskBlue(); | |
733 | ||
734 | unsigned char* data = image.GetData(); | |
735 | ||
736 | int index = 0; | |
737 | for (int y = 0; y < height; y++) | |
738 | { | |
739 | for (int x = 0; x < width; x++) | |
740 | { | |
741 | int r = data[index]; | |
742 | index++; | |
743 | int g = data[index]; | |
744 | index++; | |
745 | int b = data[index]; | |
746 | index++; | |
747 | ||
748 | if (image.HasMask()) | |
749 | { | |
750 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) | |
751 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
752 | else | |
753 | gdk_image_put_pixel( mask_image, x, y, 0 ); | |
754 | } | |
755 | ||
756 | switch (bpp) | |
757 | { | |
8ab696e0 | 758 | case 8: |
b5f01ae0 VS |
759 | { |
760 | int pixel = -1; | |
761 | if (wxTheApp->m_colorCube) | |
762 | { | |
763 | pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; | |
764 | } | |
765 | else | |
766 | { | |
767 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
768 | GdkColor *colors = cmap->colors; | |
769 | int max = 3 * (65536); | |
2eefae6e | 770 | |
b5f01ae0 VS |
771 | for (int i = 0; i < cmap->size; i++) |
772 | { | |
773 | int rdiff = (r << 8) - colors[i].red; | |
774 | int gdiff = (g << 8) - colors[i].green; | |
775 | int bdiff = (b << 8) - colors[i].blue; | |
776 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
777 | if (sum < max) { pixel = i; max = sum; } | |
778 | } | |
779 | } | |
2eefae6e | 780 | |
b5f01ae0 | 781 | gdk_image_put_pixel( data_image, x, y, pixel ); |
2eefae6e | 782 | |
b5f01ae0 VS |
783 | break; |
784 | } | |
8ab696e0 | 785 | case 12: // SGI only |
b5f01ae0 | 786 | { |
8ab696e0 RR |
787 | guint32 pixel = 0; |
788 | switch (b_o) | |
789 | { | |
790 | case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break; | |
791 | case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break; | |
792 | case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break; | |
793 | case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break; | |
794 | case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break; | |
795 | case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break; | |
796 | } | |
b5f01ae0 VS |
797 | gdk_image_put_pixel( data_image, x, y, pixel ); |
798 | break; | |
799 | } | |
8ab696e0 | 800 | case 15: |
b5f01ae0 | 801 | { |
8ab696e0 RR |
802 | guint32 pixel = 0; |
803 | switch (b_o) | |
804 | { | |
805 | case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
806 | case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
807 | case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
808 | case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
809 | case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
810 | case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
811 | } | |
b5f01ae0 VS |
812 | gdk_image_put_pixel( data_image, x, y, pixel ); |
813 | break; | |
814 | } | |
8ab696e0 | 815 | case 16: |
b5f01ae0 | 816 | { |
8ab696e0 RR |
817 | // I actually don't know if for 16-bit displays, it is alway the green |
818 | // component or the second component which has 6 bits. | |
b5f01ae0 VS |
819 | guint32 pixel = 0; |
820 | switch (b_o) | |
821 | { | |
005f5d18 RR |
822 | case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break; |
823 | case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
824 | case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break; | |
825 | case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
826 | case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
827 | case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
8ab696e0 RR |
828 | } |
829 | gdk_image_put_pixel( data_image, x, y, pixel ); | |
830 | break; | |
831 | } | |
832 | case 32: | |
833 | case 24: | |
834 | { | |
835 | guint32 pixel = 0; | |
836 | switch (b_o) | |
837 | { | |
838 | case RGB: pixel = (r << 16) | (g << 8) | b; break; | |
839 | case RBG: pixel = (r << 16) | (b << 8) | g; break; | |
840 | case BRG: pixel = (b << 16) | (r << 8) | g; break; | |
841 | case BGR: pixel = (b << 16) | (g << 8) | r; break; | |
842 | case GRB: pixel = (g << 16) | (r << 8) | b; break; | |
843 | case GBR: pixel = (g << 16) | (b << 8) | r; break; | |
b5f01ae0 VS |
844 | } |
845 | gdk_image_put_pixel( data_image, x, y, pixel ); | |
846 | } | |
8ab696e0 | 847 | default: break; |
b5f01ae0 VS |
848 | } |
849 | } // for | |
850 | } // for | |
851 | ||
852 | // Blit picture | |
853 | ||
854 | GdkGC *data_gc = gdk_gc_new( GetPixmap() ); | |
855 | ||
856 | gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); | |
857 | ||
858 | gdk_image_destroy( data_image ); | |
859 | gdk_gc_unref( data_gc ); | |
860 | ||
861 | // Blit mask | |
862 | ||
863 | if (image.HasMask()) | |
864 | { | |
865 | GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() ); | |
866 | ||
867 | gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); | |
868 | ||
869 | gdk_image_destroy( mask_image ); | |
870 | gdk_gc_unref( mask_gc ); | |
871 | } | |
872 | } | |
873 | ||
874 | return TRUE; | |
875 | } | |
876 | ||
877 | wxImage wxBitmap::ConvertToImage() const | |
878 | { | |
2eefae6e VZ |
879 | // the colour used as transparent one in wxImage and the one it is replaced |
880 | // with when it really occurs in the bitmap | |
d694de12 VZ |
881 | static const int MASK_RED = 1; |
882 | static const int MASK_GREEN = 2; | |
883 | static const int MASK_BLUE = 3; | |
884 | static const int MASK_BLUE_REPLACEMENT = 2; | |
2eefae6e | 885 | |
b5f01ae0 | 886 | wxImage image; |
c2fa61e8 | 887 | |
b5f01ae0 VS |
888 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
889 | ||
890 | GdkImage *gdk_image = (GdkImage*) NULL; | |
891 | if (GetPixmap()) | |
892 | { | |
893 | gdk_image = gdk_image_get( GetPixmap(), | |
2eefae6e VZ |
894 | 0, 0, |
895 | GetWidth(), GetHeight() ); | |
896 | } | |
897 | else if (GetBitmap()) | |
b5f01ae0 VS |
898 | { |
899 | gdk_image = gdk_image_get( GetBitmap(), | |
2eefae6e VZ |
900 | 0, 0, |
901 | GetWidth(), GetHeight() ); | |
902 | } | |
903 | else | |
b5f01ae0 VS |
904 | { |
905 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); | |
906 | } | |
907 | ||
908 | wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") ); | |
c2fa61e8 | 909 | |
b5f01ae0 VS |
910 | image.Create( GetWidth(), GetHeight() ); |
911 | char unsigned *data = image.GetData(); | |
2eefae6e | 912 | |
b5f01ae0 VS |
913 | if (!data) |
914 | { | |
915 | gdk_image_destroy( gdk_image ); | |
916 | wxFAIL_MSG( wxT("couldn't create image") ); | |
917 | return wxNullImage; | |
918 | } | |
919 | ||
920 | GdkImage *gdk_image_mask = (GdkImage*) NULL; | |
921 | if (GetMask()) | |
922 | { | |
923 | gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(), | |
2eefae6e VZ |
924 | 0, 0, |
925 | GetWidth(), GetHeight() ); | |
b5f01ae0 | 926 | |
2eefae6e | 927 | image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE ); |
b5f01ae0 VS |
928 | } |
929 | ||
930 | int bpp = -1; | |
931 | int red_shift_right = 0; | |
932 | int green_shift_right = 0; | |
933 | int blue_shift_right = 0; | |
934 | int red_shift_left = 0; | |
935 | int green_shift_left = 0; | |
936 | int blue_shift_left = 0; | |
937 | bool use_shift = FALSE; | |
938 | ||
939 | if (GetPixmap()) | |
940 | { | |
941 | GdkVisual *visual = gdk_window_get_visual( GetPixmap() ); | |
005f5d18 RR |
942 | if (visual == NULL) |
943 | visual = wxTheApp->GetGdkVisual(); | |
2eefae6e | 944 | |
b5f01ae0 | 945 | bpp = visual->depth; |
2eefae6e VZ |
946 | if (bpp == 16) |
947 | bpp = visual->red_prec + visual->green_prec + visual->blue_prec; | |
b5f01ae0 VS |
948 | red_shift_right = visual->red_shift; |
949 | red_shift_left = 8-visual->red_prec; | |
950 | green_shift_right = visual->green_shift; | |
951 | green_shift_left = 8-visual->green_prec; | |
952 | blue_shift_right = visual->blue_shift; | |
953 | blue_shift_left = 8-visual->blue_prec; | |
954 | ||
955 | use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR); | |
956 | } | |
957 | if (GetBitmap()) | |
958 | { | |
959 | bpp = 1; | |
960 | } | |
961 | ||
962 | ||
963 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
964 | ||
965 | long pos = 0; | |
966 | for (int j = 0; j < GetHeight(); j++) | |
967 | { | |
968 | for (int i = 0; i < GetWidth(); i++) | |
969 | { | |
970 | wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j ); | |
8ab696e0 RR |
971 | if (bpp == 1) |
972 | { | |
973 | if (pixel == 0) | |
b5f01ae0 | 974 | { |
b5f01ae0 VS |
975 | data[pos] = 0; |
976 | data[pos+1] = 0; | |
977 | data[pos+2] = 0; | |
8ab696e0 RR |
978 | } |
979 | else | |
980 | { | |
b5f01ae0 VS |
981 | data[pos] = 255; |
982 | data[pos+1] = 255; | |
983 | data[pos+2] = 255; | |
8ab696e0 | 984 | } |
b5f01ae0 VS |
985 | } |
986 | else if (use_shift) | |
987 | { | |
988 | data[pos] = (pixel >> red_shift_right) << red_shift_left; | |
989 | data[pos+1] = (pixel >> green_shift_right) << green_shift_left; | |
990 | data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left; | |
8ab696e0 | 991 | } |
b5f01ae0 VS |
992 | else if (cmap->colors) |
993 | { | |
994 | data[pos] = cmap->colors[pixel].red >> 8; | |
995 | data[pos+1] = cmap->colors[pixel].green >> 8; | |
996 | data[pos+2] = cmap->colors[pixel].blue >> 8; | |
997 | } | |
998 | else | |
999 | { | |
1000 | wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") ); | |
1001 | } | |
1002 | ||
1003 | if (gdk_image_mask) | |
1004 | { | |
1005 | int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j ); | |
1006 | if (mask_pixel == 0) | |
1007 | { | |
2eefae6e VZ |
1008 | data[pos] = MASK_RED; |
1009 | data[pos+1] = MASK_GREEN; | |
1010 | data[pos+2] = MASK_BLUE; | |
1011 | } | |
1012 | else if ( data[pos] == MASK_RED && | |
1013 | data[pos+1] == MASK_GREEN && | |
1014 | data[pos+2] == MASK_BLUE ) | |
1015 | { | |
1016 | data[pos+2] = MASK_BLUE_REPLACEMENT; | |
b5f01ae0 VS |
1017 | } |
1018 | } | |
1019 | ||
1020 | pos += 3; | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | gdk_image_destroy( gdk_image ); | |
c2fa61e8 | 1025 | if (gdk_image_mask) gdk_image_destroy( gdk_image_mask ); |
b5f01ae0 VS |
1026 | |
1027 | return image; | |
1028 | } | |
1029 | ||
c801d85f | 1030 | wxBitmap::wxBitmap( const wxBitmap& bmp ) |
3ebcd89d | 1031 | : wxGDIObject() |
c801d85f | 1032 | { |
fd0eed64 | 1033 | Ref( bmp ); |
ff7b1510 | 1034 | } |
6f65e337 | 1035 | |
debe6624 | 1036 | wxBitmap::wxBitmap( const wxString &filename, int type ) |
c801d85f | 1037 | { |
fd0eed64 | 1038 | LoadFile( filename, type ); |
ff7b1510 | 1039 | } |
c801d85f | 1040 | |
debe6624 | 1041 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth)) |
6f65e337 | 1042 | { |
3ebcd89d VZ |
1043 | if ( width > 0 && height > 0 ) |
1044 | { | |
1045 | m_refData = new wxBitmapRefData(); | |
6f65e337 | 1046 | |
3ebcd89d VZ |
1047 | M_BMPDATA->m_mask = (wxMask *) NULL; |
1048 | M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data | |
1049 | ( | |
1050 | wxGetRootWindow()->window, | |
1051 | (gchar *) bits, | |
1052 | width, | |
1053 | height | |
1054 | ); | |
1055 | M_BMPDATA->m_width = width; | |
1056 | M_BMPDATA->m_height = height; | |
1057 | M_BMPDATA->m_bpp = 1; | |
6f65e337 | 1058 | |
3ebcd89d VZ |
1059 | wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") ); |
1060 | } | |
6f65e337 | 1061 | } |
8bbe427f VZ |
1062 | |
1063 | wxBitmap::~wxBitmap() | |
c801d85f | 1064 | { |
ff7b1510 | 1065 | } |
8bbe427f | 1066 | |
c801d85f KB |
1067 | wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp ) |
1068 | { | |
7ecb8b06 VZ |
1069 | if ( m_refData != bmp.m_refData ) |
1070 | Ref( bmp ); | |
1071 | ||
8bbe427f | 1072 | return *this; |
ff7b1510 | 1073 | } |
8bbe427f | 1074 | |
f6bcfd97 | 1075 | bool wxBitmap::operator == ( const wxBitmap& bmp ) const |
c801d85f | 1076 | { |
8bbe427f | 1077 | return m_refData == bmp.m_refData; |
ff7b1510 | 1078 | } |
8bbe427f | 1079 | |
f6bcfd97 | 1080 | bool wxBitmap::operator != ( const wxBitmap& bmp ) const |
c801d85f | 1081 | { |
8bbe427f | 1082 | return m_refData != bmp.m_refData; |
ff7b1510 | 1083 | } |
8bbe427f | 1084 | |
91b8de8d | 1085 | bool wxBitmap::Ok() const |
c801d85f | 1086 | { |
024e0f69 | 1087 | return (m_refData != NULL) && (M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap); |
ff7b1510 | 1088 | } |
8bbe427f | 1089 | |
91b8de8d | 1090 | int wxBitmap::GetHeight() const |
c801d85f | 1091 | { |
223d09f6 | 1092 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
e55ad60e | 1093 | |
fd0eed64 | 1094 | return M_BMPDATA->m_height; |
ff7b1510 | 1095 | } |
c801d85f | 1096 | |
91b8de8d | 1097 | int wxBitmap::GetWidth() const |
c801d85f | 1098 | { |
223d09f6 | 1099 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 1100 | |
fd0eed64 | 1101 | return M_BMPDATA->m_width; |
ff7b1510 | 1102 | } |
c801d85f | 1103 | |
91b8de8d | 1104 | int wxBitmap::GetDepth() const |
c801d85f | 1105 | { |
223d09f6 | 1106 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 1107 | |
fd0eed64 | 1108 | return M_BMPDATA->m_bpp; |
ff7b1510 | 1109 | } |
c801d85f | 1110 | |
91b8de8d | 1111 | wxMask *wxBitmap::GetMask() const |
c801d85f | 1112 | { |
223d09f6 | 1113 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1114 | |
fd0eed64 | 1115 | return M_BMPDATA->m_mask; |
ff7b1510 | 1116 | } |
c801d85f KB |
1117 | |
1118 | void wxBitmap::SetMask( wxMask *mask ) | |
1119 | { | |
223d09f6 | 1120 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); |
8bbe427f | 1121 | |
fd0eed64 | 1122 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; |
8bbe427f | 1123 | |
fd0eed64 | 1124 | M_BMPDATA->m_mask = mask; |
ff7b1510 | 1125 | } |
c801d85f | 1126 | |
db0aec83 VS |
1127 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
1128 | { | |
1129 | *this = icon; | |
1130 | return TRUE; | |
1131 | } | |
1132 | ||
17bec151 RR |
1133 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const |
1134 | { | |
1135 | wxCHECK_MSG( Ok() && | |
13111b2a VZ |
1136 | (rect.x >= 0) && (rect.y >= 0) && |
1137 | (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height), | |
17bec151 | 1138 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); |
13111b2a | 1139 | |
17bec151 RR |
1140 | wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp ); |
1141 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
13111b2a | 1142 | |
17bec151 RR |
1143 | if (ret.GetPixmap()) |
1144 | { | |
1145 | GdkGC *gc = gdk_gc_new( ret.GetPixmap() ); | |
13111b2a VZ |
1146 | gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); |
1147 | gdk_gc_destroy( gc ); | |
17bec151 RR |
1148 | } |
1149 | else | |
1150 | { | |
1151 | GdkGC *gc = gdk_gc_new( ret.GetBitmap() ); | |
f9ebac93 RR |
1152 | GdkColor col; |
1153 | col.pixel = 0xFFFFFF; | |
1154 | gdk_gc_set_foreground( gc, &col ); | |
1155 | col.pixel = 0; | |
1156 | gdk_gc_set_background( gc, &col ); | |
f6bcfd97 | 1157 | gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); |
13111b2a | 1158 | gdk_gc_destroy( gc ); |
17bec151 | 1159 | } |
13111b2a | 1160 | |
17bec151 RR |
1161 | if (GetMask()) |
1162 | { | |
1163 | wxMask *mask = new wxMask; | |
c2fa61e8 | 1164 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 ); |
13111b2a | 1165 | |
17bec151 | 1166 | GdkGC *gc = gdk_gc_new( mask->m_bitmap ); |
f9ebac93 RR |
1167 | GdkColor col; |
1168 | col.pixel = 0xFFFFFF; | |
1169 | gdk_gc_set_foreground( gc, &col ); | |
1170 | col.pixel = 0; | |
1171 | gdk_gc_set_background( gc, &col ); | |
1172 | gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
13111b2a VZ |
1173 | gdk_gc_destroy( gc ); |
1174 | ||
1175 | ret.SetMask( mask ); | |
17bec151 | 1176 | } |
13111b2a | 1177 | |
17bec151 RR |
1178 | return ret; |
1179 | } | |
1180 | ||
fd0eed64 | 1181 | bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) ) |
c801d85f | 1182 | { |
223d09f6 | 1183 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") ); |
8bbe427f | 1184 | |
b75dd496 | 1185 | // Try to save the bitmap via wxImage handlers: |
fd0eed64 | 1186 | { |
368d59f0 | 1187 | wxImage image = ConvertToImage(); |
284b4c88 | 1188 | if (image.Ok()) return image.SaveFile( name, type ); |
fd0eed64 | 1189 | } |
8bbe427f | 1190 | |
fd0eed64 | 1191 | return FALSE; |
ff7b1510 | 1192 | } |
c801d85f | 1193 | |
fd0eed64 | 1194 | bool wxBitmap::LoadFile( const wxString &name, int type ) |
c801d85f | 1195 | { |
fd0eed64 | 1196 | UnRef(); |
8bbe427f | 1197 | |
3ebcd89d VZ |
1198 | if (!wxFileExists(name)) |
1199 | return FALSE; | |
8bbe427f | 1200 | |
005f5d18 | 1201 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 1202 | |
fd0eed64 RR |
1203 | if (type == wxBITMAP_TYPE_XPM) |
1204 | { | |
1205 | m_refData = new wxBitmapRefData(); | |
8bbe427f | 1206 | |
fd0eed64 | 1207 | GdkBitmap *mask = (GdkBitmap*) NULL; |
8bbe427f | 1208 | |
3ebcd89d VZ |
1209 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm |
1210 | ( | |
1211 | wxGetRootWindow()->window, | |
1212 | &mask, | |
1213 | NULL, | |
1214 | name.fn_str() | |
1215 | ); | |
8bbe427f | 1216 | |
fd0eed64 RR |
1217 | if (mask) |
1218 | { | |
1219 | M_BMPDATA->m_mask = new wxMask(); | |
1220 | M_BMPDATA->m_mask->m_bitmap = mask; | |
1221 | } | |
8bbe427f | 1222 | |
fd0eed64 | 1223 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
c2fa61e8 | 1224 | |
103aab26 | 1225 | M_BMPDATA->m_bpp = visual->depth; |
fd0eed64 | 1226 | } |
b75dd496 | 1227 | else // try if wxImage can load it |
fd0eed64 RR |
1228 | { |
1229 | wxImage image; | |
3ebcd89d VZ |
1230 | if ( !image.LoadFile( name, type ) || !image.Ok() ) |
1231 | return FALSE; | |
1232 | ||
1233 | *this = wxBitmap(image); | |
fd0eed64 | 1234 | } |
8bbe427f | 1235 | |
fd0eed64 | 1236 | return TRUE; |
ff7b1510 | 1237 | } |
8bbe427f | 1238 | |
91b8de8d | 1239 | wxPalette *wxBitmap::GetPalette() const |
c801d85f | 1240 | { |
3ebcd89d VZ |
1241 | if (!Ok()) |
1242 | return (wxPalette *) NULL; | |
8bbe427f | 1243 | |
fd0eed64 | 1244 | return M_BMPDATA->m_palette; |
ff7b1510 | 1245 | } |
c801d85f | 1246 | |
4bc67cc5 RR |
1247 | void wxBitmap::SetHeight( int height ) |
1248 | { | |
3ebcd89d VZ |
1249 | if (!m_refData) |
1250 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1251 | |
1252 | M_BMPDATA->m_height = height; | |
1253 | } | |
1254 | ||
1255 | void wxBitmap::SetWidth( int width ) | |
1256 | { | |
3ebcd89d VZ |
1257 | if (!m_refData) |
1258 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1259 | |
1260 | M_BMPDATA->m_width = width; | |
1261 | } | |
1262 | ||
1263 | void wxBitmap::SetDepth( int depth ) | |
1264 | { | |
3ebcd89d VZ |
1265 | if (!m_refData) |
1266 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1267 | |
1268 | M_BMPDATA->m_bpp = depth; | |
1269 | } | |
1270 | ||
1271 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
1272 | { | |
3ebcd89d VZ |
1273 | if (!m_refData) |
1274 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1275 | |
1276 | M_BMPDATA->m_pixmap = pixmap; | |
1277 | } | |
1278 | ||
82ea63e6 RR |
1279 | void wxBitmap::SetBitmap( GdkPixmap *bitmap ) |
1280 | { | |
3ebcd89d VZ |
1281 | if (!m_refData) |
1282 | m_refData = new wxBitmapRefData(); | |
82ea63e6 RR |
1283 | |
1284 | M_BMPDATA->m_bitmap = bitmap; | |
1285 | } | |
1286 | ||
91b8de8d | 1287 | GdkPixmap *wxBitmap::GetPixmap() const |
c801d85f | 1288 | { |
223d09f6 | 1289 | wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1290 | |
fd0eed64 | 1291 | return M_BMPDATA->m_pixmap; |
ff7b1510 | 1292 | } |
8bbe427f | 1293 | |
91b8de8d | 1294 | GdkBitmap *wxBitmap::GetBitmap() const |
6f65e337 | 1295 | { |
223d09f6 | 1296 | wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1297 | |
fd0eed64 | 1298 | return M_BMPDATA->m_bitmap; |
ff7b1510 | 1299 | } |