From 38274997413342f48109383b96e6d295c54a5f97 Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Wed, 13 Jan 1999 12:03:32 +0000 Subject: [PATCH] more wxImage on Motif git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1394 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/image.tex | 2 - samples/image/image.cpp | 14 ---- src/common/image.cpp | 151 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 136 insertions(+), 31 deletions(-) diff --git a/docs/latex/wx/image.tex b/docs/latex/wx/image.tex index 2037c9e7db..a7808cd051 100644 --- a/docs/latex/wx/image.tex +++ b/docs/latex/wx/image.tex @@ -10,8 +10,6 @@ A wxImage cannot (currently) be drawn directly to a wxDC. Instead, a platform-sp wxBitmap object must be created from it, and that bitmap drawn on the wxDC, using wxDC::DrawBitmap. -This class is currently only available under GTK and Windows. - \wxheading{Derived from} \helpref{wxObject}{wxobject} diff --git a/samples/image/image.cpp b/samples/image/image.cpp index ce48fb032c..cdbac91bc3 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -99,32 +99,20 @@ MyCanvas::MyCanvas( wxWindow *parent, const wxWindowID id, dir = wxString("../"); #endif -//#ifdef __WXMOTIF__ - dir = wxString("../"); -//#endif - -#ifndef __WXMOTIF__ wxImage image( bitmap ); image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ); -#else - wxImage image; -#endif image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG ); my_horse = new wxBitmap( image.ConvertToBitmap() ); -#ifndef __WXMOTIF__ image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ); my_square = new wxBitmap( image.ConvertToBitmap() ); -#endif } MyCanvas::~MyCanvas() { delete my_horse; -#ifndef __WXMOTIF__ delete my_square; -#endif } void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) @@ -132,7 +120,6 @@ void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) wxPaintDC dc( this ); PrepareDC( dc ); -#ifndef __WXMOTIF__ dc.DrawText( "Loaded image", 30, 100 ); if (my_square->Ok()) dc.DrawBitmap( *my_square, 30, 120 ); @@ -140,7 +127,6 @@ void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) dc.SetBrush( wxBrush( "orange", wxSOLID ) ); dc.SetPen( *wxWHITE_PEN ); dc.DrawRectangle( 150, 120, 100, 100 ); -#endif if (my_horse->Ok()) dc.DrawBitmap( *my_horse, 30, 240 ); } diff --git a/src/common/image.cpp b/src/common/image.cpp index 414ea15c25..7b0dc57f73 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -1417,8 +1417,7 @@ wxBitmap wxImage::ConvertToBitmap() const int pixel = -1; if (wxTheApp->m_colorCube) { - pixel = wxTheApp->m_colorCube - [ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; + pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; } else { @@ -1588,6 +1587,7 @@ wxImage::wxImage( const wxBitmap &bitmap ) #include #include "wx/utils.h" +#include wxBitmap wxImage::ConvertToBitmap() const { @@ -1630,8 +1630,6 @@ wxBitmap wxImage::ConvertToBitmap() const } */ -// bitmap.SetDepth( bpp ); - // Retrieve depth info XVisualInfo vinfo_template; @@ -1650,6 +1648,8 @@ wxBitmap wxImage::ConvertToBitmap() const return wxNullBitmap; } + XFree( vi ); + if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15; if (bpp < 8) bpp = 8; @@ -1674,6 +1674,15 @@ wxBitmap wxImage::ConvertToBitmap() const int b_mask = GetMaskBlue(); */ + XColor colors[256]; + if (bpp == 8) + { + Colormap cmap = (Colormap) wxTheApp->GetMainColormap( dpy ); + + for (int i = 0; i < 256; i++) colors[i].pixel = i; + XQueryColors( dpy, cmap, colors, 256 ); + } + unsigned char* data = GetData(); int index = 0; @@ -1702,8 +1711,8 @@ wxBitmap wxImage::ConvertToBitmap() const { case 8: { -/* int pixel = -1; +/* if (wxTheApp->m_colorCube) { pixel = wxTheApp->m_colorCube @@ -1711,23 +1720,20 @@ wxBitmap wxImage::ConvertToBitmap() const } else { - GdkColormap *cmap = gtk_widget_get_default_colormap(); - GdkColor *colors = cmap->colors; +*/ int max = 3 * (65536); - - for (int i = 0; i < cmap->size; i++) + for (int i = 0; i < 256; i++) { int rdiff = (r << 8) - colors[i].red; int gdiff = (g << 8) - colors[i].green; int bdiff = (b << 8) - colors[i].blue; - int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); + int sum = abs (rdiff) + abs (gdiff) + abs (bdiff); if (sum < max) { pixel = i; max = sum; } } - } - - gdk_image_put_pixel( data_image, x, y, pixel ); +/* + } */ - + XPutPixel( data_image, x, y, pixel ); break; } case 15: @@ -1791,6 +1797,121 @@ wxBitmap wxImage::ConvertToBitmap() const wxImage::wxImage( const wxBitmap &bitmap ) { - wxFAIL_MSG("Sorry, wxImage::wxImage(const wxBitmap&) isn't implemented for wxMotif yet."); + wxCHECK_RET( bitmap.Ok(), "invalid bitmap" ); + + Display *dpy = (Display*) wxGetDisplay(); + Visual* vis = DefaultVisual( dpy, DefaultScreen( dpy ) ); + int bpp = DefaultDepth( dpy, DefaultScreen( dpy ) ); + + XImage *ximage = XGetImage( dpy, + (Drawable)bitmap.GetPixmap(), + 0, 0, + bitmap.GetWidth(), bitmap.GetHeight(), + AllPlanes, ZPixmap ); + + wxCHECK_RET( ximage, "couldn't create image" ); + + Create( bitmap.GetWidth(), bitmap.GetHeight() ); + char unsigned *data = GetData(); + + if (!data) + { + XDestroyImage( ximage ); + wxFAIL_MSG( "couldn't create image" ); + return; + } + +/* + GdkImage *gdk_image_mask = (GdkImage*) NULL; + if (bitmap.GetMask()) + { + gdk_image_mask = gdk_image_get( bitmap.GetMask()->GetBitmap(), + 0, 0, + bitmap.GetWidth(), bitmap.GetHeight() ); + + SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable + } +*/ + + // Retrieve depth info + + XVisualInfo vinfo_template; + XVisualInfo *vi; + + vinfo_template.visual = vis; + vinfo_template.visualid = XVisualIDFromVisual( vis ); + vinfo_template.depth = bpp; + int nitem = 0; + + vi = XGetVisualInfo( dpy, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem ); + + if (!vi) + { + printf("no visual.\n" ); + return; + } + + if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15; + + XFree( vi ); + + XColor colors[256]; + if (bpp == 8) + { + Colormap cmap = (Colormap)wxTheApp->GetMainColormap( dpy ); + + for (int i = 0; i < 256; i++) colors[i].pixel = i; + XQueryColors( dpy, cmap, colors, 256 ); + } + + long pos = 0; + for (int j = 0; j < bitmap.GetHeight(); j++) + { + for (int i = 0; i < bitmap.GetWidth(); i++) + { + int pixel = XGetPixel( ximage, i, j ); + if (bpp <= 8) + { + data[pos] = colors[pixel].red >> 8; + data[pos+1] = colors[pixel].green >> 8; + data[pos+2] = colors[pixel].blue >> 8; + } else if (bpp == 15) + { + data[pos] = (pixel >> 7) & 0xf8; + data[pos+1] = (pixel >> 2) & 0xf8; + data[pos+2] = (pixel << 3) & 0xf8; + } else if (bpp == 16) + { + data[pos] = (pixel >> 8) & 0xf8; + data[pos+1] = (pixel >> 3) & 0xfc; + data[pos+2] = (pixel << 3) & 0xf8; + } else + { + data[pos] = (pixel >> 16) & 0xff; + data[pos+1] = (pixel >> 8) & 0xff; + data[pos+2] = pixel & 0xff; + } + +/* + if (gdk_image_mask) + { + int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j ); + if (mask_pixel == 0) + { + data[pos] = 16; + data[pos+1] = 16; + data[pos+2] = 16; + } + } +*/ + + pos += 3; + } + } + + XDestroyImage( ximage ); +/* + if (gdk_image_mask) gdk_image_destroy( gdk_image_mask ); +*/ } #endif -- 2.45.2