]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "bitmap.h"
14 #include "wx/bitmap.h"
16 #include "gdk/gdkprivate.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
27 m_bitmap
= (GdkBitmap
*) NULL
;
30 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
), const wxColour
& WXUNUSED(colour
) )
34 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
), int WXUNUSED(paletteIndex
) )
38 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
) )
44 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
47 GdkBitmap
*wxMask::GetBitmap(void) const
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 class wxBitmapRefData
: public wxObjectRefData
60 wxBitmapRefData(void);
61 ~wxBitmapRefData(void);
72 wxBitmapRefData::wxBitmapRefData(void)
74 m_pixmap
= (GdkPixmap
*) NULL
;
75 m_bitmap
= (GdkBitmap
*) NULL
;
76 m_mask
= (wxMask
*) NULL
;
80 m_palette
= (wxPalette
*) NULL
;
83 wxBitmapRefData::~wxBitmapRefData(void)
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
;
91 //-----------------------------------------------------------------------------
93 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
95 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
97 wxBitmap::wxBitmap(void)
99 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
102 wxBitmap::wxBitmap( int width
, int height
, int depth
)
104 m_refData
= new wxBitmapRefData();
105 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
106 M_BMPDATA
->m_pixmap
=
107 gdk_pixmap_new( (GdkWindow
*) &gdk_root_parent
, width
, height
, depth
);
108 M_BMPDATA
->m_width
= width
;
109 M_BMPDATA
->m_height
= height
;
110 M_BMPDATA
->m_bpp
= depth
;
112 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
115 wxBitmap::wxBitmap( char **bits
)
117 m_refData
= new wxBitmapRefData();
119 GdkBitmap
*mask
= NULL
;
121 M_BMPDATA
->m_pixmap
=
122 gdk_pixmap_create_from_xpm_d( (GdkWindow
*) &gdk_root_parent
, &mask
, NULL
, (gchar
**) bits
);
126 M_BMPDATA
->m_mask
= new wxMask();
127 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
130 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
132 M_BMPDATA
->m_bpp
= 24; // ?
134 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
137 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
141 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
144 wxBitmap::wxBitmap( const wxBitmap
* bmp
)
146 if (bmp
) Ref( *bmp
);
148 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
151 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
153 LoadFile( filename
, type
);
155 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
158 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
160 m_refData
= new wxBitmapRefData();
162 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
163 M_BMPDATA
->m_bitmap
=
164 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
165 M_BMPDATA
->m_width
= width
;
166 M_BMPDATA
->m_height
= height
;
167 M_BMPDATA
->m_bpp
= 1;
169 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
172 wxBitmap::~wxBitmap(void)
174 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
177 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
179 if (*this == bmp
) return (*this);
184 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
186 return m_refData
== bmp
.m_refData
;
189 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
191 return m_refData
!= bmp
.m_refData
;
194 bool wxBitmap::Ok(void) const
196 return (m_refData
!= NULL
);
199 int wxBitmap::GetHeight(void) const
203 wxFAIL_MSG( "invalid bitmap" );
207 return M_BMPDATA
->m_height
;
210 int wxBitmap::GetWidth(void) const
214 wxFAIL_MSG( "invalid bitmap" );
218 return M_BMPDATA
->m_width
;
221 int wxBitmap::GetDepth(void) const
225 wxFAIL_MSG( "invalid bitmap" );
229 return M_BMPDATA
->m_bpp
;
232 void wxBitmap::SetHeight( int height
)
236 wxFAIL_MSG( "wxBitmap::SetHeight not implemented" );
238 M_BMPDATA
->m_height
= height
;
241 void wxBitmap::SetWidth( int width
)
245 wxFAIL_MSG( "wxBitmap::SetWidth not implemented" );
247 M_BMPDATA
->m_width
= width
;
250 void wxBitmap::SetDepth( int depth
)
254 wxFAIL_MSG( "wxBitmap::SetDepth not implemented" );
256 M_BMPDATA
->m_bpp
= depth
;
259 wxMask
*wxBitmap::GetMask(void) const
263 wxFAIL_MSG( "invalid bitmap" );
264 return (wxMask
*) NULL
;
267 return M_BMPDATA
->m_mask
;
270 void wxBitmap::SetMask( wxMask
*mask
)
274 wxFAIL_MSG( "invalid bitmap" );
278 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
280 M_BMPDATA
->m_mask
= mask
;
283 bool wxBitmap::SaveFile( const wxString
&WXUNUSED(name
), int WXUNUSED(type
),
284 wxPalette
*WXUNUSED(palette
) )
288 wxFAIL_MSG( "invalid bitmap" );
295 bool wxBitmap::LoadFile( const wxString
&WXUNUSED(name
), int WXUNUSED(type
) )
299 wxFAIL_MSG( "invalid bitmap" );
306 wxPalette
*wxBitmap::GetPalette(void) const
308 if (!Ok()) return (wxPalette
*) NULL
;
309 return M_BMPDATA
->m_palette
;
312 GdkPixmap
*wxBitmap::GetPixmap(void) const
316 wxFAIL_MSG( "invalid bitmap" );
317 return (GdkPixmap
*) NULL
;
320 return M_BMPDATA
->m_pixmap
;
323 GdkBitmap
*wxBitmap::GetBitmap(void) const
327 wxFAIL_MSG( "invalid bitmap" );
328 return (GdkBitmap
*) NULL
;
331 return M_BMPDATA
->m_bitmap
;
334 wxBitmap::wxBitmap( const wxImage
&image
)
336 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
338 if (!image
.Ok()) return;
340 m_refData
= new wxBitmapRefData();
342 M_BMPDATA
->m_height
= image
.GetHeight();
343 M_BMPDATA
->m_width
= image
.GetWidth();
344 int width
= image
.GetWidth();
345 int height
= image
.GetHeight();
349 GdkImage
*data_image
=
350 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
352 M_BMPDATA
->m_pixmap
=
353 gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 );
357 GdkImage
*mask_image
= (GdkImage
*) NULL
;
361 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
363 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
365 M_BMPDATA
->m_mask
= new wxMask();
366 M_BMPDATA
->m_mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
371 M_BMPDATA
->m_bpp
= data_image
->depth
;
373 int render_depth
= 8;
374 if (M_BMPDATA
->m_bpp
> 8) render_depth
= M_BMPDATA
->m_bpp
;
378 int r_mask
= image
.GetMaskRed();
379 int g_mask
= image
.GetMaskGreen();
380 int b_mask
= image
.GetMaskBlue();
382 unsigned char* data
= image
.GetData();
385 for (int y
= 0; y
< height
; y
++)
386 for (int x
= 0; x
< width
; x
++)
397 if ((r
== r_mask
) && (b
= b_mask
) && (g
= g_mask
))
398 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
400 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
403 switch (render_depth
)
407 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
408 GdkColor
*colors
= cmap
->colors
;
409 int max
= 3 * (65536);
412 for (int i
= 0; i
< cmap
->size
; i
++)
414 int rdiff
= (r
<< 8) - colors
[i
].red
;
415 int gdiff
= (g
<< 8) - colors
[i
].green
;
416 int bdiff
= (b
<< 8) - colors
[i
].blue
;
417 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
418 if (sum
< max
) { index
= i
; max
= sum
; }
421 gdk_image_put_pixel( data_image
, x
, y
, index
);
427 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
428 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
433 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
434 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
451 GdkGC
*data_gc
= gdk_gc_new( M_BMPDATA
->m_pixmap
);
453 gdk_draw_image( M_BMPDATA
->m_pixmap
, data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
455 gdk_image_destroy( data_image
);
456 gdk_gc_unref( data_gc
);
462 GdkGC
*mask_gc
= gdk_gc_new( M_BMPDATA
->m_mask
->m_bitmap
);
464 gdk_draw_image( M_BMPDATA
->m_mask
->m_bitmap
, mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
466 gdk_image_destroy( mask_image
);
467 gdk_gc_unref( mask_gc
);
472 wxImage
wxBitmap::ConvertToImage() const
478 wxFAIL_MSG( "invalid bitmap" );
482 GdkImage
*gdk_image
= gdk_image_get( M_BMPDATA
->m_pixmap
, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
484 if (!gdk_image
) return image
;
486 image
.Create( M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
487 char unsigned *data
= image
.GetData();
489 int bpp
= gdk_image
->bpp
;
490 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
493 for (int j
= 0; j
< M_BMPDATA
->m_height
; j
++)
495 for (int i
= 0; i
< M_BMPDATA
->m_width
; i
++)
497 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
500 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
501 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
502 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
503 } else if (bpp
== 15)
505 data
[pos
] = (pixel
>> 7) & 0xf8;
506 data
[pos
+1] = (pixel
>> 3) & 0xf8;
507 data
[pos
+2] = (pixel
<< 3) & 0xf8;
508 } else if (bpp
== 16)
510 data
[pos
] = (pixel
>> 8) & 0xf8;
511 data
[pos
+1] = (pixel
>> 3) & 0xfc;
512 data
[pos
+2] = (pixel
<< 3) & 0xf8;
515 data
[pos
] = (pixel
>> 16) & 0xff;
516 data
[pos
+1] = (pixel
>> 8) & 0xff;
517 data
[pos
+2] = pixel
& 0xff;
524 gdk_image_destroy( gdk_image
);