]>
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 "wx/filefn.h"
17 #include "gdk/gdkprivate.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
29 m_bitmap
= (GdkBitmap
*) NULL
;
32 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
), const wxColour
& WXUNUSED(colour
) )
36 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
), int WXUNUSED(paletteIndex
) )
40 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
) )
46 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
49 GdkBitmap
*wxMask::GetBitmap(void) const
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 class wxBitmapRefData
: public wxObjectRefData
61 wxBitmapRefData(void);
62 ~wxBitmapRefData(void);
73 wxBitmapRefData::wxBitmapRefData(void)
75 m_pixmap
= (GdkPixmap
*) NULL
;
76 m_bitmap
= (GdkBitmap
*) NULL
;
77 m_mask
= (wxMask
*) NULL
;
81 m_palette
= (wxPalette
*) NULL
;
84 wxBitmapRefData::~wxBitmapRefData(void)
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
;
92 //-----------------------------------------------------------------------------
94 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
96 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
98 wxBitmap::wxBitmap(void)
100 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
103 wxBitmap::wxBitmap( int width
, int height
, int depth
)
105 wxCHECK_RET( (width
> 0) && (height
> 0), "invalid bitmap size" )
106 wxCHECK_RET( (depth
> 0) || (depth
== -1), "invalid bitmap depth" )
108 m_refData
= new wxBitmapRefData();
110 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
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
;
118 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
121 wxBitmap::wxBitmap( const char **bits
)
123 wxCHECK_RET( bits
!= NULL
, "invalid bitmap data" )
125 m_refData
= new wxBitmapRefData();
127 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
128 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
130 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
134 M_BMPDATA
->m_mask
= new wxMask();
135 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
138 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
140 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
141 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
144 wxBitmap::wxBitmap( char **bits
)
146 wxCHECK_RET( bits
!= NULL
, "invalid bitmap data" )
148 m_refData
= new wxBitmapRefData();
150 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
151 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
153 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
155 wxCHECK_RET( M_BMPDATA
->m_pixmap
, "couldn't create pixmap" );
159 M_BMPDATA
->m_mask
= new wxMask();
160 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
163 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
165 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
166 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
169 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
173 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
176 wxBitmap::wxBitmap( const wxBitmap
* bmp
)
178 if (bmp
) Ref( *bmp
);
180 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
183 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
185 LoadFile( filename
, type
);
187 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
190 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
192 m_refData
= new wxBitmapRefData();
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;
201 wxCHECK_RET( M_BMPDATA
->m_bitmap
, "couldn't create bitmap" );
203 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
206 wxBitmap::~wxBitmap(void)
208 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
211 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
213 if (*this == bmp
) return (*this);
218 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
220 return m_refData
== bmp
.m_refData
;
223 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
225 return m_refData
!= bmp
.m_refData
;
228 bool wxBitmap::Ok(void) const
230 return (m_refData
!= NULL
);
233 int wxBitmap::GetHeight(void) const
235 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
237 return M_BMPDATA
->m_height
;
240 int wxBitmap::GetWidth(void) const
242 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
244 return M_BMPDATA
->m_width
;
247 int wxBitmap::GetDepth(void) const
249 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
251 return M_BMPDATA
->m_bpp
;
254 wxMask
*wxBitmap::GetMask(void) const
256 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, "invalid bitmap" );
258 return M_BMPDATA
->m_mask
;
261 void wxBitmap::SetMask( wxMask
*mask
)
263 wxCHECK_RET( Ok(), "invalid bitmap" );
265 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
267 M_BMPDATA
->m_mask
= mask
;
270 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
272 wxCHECK_MSG( Ok(), FALSE
, "invalid bitmap" );
274 if (type
== wxBITMAP_TYPE_PNG
)
276 wxImage
image( *this );
277 if (image
.Ok()) return image
.SaveFile( name
, type
);
283 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
287 if (!wxFileExists(name
)) return FALSE
;
289 if (type
== wxBITMAP_TYPE_XPM
)
291 m_refData
= new wxBitmapRefData();
293 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
294 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
296 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
);
300 M_BMPDATA
->m_mask
= new wxMask();
301 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
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
;
307 else if (type
== wxBITMAP_TYPE_PNG
)
310 image
.LoadFile( name
, type
);
311 if (image
.Ok()) *this = image
.ConvertToBitmap();
313 else if (type
== wxBITMAP_TYPE_BMP
)
316 image
.LoadFile( name
, type
);
317 if (image
.Ok()) *this = image
.ConvertToBitmap();
325 wxPalette
*wxBitmap::GetPalette(void) const
327 if (!Ok()) return (wxPalette
*) NULL
;
329 return M_BMPDATA
->m_palette
;
332 void wxBitmap::SetHeight( int height
)
334 if (!m_refData
) m_refData
= new wxBitmapRefData();
336 M_BMPDATA
->m_height
= height
;
339 void wxBitmap::SetWidth( int width
)
341 if (!m_refData
) m_refData
= new wxBitmapRefData();
343 M_BMPDATA
->m_width
= width
;
346 void wxBitmap::SetDepth( int depth
)
348 if (!m_refData
) m_refData
= new wxBitmapRefData();
350 M_BMPDATA
->m_bpp
= depth
;
353 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
355 if (!m_refData
) m_refData
= new wxBitmapRefData();
357 M_BMPDATA
->m_pixmap
= pixmap
;
360 GdkPixmap
*wxBitmap::GetPixmap(void) const
362 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, "invalid bitmap" );
364 return M_BMPDATA
->m_pixmap
;
367 GdkBitmap
*wxBitmap::GetBitmap(void) const
369 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, "invalid bitmap" );
371 return M_BMPDATA
->m_bitmap
;
374 //-----------------------------------------------------------------------------
376 //-----------------------------------------------------------------------------
378 wxBitmap
wxImage::ConvertToBitmap() const
382 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
384 int width
= GetWidth();
385 int height
= GetHeight();
387 bitmap
.SetHeight( height
);
388 bitmap
.SetWidth( width
);
392 GdkImage
*data_image
=
393 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
395 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
399 GdkImage
*mask_image
= (GdkImage
*) NULL
;
403 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
405 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
407 wxMask
*mask
= new wxMask();
408 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
410 bitmap
.SetMask( mask
);
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;
423 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
424 byte_order b_o
= RGB
;
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
;
437 int r_mask
= GetMaskRed();
438 int g_mask
= GetMaskGreen();
439 int b_mask
= GetMaskBlue();
441 unsigned char* data
= GetData();
444 for (int y
= 0; y
< height
; y
++)
446 for (int x
= 0; x
< width
; x
++)
457 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
458 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
460 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
467 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
468 GdkColor
*colors
= cmap
->colors
;
469 int max
= 3 * (65536);
472 for (int i
= 0; i
< cmap
->size
; i
++)
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
; }
481 gdk_image_put_pixel( data_image
, x
, y
, index
);
487 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
488 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
493 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
494 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
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;
510 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
519 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
521 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
523 gdk_image_destroy( data_image
);
524 gdk_gc_unref( data_gc
);
530 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
532 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
534 gdk_image_destroy( mask_image
);
535 gdk_gc_unref( mask_gc
);
541 wxImage::wxImage( const wxBitmap
&bitmap
)
543 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
545 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
547 bitmap
.GetWidth(), bitmap
.GetHeight() );
549 wxCHECK_RET( gdk_image
, "couldn't create image" );
551 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
552 char unsigned *data
= GetData();
556 gdk_image_destroy( gdk_image
);
557 wxFAIL_MSG( "couldn't create image" );
561 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
562 if (bitmap
.GetMask())
564 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
566 bitmap
.GetWidth(), bitmap
.GetHeight() );
568 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
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;
576 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
579 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
581 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
583 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
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)
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)
596 data
[pos
] = (pixel
>> 8) & 0xf8;
597 data
[pos
+1] = (pixel
>> 3) & 0xfc;
598 data
[pos
+2] = (pixel
<< 3) & 0xf8;
601 data
[pos
] = (pixel
>> 16) & 0xff;
602 data
[pos
+1] = (pixel
>> 8) & 0xff;
603 data
[pos
+2] = pixel
& 0xff;
608 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
621 gdk_image_destroy( gdk_image
);
622 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);