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