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