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