From: Robert Roebling Date: Mon, 27 Dec 1999 13:01:07 +0000 (+0000) Subject: Added mono bitmap to image conversion. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/be25e4809325d89075f6477025e02706137e6ffd Added mono bitmap to image conversion. Added wxImage::Replace() (replaces one colour with another). Toolbar tips no longer eats ana new colour. This might prevent the wrong colour behaviour reported from some 8-bit visuals. Minor fixes and test code. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5122 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/image.h b/include/wx/image.h index b66e23c620..893e272c2e 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -37,48 +37,46 @@ class WXDLLEXPORT wxImage; class WXDLLEXPORT wxImageHandler: public wxObject { - DECLARE_CLASS(wxImageHandler) - public: - wxImageHandler() { m_name = ""; m_extension = ""; m_type = 0; } + wxImageHandler() { m_name = ""; m_extension = ""; m_type = 0; } #if wxUSE_STREAMS - virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 ); - virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); + virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 ); + virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE ); - virtual int GetImageCount( wxInputStream& stream ); + virtual int GetImageCount( wxInputStream& stream ); - bool CanRead( wxInputStream& stream ) { return DoCanRead(stream); } - bool CanRead( const wxString& name ); + bool CanRead( wxInputStream& stream ) { return DoCanRead(stream); } + bool CanRead( const wxString& name ); #endif // wxUSE_STREAMS - void SetName(const wxString& name) { m_name = name; } - void SetExtension(const wxString& ext) { m_extension = ext; } - void SetType(long type) { m_type = type; } - void SetMimeType(const wxString& type) { m_mime = type; } - wxString GetName() const { return m_name; } - wxString GetExtension() const { return m_extension; } - long GetType() const { return m_type; } - wxString GetMimeType() const { return m_mime; } + void SetName(const wxString& name) { m_name = name; } + void SetExtension(const wxString& ext) { m_extension = ext; } + void SetType(long type) { m_type = type; } + void SetMimeType(const wxString& type) { m_mime = type; } + wxString GetName() const { return m_name; } + wxString GetExtension() const { return m_extension; } + long GetType() const { return m_type; } + wxString GetMimeType() const { return m_mime; } protected: #if wxUSE_STREAMS - virtual bool DoCanRead( wxInputStream& stream ) = 0; + virtual bool DoCanRead( wxInputStream& stream ) = 0; #endif // wxUSE_STREAMS - wxString m_name; - wxString m_extension; - wxString m_mime; - long m_type; + wxString m_name; + wxString m_extension; + wxString m_mime; + long m_type; + +private: + DECLARE_CLASS(wxImageHandler) }; - //----------------------------------------------------------------------------- // wxImage //----------------------------------------------------------------------------- - -// GRG: Dic/99 class WXDLLEXPORT wxHNode { public: @@ -86,113 +84,112 @@ public: unsigned long value; }; - class WXDLLEXPORT wxImage: public wxObject { - DECLARE_DYNAMIC_CLASS(wxImage) - - friend class WXDLLEXPORT wxImageHandler; - public: + wxImage(); + wxImage( int width, int height ); + wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY ); + wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY ); + wxImage( const wxString& name, const wxString& mimetype ); + wxImage( wxInputStream& stream, const wxString& mimetype ); - wxImage(); - wxImage( int width, int height ); - wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY ); - wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY ); - wxImage( const wxString& name, const wxString& mimetype ); - wxImage( wxInputStream& stream, const wxString& mimetype ); + wxImage( const wxImage& image ); + wxImage( const wxImage* image ); - wxImage( const wxImage& image ); - wxImage( const wxImage* image ); + // these functions get implemented in /src/(platform)/bitmap.cpp + wxImage( const wxBitmap &bitmap ); + operator wxBitmap() const { return ConvertToBitmap(); } + wxBitmap ConvertToBitmap() const; - // these functions get implemented in /src/(platform)/bitmap.cpp - wxImage( const wxBitmap &bitmap ); - operator wxBitmap() const { return ConvertToBitmap(); } - wxBitmap ConvertToBitmap() const; + void Create( int width, int height ); + void Destroy(); - void Create( int width, int height ); - void Destroy(); + // return the new image with size width*height + wxImage GetSubImage( const wxRect& ) const; - // return the new image with size width*height - wxImage GetSubImage( const wxRect& ) const; + // return the new image with size width*height + wxImage Scale( int width, int height ) const; - // return the new image with size width*height - wxImage Scale( int width, int height ) const; + // rescales the image in place + wxImage& Rescale( int width, int height ) { return *this = Scale(width, height); } - // rescales the image in place - wxImage& Rescale( int width, int height ) { return *this = Scale(width, height); } + // replace one colour with another + void Replace( unsigned char r1, unsigned char g1, unsigned char b1, + unsigned char r2, unsigned char g2, unsigned char b2 ); - // these routines are slow but safe - void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ); - unsigned char GetRed( int x, int y ); - unsigned char GetGreen( int x, int y ); - unsigned char GetBlue( int x, int y ); + // these routines are slow but safe + void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ); + unsigned char GetRed( int x, int y ); + unsigned char GetGreen( int x, int y ); + unsigned char GetBlue( int x, int y ); - static bool CanRead( const wxString& name ); - virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY ); - virtual bool LoadFile( const wxString& name, const wxString& mimetype ); + static bool CanRead( const wxString& name ); + virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY ); + virtual bool LoadFile( const wxString& name, const wxString& mimetype ); #if wxUSE_STREAMS - static bool CanRead( wxInputStream& stream ); - virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY ); - virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype ); + static bool CanRead( wxInputStream& stream ); + virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY ); + virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype ); #endif - virtual bool SaveFile( const wxString& name, int type ); - virtual bool SaveFile( const wxString& name, const wxString& mimetype ); + virtual bool SaveFile( const wxString& name, int type ); + virtual bool SaveFile( const wxString& name, const wxString& mimetype ); #if wxUSE_STREAMS - virtual bool SaveFile( wxOutputStream& stream, int type ); - virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype ); + virtual bool SaveFile( wxOutputStream& stream, int type ); + virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype ); #endif - bool Ok() const; - int GetWidth() const; - int GetHeight() const; - - char unsigned *GetData() const; - void SetData( char unsigned *data ); - - void SetMaskColour( unsigned char r, unsigned char g, unsigned char b ); - unsigned char GetMaskRed() const; - unsigned char GetMaskGreen() const; - unsigned char GetMaskBlue() const; - void SetMask( bool mask = TRUE ); - bool HasMask() const; - - wxImage& operator = (const wxImage& image) - { - if ( (*this) != image ) - Ref(image); - return *this; - } - - bool operator == (const wxImage& image) - { return m_refData == image.m_refData; } - bool operator != (const wxImage& image) - { return m_refData != image.m_refData; } - - static wxList& GetHandlers() { return sm_handlers; } - static void AddHandler( wxImageHandler *handler ); - static void InsertHandler( wxImageHandler *handler ); - static bool RemoveHandler( const wxString& name ); - static wxImageHandler *FindHandler( const wxString& name ); - static wxImageHandler *FindHandler( const wxString& extension, long imageType ); - static wxImageHandler *FindHandler( long imageType ); - static wxImageHandler *FindHandlerMime( const wxString& mimetype ); - - static void CleanUpHandlers(); - static void InitStandardHandlers(); - - // GRG: Dic/99 - unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ); - unsigned long ComputeHistogram( wxHashTable &h ); - + bool Ok() const; + int GetWidth() const; + int GetHeight() const; + + char unsigned *GetData() const; + void SetData( char unsigned *data ); + + void SetMaskColour( unsigned char r, unsigned char g, unsigned char b ); + unsigned char GetMaskRed() const; + unsigned char GetMaskGreen() const; + unsigned char GetMaskBlue() const; + void SetMask( bool mask = TRUE ); + bool HasMask() const; + + unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ); + unsigned long ComputeHistogram( wxHashTable &h ); + + wxImage& operator = (const wxImage& image) + { + if ( (*this) != image ) + Ref(image); + return *this; + } + + bool operator == (const wxImage& image) + { return m_refData == image.m_refData; } + bool operator != (const wxImage& image) + { return m_refData != image.m_refData; } + + static wxList& GetHandlers() { return sm_handlers; } + static void AddHandler( wxImageHandler *handler ); + static void InsertHandler( wxImageHandler *handler ); + static bool RemoveHandler( const wxString& name ); + static wxImageHandler *FindHandler( const wxString& name ); + static wxImageHandler *FindHandler( const wxString& extension, long imageType ); + static wxImageHandler *FindHandler( long imageType ); + static wxImageHandler *FindHandlerMime( const wxString& mimetype ); + + static void CleanUpHandlers(); + static void InitStandardHandlers(); protected: + static wxList sm_handlers; - static wxList sm_handlers; +private: + friend class WXDLLEXPORT wxImageHandler; + DECLARE_DYNAMIC_CLASS(wxImage) }; diff --git a/samples/image/image.cpp b/samples/image/image.cpp index 6d63802c5f..e75e7177d4 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -201,55 +201,69 @@ MyCanvas::~MyCanvas() void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) { - wxPaintDC dc( this ); - PrepareDC( dc ); + wxPaintDC dc( this ); + PrepareDC( dc ); - dc.DrawText( "Loaded image", 30, 10 ); - if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 ); + dc.DrawText( "Loaded image", 30, 10 ); + if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 ); - dc.DrawText( "Drawn directly", 150, 10 ); - dc.SetBrush( wxBrush( "orange", wxSOLID ) ); - dc.SetPen( *wxWHITE_PEN ); - dc.DrawRectangle( 150, 30, 100, 100 ); - - if (my_anti && my_anti->Ok()) dc.DrawBitmap( *my_anti, 280, 30 ); - - dc.DrawText( "PNG handler", 30, 135 ); - if (my_horse_png && my_horse_png->Ok()) - { - dc.DrawBitmap( *my_horse_png, 30, 150 ); - wxRect rect(0,0,100,100); - wxBitmap sub( my_horse_png->GetSubBitmap(rect) ); - dc.DrawText( "GetSubBitmap()", 280, 190 ); - dc.DrawBitmap( sub, 280, 210 ); - } + dc.DrawText( "Drawn directly", 150, 10 ); + dc.SetBrush( wxBrush( "orange", wxSOLID ) ); + dc.SetPen( *wxWHITE_PEN ); + dc.DrawRectangle( 150, 30, 100, 100 ); + + if (my_anti && my_anti->Ok()) + dc.DrawBitmap( *my_anti, 280, 30 ); + + dc.DrawText( "PNG handler", 30, 135 ); + if (my_horse_png && my_horse_png->Ok()) + { + dc.DrawBitmap( *my_horse_png, 30, 150 ); + wxRect rect(0,0,100,100); + wxBitmap sub( my_horse_png->GetSubBitmap(rect) ); + dc.DrawText( "GetSubBitmap()", 280, 190 ); + dc.DrawBitmap( sub, 280, 210 ); + } - dc.DrawText( "JPEG handler", 30, 365 ); - if (my_horse_jpeg && my_horse_jpeg->Ok()) dc.DrawBitmap( *my_horse_jpeg, 30, 380 ); + dc.DrawText( "JPEG handler", 30, 365 ); + if (my_horse_jpeg && my_horse_jpeg->Ok()) + dc.DrawBitmap( *my_horse_jpeg, 30, 380 ); - dc.DrawText( "GIF handler", 30, 595 ); - if (my_horse_gif && my_horse_gif->Ok()) dc.DrawBitmap( *my_horse_gif, 30, 610 ); + dc.DrawText( "GIF handler", 30, 595 ); + if (my_horse_gif && my_horse_gif->Ok()) + dc.DrawBitmap( *my_horse_gif, 30, 610 ); - dc.DrawText( "PCX handler", 30, 825 ); - if (my_horse_pcx && my_horse_pcx->Ok()) dc.DrawBitmap( *my_horse_pcx, 30, 840 ); + dc.DrawText( "PCX handler", 30, 825 ); + if (my_horse_pcx && my_horse_pcx->Ok()) + dc.DrawBitmap( *my_horse_pcx, 30, 840 ); - dc.DrawText( "BMP handler", 30, 1055 ); - if (my_horse_bmp && my_horse_bmp->Ok()) dc.DrawBitmap( *my_horse_bmp, 30, 1070 ); + dc.DrawText( "BMP handler", 30, 1055 ); + if (my_horse_bmp && my_horse_bmp->Ok()) + dc.DrawBitmap( *my_horse_bmp, 30, 1070 ); - dc.DrawText( "PNM handler", 30, 1285 ); - if (my_horse_pnm && my_horse_pnm->Ok()) dc.DrawBitmap( *my_horse_pnm, 30, 1300 ); + dc.DrawText( "PNM handler", 30, 1285 ); + if (my_horse_pnm && my_horse_pnm->Ok()) + dc.DrawBitmap( *my_horse_pnm, 30, 1300 ); - dc.DrawText( "TIFF handler", 30, 1515 ); - if (my_horse_tiff && my_horse_tiff->Ok()) dc.DrawBitmap( *my_horse_pnm, 30, 1530 ); - - dc.DrawText( "XBM bitmap", 30, 1745 ); - dc.SetPen( *wxRED_PEN ); - if (my_smile_xbm && my_smile_xbm->Ok()) { - dc.DrawBitmap( *my_smile_xbm, 30, 1760 ); - dc.DrawText( "..after wxImage conversion", 150, 1745 ); - wxImage i( *my_smile_xbm ); - dc.DrawBitmap( i.ConvertToBitmap(), 150, 1760 ); - } + dc.DrawText( "TIFF handler", 30, 1515 ); + if (my_horse_tiff && my_horse_tiff->Ok()) + dc.DrawBitmap( *my_horse_pnm, 30, 1530 ); + + if (my_smile_xbm && my_smile_xbm->Ok()) + { + dc.DrawText( "XBM bitmap", 30, 1745 ); + dc.SetPen( *wxRED_PEN ); + dc.DrawBitmap( *my_smile_xbm, 30, 1760 ); + + dc.DrawText( "After wxImage conversion", 150, 1745 ); + wxImage i( *my_smile_xbm ); + i.SetMaskColour( 0,0,0 ); + i.Replace( 255,255,255, + wxRED_PEN->GetColour().Red(), + wxRED_PEN->GetColour().Green(), + wxRED_PEN->GetColour().Blue() ); + dc.DrawBitmap( i.ConvertToBitmap(), 150, 1760, TRUE ); + } } void MyCanvas::CreateAntiAliasedBitmap() diff --git a/src/common/image.cpp b/src/common/image.cpp index fcdc3d34e9..9b099758f2 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -224,6 +224,29 @@ wxImage wxImage::GetSubImage( const wxRect &rect ) const return image; } +void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1, + unsigned char r2, unsigned char g2, unsigned char b2 ) +{ + wxCHECK_RET( Ok(), wxT("invalid image") ); + + char unsigned *data = GetData(); + + const int w = GetWidth(); + const int h = GetHeight(); + + for (int j = 0; j < h; j++) + for (int i = 0; i < w; i++) + { + if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1)) + { + data[0] = r2; + data[1] = g2; + data[2] = b2; + } + data += 3; + } +} + void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ) { wxCHECK_RET( Ok(), wxT("invalid image") ); @@ -1541,15 +1564,19 @@ wxImage::wxImage( const wxBitmap &bitmap ) SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable } - GdkVisual *visual = (GdkVisual*) NULL; + int bpp = -1; if (bitmap.GetPixmap()) - visual = gdk_window_get_visual( bitmap.GetPixmap() ); - else - visual = gdk_window_get_visual( bitmap.GetBitmap() ); + { + GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() ); - if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent ); - int bpp = visual->depth; - if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15; + if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent ); + bpp = visual->depth; + if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15; + } + if (bitmap.GetBitmap()) + { + bpp = 1; + } GdkColormap *cmap = gtk_widget_get_default_colormap(); @@ -1559,7 +1586,21 @@ wxImage::wxImage( const wxBitmap &bitmap ) for (int i = 0; i < bitmap.GetWidth(); i++) { wxInt32 pixel = gdk_image_get_pixel( gdk_image, i, j ); - if (bpp <= 8) + if (bpp == 1) + { + if (pixel == 0) + { + data[pos] = 0; + data[pos+1] = 0; + data[pos+2] = 0; + } + else + { + data[pos] = 255; + data[pos+1] = 255; + data[pos+2] = 255; + } + } else if (bpp <= 8) { data[pos] = cmap->colors[pixel].red >> 8; data[pos+1] = cmap->colors[pixel].green >> 8; diff --git a/src/gtk/tbargtk.cpp b/src/gtk/tbargtk.cpp index 5cbd3c990e..de64297fd4 100644 --- a/src/gtk/tbargtk.cpp +++ b/src/gtk/tbargtk.cpp @@ -272,24 +272,26 @@ bool wxToolBar::Create( wxWindow *parent, gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE ); -#if (GTK_MINOR_VERSION > 0) if (style & wxTB_FLAT) gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE ); -#endif + m_fg = new GdkColor; - m_fg->red = 0; - m_fg->green = 0; + m_fg->red = 0; + m_fg->green = 0; m_fg->blue = 0; - gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_fg ); - + wxColour fg(0,0,0); + fg.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ) ); + m_fg->pixel = fg.GetPixel(); + m_bg = new GdkColor; m_bg->red = 65535; m_bg->green = 65535; - m_bg->blue = 50000; - gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_bg ); - -#if (GTK_MINOR_VERSION > 0) + m_bg->blue = 49980; + wxColour bg(255,255,196); + bg.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ) ); + m_bg->pixel = bg.GetPixel(); + gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar)->tooltips ); GtkStyle *g_style = @@ -299,9 +301,7 @@ bool wxToolBar::Create( wxWindow *parent, g_style->bg[GTK_STATE_NORMAL] = *m_bg; gtk_widget_set_style( GTK_TOOLBAR(m_toolbar)->tooltips->tip_window, g_style ); -#else - gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar)->tooltips, m_bg, m_fg ); -#endif + m_parent->DoAddChild( this ); diff --git a/src/gtk1/tbargtk.cpp b/src/gtk1/tbargtk.cpp index 5cbd3c990e..de64297fd4 100644 --- a/src/gtk1/tbargtk.cpp +++ b/src/gtk1/tbargtk.cpp @@ -272,24 +272,26 @@ bool wxToolBar::Create( wxWindow *parent, gtk_toolbar_set_tooltips( GTK_TOOLBAR(m_toolbar), TRUE ); -#if (GTK_MINOR_VERSION > 0) if (style & wxTB_FLAT) gtk_toolbar_set_button_relief( GTK_TOOLBAR(m_toolbar), GTK_RELIEF_NONE ); -#endif + m_fg = new GdkColor; - m_fg->red = 0; - m_fg->green = 0; + m_fg->red = 0; + m_fg->green = 0; m_fg->blue = 0; - gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_fg ); - + wxColour fg(0,0,0); + fg.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ) ); + m_fg->pixel = fg.GetPixel(); + m_bg = new GdkColor; m_bg->red = 65535; m_bg->green = 65535; - m_bg->blue = 50000; - gdk_color_alloc( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ), m_bg ); - -#if (GTK_MINOR_VERSION > 0) + m_bg->blue = 49980; + wxColour bg(255,255,196); + bg.CalcPixel( gtk_widget_get_colormap( GTK_WIDGET(m_toolbar) ) ); + m_bg->pixel = bg.GetPixel(); + gtk_tooltips_force_window( GTK_TOOLBAR(m_toolbar)->tooltips ); GtkStyle *g_style = @@ -299,9 +301,7 @@ bool wxToolBar::Create( wxWindow *parent, g_style->bg[GTK_STATE_NORMAL] = *m_bg; gtk_widget_set_style( GTK_TOOLBAR(m_toolbar)->tooltips->tip_window, g_style ); -#else - gtk_tooltips_set_colors( GTK_TOOLBAR(m_toolbar)->tooltips, m_bg, m_fg ); -#endif + m_parent->DoAddChild( this ); diff --git a/wxGTK.spec b/wxGTK.spec index 7e2c946ac8..e07a95fd65 100644 --- a/wxGTK.spec +++ b/wxGTK.spec @@ -1,6 +1,6 @@ # Note that this is NOT a relocatable package %define pref /usr -%define ver 2.1.11 +%define ver 2.1.12 %define rel 0 Summary: The GTK+ 1.2 port of the wxWindows library @@ -9,7 +9,7 @@ Version: %{ver} Release: %{rel} Copyright: wxWindows Licence Group: X11/Libraries -Source: ftp://wesley.informatik.uni-freiburg.de/pub/linux/wxxt/source/wxGTK-2.1.11.tgz +Source: ftp://wesley.informatik.uni-freiburg.de/pub/linux/wxxt/source/wxGTK-2.1.12.tgz URL: http://wesley.informatik.uni-freiburg.de/~wxxt/docs.html Packager: Robert Roebling BuildRoot: /tmp/wxgtk_root