]>
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 RR |
230 | wxBitmapRefData(); |
231 | ~wxBitmapRefData(); | |
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 | |
e838cc14 | 315 | bool wxBitmap::CreateFromXpm( const char **bits ) |
e52f60e6 | 316 | { |
a11672a4 RR |
317 | UnRef(); |
318 | ||
637b7e4f | 319 | wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") ); |
8bbe427f | 320 | |
005f5d18 | 321 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 322 | |
e52f60e6 RR |
323 | m_refData = new wxBitmapRefData(); |
324 | ||
325 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
8bbe427f | 326 | |
c2fa61e8 | 327 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits ); |
8bbe427f | 328 | |
902725ee | 329 | wxCHECK_MSG( M_BMPDATA->m_pixmap, false, wxT("couldn't create pixmap") ); |
8bbe427f | 330 | |
fd0eed64 RR |
331 | if (mask) |
332 | { | |
333 | M_BMPDATA->m_mask = new wxMask(); | |
334 | M_BMPDATA->m_mask->m_bitmap = mask; | |
335 | } | |
8bbe427f | 336 | |
fd0eed64 | 337 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
2eefae6e | 338 | |
8cce8940 | 339 | M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ? |
c2fa61e8 | 340 | |
902725ee | 341 | return true; |
ff7b1510 | 342 | } |
b5f01ae0 | 343 | |
783da845 RR |
344 | wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy ) |
345 | { | |
346 | wxCHECK_MSG( Ok(), wxNullBitmap, wxT("invalid bitmap") ); | |
347 | ||
348 | if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height) | |
349 | return *this; | |
902725ee | 350 | |
783da845 RR |
351 | int width = wxMax(newx, 1); |
352 | int height = wxMax(newy, 1); | |
353 | width = wxMin(width, clipwidth); | |
354 | height = wxMin(height, clipheight); | |
902725ee | 355 | |
feac7937 | 356 | wxBitmap bmp; |
783da845 | 357 | |
3cbab641 MR |
358 | GdkImage *img = (GdkImage*) NULL; |
359 | if (GetPixmap()) | |
360 | img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() ); | |
361 | else if (GetBitmap()) | |
362 | img = gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() ); | |
363 | else | |
364 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); | |
365 | ||
366 | wxCHECK_MSG( img, wxNullBitmap, wxT("couldn't create image") ); | |
367 | ||
368 | int bpp = -1; | |
369 | ||
370 | ||
371 | GdkGC *gc = NULL; | |
372 | GdkPixmap *dstpix = NULL; | |
373 | if (GetPixmap()) | |
783da845 | 374 | { |
3cbab641 MR |
375 | GdkVisual *visual = gdk_window_get_visual( GetPixmap() ); |
376 | if (visual == NULL) | |
377 | visual = wxTheApp->GetGdkVisual(); | |
378 | ||
379 | bpp = visual->depth; | |
380 | bmp = wxBitmap(width,height,bpp); | |
381 | dstpix = bmp.GetPixmap(); | |
382 | gc = gdk_gc_new( dstpix ); | |
783da845 | 383 | } |
3cbab641 MR |
384 | |
385 | char *dst = NULL; | |
386 | long dstbyteperline = 0; | |
387 | ||
388 | if (GetBitmap()) | |
783da845 | 389 | { |
3cbab641 MR |
390 | bpp = 1; |
391 | dstbyteperline = width/8*M_BMPDATA->m_bpp; | |
392 | if (width*M_BMPDATA->m_bpp % 8 != 0) | |
393 | dstbyteperline++; | |
394 | dst = (char*) malloc(dstbyteperline*height); | |
395 | } | |
feac7937 | 396 | |
3cbab641 MR |
397 | // be careful to use the right scaling factor |
398 | float scx = (float)M_BMPDATA->m_width/(float)newx; | |
399 | float scy = (float)M_BMPDATA->m_height/(float)newy; | |
400 | // prepare accel-tables | |
401 | int *tablex = (int *)calloc(width,sizeof(int)); | |
402 | int *tabley = (int *)calloc(height,sizeof(int)); | |
783da845 | 403 | |
3cbab641 MR |
404 | // accel table filled with clipped values |
405 | for (int x = 0; x < width; x++) | |
406 | tablex[x] = (int) (scx * (x+clipx)); | |
407 | for (int y = 0; y < height; y++) | |
408 | tabley[y] = (int) (scy * (y+clipy)); | |
feac7937 | 409 | |
3cbab641 MR |
410 | // Main rescaling routine starts here |
411 | for (int h = 0; h < height; h++) | |
412 | { | |
413 | char outbyte = 0; | |
414 | int old_x = -1; | |
415 | guint32 old_pixval = 0; | |
902725ee | 416 | |
3cbab641 | 417 | for (int w = 0; w < width; w++) |
783da845 | 418 | { |
3cbab641 MR |
419 | guint32 pixval; |
420 | int x = tablex[w]; | |
421 | if (x == old_x) | |
422 | pixval = old_pixval; | |
423 | else | |
424 | { | |
425 | pixval = gdk_image_get_pixel( img, x, tabley[h] ); | |
426 | old_pixval = pixval; | |
427 | old_x = x; | |
428 | } | |
783da845 | 429 | |
3cbab641 MR |
430 | if (bpp == 1) |
431 | { | |
432 | if (!pixval) | |
433 | { | |
434 | char bit=1; | |
435 | char shift = bit << (w % 8); | |
436 | outbyte |= shift; | |
437 | } | |
902725ee | 438 | |
3cbab641 MR |
439 | if ((w+1)%8==0) |
440 | { | |
441 | dst[h*dstbyteperline+w/8] = outbyte; | |
442 | outbyte = 0; | |
443 | } | |
444 | } | |
445 | else | |
446 | { | |
447 | GdkColor col; | |
448 | col.pixel = pixval; | |
449 | gdk_gc_set_foreground( gc, &col ); | |
450 | gdk_draw_point( dstpix, gc, w, h); | |
451 | } | |
feac7937 | 452 | } |
902725ee | 453 | |
3cbab641 MR |
454 | // do not forget the last byte |
455 | if ((bpp == 1) && (width % 8 != 0)) | |
456 | dst[h*dstbyteperline+width/8] = outbyte; | |
457 | } | |
feac7937 | 458 | |
3cbab641 MR |
459 | gdk_image_destroy( img ); |
460 | if (gc) gdk_gc_unref( gc ); | |
461 | ||
462 | if (bpp == 1) | |
463 | { | |
464 | bmp = wxBitmap( (const char *)dst, width, height, 1 ); | |
465 | free( dst ); | |
466 | } | |
467 | ||
468 | if (GetMask()) | |
469 | { | |
470 | dstbyteperline = width/8; | |
471 | if (width % 8 != 0) | |
472 | dstbyteperline++; | |
473 | dst = (char*) malloc(dstbyteperline*height); | |
474 | img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() ); | |
783da845 RR |
475 | |
476 | for (int h = 0; h < height; h++) | |
477 | { | |
478 | char outbyte = 0; | |
f9ebac93 | 479 | int old_x = -1; |
b63b07a8 | 480 | guint32 old_pixval = 0; |
feac7937 | 481 | |
f9ebac93 | 482 | for (int w = 0; w < width; w++) |
783da845 | 483 | { |
f9ebac93 RR |
484 | guint32 pixval; |
485 | int x = tablex[w]; | |
486 | if (x == old_x) | |
487 | pixval = old_pixval; | |
488 | else | |
489 | { | |
490 | pixval = gdk_image_get_pixel( img, x, tabley[h] ); | |
491 | old_pixval = pixval; | |
492 | old_x = x; | |
493 | } | |
902725ee | 494 | |
3cbab641 | 495 | if (pixval) |
783da845 | 496 | { |
3cbab641 MR |
497 | char bit=1; |
498 | char shift = bit << (w % 8); | |
499 | outbyte |= shift; | |
783da845 | 500 | } |
3cbab641 MR |
501 | |
502 | if ((w+1)%8 == 0) | |
783da845 | 503 | { |
3cbab641 MR |
504 | dst[h*dstbyteperline+w/8] = outbyte; |
505 | outbyte = 0; | |
783da845 RR |
506 | } |
507 | } | |
902725ee | 508 | |
783da845 | 509 | // do not forget the last byte |
3cbab641 | 510 | if (width % 8 != 0) |
f9ebac93 | 511 | dst[h*dstbyteperline+width/8] = outbyte; |
783da845 | 512 | } |
3cbab641 MR |
513 | wxMask* mask = new wxMask; |
514 | mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height ); | |
515 | bmp.SetMask(mask); | |
902725ee | 516 | |
3cbab641 | 517 | free( dst ); |
783da845 | 518 | gdk_image_destroy( img ); |
feac7937 | 519 | } |
902725ee | 520 | |
3cbab641 MR |
521 | free( tablex ); |
522 | free( tabley ); | |
523 | ||
902725ee | 524 | return bmp; |
783da845 RR |
525 | } |
526 | ||
feac7937 | 527 | bool wxBitmap::CreateFromImage(const wxImage& image, int depth) |
b5f01ae0 | 528 | { |
a11672a4 RR |
529 | UnRef(); |
530 | ||
637b7e4f VZ |
531 | wxCHECK_MSG( image.Ok(), false, wxT("invalid image") ); |
532 | wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") ); | |
b5f01ae0 | 533 | |
feac7937 VS |
534 | if (image.GetWidth() <= 0 || image.GetHeight() <= 0) |
535 | return false; | |
902725ee | 536 | |
feac7937 | 537 | m_refData = new wxBitmapRefData(); |
3ebcd89d | 538 | |
feac7937 | 539 | if (depth == 1) |
3ebcd89d | 540 | { |
feac7937 | 541 | return CreateFromImageAsBitmap(image); |
3ebcd89d | 542 | } |
feac7937 VS |
543 | else |
544 | { | |
feac7937 VS |
545 | return CreateFromImageAsPixmap(image); |
546 | } | |
547 | } | |
3ebcd89d | 548 | |
feac7937 VS |
549 | // conversion to mono bitmap: |
550 | bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img) | |
551 | { | |
552 | // convert alpha channel to mask, if it is present: | |
553 | wxImage image(img); | |
554 | image.ConvertAlphaToMask(); | |
902725ee | 555 | |
feac7937 VS |
556 | int width = image.GetWidth(); |
557 | int height = image.GetHeight(); | |
c2fa61e8 | 558 | |
3ebcd89d VZ |
559 | SetHeight( height ); |
560 | SetWidth( width ); | |
561 | ||
feac7937 | 562 | SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) ); |
b5f01ae0 | 563 | |
feac7937 | 564 | SetDepth( 1 ); |
2eefae6e | 565 | |
feac7937 | 566 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
b5f01ae0 | 567 | |
feac7937 | 568 | // Create picture image |
b5f01ae0 | 569 | |
feac7937 | 570 | unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); |
b5f01ae0 | 571 | |
feac7937 VS |
572 | GdkImage *data_image = |
573 | gdk_image_new_bitmap( visual, data_data, width, height ); | |
b5f01ae0 | 574 | |
feac7937 | 575 | // Create mask image |
b5f01ae0 | 576 | |
feac7937 | 577 | GdkImage *mask_image = (GdkImage*) NULL; |
b5f01ae0 | 578 | |
feac7937 VS |
579 | if (image.HasMask()) |
580 | { | |
581 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
b5f01ae0 | 582 | |
feac7937 | 583 | mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); |
b5f01ae0 | 584 | |
feac7937 VS |
585 | wxMask *mask = new wxMask(); |
586 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); | |
b5f01ae0 | 587 | |
feac7937 VS |
588 | SetMask( mask ); |
589 | } | |
b5f01ae0 | 590 | |
feac7937 VS |
591 | int r_mask = image.GetMaskRed(); |
592 | int g_mask = image.GetMaskGreen(); | |
593 | int b_mask = image.GetMaskBlue(); | |
b5f01ae0 | 594 | |
feac7937 | 595 | unsigned char* data = image.GetData(); |
b5f01ae0 | 596 | |
feac7937 VS |
597 | int index = 0; |
598 | for (int y = 0; y < height; y++) | |
599 | { | |
600 | for (int x = 0; x < width; x++) | |
b5f01ae0 | 601 | { |
feac7937 VS |
602 | int r = data[index]; |
603 | index++; | |
604 | int g = data[index]; | |
605 | index++; | |
606 | int b = data[index]; | |
607 | index++; | |
608 | ||
609 | if (image.HasMask()) | |
b5f01ae0 | 610 | { |
feac7937 VS |
611 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) |
612 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
b5f01ae0 | 613 | else |
feac7937 VS |
614 | gdk_image_put_pixel( mask_image, x, y, 0 ); |
615 | } | |
b5f01ae0 | 616 | |
feac7937 VS |
617 | if ((r == 255) && (b == 255) && (g == 255)) |
618 | gdk_image_put_pixel( data_image, x, y, 1 ); | |
619 | else | |
620 | gdk_image_put_pixel( data_image, x, y, 0 ); | |
b5f01ae0 | 621 | |
feac7937 VS |
622 | } // for |
623 | } // for | |
b5f01ae0 | 624 | |
feac7937 | 625 | // Blit picture |
b5f01ae0 | 626 | |
feac7937 | 627 | GdkGC *data_gc = gdk_gc_new( GetBitmap() ); |
b5f01ae0 | 628 | |
feac7937 | 629 | gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); |
b5f01ae0 | 630 | |
feac7937 VS |
631 | gdk_image_destroy( data_image ); |
632 | gdk_gc_unref( data_gc ); | |
b5f01ae0 | 633 | |
feac7937 | 634 | // Blit mask |
b5f01ae0 | 635 | |
feac7937 VS |
636 | if (image.HasMask()) |
637 | { | |
638 | GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() ); | |
b5f01ae0 | 639 | |
feac7937 VS |
640 | gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); |
641 | ||
642 | gdk_image_destroy( mask_image ); | |
643 | gdk_gc_unref( mask_gc ); | |
b5f01ae0 | 644 | } |
c2fa61e8 | 645 | |
feac7937 VS |
646 | return true; |
647 | } | |
902725ee | 648 | |
feac7937 VS |
649 | // conversion to colour bitmap: |
650 | bool wxBitmap::CreateFromImageAsPixmap(const wxImage& img) | |
651 | { | |
652 | // convert alpha channel to mask, if it is present: | |
653 | wxImage image(img); | |
654 | image.ConvertAlphaToMask(); | |
902725ee | 655 | |
feac7937 VS |
656 | int width = image.GetWidth(); |
657 | int height = image.GetHeight(); | |
658 | ||
659 | SetHeight( height ); | |
660 | SetWidth( width ); | |
661 | ||
662 | SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) ); | |
b5f01ae0 | 663 | |
feac7937 | 664 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
b5f01ae0 | 665 | |
feac7937 | 666 | int bpp = visual->depth; |
b5f01ae0 | 667 | |
feac7937 | 668 | SetDepth( bpp ); |
b5f01ae0 | 669 | |
feac7937 VS |
670 | if ((bpp == 16) && (visual->red_mask != 0xf800)) |
671 | bpp = 15; | |
672 | else if (bpp < 8) | |
673 | bpp = 8; | |
2eefae6e | 674 | |
feac7937 VS |
675 | // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit |
676 | // visuals are not supported by GDK so we do these ourselves, too. | |
677 | // 15-bit and 16-bit should actually work and 24-bit certainly does. | |
9b72a74d | 678 | #ifdef __sgi |
feac7937 | 679 | if (!image.HasMask() && (bpp > 16)) |
9b72a74d | 680 | #else |
feac7937 | 681 | if (!image.HasMask() && (bpp > 12)) |
9b72a74d | 682 | #endif |
feac7937 | 683 | { |
902725ee | 684 | static bool s_hasInitialized = false; |
b5f01ae0 | 685 | |
feac7937 VS |
686 | if (!s_hasInitialized) |
687 | { | |
688 | gdk_rgb_init(); | |
902725ee | 689 | s_hasInitialized = true; |
feac7937 | 690 | } |
b5f01ae0 | 691 | |
feac7937 | 692 | GdkGC *gc = gdk_gc_new( GetPixmap() ); |
b5f01ae0 | 693 | |
feac7937 VS |
694 | gdk_draw_rgb_image( GetPixmap(), |
695 | gc, | |
696 | 0, 0, | |
697 | width, height, | |
698 | GDK_RGB_DITHER_NONE, | |
699 | image.GetData(), | |
700 | width*3 ); | |
b5f01ae0 | 701 | |
feac7937 | 702 | gdk_gc_unref( gc ); |
902725ee | 703 | return true; |
feac7937 | 704 | } |
b5f01ae0 | 705 | |
feac7937 | 706 | // Create picture image |
b5f01ae0 | 707 | |
feac7937 VS |
708 | GdkImage *data_image = |
709 | gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height ); | |
b5f01ae0 | 710 | |
feac7937 | 711 | // Create mask image |
b5f01ae0 | 712 | |
feac7937 | 713 | GdkImage *mask_image = (GdkImage*) NULL; |
b5f01ae0 | 714 | |
feac7937 VS |
715 | if (image.HasMask()) |
716 | { | |
717 | unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height ); | |
b5f01ae0 | 718 | |
feac7937 | 719 | mask_image = gdk_image_new_bitmap( visual, mask_data, width, height ); |
b5f01ae0 | 720 | |
feac7937 VS |
721 | wxMask *mask = new wxMask(); |
722 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ); | |
b5f01ae0 | 723 | |
feac7937 VS |
724 | SetMask( mask ); |
725 | } | |
b5f01ae0 | 726 | |
feac7937 | 727 | // Render |
b5f01ae0 | 728 | |
feac7937 VS |
729 | enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR }; |
730 | byte_order b_o = RGB; | |
b5f01ae0 | 731 | |
feac7937 VS |
732 | if (bpp > 8) |
733 | { | |
734 | if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB; | |
735 | else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG; | |
736 | else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG; | |
737 | else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR; | |
738 | else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB; | |
739 | else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR; | |
740 | } | |
b5f01ae0 | 741 | |
feac7937 VS |
742 | int r_mask = image.GetMaskRed(); |
743 | int g_mask = image.GetMaskGreen(); | |
744 | int b_mask = image.GetMaskBlue(); | |
b5f01ae0 | 745 | |
feac7937 | 746 | unsigned char* data = image.GetData(); |
b5f01ae0 | 747 | |
feac7937 VS |
748 | int index = 0; |
749 | for (int y = 0; y < height; y++) | |
750 | { | |
751 | for (int x = 0; x < width; x++) | |
b5f01ae0 | 752 | { |
feac7937 VS |
753 | int r = data[index]; |
754 | index++; | |
755 | int g = data[index]; | |
756 | index++; | |
757 | int b = data[index]; | |
758 | index++; | |
759 | ||
760 | if (image.HasMask()) | |
b5f01ae0 | 761 | { |
feac7937 VS |
762 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) |
763 | gdk_image_put_pixel( mask_image, x, y, 1 ); | |
764 | else | |
765 | gdk_image_put_pixel( mask_image, x, y, 0 ); | |
766 | } | |
b5f01ae0 | 767 | |
feac7937 VS |
768 | switch (bpp) |
769 | { | |
770 | case 8: | |
b5f01ae0 | 771 | { |
feac7937 VS |
772 | int pixel = -1; |
773 | if (wxTheApp->m_colorCube) | |
b5f01ae0 | 774 | { |
feac7937 VS |
775 | pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; |
776 | } | |
777 | else | |
778 | { | |
779 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
780 | GdkColor *colors = cmap->colors; | |
781 | int max = 3 * (65536); | |
782 | ||
783 | for (int i = 0; i < cmap->size; i++) | |
b5f01ae0 | 784 | { |
feac7937 VS |
785 | int rdiff = (r << 8) - colors[i].red; |
786 | int gdiff = (g << 8) - colors[i].green; | |
787 | int bdiff = (b << 8) - colors[i].blue; | |
788 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
789 | if (sum < max) { pixel = i; max = sum; } | |
b5f01ae0 | 790 | } |
feac7937 | 791 | } |
2eefae6e | 792 | |
feac7937 | 793 | gdk_image_put_pixel( data_image, x, y, pixel ); |
2eefae6e | 794 | |
feac7937 VS |
795 | break; |
796 | } | |
797 | case 12: // SGI only | |
798 | { | |
799 | guint32 pixel = 0; | |
800 | switch (b_o) | |
b5f01ae0 | 801 | { |
feac7937 VS |
802 | case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break; |
803 | case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break; | |
804 | case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break; | |
805 | case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break; | |
806 | case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break; | |
807 | case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break; | |
b5f01ae0 | 808 | } |
feac7937 VS |
809 | gdk_image_put_pixel( data_image, x, y, pixel ); |
810 | break; | |
811 | } | |
812 | case 15: | |
813 | { | |
814 | guint32 pixel = 0; | |
815 | switch (b_o) | |
b5f01ae0 | 816 | { |
feac7937 VS |
817 | case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break; |
818 | case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
819 | case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
820 | case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
821 | case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
822 | case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
b5f01ae0 | 823 | } |
feac7937 VS |
824 | gdk_image_put_pixel( data_image, x, y, pixel ); |
825 | break; | |
826 | } | |
827 | case 16: | |
828 | { | |
829 | // I actually don't know if for 16-bit displays, it is alway the green | |
830 | // component or the second component which has 6 bits. | |
831 | guint32 pixel = 0; | |
832 | switch (b_o) | |
b5f01ae0 | 833 | { |
feac7937 VS |
834 | case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break; |
835 | case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
836 | case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break; | |
837 | case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
838 | case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
839 | case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
8ab696e0 | 840 | } |
feac7937 VS |
841 | gdk_image_put_pixel( data_image, x, y, pixel ); |
842 | break; | |
843 | } | |
844 | case 32: | |
845 | case 24: | |
846 | { | |
847 | guint32 pixel = 0; | |
848 | switch (b_o) | |
8ab696e0 | 849 | { |
feac7937 VS |
850 | case RGB: pixel = (r << 16) | (g << 8) | b; break; |
851 | case RBG: pixel = (r << 16) | (b << 8) | g; break; | |
852 | case BRG: pixel = (b << 16) | (r << 8) | g; break; | |
853 | case BGR: pixel = (b << 16) | (g << 8) | r; break; | |
854 | case GRB: pixel = (g << 16) | (r << 8) | b; break; | |
855 | case GBR: pixel = (g << 16) | (b << 8) | r; break; | |
b5f01ae0 | 856 | } |
feac7937 | 857 | gdk_image_put_pixel( data_image, x, y, pixel ); |
902725ee | 858 | break; |
b5f01ae0 | 859 | } |
feac7937 VS |
860 | default: break; |
861 | } | |
862 | } // for | |
863 | } // for | |
b5f01ae0 | 864 | |
feac7937 | 865 | // Blit picture |
b5f01ae0 | 866 | |
feac7937 | 867 | GdkGC *data_gc = gdk_gc_new( GetPixmap() ); |
b5f01ae0 | 868 | |
feac7937 | 869 | gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height ); |
b5f01ae0 | 870 | |
feac7937 VS |
871 | gdk_image_destroy( data_image ); |
872 | gdk_gc_unref( data_gc ); | |
b5f01ae0 | 873 | |
feac7937 | 874 | // Blit mask |
b5f01ae0 | 875 | |
feac7937 VS |
876 | if (image.HasMask()) |
877 | { | |
878 | GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() ); | |
b5f01ae0 | 879 | |
feac7937 | 880 | gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height ); |
b5f01ae0 | 881 | |
feac7937 VS |
882 | gdk_image_destroy( mask_image ); |
883 | gdk_gc_unref( mask_gc ); | |
b5f01ae0 VS |
884 | } |
885 | ||
feac7937 | 886 | return true; |
b5f01ae0 VS |
887 | } |
888 | ||
feac7937 VS |
889 | wxImage wxBitmap::ConvertToImage() const |
890 | { | |
891 | wxImage image; | |
c2fa61e8 | 892 | |
feac7937 | 893 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
902725ee | 894 | |
feac7937 VS |
895 | image.Create(GetWidth(), GetHeight()); |
896 | unsigned char *data = image.GetData(); | |
2eefae6e | 897 | |
b5f01ae0 VS |
898 | if (!data) |
899 | { | |
b5f01ae0 VS |
900 | wxFAIL_MSG( wxT("couldn't create image") ); |
901 | return wxNullImage; | |
902 | } | |
903 | ||
3cbab641 MR |
904 | // the colour used as transparent one in wxImage and the one it is |
905 | // replaced with when it really occurs in the bitmap | |
906 | static const int MASK_RED = 1; | |
907 | static const int MASK_GREEN = 2; | |
908 | static const int MASK_BLUE = 3; | |
909 | static const int MASK_BLUE_REPLACEMENT = 2; | |
b5f01ae0 | 910 | |
3cbab641 | 911 | GdkImage *gdk_image = (GdkImage*) NULL; |
b5f01ae0 | 912 | |
3cbab641 MR |
913 | if (HasPixmap()) |
914 | { | |
915 | gdk_image = gdk_image_get( GetPixmap(), | |
916 | 0, 0, | |
917 | GetWidth(), GetHeight() ); | |
918 | } | |
919 | else if (GetBitmap()) | |
920 | { | |
921 | gdk_image = gdk_image_get( GetBitmap(), | |
922 | 0, 0, | |
923 | GetWidth(), GetHeight() ); | |
b5f01ae0 | 924 | } |
feac7937 | 925 | else |
b5f01ae0 | 926 | { |
3cbab641 MR |
927 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); |
928 | } | |
b5f01ae0 | 929 | |
3cbab641 | 930 | wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") ); |
feac7937 | 931 | |
3cbab641 MR |
932 | GdkImage *gdk_image_mask = (GdkImage*) NULL; |
933 | if (GetMask()) | |
934 | { | |
935 | gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(), | |
936 | 0, 0, | |
937 | GetWidth(), GetHeight() ); | |
feac7937 | 938 | |
3cbab641 MR |
939 | image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE ); |
940 | } | |
feac7937 | 941 | |
3cbab641 MR |
942 | int bpp = -1; |
943 | int red_shift_right = 0; | |
944 | int green_shift_right = 0; | |
945 | int blue_shift_right = 0; | |
946 | int red_shift_left = 0; | |
947 | int green_shift_left = 0; | |
948 | int blue_shift_left = 0; | |
949 | bool use_shift = false; | |
feac7937 | 950 | |
3cbab641 MR |
951 | if (GetPixmap()) |
952 | { | |
953 | GdkVisual *visual = gdk_window_get_visual( GetPixmap() ); | |
954 | if (visual == NULL) | |
955 | visual = wxTheApp->GetGdkVisual(); | |
956 | ||
957 | bpp = visual->depth; | |
958 | if (bpp == 16) | |
959 | bpp = visual->red_prec + visual->green_prec + visual->blue_prec; | |
960 | red_shift_right = visual->red_shift; | |
961 | red_shift_left = 8-visual->red_prec; | |
962 | green_shift_right = visual->green_shift; | |
963 | green_shift_left = 8-visual->green_prec; | |
964 | blue_shift_right = visual->blue_shift; | |
965 | blue_shift_left = 8-visual->blue_prec; | |
966 | ||
967 | use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR); | |
968 | } | |
969 | if (GetBitmap()) | |
970 | { | |
971 | bpp = 1; | |
972 | } | |
feac7937 VS |
973 | |
974 | ||
3cbab641 | 975 | GdkColormap *cmap = gtk_widget_get_default_colormap(); |
feac7937 | 976 | |
3cbab641 MR |
977 | long pos = 0; |
978 | for (int j = 0; j < GetHeight(); j++) | |
979 | { | |
980 | for (int i = 0; i < GetWidth(); i++) | |
feac7937 | 981 | { |
3cbab641 MR |
982 | wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j ); |
983 | if (bpp == 1) | |
8ab696e0 | 984 | { |
3cbab641 | 985 | if (pixel == 0) |
8ab696e0 | 986 | { |
3cbab641 MR |
987 | data[pos] = 0; |
988 | data[pos+1] = 0; | |
989 | data[pos+2] = 0; | |
2eefae6e | 990 | } |
feac7937 | 991 | else |
2eefae6e | 992 | { |
3cbab641 MR |
993 | data[pos] = 255; |
994 | data[pos+1] = 255; | |
995 | data[pos+2] = 255; | |
b5f01ae0 | 996 | } |
3cbab641 MR |
997 | } |
998 | else if (use_shift) | |
999 | { | |
1000 | data[pos] = (pixel >> red_shift_right) << red_shift_left; | |
1001 | data[pos+1] = (pixel >> green_shift_right) << green_shift_left; | |
1002 | data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left; | |
1003 | } | |
1004 | else if (cmap->colors) | |
1005 | { | |
1006 | data[pos] = cmap->colors[pixel].red >> 8; | |
1007 | data[pos+1] = cmap->colors[pixel].green >> 8; | |
1008 | data[pos+2] = cmap->colors[pixel].blue >> 8; | |
1009 | } | |
1010 | else | |
1011 | { | |
1012 | wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") ); | |
1013 | } | |
b5f01ae0 | 1014 | |
3cbab641 MR |
1015 | if (gdk_image_mask) |
1016 | { | |
1017 | int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j ); | |
1018 | if (mask_pixel == 0) | |
feac7937 | 1019 | { |
3cbab641 MR |
1020 | data[pos] = MASK_RED; |
1021 | data[pos+1] = MASK_GREEN; | |
1022 | data[pos+2] = MASK_BLUE; | |
1023 | } | |
1024 | else if ( data[pos] == MASK_RED && | |
1025 | data[pos+1] == MASK_GREEN && | |
1026 | data[pos+2] == MASK_BLUE ) | |
1027 | { | |
1028 | data[pos+2] = MASK_BLUE_REPLACEMENT; | |
feac7937 | 1029 | } |
feac7937 | 1030 | } |
b5f01ae0 | 1031 | |
3cbab641 MR |
1032 | pos += 3; |
1033 | } | |
feac7937 | 1034 | } |
b5f01ae0 | 1035 | |
3cbab641 MR |
1036 | gdk_image_destroy( gdk_image ); |
1037 | if (gdk_image_mask) gdk_image_destroy( gdk_image_mask ); | |
1038 | ||
b5f01ae0 VS |
1039 | return image; |
1040 | } | |
1041 | ||
4611dd06 | 1042 | wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type ) |
c801d85f | 1043 | { |
fd0eed64 | 1044 | LoadFile( filename, type ); |
ff7b1510 | 1045 | } |
c801d85f | 1046 | |
debe6624 | 1047 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth)) |
6f65e337 | 1048 | { |
3ebcd89d VZ |
1049 | if ( width > 0 && height > 0 ) |
1050 | { | |
1051 | m_refData = new wxBitmapRefData(); | |
6f65e337 | 1052 | |
3ebcd89d VZ |
1053 | M_BMPDATA->m_mask = (wxMask *) NULL; |
1054 | M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data | |
1055 | ( | |
1056 | wxGetRootWindow()->window, | |
1057 | (gchar *) bits, | |
1058 | width, | |
1059 | height | |
1060 | ); | |
1061 | M_BMPDATA->m_width = width; | |
1062 | M_BMPDATA->m_height = height; | |
1063 | M_BMPDATA->m_bpp = 1; | |
6f65e337 | 1064 | |
3ebcd89d VZ |
1065 | wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") ); |
1066 | } | |
6f65e337 | 1067 | } |
8bbe427f VZ |
1068 | |
1069 | wxBitmap::~wxBitmap() | |
c801d85f | 1070 | { |
ff7b1510 | 1071 | } |
8bbe427f | 1072 | |
f6bcfd97 | 1073 | bool wxBitmap::operator == ( const wxBitmap& bmp ) const |
c801d85f | 1074 | { |
8bbe427f | 1075 | return m_refData == bmp.m_refData; |
ff7b1510 | 1076 | } |
8bbe427f | 1077 | |
f6bcfd97 | 1078 | bool wxBitmap::operator != ( const wxBitmap& bmp ) const |
c801d85f | 1079 | { |
8bbe427f | 1080 | return m_refData != bmp.m_refData; |
ff7b1510 | 1081 | } |
8bbe427f | 1082 | |
91b8de8d | 1083 | bool wxBitmap::Ok() const |
c801d85f | 1084 | { |
902725ee | 1085 | return (m_refData != NULL) && |
3cbab641 | 1086 | (M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap); |
ff7b1510 | 1087 | } |
8bbe427f | 1088 | |
91b8de8d | 1089 | int wxBitmap::GetHeight() const |
c801d85f | 1090 | { |
223d09f6 | 1091 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
e55ad60e | 1092 | |
fd0eed64 | 1093 | return M_BMPDATA->m_height; |
ff7b1510 | 1094 | } |
c801d85f | 1095 | |
91b8de8d | 1096 | int wxBitmap::GetWidth() const |
c801d85f | 1097 | { |
223d09f6 | 1098 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 1099 | |
fd0eed64 | 1100 | return M_BMPDATA->m_width; |
ff7b1510 | 1101 | } |
c801d85f | 1102 | |
91b8de8d | 1103 | int wxBitmap::GetDepth() const |
c801d85f | 1104 | { |
223d09f6 | 1105 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
8bbe427f | 1106 | |
fd0eed64 | 1107 | return M_BMPDATA->m_bpp; |
ff7b1510 | 1108 | } |
c801d85f | 1109 | |
91b8de8d | 1110 | wxMask *wxBitmap::GetMask() const |
c801d85f | 1111 | { |
223d09f6 | 1112 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1113 | |
fd0eed64 | 1114 | return M_BMPDATA->m_mask; |
ff7b1510 | 1115 | } |
c801d85f KB |
1116 | |
1117 | void wxBitmap::SetMask( wxMask *mask ) | |
1118 | { | |
223d09f6 | 1119 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); |
8bbe427f | 1120 | |
fd0eed64 | 1121 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; |
8bbe427f | 1122 | |
fd0eed64 | 1123 | M_BMPDATA->m_mask = mask; |
ff7b1510 | 1124 | } |
c801d85f | 1125 | |
db0aec83 VS |
1126 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
1127 | { | |
1128 | *this = icon; | |
902725ee | 1129 | return true; |
db0aec83 VS |
1130 | } |
1131 | ||
17bec151 RR |
1132 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const |
1133 | { | |
1134 | wxCHECK_MSG( Ok() && | |
13111b2a VZ |
1135 | (rect.x >= 0) && (rect.y >= 0) && |
1136 | (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height), | |
17bec151 | 1137 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); |
13111b2a | 1138 | |
17bec151 RR |
1139 | wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp ); |
1140 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
13111b2a | 1141 | |
3cbab641 | 1142 | if (ret.GetPixmap()) |
17bec151 | 1143 | { |
3cbab641 MR |
1144 | GdkGC *gc = gdk_gc_new( ret.GetPixmap() ); |
1145 | gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
1146 | gdk_gc_destroy( gc ); | |
17bec151 RR |
1147 | } |
1148 | else | |
1149 | { | |
3cbab641 MR |
1150 | GdkGC *gc = gdk_gc_new( ret.GetBitmap() ); |
1151 | GdkColor col; | |
1152 | col.pixel = 0xFFFFFF; | |
1153 | gdk_gc_set_foreground( gc, &col ); | |
1154 | col.pixel = 0; | |
1155 | gdk_gc_set_background( gc, &col ); | |
1156 | gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
1157 | gdk_gc_destroy( gc ); | |
17bec151 | 1158 | } |
13111b2a | 1159 | |
17bec151 RR |
1160 | if (GetMask()) |
1161 | { | |
1162 | wxMask *mask = new wxMask; | |
c2fa61e8 | 1163 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 ); |
13111b2a | 1164 | |
17bec151 | 1165 | GdkGC *gc = gdk_gc_new( mask->m_bitmap ); |
f9ebac93 RR |
1166 | GdkColor col; |
1167 | col.pixel = 0xFFFFFF; | |
1168 | gdk_gc_set_foreground( gc, &col ); | |
1169 | col.pixel = 0; | |
1170 | gdk_gc_set_background( gc, &col ); | |
1171 | 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 |
1172 | gdk_gc_destroy( gc ); |
1173 | ||
1174 | ret.SetMask( mask ); | |
17bec151 | 1175 | } |
13111b2a | 1176 | |
17bec151 RR |
1177 | return ret; |
1178 | } | |
1179 | ||
4611dd06 | 1180 | bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const |
c801d85f | 1181 | { |
902725ee | 1182 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); |
8bbe427f | 1183 | |
b75dd496 | 1184 | // Try to save the bitmap via wxImage handlers: |
fd0eed64 | 1185 | { |
368d59f0 | 1186 | wxImage image = ConvertToImage(); |
284b4c88 | 1187 | if (image.Ok()) return image.SaveFile( name, type ); |
fd0eed64 | 1188 | } |
8bbe427f | 1189 | |
902725ee | 1190 | return false; |
ff7b1510 | 1191 | } |
c801d85f | 1192 | |
4611dd06 | 1193 | bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type ) |
c801d85f | 1194 | { |
fd0eed64 | 1195 | UnRef(); |
8bbe427f | 1196 | |
3ebcd89d | 1197 | if (!wxFileExists(name)) |
902725ee | 1198 | return false; |
8bbe427f | 1199 | |
005f5d18 | 1200 | GdkVisual *visual = wxTheApp->GetGdkVisual(); |
c2fa61e8 | 1201 | |
fd0eed64 RR |
1202 | if (type == wxBITMAP_TYPE_XPM) |
1203 | { | |
1204 | m_refData = new wxBitmapRefData(); | |
8bbe427f | 1205 | |
fd0eed64 | 1206 | GdkBitmap *mask = (GdkBitmap*) NULL; |
8bbe427f | 1207 | |
3ebcd89d VZ |
1208 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm |
1209 | ( | |
1210 | wxGetRootWindow()->window, | |
1211 | &mask, | |
1212 | NULL, | |
1213 | name.fn_str() | |
1214 | ); | |
8bbe427f | 1215 | |
fd0eed64 RR |
1216 | if (mask) |
1217 | { | |
1218 | M_BMPDATA->m_mask = new wxMask(); | |
1219 | M_BMPDATA->m_mask->m_bitmap = mask; | |
1220 | } | |
8bbe427f | 1221 | |
fd0eed64 | 1222 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); |
c2fa61e8 | 1223 | |
103aab26 | 1224 | M_BMPDATA->m_bpp = visual->depth; |
fd0eed64 | 1225 | } |
b75dd496 | 1226 | else // try if wxImage can load it |
fd0eed64 RR |
1227 | { |
1228 | wxImage image; | |
3ebcd89d | 1229 | if ( !image.LoadFile( name, type ) || !image.Ok() ) |
902725ee | 1230 | return false; |
3ebcd89d VZ |
1231 | |
1232 | *this = wxBitmap(image); | |
fd0eed64 | 1233 | } |
8bbe427f | 1234 | |
902725ee | 1235 | return true; |
ff7b1510 | 1236 | } |
8bbe427f | 1237 | |
0b04c4e0 | 1238 | #if wxUSE_PALETTE |
91b8de8d | 1239 | wxPalette *wxBitmap::GetPalette() const |
c801d85f | 1240 | { |
3ebcd89d VZ |
1241 | if (!Ok()) |
1242 | return (wxPalette *) NULL; | |
8bbe427f | 1243 | |
fd0eed64 | 1244 | return M_BMPDATA->m_palette; |
ff7b1510 | 1245 | } |
c801d85f | 1246 | |
4611dd06 RR |
1247 | void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette)) |
1248 | { | |
1249 | // TODO | |
1250 | } | |
0b04c4e0 | 1251 | #endif // wxUSE_PALETTE |
4611dd06 | 1252 | |
4bc67cc5 RR |
1253 | void wxBitmap::SetHeight( int height ) |
1254 | { | |
3ebcd89d VZ |
1255 | if (!m_refData) |
1256 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1257 | |
1258 | M_BMPDATA->m_height = height; | |
1259 | } | |
1260 | ||
1261 | void wxBitmap::SetWidth( int width ) | |
1262 | { | |
3ebcd89d VZ |
1263 | if (!m_refData) |
1264 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1265 | |
1266 | M_BMPDATA->m_width = width; | |
1267 | } | |
1268 | ||
1269 | void wxBitmap::SetDepth( int depth ) | |
1270 | { | |
3ebcd89d VZ |
1271 | if (!m_refData) |
1272 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1273 | |
1274 | M_BMPDATA->m_bpp = depth; | |
1275 | } | |
1276 | ||
1277 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
1278 | { | |
3ebcd89d VZ |
1279 | if (!m_refData) |
1280 | m_refData = new wxBitmapRefData(); | |
4bc67cc5 RR |
1281 | |
1282 | M_BMPDATA->m_pixmap = pixmap; | |
1283 | } | |
1284 | ||
82ea63e6 RR |
1285 | void wxBitmap::SetBitmap( GdkPixmap *bitmap ) |
1286 | { | |
3ebcd89d VZ |
1287 | if (!m_refData) |
1288 | m_refData = new wxBitmapRefData(); | |
82ea63e6 RR |
1289 | |
1290 | M_BMPDATA->m_bitmap = bitmap; | |
1291 | } | |
1292 | ||
91b8de8d | 1293 | GdkPixmap *wxBitmap::GetPixmap() const |
c801d85f | 1294 | { |
223d09f6 | 1295 | wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1296 | |
fd0eed64 | 1297 | return M_BMPDATA->m_pixmap; |
ff7b1510 | 1298 | } |
8bbe427f | 1299 | |
feac7937 VS |
1300 | bool wxBitmap::HasPixmap() const |
1301 | { | |
1302 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
1303 | ||
1304 | return M_BMPDATA->m_pixmap != NULL; | |
1305 | } | |
1306 | ||
91b8de8d | 1307 | GdkBitmap *wxBitmap::GetBitmap() const |
6f65e337 | 1308 | { |
223d09f6 | 1309 | wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") ); |
8bbe427f | 1310 | |
fd0eed64 | 1311 | return M_BMPDATA->m_bitmap; |
ff7b1510 | 1312 | } |
feac7937 | 1313 | |
284f2b59 RR |
1314 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) |
1315 | { | |
284f2b59 | 1316 | return NULL; |
284f2b59 RR |
1317 | } |
1318 | ||
17a1ebd1 | 1319 | void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data)) |
284f2b59 RR |
1320 | { |
1321 | } | |
1322 | ||
0ff2a74d | 1323 | |
902725ee | 1324 | bool wxBitmap::HasAlpha() const |
0ff2a74d | 1325 | { |
0ff2a74d | 1326 | return false; |
0ff2a74d RR |
1327 | } |
1328 | ||
1329 | void wxBitmap::UseAlpha() | |
902725ee | 1330 | { |
0ff2a74d RR |
1331 | } |
1332 | ||
4b61c88d RR |
1333 | //----------------------------------------------------------------------------- |
1334 | // wxBitmapHandler | |
1335 | //----------------------------------------------------------------------------- | |
1336 | ||
1337 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler,wxBitmapHandlerBase) | |
1338 | ||
1339 | wxBitmapHandler::~wxBitmapHandler() | |
1340 | { | |
1341 | } | |
1342 | ||
17a1ebd1 VZ |
1343 | bool wxBitmapHandler::Create(wxBitmap * WXUNUSED(bitmap), |
1344 | void * WXUNUSED(data), | |
1345 | long WXUNUSED(type), | |
1346 | int WXUNUSED(width), | |
1347 | int WXUNUSED(height), | |
1348 | int WXUNUSED(depth)) | |
4b61c88d | 1349 | { |
17a1ebd1 VZ |
1350 | wxFAIL_MSG( _T("not implemented") ); |
1351 | ||
902725ee | 1352 | return false; |
4b61c88d RR |
1353 | } |
1354 | ||
17a1ebd1 VZ |
1355 | bool wxBitmapHandler::LoadFile(wxBitmap * WXUNUSED(bitmap), |
1356 | const wxString& WXUNUSED(name), | |
1357 | long WXUNUSED(flags), | |
1358 | int WXUNUSED(desiredWidth), | |
1359 | int WXUNUSED(desiredHeight)) | |
4b61c88d | 1360 | { |
17a1ebd1 VZ |
1361 | wxFAIL_MSG( _T("not implemented") ); |
1362 | ||
902725ee | 1363 | return false; |
4b61c88d RR |
1364 | } |
1365 | ||
17a1ebd1 VZ |
1366 | bool wxBitmapHandler::SaveFile(const wxBitmap * WXUNUSED(bitmap), |
1367 | const wxString& WXUNUSED(name), | |
1368 | int WXUNUSED(type), | |
1369 | const wxPalette * WXUNUSED(palette)) | |
4b61c88d | 1370 | { |
17a1ebd1 VZ |
1371 | wxFAIL_MSG( _T("not implemented") ); |
1372 | ||
902725ee | 1373 | return false; |
4b61c88d RR |
1374 | } |
1375 | ||
1376 | /* static */ void wxBitmap::InitStandardHandlers() | |
1377 | { | |
1378 | // TODO: Insert handler based on GdkPixbufs handler later | |
1379 | } |