]>
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 |
8bbe427f | 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; | |
417 | ||
418 | for (int w=0; w<width; w++) | |
419 | { | |
420 | guint32 pixval = gdk_image_get_pixel( img, tablex[w], tabley[h] ); | |
421 | if (bpp==1) | |
422 | { | |
423 | if (!pixval) | |
424 | { | |
425 | char bit=1; | |
426 | char shift = bit << w % 8; | |
427 | outbyte |= shift; | |
428 | } | |
429 | ||
430 | if ((w+1)%8==0) | |
431 | { | |
432 | dst[h*dstbyteperline+w/8]=~outbyte; | |
433 | outbyte = 0; | |
434 | } | |
435 | } | |
436 | else | |
437 | { | |
438 | GdkColor col; | |
439 | col.pixel = pixval; | |
440 | gdk_gc_set_foreground( gc, &col ); | |
441 | gdk_draw_point( dstpix, gc, w, h); | |
442 | } | |
443 | } | |
444 | ||
445 | // do not forget the last byte | |
446 | if (bpp == 1) | |
447 | dst[h*dstbyteperline+width/8] = ~outbyte; | |
448 | } | |
449 | ||
450 | gdk_image_destroy( img ); | |
451 | if (gc) gdk_gc_unref( gc ); | |
452 | ||
453 | if (bpp == 1) | |
454 | { | |
455 | bmp = wxBitmap( (const char *)dst, width, height, 1 ); | |
456 | free( dst ); | |
457 | } | |
458 | ||
459 | if (GetMask()) | |
460 | { | |
461 | bpp = 1; | |
462 | dstbyteperline = width/8*bpp; | |
463 | if (width*bpp % 8 != 0) | |
464 | dstbyteperline++; | |
465 | dst = (char*) malloc(dstbyteperline*height); | |
466 | img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() ); | |
467 | ||
468 | for (int h = 0; h < height; h++) | |
469 | { | |
470 | char outbyte = 0; | |
471 | ||
472 | for (int w=0; w<width; w++) | |
473 | { | |
474 | guint32 pixval = gdk_image_get_pixel( img, tablex[w], tabley[h] ); | |
475 | if (pixval) | |
476 | { | |
477 | char bit=1; | |
478 | char shift = bit << w % 8; | |
479 | outbyte |= shift; | |
480 | } | |
481 | ||
482 | if ((w+1)%8==0) | |
483 | { | |
484 | dst[h*dstbyteperline+w/8]=~outbyte; | |
485 | outbyte = 0; | |
486 | } | |
487 | } | |
488 | ||
489 | // do not forget the last byte | |
490 | if (bpp == 1) | |
491 | dst[h*dstbyteperline+width/8] = ~outbyte; | |
492 | } | |
493 | ||
494 | wxBitmap mask_bmp( (const char *)dst, width, height, 1 ); | |
495 | wxMask* mask = new wxMask(mask_bmp); | |
496 | bmp.SetMask(mask); | |
497 | ||
498 | free( dst ); | |
499 | gdk_image_destroy( img ); | |
500 | } | |
501 | ||
502 | free( tablex ); | |
503 | free( tabley ); | |
504 | ||
505 | return bmp; | |
506 | } | |
507 | ||
b5f01ae0 VS |
508 | bool wxBitmap::CreateFromImage( const wxImage& image, int depth ) |
509 | { | |
a11672a4 RR |
510 | UnRef(); |
511 | ||
b5f01ae0 VS |
512 | wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ) |
513 | wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") ) | |
514 | ||
3ebcd89d VZ |
515 | int width = image.GetWidth(); |
516 | int height = image.GetHeight(); | |
517 | ||
518 | if ( width <= 0 || height <= 0 ) | |
519 | { | |
520 | return false; | |
521 | } | |
522 | ||
b5f01ae0 | 523 | m_refData = new wxBitmapRefData(); |
c2fa61e8 | 524 | |
3ebcd89d VZ |
525 | SetHeight( height ); |
526 | SetWidth( width ); | |
527 | ||
b5f01ae0 | 528 | // ------ |
2b5f62a0 | 529 | // conversion to mono bitmap: |
b5f01ae0 VS |
530 | // ------ |
531 | if (depth == 1) | |
532 | { | |
c2fa61e8 | 533 | SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) ); |
b5f01ae0 VS |
534 | |
535 | SetDepth( 1 ); | |
2eefae6e | 536 | |
005f5d18 | 537 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
b5f01ae0 VS |
538 | |
539 | // Create picture image | |
540 | ||
541 | unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
542 | ||
543 | GdkImage *data_image = | |
544 | gdk_image_new_bitmap( visual, data_data, width, height ); | |
545 | ||
546 | // Create mask image | |
547 | ||
548 | GdkImage *mask_image = (GdkImage*) NULL; | |
549 | ||
550 | if (image.HasMask()) | |
551 | { | |
552 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
553 | ||
554 | mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); | |
555 | ||
556 | wxMask *mask = new wxMask(); | |
c2fa61e8 | 557 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); |
b5f01ae0 VS |
558 | |
559 | SetMask( mask ); | |
560 | } | |
561 | ||
562 | int r_mask = image.GetMaskRed(); | |
563 | int g_mask = image.GetMaskGreen(); | |
564 | int b_mask = image.GetMaskBlue(); | |
565 | ||
566 | unsigned char* data = image.GetData(); | |
567 | ||
568 | int index = 0; | |
569 | for (int y = 0; y < height; y++) | |
570 | { | |
571 | for (int x = 0; x < width; x++) | |
572 | { | |
573 | int r = data[index]; | |
574 | index++; | |
575 | int g = data[index]; | |
576 | index++; | |
577 | int b = data[index]; | |
578 | index++; | |
579 | ||
580 | if (image.HasMask()) | |
581 | { | |
582 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) | |
583 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
584 | else | |
585 | gdk_image_put_pixel( mask_image, x, y, 0 ); | |
586 | } | |
587 | ||
588 | if ((r == 255) && (b == 255) && (g == 255)) | |
589 | gdk_image_put_pixel( data_image, x, y, 1 ); | |
590 | else | |
591 | gdk_image_put_pixel( data_image, x, y, 0 ); | |
592 | ||
593 | } // for | |
594 | } // for | |
595 | ||
596 | // Blit picture | |
597 | ||
598 | GdkGC *data_gc = gdk_gc_new( GetBitmap() ); | |
599 | ||
600 | gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); | |
601 | ||
602 | gdk_image_destroy( data_image ); | |
603 | gdk_gc_unref( data_gc ); | |
604 | ||
605 | // Blit mask | |
606 | ||
607 | if (image.HasMask()) | |
608 | { | |
609 | GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() ); | |
610 | ||
611 | gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); | |
612 | ||
613 | gdk_image_destroy( mask_image ); | |
614 | gdk_gc_unref( mask_gc ); | |
615 | } | |
616 | } | |
c2fa61e8 | 617 | |
b5f01ae0 | 618 | // ------ |
2b5f62a0 | 619 | // conversion to colour bitmap: |
b5f01ae0 VS |
620 | // ------ |
621 | else | |
622 | { | |
c2fa61e8 | 623 | SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) ); |
b5f01ae0 | 624 | |
005f5d18 | 625 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
b5f01ae0 VS |
626 | |
627 | int bpp = visual->depth; | |
628 | ||
629 | SetDepth( bpp ); | |
b5f01ae0 | 630 | |
2eefae6e VZ |
631 | if ((bpp == 16) && (visual->red_mask != 0xf800)) |
632 | bpp = 15; | |
633 | else if (bpp < 8) | |
634 | bpp = 8; | |
635 | ||
636 | // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit | |
8ab696e0 RR |
637 | // visuals are not supported by GDK so we do these ourselves, too. |
638 | // 15-bit and 16-bit should actually work and 24-bit certainly does. | |
9b72a74d RR |
639 | #ifdef __sgi |
640 | if (!image.HasMask() && (bpp > 16)) | |
641 | #else | |
8ab696e0 | 642 | if (!image.HasMask() && (bpp > 12)) |
9b72a74d | 643 | #endif |
b5f01ae0 VS |
644 | { |
645 | static bool s_hasInitialized = FALSE; | |
646 | ||
647 | if (!s_hasInitialized) | |
648 | { | |
649 | gdk_rgb_init(); | |
650 | s_hasInitialized = TRUE; | |
651 | } | |
652 | ||
653 | GdkGC *gc = gdk_gc_new( GetPixmap() ); | |
654 | ||
655 | gdk_draw_rgb_image( GetPixmap(), | |
656 | gc, | |
657 | 0, 0, | |
658 | width, height, | |
659 | GDK_RGB_DITHER_NONE, | |
660 | image.GetData(), | |
661 | width*3 ); | |
662 | ||
663 | gdk_gc_unref( gc ); | |
664 | return TRUE; | |
665 | } | |
666 | ||
b5f01ae0 VS |
667 | // Create picture image |
668 | ||
669 | GdkImage *data_image = | |
670 | gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height ); | |
671 | ||
672 | // Create mask image | |
673 | ||
674 | GdkImage *mask_image = (GdkImage*) NULL; | |
675 | ||
676 | if (image.HasMask()) | |
677 | { | |
678 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
679 | ||
680 | mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); | |
681 | ||
682 | wxMask *mask = new wxMask(); | |
c2fa61e8 | 683 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); |
b5f01ae0 VS |
684 | |
685 | SetMask( mask ); | |
686 | } | |
687 | ||
688 | // Render | |
689 | ||
690 | enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR }; | |
691 | byte_order b_o = RGB; | |
692 | ||
8ab696e0 | 693 | if (bpp > 8) |
b5f01ae0 VS |
694 | { |
695 | if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB; | |
a11672a4 | 696 | else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG; |
b5f01ae0 VS |
697 | else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG; |
698 | else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR; | |
699 | else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB; | |
700 | else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR; | |
701 | } | |
702 | ||
703 | int r_mask = image.GetMaskRed(); | |
704 | int g_mask = image.GetMaskGreen(); | |
705 | int b_mask = image.GetMaskBlue(); | |
706 | ||
707 | unsigned char* data = image.GetData(); | |
708 | ||
709 | int index = 0; | |
710 | for (int y = 0; y < height; y++) | |
711 | { | |
712 | for (int x = 0; x < width; x++) | |
713 | { | |
714 | int r = data[index]; | |
715 | index++; | |
716 | int g = data[index]; | |
717 | index++; | |
718 | int b = data[index]; | |
719 | index++; | |
720 | ||
721 | if (image.HasMask()) | |
722 | { | |
723 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) | |
724 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
725 | else | |
726 | gdk_image_put_pixel( mask_image, x, y, 0 ); | |
727 | } | |
728 | ||
729 | switch (bpp) | |
730 | { | |
8ab696e0 | 731 | case 8: |
b5f01ae0 VS |
732 | { |
733 | int pixel = -1; | |
734 | if (wxTheApp->m_colorCube) | |
735 | { | |
736 | pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; | |
737 | } | |
738 | else | |
739 | { | |
740 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
741 | GdkColor *colors = cmap->colors; | |
742 | int max = 3 * (65536); | |
2eefae6e | 743 | |
b5f01ae0 VS |
744 | for (int i = 0; i < cmap->size; i++) |
745 | { | |
746 | int rdiff = (r << 8) - colors[i].red; | |
747 | int gdiff = (g << 8) - colors[i].green; | |
748 | int bdiff = (b << 8) - colors[i].blue; | |
749 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
750 | if (sum < max) { pixel = i; max = sum; } | |
751 | } | |
752 | } | |
2eefae6e | 753 | |
b5f01ae0 | 754 | gdk_image_put_pixel( data_image, x, y, pixel ); |
2eefae6e | 755 | |
b5f01ae0 VS |
756 | break; |
757 | } | |
8ab696e0 | 758 | case 12: // SGI only |
b5f01ae0 | 759 | { |
8ab696e0 RR |
760 | guint32 pixel = 0; |
761 | switch (b_o) | |
762 | { | |
763 | case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break; | |
764 | case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break; | |
765 | case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break; | |
766 | case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break; | |
767 | case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break; | |
768 | case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break; | |
769 | } | |
b5f01ae0 VS |
770 | gdk_image_put_pixel( data_image, x, y, pixel ); |
771 | break; | |
772 | } | |
8ab696e0 | 773 | case 15: |
b5f01ae0 | 774 | { |
8ab696e0 RR |
775 | guint32 pixel = 0; |
776 | switch (b_o) | |
777 | { | |
778 | case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
779 | case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
780 | case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
781 | case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
782 | case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
783 | case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
784 | } | |
b5f01ae0 VS |
785 | gdk_image_put_pixel( data_image, x, y, pixel ); |
786 | break; | |
787 | } | |
8ab696e0 | 788 | case 16: |
b5f01ae0 | 789 | { |
8ab696e0 RR |
790 | // I actually don't know if for 16-bit displays, it is alway the green |
791 | // component or the second component which has 6 bits. | |
b5f01ae0 VS |
792 | guint32 pixel = 0; |
793 | switch (b_o) | |
794 | { | |
005f5d18 RR |
795 | case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break; |
796 | case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
797 | case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break; | |
798 | case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
799 | case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
800 | case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
8ab696e0 RR |
801 | } |
802 | gdk_image_put_pixel( data_image, x, y, pixel ); | |
803 | break; | |
804 | } | |
805 | case 32: | |
806 | case 24: | |
807 | { | |
808 | guint32 pixel = 0; | |
809 | switch (b_o) | |
810 | { | |
811 | case RGB: pixel = (r << 16) | (g << 8) | b; break; | |
812 | case RBG: pixel = (r << 16) | (b << 8) | g; break; | |
813 | case BRG: pixel = (b << 16) | (r << 8) | g; break; | |
814 | case BGR: pixel = (b << 16) | (g << 8) | r; break; | |
815 | case GRB: pixel = (g << 16) | (r << 8) | b; break; | |
816 | case GBR: pixel = (g << 16) | (b << 8) | r; break; | |
b5f01ae0 VS |
817 | } |
818 | gdk_image_put_pixel( data_image, x, y, pixel ); | |
819 | } | |
8ab696e0 | 820 | default: break; |
b5f01ae0 VS |
821 | } |
822 | } // for | |
823 | } // for | |
824 | ||
825 | // Blit picture | |
826 | ||
827 | GdkGC *data_gc = gdk_gc_new( GetPixmap() ); | |
828 | ||
829 | gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); | |
830 | ||
831 | gdk_image_destroy( data_image ); | |
832 | gdk_gc_unref( data_gc ); | |
833 | ||
834 | // Blit mask | |
835 | ||
836 | if (image.HasMask()) | |
837 | { | |
838 | GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() ); | |
839 | ||
840 | gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); | |
841 | ||
842 | gdk_image_destroy( mask_image ); | |
843 | gdk_gc_unref( mask_gc ); | |
844 | } | |
845 | } | |
846 | ||
847 | return TRUE; | |
848 | } | |
849 | ||
850 | wxImage wxBitmap::ConvertToImage() const | |
851 | { | |
2eefae6e VZ |
852 | // the colour used as transparent one in wxImage and the one it is replaced |
853 | // with when it really occurs in the bitmap | |
d694de12 VZ |
854 | static const int MASK_RED = 1; |
855 | static const int MASK_GREEN = 2; | |
856 | static const int MASK_BLUE = 3; | |
857 | static const int MASK_BLUE_REPLACEMENT = 2; | |
2eefae6e | 858 | |
b5f01ae0 | 859 | wxImage image; |
c2fa61e8 | 860 | |
b5f01ae0 VS |
861 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
862 | ||
863 | GdkImage *gdk_image = (GdkImage*) NULL; | |
864 | if (GetPixmap()) | |
865 | { | |
866 | gdk_image = gdk_image_get( GetPixmap(), | |
2eefae6e VZ |
867 | 0, 0, |
868 | GetWidth(), GetHeight() ); | |
869 | } | |
870 | else if (GetBitmap()) | |
b5f01ae0 VS |
871 | { |
872 | gdk_image = gdk_image_get( GetBitmap(), | |
2eefae6e VZ |
873 | 0, 0, |
874 | GetWidth(), GetHeight() ); | |
875 | } | |
876 | else | |
b5f01ae0 VS |
877 | { |
878 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); | |
879 | } | |
880 | ||
881 | wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") ); | |
c2fa61e8 | 882 | |
b5f01ae0 VS |
883 | image.Create( GetWidth(), GetHeight() ); |
884 | char unsigned *data = image.GetData(); | |
2eefae6e | 885 | |
b5f01ae0 VS |
886 | if (!data) |
887 | { | |
888 | gdk_image_destroy( gdk_image ); | |
889 | wxFAIL_MSG( wxT("couldn't create image") ); | |
890 | return wxNullImage; | |
891 | } | |
892 | ||
893 | GdkImage *gdk_image_mask = (GdkImage*) NULL; | |
894 | if (GetMask()) | |
895 | { | |
896 | gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(), | |
2eefae6e VZ |
897 | 0, 0, |
898 | GetWidth(), GetHeight() ); | |
b5f01ae0 | 899 | |
2eefae6e | 900 | image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE ); |
b5f01ae0 VS |
901 | } |
902 | ||
903 | int bpp = -1; | |
904 | int red_shift_right = 0; | |
905 | int green_shift_right = 0; | |
906 | int blue_shift_right = 0; | |
907 | int red_shift_left = 0; | |
908 | int green_shift_left = 0; | |
909 | int blue_shift_left = 0; | |
910 | bool use_shift = FALSE; | |
911 | ||
912 | if (GetPixmap()) | |
913 | { | |
914 | GdkVisual *visual = gdk_window_get_visual( GetPixmap() ); | |
005f5d18 RR |
915 | if (visual == NULL) |
916 | visual = wxTheApp->GetGdkVisual(); | |
2eefae6e | 917 | |
b5f01ae0 | 918 | bpp = visual->depth; |
2eefae6e VZ |
919 | if (bpp == 16) |
920 | bpp = visual->red_prec + visual->green_prec + visual->blue_prec; | |
b5f01ae0 VS |
921 | red_shift_right = visual->red_shift; |
922 | red_shift_left = 8-visual->red_prec; | |
923 | green_shift_right = visual->green_shift; | |
924 | green_shift_left = 8-visual->green_prec; | |
925 | blue_shift_right = visual->blue_shift; | |
926 | blue_shift_left = 8-visual->blue_prec; | |
927 | ||
928 | use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR); | |
929 | } | |
930 | if (GetBitmap()) | |
931 | { | |
932 | bpp = 1; | |
933 | } | |
934 | ||
935 | ||
936 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
937 | ||
938 | long pos = 0; | |
939 | for (int j = 0; j < GetHeight(); j++) | |
940 | { | |
941 | for (int i = 0; i < GetWidth(); i++) | |
942 | { | |
943 | wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j ); | |
8ab696e0 RR |
944 | if (bpp == 1) |
945 | { | |
946 | if (pixel == 0) | |
b5f01ae0 | 947 | { |
b5f01ae0 VS |
948 | data[pos] = 0; |
949 | data[pos+1] = 0; | |
950 | data[pos+2] = 0; | |
8ab696e0 RR |
951 | } |
952 | else | |
953 | { | |
b5f01ae0 VS |
954 | data[pos] = 255; |
955 | data[pos+1] = 255; | |
956 | data[pos+2] = 255; | |
8ab696e0 | 957 | } |
b5f01ae0 VS |
958 | } |
959 | else if (use_shift) | |
960 | { | |
961 | data[pos] = (pixel >> red_shift_right) << red_shift_left; | |
962 | data[pos+1] = (pixel >> green_shift_right) << green_shift_left; | |
963 | data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left; | |
8ab696e0 | 964 | } |
b5f01ae0 VS |
965 | else if (cmap->colors) |
966 | { | |
967 | data[pos] = cmap->colors[pixel].red >> 8; | |
968 | data[pos+1] = cmap->colors[pixel].green >> 8; | |
969 | data[pos+2] = cmap->colors[pixel].blue >> 8; | |
970 | } | |
971 | else | |
972 | { | |
973 | wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") ); | |
974 | } | |
975 | ||
976 | if (gdk_image_mask) | |
977 | { | |
978 | int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j ); | |
979 | if (mask_pixel == 0) | |
980 | { | |
2eefae6e VZ |
981 | data[pos] = MASK_RED; |
982 | data[pos+1] = MASK_GREEN; | |
983 | data[pos+2] = MASK_BLUE; | |
984 | } | |
985 | else if ( data[pos] == MASK_RED && | |
986 | data[pos+1] == MASK_GREEN && | |
987 | data[pos+2] == MASK_BLUE ) | |
988 | { | |
989 | data[pos+2] = MASK_BLUE_REPLACEMENT; | |
b5f01ae0 VS |
990 | } |
991 | } | |
992 | ||
993 | pos += 3; | |
994 | } | |
995 | } | |
996 | ||
997 | gdk_image_destroy( gdk_image ); | |
c2fa61e8 | 998 | if (gdk_image_mask) gdk_image_destroy( gdk_image_mask ); |
b5f01ae0 VS |
999 | |
1000 | return image; | |
1001 | } | |
1002 | ||
c801d85f | 1003 | wxBitmap::wxBitmap( const wxBitmap& bmp ) |
3ebcd89d | 1004 | : wxGDIObject() |
c801d85f | 1005 | { |
fd0eed64 | 1006 | Ref( bmp ); |
ff7b1510 | 1007 | } |
6f65e337 | 1008 | |
debe6624 | 1009 | wxBitmap::wxBitmap( const wxString &filename, int type ) |
c801d85f | 1010 | { |
fd0eed64 | 1011 | LoadFile( filename, type ); |
ff7b1510 | 1012 | } |
c801d85f | 1013 | |
debe6624 | 1014 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth)) |
6f65e337 | 1015 | { |
3ebcd89d VZ |
1016 | if ( width > 0 && height > 0 ) |
1017 | { | |
1018 | m_refData = new wxBitmapRefData(); | |
6f65e337 | 1019 | |
3ebcd89d VZ |
1020 | M_BMPDATA->m_mask = (wxMask *) NULL; |
1021 | M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data | |
1022 | ( | |
1023 | wxGetRootWindow()->window, | |
1024 | (gchar *) bits, | |
1025 | width, | |
1026 | height | |
1027 | ); | |
1028 | M_BMPDATA->m_width = width; | |
1029 | M_BMPDATA->m_height = height; | |
1030 | M_BMPDATA->m_bpp = 1; | |
6f65e337 | 1031 | |
3ebcd89d VZ |
1032 | wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") ); |
1033 | } | |
6f65e337 | 1034 | } |
8bbe427f VZ |
1035 | |
1036 | wxBitmap::~wxBitmap() | |
c801d85f | 1037 | { |
ff7b1510 | 1038 | } |
8bbe427f | 1039 | |
c801d85f KB |
1040 | wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp ) |
1041 | { | |
7ecb8b06 VZ |
1042 | if ( m_refData != bmp.m_refData ) |
1043 | Ref( bmp ); | |
1044 | ||
8bbe427f | 1045 | return *this; |
ff7b1510 | 1046 | } |
8bbe427f | 1047 | |
f6bcfd97 | 1048 | bool wxBitmap::operator == ( const wxBitmap& bmp ) const |
c801d85f | 1049 | { |
8bbe427f | 1050 | return m_refData == bmp.m_refData; |
ff7b1510 | 1051 | } |
8bbe427f | 1052 | |
f6bcfd97 | 1053 | bool wxBitmap::operator != ( const wxBitmap& bmp ) const |
c801d85f | 1054 | { |
8bbe427f | 1055 | return m_refData != bmp.m_refData; |
ff7b1510 | 1056 | } |
8bbe427f | 1057 | |
91b8de8d | 1058 | bool wxBitmap::Ok() const |
c801d85f | 1059 | { |
fd0eed64 | 1060 | return (m_refData != NULL); |
ff7b1510 | 1061 | } |
8bbe427f | 1062 | |
91b8de8d | 1063 | int wxBitmap::GetHeight() const |
c801d85f | 1064 | { |
223d09f6 | 1065 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
e55ad60e | 1066 | |
fd0eed64 | 1067 | return M_BMPDATA->m_height; |
ff7b1510 | 1068 | } |
c801d85f | 1069 | |
91b8de8d | 1070 | int wxBitmap::GetWidth() const |
c801d85f | 1071 | { |
223d09f6 | 1072 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 1073 | |
fd0eed64 | 1074 | return M_BMPDATA->m_width; |
ff7b1510 | 1075 | } |
c801d85f | 1076 | |
91b8de8d | 1077 | int wxBitmap::GetDepth() const |
c801d85f | 1078 | { |
223d09f6 | 1079 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 1080 | |
fd0eed64 | 1081 | return M_BMPDATA->m_bpp; |
ff7b1510 | 1082 | } |
c801d85f | 1083 | |
91b8de8d | 1084 | wxMask *wxBitmap::GetMask() const |
c801d85f | 1085 | { |
223d09f6 | 1086 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1087 | |
fd0eed64 | 1088 | return M_BMPDATA->m_mask; |
ff7b1510 | 1089 | } |
c801d85f KB |
1090 | |
1091 | void wxBitmap::SetMask( wxMask *mask ) | |
1092 | { | |
223d09f6 | 1093 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); |
8bbe427f | 1094 | |
fd0eed64 | 1095 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; |
8bbe427f | 1096 | |
fd0eed64 | 1097 | M_BMPDATA->m_mask = mask; |
ff7b1510 | 1098 | } |
c801d85f | 1099 | |
db0aec83 VS |
1100 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
1101 | { | |
1102 | *this = icon; | |
1103 | return TRUE; | |
1104 | } | |
1105 | ||
17bec151 RR |
1106 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const |
1107 | { | |
1108 | wxCHECK_MSG( Ok() && | |
13111b2a VZ |
1109 | (rect.x >= 0) && (rect.y >= 0) && |
1110 | (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height), | |
17bec151 | 1111 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); |
13111b2a | 1112 | |
17bec151 RR |
1113 | wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp ); |
1114 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
13111b2a | 1115 | |
17bec151 RR |
1116 | if (ret.GetPixmap()) |
1117 | { | |
1118 | GdkGC *gc = gdk_gc_new( ret.GetPixmap() ); | |
13111b2a VZ |
1119 | gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); |
1120 | gdk_gc_destroy( gc ); | |
17bec151 RR |
1121 | } |
1122 | else | |
1123 | { | |
1124 | GdkGC *gc = gdk_gc_new( ret.GetBitmap() ); | |
f6bcfd97 | 1125 | gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); |
13111b2a | 1126 | gdk_gc_destroy( gc ); |
17bec151 | 1127 | } |
13111b2a | 1128 | |
17bec151 RR |
1129 | if (GetMask()) |
1130 | { | |
1131 | wxMask *mask = new wxMask; | |
c2fa61e8 | 1132 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 ); |
13111b2a | 1133 | |
17bec151 | 1134 | GdkGC *gc = gdk_gc_new( mask->m_bitmap ); |
f6bcfd97 | 1135 | gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height ); |
13111b2a VZ |
1136 | gdk_gc_destroy( gc ); |
1137 | ||
1138 | ret.SetMask( mask ); | |
17bec151 | 1139 | } |
13111b2a | 1140 | |
17bec151 RR |
1141 | return ret; |
1142 | } | |
1143 | ||
fd0eed64 | 1144 | bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) ) |
c801d85f | 1145 | { |
223d09f6 | 1146 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") ); |
8bbe427f | 1147 | |
b75dd496 | 1148 | // Try to save the bitmap via wxImage handlers: |
fd0eed64 | 1149 | { |
368d59f0 | 1150 | wxImage image = ConvertToImage(); |
284b4c88 | 1151 | if (image.Ok()) return image.SaveFile( name, type ); |
fd0eed64 | 1152 | } |
8bbe427f | 1153 | |
fd0eed64 | 1154 | return FALSE; |
ff7b1510 | 1155 | } |
c801d85f | 1156 | |
fd0eed64 | 1157 | bool wxBitmap::LoadFile( const wxString &name, int type ) |
c801d85f | 1158 | { |
fd0eed64 | 1159 | UnRef(); |
8bbe427f | 1160 | |
3ebcd89d VZ |
1161 | if (!wxFileExists(name)) |
1162 | return FALSE; | |
8bbe427f | 1163 | |
005f5d18 | 1164 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 1165 | |
fd0eed64 RR |
1166 | if (type == wxBITMAP_TYPE_XPM) |
1167 | { | |
1168 | m_refData = new wxBitmapRefData(); | |
8bbe427f | 1169 | |
fd0eed64 | 1170 | GdkBitmap *mask = (GdkBitmap*) NULL; |
8bbe427f | 1171 | |
3ebcd89d VZ |
1172 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm |
1173 | ( | |
1174 | wxGetRootWindow()->window, | |
1175 | &mask, | |
1176 | NULL, | |
1177 | name.fn_str() | |
1178 | ); | |
8bbe427f | 1179 | |
fd0eed64 RR |
1180 | if (mask) |
1181 | { | |
1182 | M_BMPDATA->m_mask = new wxMask(); | |
1183 | M_BMPDATA->m_mask->m_bitmap = mask; | |
1184 | } | |
8bbe427f | 1185 | |
fd0eed64 | 1186 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
c2fa61e8 | 1187 | |
103aab26 | 1188 | M_BMPDATA->m_bpp = visual->depth; |
fd0eed64 | 1189 | } |
b75dd496 | 1190 | else // try if wxImage can load it |
fd0eed64 RR |
1191 | { |
1192 | wxImage image; | |
3ebcd89d VZ |
1193 | if ( !image.LoadFile( name, type ) || !image.Ok() ) |
1194 | return FALSE; | |
1195 | ||
1196 | *this = wxBitmap(image); | |
fd0eed64 | 1197 | } |
8bbe427f | 1198 | |
fd0eed64 | 1199 | return TRUE; |
ff7b1510 | 1200 | } |
8bbe427f | 1201 | |
91b8de8d | 1202 | wxPalette *wxBitmap::GetPalette() const |
c801d85f | 1203 | { |
3ebcd89d VZ |
1204 | if (!Ok()) |
1205 | return (wxPalette *) NULL; | |
8bbe427f | 1206 | |
fd0eed64 | 1207 | return M_BMPDATA->m_palette; |
ff7b1510 | 1208 | } |
c801d85f | 1209 | |
4bc67cc5 RR |
1210 | void wxBitmap::SetHeight( int height ) |
1211 | { | |
3ebcd89d VZ |
1212 | if (!m_refData) |
1213 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1214 | |
1215 | M_BMPDATA->m_height = height; | |
1216 | } | |
1217 | ||
1218 | void wxBitmap::SetWidth( int width ) | |
1219 | { | |
3ebcd89d VZ |
1220 | if (!m_refData) |
1221 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1222 | |
1223 | M_BMPDATA->m_width = width; | |
1224 | } | |
1225 | ||
1226 | void wxBitmap::SetDepth( int depth ) | |
1227 | { | |
3ebcd89d VZ |
1228 | if (!m_refData) |
1229 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1230 | |
1231 | M_BMPDATA->m_bpp = depth; | |
1232 | } | |
1233 | ||
1234 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
1235 | { | |
3ebcd89d VZ |
1236 | if (!m_refData) |
1237 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1238 | |
1239 | M_BMPDATA->m_pixmap = pixmap; | |
1240 | } | |
1241 | ||
82ea63e6 RR |
1242 | void wxBitmap::SetBitmap( GdkPixmap *bitmap ) |
1243 | { | |
3ebcd89d VZ |
1244 | if (!m_refData) |
1245 | m_refData = new wxBitmapRefData(); | |
82ea63e6 RR |
1246 | |
1247 | M_BMPDATA->m_bitmap = bitmap; | |
1248 | } | |
1249 | ||
91b8de8d | 1250 | GdkPixmap *wxBitmap::GetPixmap() const |
c801d85f | 1251 | { |
223d09f6 | 1252 | wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1253 | |
fd0eed64 | 1254 | return M_BMPDATA->m_pixmap; |
ff7b1510 | 1255 | } |
8bbe427f | 1256 | |
91b8de8d | 1257 | GdkBitmap *wxBitmap::GetBitmap() const |
6f65e337 | 1258 | { |
223d09f6 | 1259 | wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1260 | |
fd0eed64 | 1261 | return M_BMPDATA->m_bitmap; |
ff7b1510 | 1262 | } |