]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bitmap.cpp
Build fix in wxLog
[wxWidgets.git] / src / gtk1 / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
11 #pragma implementation "bitmap.h"
12 #endif
13
14 #include "wx/bitmap.h"
15 #include "wx/icon.h"
16 #include "wx/filefn.h"
17 #include "gdk/gdkprivate.h"
18 #include "gdk/gdkx.h"
19
20 //-----------------------------------------------------------------------------
21 // wxMask
22 //-----------------------------------------------------------------------------
23
24 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
25
26 wxMask::wxMask(void)
27 {
28 m_bitmap = (GdkBitmap *) NULL;
29 }
30
31 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), const wxColour& WXUNUSED(colour) )
32 {
33 }
34
35 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), int WXUNUSED(paletteIndex) )
36 {
37 }
38
39 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap) )
40 {
41 }
42
43 wxMask::~wxMask(void)
44 {
45 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
46 }
47
48 GdkBitmap *wxMask::GetBitmap(void) const
49 {
50 return m_bitmap;
51 }
52
53 //-----------------------------------------------------------------------------
54 // wxBitmap
55 //-----------------------------------------------------------------------------
56
57 class wxBitmapRefData: public wxObjectRefData
58 {
59 public:
60 wxBitmapRefData(void);
61 ~wxBitmapRefData(void);
62
63 GdkPixmap *m_pixmap;
64 GdkBitmap *m_bitmap;
65 wxMask *m_mask;
66 int m_width;
67 int m_height;
68 int m_bpp;
69 wxPalette *m_palette;
70 };
71
72 wxBitmapRefData::wxBitmapRefData(void)
73 {
74 m_pixmap = (GdkPixmap *) NULL;
75 m_bitmap = (GdkBitmap *) NULL;
76 m_mask = (wxMask *) NULL;
77 m_width = 0;
78 m_height = 0;
79 m_bpp = 0;
80 m_palette = (wxPalette *) NULL;
81 }
82
83 wxBitmapRefData::~wxBitmapRefData(void)
84 {
85 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
86 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
87 if (m_mask) delete m_mask;
88 if (m_palette) delete m_palette;
89 }
90
91 //-----------------------------------------------------------------------------
92
93 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
94
95 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
96
97 wxBitmap::wxBitmap(void)
98 {
99 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
100 }
101
102 wxBitmap::wxBitmap( int width, int height, int depth )
103 {
104 wxCHECK_RET( (width > 0) && (height > 0), "invalid bitmap size" )
105 wxCHECK_RET( (depth > 0) || (depth == -1), "invalid bitmap depth" )
106
107 m_refData = new wxBitmapRefData();
108
109 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
110
111 M_BMPDATA->m_mask = (wxMask *) NULL;
112 M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth );
113 M_BMPDATA->m_width = width;
114 M_BMPDATA->m_height = height;
115 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
116
117 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
118 }
119
120 wxBitmap::wxBitmap( const char **bits )
121 {
122 wxCHECK_RET( bits != NULL, "invalid bitmap data" )
123
124 m_refData = new wxBitmapRefData();
125
126 GdkBitmap *mask = (GdkBitmap*) NULL;
127 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
128
129 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
130
131 if (mask)
132 {
133 M_BMPDATA->m_mask = new wxMask();
134 M_BMPDATA->m_mask->m_bitmap = mask;
135 }
136
137 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
138
139 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
140 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
141 }
142
143 wxBitmap::wxBitmap( char **bits )
144 {
145 wxCHECK_RET( bits != NULL, "invalid bitmap data" )
146
147 m_refData = new wxBitmapRefData();
148
149 GdkBitmap *mask = (GdkBitmap*) NULL;
150 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
151
152 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
153
154 if (mask)
155 {
156 M_BMPDATA->m_mask = new wxMask();
157 M_BMPDATA->m_mask->m_bitmap = mask;
158 }
159
160 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
161
162 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
163 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
164 }
165
166 wxBitmap::wxBitmap( const wxBitmap& bmp )
167 {
168 Ref( bmp );
169
170 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
171 }
172
173 wxBitmap::wxBitmap( const wxBitmap* bmp )
174 {
175 if (bmp) Ref( *bmp );
176
177 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
178 }
179
180 wxBitmap::wxBitmap( const wxString &filename, int type )
181 {
182 LoadFile( filename, type );
183
184 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
185 }
186
187 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
188 {
189 m_refData = new wxBitmapRefData();
190
191 M_BMPDATA->m_mask = (wxMask *) NULL;
192 M_BMPDATA->m_bitmap =
193 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
194 M_BMPDATA->m_width = width;
195 M_BMPDATA->m_height = height;
196 M_BMPDATA->m_bpp = 1;
197
198 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
199 }
200
201 wxBitmap::~wxBitmap(void)
202 {
203 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
204 }
205
206 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
207 {
208 if (*this == bmp) return (*this);
209 Ref( bmp );
210 return *this;
211 }
212
213 bool wxBitmap::operator == ( const wxBitmap& bmp )
214 {
215 return m_refData == bmp.m_refData;
216 }
217
218 bool wxBitmap::operator != ( const wxBitmap& bmp )
219 {
220 return m_refData != bmp.m_refData;
221 }
222
223 bool wxBitmap::Ok(void) const
224 {
225 return (m_refData != NULL);
226 }
227
228 int wxBitmap::GetHeight(void) const
229 {
230 if (!Ok())
231 {
232 wxFAIL_MSG( "invalid bitmap" );
233 return -1;
234 }
235
236 return M_BMPDATA->m_height;
237 }
238
239 int wxBitmap::GetWidth(void) const
240 {
241 if (!Ok())
242 {
243 wxFAIL_MSG( "invalid bitmap" );
244 return -1;
245 }
246
247 return M_BMPDATA->m_width;
248 }
249
250 int wxBitmap::GetDepth(void) const
251 {
252 if (!Ok())
253 {
254 wxFAIL_MSG( "invalid bitmap" );
255 return -1;
256 }
257
258 return M_BMPDATA->m_bpp;
259 }
260
261 void wxBitmap::SetHeight( int height )
262 {
263 if (!Ok()) return;
264
265 wxFAIL_MSG( "wxBitmap::SetHeight not implemented" );
266
267 M_BMPDATA->m_height = height;
268 }
269
270 void wxBitmap::SetWidth( int width )
271 {
272 if (!Ok()) return;
273
274 wxFAIL_MSG( "wxBitmap::SetWidth not implemented" );
275
276 M_BMPDATA->m_width = width;
277 }
278
279 void wxBitmap::SetDepth( int depth )
280 {
281 if (!Ok()) return;
282
283 wxFAIL_MSG( "wxBitmap::SetDepth not implemented" );
284
285 M_BMPDATA->m_bpp = depth;
286 }
287
288 wxMask *wxBitmap::GetMask(void) const
289 {
290 if (!Ok())
291 {
292 wxFAIL_MSG( "invalid bitmap" );
293 return (wxMask *) NULL;
294 }
295
296 return M_BMPDATA->m_mask;
297 }
298
299 void wxBitmap::SetMask( wxMask *mask )
300 {
301 if (!Ok())
302 {
303 wxFAIL_MSG( "invalid bitmap" );
304 return;
305 }
306
307 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
308
309 M_BMPDATA->m_mask = mask;
310 }
311
312 bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
313 {
314 if (!Ok())
315 {
316 wxFAIL_MSG( "invalid bitmap" );
317 return FALSE;
318 }
319
320 if (type == wxBITMAP_TYPE_PNG)
321 {
322 wxImage image = ConvertToImage();
323 if (image.Ok()) return image.SaveFile( name, type );
324 }
325
326 return FALSE;
327 }
328
329 bool wxBitmap::LoadFile( const wxString &name, int type )
330 {
331 UnRef();
332
333 if (!wxFileExists(name)) return FALSE;
334
335 if (type == wxBITMAP_TYPE_XPM)
336 {
337 m_refData = new wxBitmapRefData();
338
339 GdkBitmap *mask = (GdkBitmap*) NULL;
340 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
341
342 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name );
343
344 if (mask)
345 {
346 M_BMPDATA->m_mask = new wxMask();
347 M_BMPDATA->m_mask->m_bitmap = mask;
348 }
349
350 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
351 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
352 }
353 else if (type == wxBITMAP_TYPE_PNG)
354 {
355 wxImage image;
356 image.LoadFile( name, type );
357 if (image.Ok()) *this = wxBitmap( image );
358 }
359 else if (type == wxBITMAP_TYPE_BMP)
360 {
361 wxImage image;
362 image.LoadFile( name, type );
363 if (image.Ok()) *this = wxBitmap( image );
364 }
365 else
366 return FALSE;
367
368 return TRUE;
369 }
370
371 wxPalette *wxBitmap::GetPalette(void) const
372 {
373 if (!Ok()) return (wxPalette *) NULL;
374 return M_BMPDATA->m_palette;
375 }
376
377 GdkPixmap *wxBitmap::GetPixmap(void) const
378 {
379 if (!Ok())
380 {
381 wxFAIL_MSG( "invalid bitmap" );
382 return (GdkPixmap *) NULL;
383 }
384
385 return M_BMPDATA->m_pixmap;
386 }
387
388 GdkBitmap *wxBitmap::GetBitmap(void) const
389 {
390 if (!Ok())
391 {
392 wxFAIL_MSG( "invalid bitmap" );
393 return (GdkBitmap *) NULL;
394 }
395
396 return M_BMPDATA->m_bitmap;
397 }
398
399 wxBitmap::wxBitmap( const wxImage &image )
400 {
401 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
402
403 if (!image.Ok()) return;
404
405 m_refData = new wxBitmapRefData();
406
407 M_BMPDATA->m_height = image.GetHeight();
408 M_BMPDATA->m_width = image.GetWidth();
409 int width = image.GetWidth();
410 int height = image.GetHeight();
411
412 // Create picture
413
414 GdkImage *data_image =
415 gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height );
416
417 M_BMPDATA->m_pixmap =
418 gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 );
419
420 // Create mask
421
422 GdkImage *mask_image = (GdkImage*) NULL;
423
424 if (image.HasMask())
425 {
426 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
427
428 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
429
430 M_BMPDATA->m_mask = new wxMask();
431 M_BMPDATA->m_mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
432 }
433
434 // Retrieve depth
435
436 GdkVisual *visual = gdk_window_get_visual( M_BMPDATA->m_pixmap );
437 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
438 int bpp = visual->depth;
439 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
440 if (bpp < 8) bpp = 8;
441
442 // Render
443
444 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
445 byte_order b_o = RGB;
446
447 if (bpp >= 24)
448 {
449 GdkVisual *visual = gdk_visual_get_system();
450 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
451 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB;
452 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
453 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
454 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
455 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
456 }
457
458 int r_mask = image.GetMaskRed();
459 int g_mask = image.GetMaskGreen();
460 int b_mask = image.GetMaskBlue();
461
462 unsigned char* data = image.GetData();
463
464 int index = 0;
465 for (int y = 0; y < height; y++)
466 {
467 for (int x = 0; x < width; x++)
468 {
469 int r = data[index];
470 index++;
471 int g = data[index];
472 index++;
473 int b = data[index];
474 index++;
475
476 if (image.HasMask())
477 {
478 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
479 gdk_image_put_pixel( mask_image, x, y, 1 );
480 else
481 gdk_image_put_pixel( mask_image, x, y, 0 );
482 }
483
484 switch (bpp)
485 {
486 case 8:
487 {
488 GdkColormap *cmap = gtk_widget_get_default_colormap();
489 GdkColor *colors = cmap->colors;
490 int max = 3 * (65536);
491 int index = -1;
492
493 for (int i = 0; i < cmap->size; i++)
494 {
495 int rdiff = (r << 8) - colors[i].red;
496 int gdiff = (g << 8) - colors[i].green;
497 int bdiff = (b << 8) - colors[i].blue;
498 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
499 if (sum < max) { index = i; max = sum; }
500 }
501
502 gdk_image_put_pixel( data_image, x, y, index );
503
504 break;
505 }
506 case 15:
507 {
508 guint32 pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
509 gdk_image_put_pixel( data_image, x, y, pixel );
510 break;
511 }
512 case 16:
513 {
514 guint32 pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
515 gdk_image_put_pixel( data_image, x, y, pixel );
516 break;
517 }
518 case 32:
519 case 24:
520 {
521 guint32 pixel = 0;
522 switch (b_o)
523 {
524 case RGB: pixel = (r << 16) | (g << 8) | b; break;
525 case RBG: pixel = (r << 16) | (b << 8) | g; break;
526 case BRG: pixel = (b << 16) | (r << 8) | g; break;
527 case BGR: pixel = (b << 16) | (g << 8) | r; break;
528 case GRB: pixel = (g << 16) | (r << 8) | b; break;
529 case GBR: pixel = (g << 16) | (b << 8) | r; break;
530 }
531 gdk_image_put_pixel( data_image, x, y, pixel );
532 }
533 default: break;
534 }
535 } // for
536 } // for
537
538 // Blit picture
539
540 GdkGC *data_gc = gdk_gc_new( M_BMPDATA->m_pixmap );
541
542 gdk_draw_image( M_BMPDATA->m_pixmap, data_gc, data_image, 0, 0, 0, 0, width, height );
543
544 gdk_image_destroy( data_image );
545 gdk_gc_unref( data_gc );
546
547 // Blit mask
548
549 if (image.HasMask())
550 {
551 GdkGC *mask_gc = gdk_gc_new( M_BMPDATA->m_mask->m_bitmap );
552
553 gdk_draw_image( M_BMPDATA->m_mask->m_bitmap, mask_gc, mask_image, 0, 0, 0, 0, width, height );
554
555 gdk_image_destroy( mask_image );
556 gdk_gc_unref( mask_gc );
557 }
558 }
559
560 wxImage wxBitmap::ConvertToImage() const
561 {
562 wxImage image;
563
564 if (!Ok())
565 {
566 wxFAIL_MSG( "invalid bitmap" );
567 return image;
568 }
569
570 GdkImage *gdk_image = gdk_image_get( M_BMPDATA->m_pixmap, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height );
571
572 if (!gdk_image) return image;
573
574 image.Create( M_BMPDATA->m_width, M_BMPDATA->m_height );
575 char unsigned *data = image.GetData();
576
577 GdkVisual *visual = gdk_window_get_visual( M_BMPDATA->m_pixmap );
578 if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
579 int bpp = visual->depth;
580 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
581
582 GdkColormap *cmap = gtk_widget_get_default_colormap();
583
584 long pos = 0;
585 for (int j = 0; j < M_BMPDATA->m_height; j++)
586 {
587 for (int i = 0; i < M_BMPDATA->m_width; i++)
588 {
589 int pixel = gdk_image_get_pixel( gdk_image, i, j );
590 if (bpp <= 8)
591 {
592 data[pos] = cmap->colors[pixel].red >> 8;
593 data[pos+1] = cmap->colors[pixel].green >> 8;
594 data[pos+2] = cmap->colors[pixel].blue >> 8;
595 } else if (bpp == 15)
596 {
597 data[pos] = (pixel >> 7) & 0xf8;
598 data[pos+1] = (pixel >> 2) & 0xf8;
599 data[pos+2] = (pixel << 3) & 0xf8;
600 } else if (bpp == 16)
601 {
602 data[pos] = (pixel >> 8) & 0xf8;
603 data[pos+1] = (pixel >> 3) & 0xfc;
604 data[pos+2] = (pixel << 3) & 0xf8;
605 } else
606 {
607 data[pos] = (pixel >> 16) & 0xff;
608 data[pos+1] = (pixel >> 8) & 0xff;
609 data[pos+2] = pixel & 0xff;
610 }
611
612 pos += 3;
613 }
614 }
615
616 gdk_image_destroy( gdk_image );
617
618 return image;
619 }
620
621
622