]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bitmap.cpp
It's possible now to save to a PNG. OK, I still
[wxWidgets.git] / src / gtk1 / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // RCS-ID: $Id$
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "bitmap.h"
13 #endif
14
15 #include "wx/bitmap.h"
16 #include "wx/icon.h"
17 #include "gdk/gdkprivate.h"
18
19 #ifdef USE_GDK_IMLIB
20
21 #include "../gdk_imlib/gdk_imlib.h"
22 #include "gdk/gdkx.h" // GDK_DISPLAY
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25
26 #endif
27
28 //-----------------------------------------------------------------------------
29 // wxMask
30 //-----------------------------------------------------------------------------
31
32 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
33
34 wxMask::wxMask(void)
35 {
36 m_bitmap = (GdkBitmap *) NULL;
37 }
38
39 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), const wxColour& WXUNUSED(colour) )
40 {
41 }
42
43 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), int WXUNUSED(paletteIndex) )
44 {
45 }
46
47 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap) )
48 {
49 }
50
51 wxMask::~wxMask(void)
52 {
53 #ifdef USE_GDK_IMLIB
54 // do not delete the mask, gdk_imlib does it for you
55 #else
56 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
57 #endif
58 }
59
60 GdkBitmap *wxMask::GetBitmap(void) const
61 {
62 return m_bitmap;
63 }
64
65 //-----------------------------------------------------------------------------
66 // wxBitmap
67 //-----------------------------------------------------------------------------
68
69 // CMB 20/5/98: added m_bitmap for GdkBitmaps
70 class wxBitmapRefData: public wxObjectRefData
71 {
72 public:
73
74 wxBitmapRefData(void);
75 ~wxBitmapRefData(void);
76
77 GdkPixmap *m_pixmap;
78 GdkBitmap *m_bitmap;
79 wxMask *m_mask;
80 int m_width;
81 int m_height;
82 int m_bpp;
83 #ifdef USE_GDK_IMLIB
84 GdkImlibImage *m_image;
85 #endif
86 wxPalette *m_palette;
87 };
88
89 wxBitmapRefData::wxBitmapRefData(void)
90 {
91 m_pixmap = (GdkPixmap *) NULL;
92 m_bitmap = (GdkBitmap *) NULL;
93 m_mask = (wxMask *) NULL;
94 m_width = 0;
95 m_height = 0;
96 m_bpp = 0;
97 m_palette = (wxPalette *) NULL;
98 #ifdef USE_GDK_IMLIB
99 m_image = (GdkImlibImage *) NULL;
100 #endif
101 }
102
103 wxBitmapRefData::~wxBitmapRefData(void)
104 {
105 #ifdef USE_GDK_IMLIB
106 if (m_pixmap) gdk_imlib_free_pixmap( m_pixmap );
107 if (m_image) gdk_imlib_kill_image( m_image );
108 #else
109 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
110 #endif
111 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
112 if (m_mask) delete m_mask;
113 if (m_palette) delete m_palette;
114 }
115
116 //-----------------------------------------------------------------------------
117
118 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
119
120 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
121
122 wxBitmap::wxBitmap(void)
123 {
124 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
125 }
126
127 wxBitmap::wxBitmap( int width, int height, int depth )
128 {
129 m_refData = new wxBitmapRefData();
130 M_BMPDATA->m_mask = (wxMask *) NULL;
131 M_BMPDATA->m_pixmap =
132 gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, width, height, depth );
133 M_BMPDATA->m_width = width;
134 M_BMPDATA->m_height = height;
135 M_BMPDATA->m_bpp = depth;
136
137 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
138 }
139
140 wxBitmap::wxBitmap( char **bits )
141 {
142 m_refData = new wxBitmapRefData();
143
144 #ifndef USE_GDK_IMLIB
145
146 GdkBitmap *mask = NULL;
147
148 M_BMPDATA->m_pixmap =
149 gdk_pixmap_create_from_xpm_d( (GdkWindow*) &gdk_root_parent, &mask, NULL, (gchar **) bits );
150
151 if (mask)
152 {
153 M_BMPDATA->m_mask = new wxMask();
154 M_BMPDATA->m_mask->m_bitmap = mask;
155 }
156
157 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
158
159 #else
160
161 M_BMPDATA->m_image = gdk_imlib_create_image_from_xpm_data( bits );
162 Render();
163
164 #endif
165
166 M_BMPDATA->m_bpp = 24; // ?
167
168 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
169 }
170
171 wxBitmap::wxBitmap( const wxBitmap& bmp )
172 {
173 Ref( bmp );
174
175 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
176 }
177
178 wxBitmap::wxBitmap( const wxBitmap* bmp )
179 {
180 if (bmp) Ref( *bmp );
181
182 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
183 }
184
185 wxBitmap::wxBitmap( const wxString &filename, int type )
186 {
187 LoadFile( filename, type );
188
189 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
190 }
191
192 // CMB 15/5/98: add constructor for xbm bitmaps
193 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
194 {
195 m_refData = new wxBitmapRefData();
196
197 M_BMPDATA->m_mask = (wxMask *) NULL;
198 M_BMPDATA->m_bitmap =
199 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
200 M_BMPDATA->m_width = width;
201 M_BMPDATA->m_height = height;
202 M_BMPDATA->m_bpp = 1;
203
204 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
205 }
206
207 wxBitmap::~wxBitmap(void)
208 {
209 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
210 }
211
212 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
213 {
214 if (*this == bmp) return (*this);
215 Ref( bmp );
216 return *this;
217 }
218
219 bool wxBitmap::operator == ( const wxBitmap& bmp )
220 {
221 return m_refData == bmp.m_refData;
222 }
223
224 bool wxBitmap::operator != ( const wxBitmap& bmp )
225 {
226 return m_refData != bmp.m_refData;
227 }
228
229 bool wxBitmap::Ok(void) const
230 {
231 wxASSERT_MSG( m_refData != NULL, "invalid bitmap" );
232 return (m_refData != NULL);
233 }
234
235 int wxBitmap::GetHeight(void) const
236 {
237 if (!Ok()) return 0;
238 return M_BMPDATA->m_height;
239 }
240
241 int wxBitmap::GetWidth(void) const
242 {
243 if (!Ok()) return 0;
244 return M_BMPDATA->m_width;
245 }
246
247 int wxBitmap::GetDepth(void) const
248 {
249 if (!Ok()) return 0;
250 return M_BMPDATA->m_bpp;
251 }
252
253 void wxBitmap::SetHeight( int height )
254 {
255 if (!Ok()) return;
256
257 wxFAIL_MSG( "wxBitmap::SetHeight not implemented" );
258
259 M_BMPDATA->m_height = height;
260 }
261
262 void wxBitmap::SetWidth( int width )
263 {
264 if (!Ok()) return;
265
266 wxFAIL_MSG( "wxBitmap::SetWidth not implemented" );
267
268 M_BMPDATA->m_width = width;
269 }
270
271 void wxBitmap::SetDepth( int depth )
272 {
273 if (!Ok()) return;
274
275 wxFAIL_MSG( "wxBitmap::SetDepth not implemented" );
276
277 M_BMPDATA->m_bpp = depth;
278 }
279
280 wxMask *wxBitmap::GetMask(void) const
281 {
282 if (!Ok()) return (wxMask *) NULL;
283
284 return M_BMPDATA->m_mask;
285 }
286
287 void wxBitmap::SetMask( wxMask *mask )
288 {
289 if (!Ok()) return;
290
291 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
292
293 M_BMPDATA->m_mask = mask;
294 }
295
296 void wxBitmap::Resize( int height, int width )
297 {
298 if (!Ok()) return;
299
300 #ifdef USE_GDK_IMLIB
301
302 if (M_BMPDATA->m_bitmap) return; // not supported for bitmaps
303
304 if (!M_BMPDATA->m_image) RecreateImage();
305
306 if (M_BMPDATA->m_pixmap) gdk_imlib_free_pixmap( M_BMPDATA->m_pixmap );
307 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
308
309 GdkImlibImage* image = gdk_imlib_clone_scaled_image( M_BMPDATA->m_image, height, width );
310 gdk_imlib_destroy_image( M_BMPDATA->m_image );
311 M_BMPDATA->m_image = image;
312 M_BMPDATA->m_height = height;
313 M_BMPDATA->m_width = width;
314
315 Render();
316
317 #else
318
319 wxFAIL_MSG( "wxBitmap::Resize not implemented without GdkImlib" );
320
321 #endif
322 }
323
324 bool wxBitmap::SaveFile( const wxString &name, int WXUNUSED(type),
325 wxPalette *WXUNUSED(palette) )
326 {
327 #ifdef USE_GDK_IMLIB
328
329 if (!Ok()) return FALSE;
330
331 if (!M_BMPDATA->m_image) RecreateImage();
332
333 return gdk_imlib_save_image( M_BMPDATA->m_image, WXSTRINGCAST name, (GdkImlibSaveInfo *) NULL );
334
335 #else
336
337 wxFAIL_MSG( "wxBitmap::SaveFile not implemented without GdkImlib" );
338
339 #endif
340
341 return FALSE;
342 }
343
344 bool wxBitmap::LoadFile( const wxString &name, int WXUNUSED(type) )
345 {
346 #ifdef USE_GDK_IMLIB
347
348 UnRef();
349 m_refData = new wxBitmapRefData();
350
351 M_BMPDATA->m_image = gdk_imlib_load_image( WXSTRINGCAST name );
352
353 if (!M_BMPDATA->m_image)
354 {
355 UnRef();
356 return FALSE;
357 }
358
359 Render();
360
361 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
362 M_BMPDATA->m_bpp = 24; // ?
363
364 return TRUE;
365
366 #else
367
368 wxFAIL_MSG( "wxBitmap::LoadFile not implemented without GdkImlib" );
369
370 #endif
371
372 return FALSE;
373 }
374
375 wxPalette *wxBitmap::GetPalette(void) const
376 {
377 if (!Ok()) return (wxPalette *) NULL;
378 return M_BMPDATA->m_palette;
379 }
380
381 GdkPixmap *wxBitmap::GetPixmap(void) const
382 {
383 if (!Ok()) return (GdkPixmap *) NULL;
384
385 // if (!M_BMPDATA->m_image) RecreateImage();
386
387 return M_BMPDATA->m_pixmap;
388 }
389
390 GdkBitmap *wxBitmap::GetBitmap(void) const
391 {
392 if (!Ok()) return (GdkBitmap *) NULL;
393
394 return M_BMPDATA->m_bitmap;
395 }
396
397 void wxBitmap::DestroyImage(void)
398 {
399 if (!Ok()) return;
400
401 if (M_BMPDATA->m_image)
402 {
403 gdk_imlib_destroy_image( M_BMPDATA->m_image );
404 M_BMPDATA->m_image = (GdkImlibImage *) NULL;
405 }
406 }
407
408 void wxBitmap::RecreateImage(void)
409 {
410 if (!Ok()) return;
411
412 #ifdef USE_GDK_IMLIB
413
414 DestroyImage();
415
416 wxCHECK_RET( M_BMPDATA->m_pixmap != NULL, "invalid bitmap" );
417
418 long size = (long)(M_BMPDATA->m_width)*(long)(M_BMPDATA->m_height)*(long)3;
419 unsigned char *data = new unsigned char[size];
420 for (long i = 0; i < size; i++) data[i] = 100;
421
422 GdkImage *image = gdk_image_get( M_BMPDATA->m_pixmap, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height );
423
424 long pos = 0;
425 for (int j = 0; j < M_BMPDATA->m_height; j++)
426 {
427 for (int i = 0; i < M_BMPDATA->m_width; i++)
428 {
429 XColor xcol;
430 xcol.pixel = gdk_image_get_pixel( image, i, j );
431 Colormap cm = ((GdkColormapPrivate*)gdk_imlib_get_colormap())->xcolormap;
432 XQueryColor( gdk_display, cm, &xcol );
433
434 data[pos] = xcol.red;
435 data[pos+1] = xcol.green;
436 data[pos+2] = xcol.blue;
437 pos += 3;
438 }
439 }
440
441 wxCHECK_RET( M_BMPDATA->m_pixmap != NULL, "invalid bitmap" );
442
443 M_BMPDATA->m_image = gdk_imlib_create_image_from_data(
444 data, (unsigned char*)NULL, M_BMPDATA->m_width, M_BMPDATA->m_height );
445
446 delete[] data;
447
448 gdk_image_destroy( image );
449
450 Render();
451
452 #else
453
454 wxFAIL_MSG( "wxBitmap::RecreateImage not implemented without GdkImlib" );
455
456 #endif
457 }
458
459 void wxBitmap::Render(void)
460 {
461 if (!Ok()) return;
462
463 #ifdef USE_GDK_IMLIB
464
465 if (!M_BMPDATA->m_image) RecreateImage();
466
467 if (M_BMPDATA->m_pixmap)
468 {
469 gdk_imlib_free_pixmap( M_BMPDATA->m_pixmap );
470 M_BMPDATA->m_pixmap = (GdkPixmap*) NULL;
471 }
472 if (M_BMPDATA->m_mask)
473 {
474 delete M_BMPDATA->m_mask;
475 M_BMPDATA->m_mask = (wxMask*) NULL;
476 }
477
478 gdk_imlib_render( M_BMPDATA->m_image, M_BMPDATA->m_image->rgb_width, M_BMPDATA->m_image->rgb_height );
479 M_BMPDATA->m_width = M_BMPDATA->m_image->rgb_width;
480 M_BMPDATA->m_height = M_BMPDATA->m_image->rgb_height;
481 M_BMPDATA->m_pixmap = gdk_imlib_move_image( M_BMPDATA->m_image );
482
483 wxCHECK_RET( M_BMPDATA->m_pixmap != NULL, "pixmap rendering failed" )
484
485 GdkBitmap *mask = gdk_imlib_move_mask( M_BMPDATA->m_image );
486 if (mask)
487 {
488 M_BMPDATA->m_mask = new wxMask();
489 M_BMPDATA->m_mask->m_bitmap = mask;
490 }
491
492 #else
493
494 wxFAIL_MSG( "wxBitmap::Render not implemented without GdkImlib" );
495
496 #endif
497 }
498
499