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