]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/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()
75 m_pixmap
= (GdkPixmap
*) NULL
;
76 m_bitmap
= (GdkBitmap
*) NULL
;
77 m_mask
= (wxMask
*) NULL
;
81 m_palette
= (wxPalette
*) NULL
;
84 wxBitmapRefData::~wxBitmapRefData()
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
)
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 wxString
&filename
, int type
)
178 LoadFile( filename
, type
);
180 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
183 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
185 m_refData
= new wxBitmapRefData();
187 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
188 M_BMPDATA
->m_bitmap
=
189 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
190 M_BMPDATA
->m_width
= width
;
191 M_BMPDATA
->m_height
= height
;
192 M_BMPDATA
->m_bpp
= 1;
194 wxCHECK_RET( M_BMPDATA
->m_bitmap
, "couldn't create bitmap" );
196 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
199 wxBitmap::~wxBitmap()
201 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
204 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
206 if (*this == bmp
) return (*this);
211 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
213 return m_refData
== bmp
.m_refData
;
216 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
218 return m_refData
!= bmp
.m_refData
;
221 bool wxBitmap::Ok(void) const
223 return (m_refData
!= NULL
);
226 int wxBitmap::GetHeight(void) const
228 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
230 return M_BMPDATA
->m_height
;
233 int wxBitmap::GetWidth(void) const
235 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
237 return M_BMPDATA
->m_width
;
240 int wxBitmap::GetDepth(void) const
242 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
244 return M_BMPDATA
->m_bpp
;
247 wxMask
*wxBitmap::GetMask(void) const
249 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, "invalid bitmap" );
251 return M_BMPDATA
->m_mask
;
254 void wxBitmap::SetMask( wxMask
*mask
)
256 wxCHECK_RET( Ok(), "invalid bitmap" );
258 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
260 M_BMPDATA
->m_mask
= mask
;
263 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
265 wxCHECK_MSG( Ok(), FALSE
, "invalid bitmap" );
267 if (type
== wxBITMAP_TYPE_PNG
)
269 wxImage
image( *this );
270 if (image
.Ok()) return image
.SaveFile( name
, type
);
276 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
280 if (!wxFileExists(name
)) return FALSE
;
282 if (type
== wxBITMAP_TYPE_XPM
)
284 m_refData
= new wxBitmapRefData();
286 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
287 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
289 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
);
293 M_BMPDATA
->m_mask
= new wxMask();
294 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
297 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
298 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
300 else if (type
== wxBITMAP_TYPE_PNG
)
303 image
.LoadFile( name
, type
);
304 if (image
.Ok()) *this = image
.ConvertToBitmap();
306 else if (type
== wxBITMAP_TYPE_BMP
)
309 image
.LoadFile( name
, type
);
310 if (image
.Ok()) *this = image
.ConvertToBitmap();
318 wxPalette
*wxBitmap::GetPalette(void) const
320 if (!Ok()) return (wxPalette
*) NULL
;
322 return M_BMPDATA
->m_palette
;
325 void wxBitmap::SetHeight( int height
)
327 if (!m_refData
) m_refData
= new wxBitmapRefData();
329 M_BMPDATA
->m_height
= height
;
332 void wxBitmap::SetWidth( int width
)
334 if (!m_refData
) m_refData
= new wxBitmapRefData();
336 M_BMPDATA
->m_width
= width
;
339 void wxBitmap::SetDepth( int depth
)
341 if (!m_refData
) m_refData
= new wxBitmapRefData();
343 M_BMPDATA
->m_bpp
= depth
;
346 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
348 if (!m_refData
) m_refData
= new wxBitmapRefData();
350 M_BMPDATA
->m_pixmap
= pixmap
;
353 GdkPixmap
*wxBitmap::GetPixmap(void) const
355 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, "invalid bitmap" );
357 return M_BMPDATA
->m_pixmap
;
360 GdkBitmap
*wxBitmap::GetBitmap(void) const
362 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, "invalid bitmap" );
364 return M_BMPDATA
->m_bitmap
;
367 //-----------------------------------------------------------------------------
369 //-----------------------------------------------------------------------------
371 wxBitmap
wxImage::ConvertToBitmap() const
375 wxCHECK_MSG( Ok(), bitmap
, "invalid image" );
377 int width
= GetWidth();
378 int height
= GetHeight();
380 bitmap
.SetHeight( height
);
381 bitmap
.SetWidth( width
);
385 GdkImage
*data_image
=
386 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
388 bitmap
.SetPixmap( gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 ) );
392 GdkImage
*mask_image
= (GdkImage
*) NULL
;
396 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
398 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
400 wxMask
*mask
= new wxMask();
401 mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
403 bitmap
.SetMask( mask
);
408 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
409 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
410 int bpp
= visual
->depth
;
411 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
412 if (bpp
< 8) bpp
= 8;
416 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
417 byte_order b_o
= RGB
;
421 GdkVisual
*visual
= gdk_visual_get_system();
422 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
423 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
424 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
425 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
426 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
427 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
430 int r_mask
= GetMaskRed();
431 int g_mask
= GetMaskGreen();
432 int b_mask
= GetMaskBlue();
434 unsigned char* data
= GetData();
437 for (int y
= 0; y
< height
; y
++)
439 for (int x
= 0; x
< width
; x
++)
450 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
451 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
453 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
460 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
461 GdkColor
*colors
= cmap
->colors
;
462 int max
= 3 * (65536);
465 for (int i
= 0; i
< cmap
->size
; i
++)
467 int rdiff
= (r
<< 8) - colors
[i
].red
;
468 int gdiff
= (g
<< 8) - colors
[i
].green
;
469 int bdiff
= (b
<< 8) - colors
[i
].blue
;
470 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
471 if (sum
< max
) { index
= i
; max
= sum
; }
474 gdk_image_put_pixel( data_image
, x
, y
, index
);
480 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
481 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
486 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
487 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
496 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
497 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
498 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
499 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
500 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
501 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
503 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
512 GdkGC
*data_gc
= gdk_gc_new( bitmap
.GetPixmap() );
514 gdk_draw_image( bitmap
.GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
516 gdk_image_destroy( data_image
);
517 gdk_gc_unref( data_gc
);
523 GdkGC
*mask_gc
= gdk_gc_new( bitmap
.GetMask()->GetBitmap() );
525 gdk_draw_image( bitmap
.GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
527 gdk_image_destroy( mask_image
);
528 gdk_gc_unref( mask_gc
);
534 wxImage::wxImage( const wxBitmap
&bitmap
)
536 wxCHECK_RET( bitmap
.Ok(), "invalid bitmap" );
538 GdkImage
*gdk_image
= gdk_image_get( bitmap
.GetPixmap(),
540 bitmap
.GetWidth(), bitmap
.GetHeight() );
542 wxCHECK_RET( gdk_image
, "couldn't create image" );
544 Create( bitmap
.GetWidth(), bitmap
.GetHeight() );
545 char unsigned *data
= GetData();
549 gdk_image_destroy( gdk_image
);
550 wxFAIL_MSG( "couldn't create image" );
554 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
555 if (bitmap
.GetMask())
557 gdk_image_mask
= gdk_image_get( bitmap
.GetMask()->GetBitmap(),
559 bitmap
.GetWidth(), bitmap
.GetHeight() );
561 SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
564 GdkVisual
*visual
= gdk_window_get_visual( bitmap
.GetPixmap() );
565 if (visual
== NULL
) visual
= gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
);
566 int bpp
= visual
->depth
;
567 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
569 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
572 for (int j
= 0; j
< bitmap
.GetHeight(); j
++)
574 for (int i
= 0; i
< bitmap
.GetWidth(); i
++)
576 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
579 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
580 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
581 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
582 } else if (bpp
== 15)
584 data
[pos
] = (pixel
>> 7) & 0xf8;
585 data
[pos
+1] = (pixel
>> 2) & 0xf8;
586 data
[pos
+2] = (pixel
<< 3) & 0xf8;
587 } else if (bpp
== 16)
589 data
[pos
] = (pixel
>> 8) & 0xf8;
590 data
[pos
+1] = (pixel
>> 3) & 0xfc;
591 data
[pos
+2] = (pixel
<< 3) & 0xf8;
594 data
[pos
] = (pixel
>> 16) & 0xff;
595 data
[pos
+1] = (pixel
>> 8) & 0xff;
596 data
[pos
+2] = pixel
& 0xff;
601 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
614 gdk_image_destroy( gdk_image
);
615 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);