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