]>
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 "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 wxCHECK_RET( (width
> 0) && (height
> 0), "invalid bitmap size" )
105 wxCHECK_RET( (depth
> 0) || (depth
== -1), "invalid bitmap depth" )
107 m_refData
= new wxBitmapRefData();
109 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
111 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
112 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( parent
, width
, height
, depth
);
113 M_BMPDATA
->m_width
= width
;
114 M_BMPDATA
->m_height
= height
;
115 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
117 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
120 wxBitmap::wxBitmap( char **bits
)
122 wxCHECK_RET( bits
!= NULL
, "invalid bitmap data" )
124 m_refData
= new wxBitmapRefData();
126 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
127 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
129 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
133 M_BMPDATA
->m_mask
= new wxMask();
134 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
137 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
139 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
141 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
144 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
148 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
151 wxBitmap::wxBitmap( const wxBitmap
* bmp
)
153 if (bmp
) Ref( *bmp
);
155 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
158 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
160 LoadFile( filename
, type
);
162 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
165 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
167 m_refData
= new wxBitmapRefData();
169 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
170 M_BMPDATA
->m_bitmap
=
171 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
172 M_BMPDATA
->m_width
= width
;
173 M_BMPDATA
->m_height
= height
;
174 M_BMPDATA
->m_bpp
= 1;
176 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
179 wxBitmap::~wxBitmap(void)
181 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
184 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
186 if (*this == bmp
) return (*this);
191 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
193 return m_refData
== bmp
.m_refData
;
196 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
198 return m_refData
!= bmp
.m_refData
;
201 bool wxBitmap::Ok(void) const
203 return (m_refData
!= NULL
);
206 int wxBitmap::GetHeight(void) const
210 wxFAIL_MSG( "invalid bitmap" );
214 return M_BMPDATA
->m_height
;
217 int wxBitmap::GetWidth(void) const
221 wxFAIL_MSG( "invalid bitmap" );
225 return M_BMPDATA
->m_width
;
228 int wxBitmap::GetDepth(void) const
232 wxFAIL_MSG( "invalid bitmap" );
236 return M_BMPDATA
->m_bpp
;
239 void wxBitmap::SetHeight( int height
)
243 wxFAIL_MSG( "wxBitmap::SetHeight not implemented" );
245 M_BMPDATA
->m_height
= height
;
248 void wxBitmap::SetWidth( int width
)
252 wxFAIL_MSG( "wxBitmap::SetWidth not implemented" );
254 M_BMPDATA
->m_width
= width
;
257 void wxBitmap::SetDepth( int depth
)
261 wxFAIL_MSG( "wxBitmap::SetDepth not implemented" );
263 M_BMPDATA
->m_bpp
= depth
;
266 wxMask
*wxBitmap::GetMask(void) const
270 wxFAIL_MSG( "invalid bitmap" );
271 return (wxMask
*) NULL
;
274 return M_BMPDATA
->m_mask
;
277 void wxBitmap::SetMask( wxMask
*mask
)
281 wxFAIL_MSG( "invalid bitmap" );
285 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
287 M_BMPDATA
->m_mask
= mask
;
290 bool wxBitmap::SaveFile( const wxString
&WXUNUSED(name
), int WXUNUSED(type
),
291 wxPalette
*WXUNUSED(palette
) )
295 wxFAIL_MSG( "invalid bitmap" );
302 bool wxBitmap::LoadFile( const wxString
&WXUNUSED(name
), int WXUNUSED(type
) )
306 wxFAIL_MSG( "invalid bitmap" );
313 wxPalette
*wxBitmap::GetPalette(void) const
315 if (!Ok()) return (wxPalette
*) NULL
;
316 return M_BMPDATA
->m_palette
;
319 GdkPixmap
*wxBitmap::GetPixmap(void) const
323 wxFAIL_MSG( "invalid bitmap" );
324 return (GdkPixmap
*) NULL
;
327 return M_BMPDATA
->m_pixmap
;
330 GdkBitmap
*wxBitmap::GetBitmap(void) const
334 wxFAIL_MSG( "invalid bitmap" );
335 return (GdkBitmap
*) NULL
;
338 return M_BMPDATA
->m_bitmap
;
341 wxBitmap::wxBitmap( const wxImage
&image
)
343 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
345 if (!image
.Ok()) return;
347 m_refData
= new wxBitmapRefData();
349 M_BMPDATA
->m_height
= image
.GetHeight();
350 M_BMPDATA
->m_width
= image
.GetWidth();
351 int width
= image
.GetWidth();
352 int height
= image
.GetHeight();
356 GdkImage
*data_image
=
357 gdk_image_new( GDK_IMAGE_FASTEST
, gdk_visual_get_system(), width
, height
);
359 M_BMPDATA
->m_pixmap
=
360 gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, -1 );
364 GdkImage
*mask_image
= (GdkImage
*) NULL
;
368 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
370 mask_image
= gdk_image_new_bitmap( gdk_visual_get_system(), mask_data
, width
, height
);
372 M_BMPDATA
->m_mask
= new wxMask();
373 M_BMPDATA
->m_mask
->m_bitmap
= gdk_pixmap_new( (GdkWindow
*)&gdk_root_parent
, width
, height
, 1 );
378 M_BMPDATA
->m_bpp
= data_image
->depth
;
380 int render_depth
= 8;
381 if (M_BMPDATA
->m_bpp
> 8) render_depth
= M_BMPDATA
->m_bpp
;
385 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
388 if (render_depth
>= 24)
390 GdkVisual
*visual
= gdk_visual_get_system();
391 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
392 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RGB
;
393 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
394 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
395 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
396 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
399 int r_mask
= image
.GetMaskRed();
400 int g_mask
= image
.GetMaskGreen();
401 int b_mask
= image
.GetMaskBlue();
403 unsigned char* data
= image
.GetData();
406 for (int y
= 0; y
< height
; y
++)
407 for (int x
= 0; x
< width
; x
++)
418 if ((r
== r_mask
) && (b
= b_mask
) && (g
= g_mask
))
419 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
421 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
424 switch (render_depth
)
428 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
429 GdkColor
*colors
= cmap
->colors
;
430 int max
= 3 * (65536);
433 for (int i
= 0; i
< cmap
->size
; i
++)
435 int rdiff
= (r
<< 8) - colors
[i
].red
;
436 int gdiff
= (g
<< 8) - colors
[i
].green
;
437 int bdiff
= (b
<< 8) - colors
[i
].blue
;
438 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
439 if (sum
< max
) { index
= i
; max
= sum
; }
442 gdk_image_put_pixel( data_image
, x
, y
, index
);
448 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
449 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
454 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
455 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
464 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
465 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
466 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
467 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
468 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
469 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
471 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
479 GdkGC
*data_gc
= gdk_gc_new( M_BMPDATA
->m_pixmap
);
481 gdk_draw_image( M_BMPDATA
->m_pixmap
, data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
483 gdk_image_destroy( data_image
);
484 gdk_gc_unref( data_gc
);
490 GdkGC
*mask_gc
= gdk_gc_new( M_BMPDATA
->m_mask
->m_bitmap
);
492 gdk_draw_image( M_BMPDATA
->m_mask
->m_bitmap
, mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
494 gdk_image_destroy( mask_image
);
495 gdk_gc_unref( mask_gc
);
500 wxImage
wxBitmap::ConvertToImage() const
506 wxFAIL_MSG( "invalid bitmap" );
510 GdkImage
*gdk_image
= gdk_image_get( M_BMPDATA
->m_pixmap
, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
512 if (!gdk_image
) return image
;
514 image
.Create( M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
515 char unsigned *data
= image
.GetData();
517 int bpp
= gdk_image
->bpp
;
518 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
521 for (int j
= 0; j
< M_BMPDATA
->m_height
; j
++)
523 for (int i
= 0; i
< M_BMPDATA
->m_width
; i
++)
525 int pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
528 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
529 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
530 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
531 } else if (bpp
== 15)
533 data
[pos
] = (pixel
>> 7) & 0xf8;
534 data
[pos
+1] = (pixel
>> 3) & 0xf8;
535 data
[pos
+2] = (pixel
<< 3) & 0xf8;
536 } else if (bpp
== 16)
538 data
[pos
] = (pixel
>> 8) & 0xf8;
539 data
[pos
+1] = (pixel
>> 3) & 0xfc;
540 data
[pos
+2] = (pixel
<< 3) & 0xf8;
543 data
[pos
] = (pixel
>> 16) & 0xff;
544 data
[pos
+1] = (pixel
>> 8) & 0xff;
545 data
[pos
+2] = pixel
& 0xff;
552 gdk_image_destroy( gdk_image
);