more wxImage on Motif
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1394 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -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
|
wxBitmap object must be created from it, and that bitmap drawn on the wxDC, using
|
||||||
wxDC::DrawBitmap.
|
wxDC::DrawBitmap.
|
||||||
|
|
||||||
This class is currently only available under GTK and Windows.
|
|
||||||
|
|
||||||
\wxheading{Derived from}
|
\wxheading{Derived from}
|
||||||
|
|
||||||
\helpref{wxObject}{wxobject}
|
\helpref{wxObject}{wxobject}
|
||||||
|
@@ -99,32 +99,20 @@ MyCanvas::MyCanvas( wxWindow *parent, const wxWindowID id,
|
|||||||
dir = wxString("../");
|
dir = wxString("../");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//#ifdef __WXMOTIF__
|
|
||||||
dir = wxString("../");
|
|
||||||
//#endif
|
|
||||||
|
|
||||||
#ifndef __WXMOTIF__
|
|
||||||
wxImage image( bitmap );
|
wxImage image( bitmap );
|
||||||
image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
|
image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
|
||||||
#else
|
|
||||||
wxImage image;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG );
|
image.LoadFile( dir + wxString("horse.png"), wxBITMAP_TYPE_PNG );
|
||||||
my_horse = new wxBitmap( image.ConvertToBitmap() );
|
my_horse = new wxBitmap( image.ConvertToBitmap() );
|
||||||
|
|
||||||
#ifndef __WXMOTIF__
|
|
||||||
image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
|
image.LoadFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG );
|
||||||
my_square = new wxBitmap( image.ConvertToBitmap() );
|
my_square = new wxBitmap( image.ConvertToBitmap() );
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MyCanvas::~MyCanvas()
|
MyCanvas::~MyCanvas()
|
||||||
{
|
{
|
||||||
delete my_horse;
|
delete my_horse;
|
||||||
#ifndef __WXMOTIF__
|
|
||||||
delete my_square;
|
delete my_square;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
|
void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
|
||||||
@@ -132,7 +120,6 @@ void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
|
|||||||
wxPaintDC dc( this );
|
wxPaintDC dc( this );
|
||||||
PrepareDC( dc );
|
PrepareDC( dc );
|
||||||
|
|
||||||
#ifndef __WXMOTIF__
|
|
||||||
dc.DrawText( "Loaded image", 30, 100 );
|
dc.DrawText( "Loaded image", 30, 100 );
|
||||||
if (my_square->Ok()) dc.DrawBitmap( *my_square, 30, 120 );
|
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.SetBrush( wxBrush( "orange", wxSOLID ) );
|
||||||
dc.SetPen( *wxWHITE_PEN );
|
dc.SetPen( *wxWHITE_PEN );
|
||||||
dc.DrawRectangle( 150, 120, 100, 100 );
|
dc.DrawRectangle( 150, 120, 100, 100 );
|
||||||
#endif
|
|
||||||
|
|
||||||
if (my_horse->Ok()) dc.DrawBitmap( *my_horse, 30, 240 );
|
if (my_horse->Ok()) dc.DrawBitmap( *my_horse, 30, 240 );
|
||||||
}
|
}
|
||||||
|
@@ -1417,8 +1417,7 @@ wxBitmap wxImage::ConvertToBitmap() const
|
|||||||
int pixel = -1;
|
int pixel = -1;
|
||||||
if (wxTheApp->m_colorCube)
|
if (wxTheApp->m_colorCube)
|
||||||
{
|
{
|
||||||
pixel = wxTheApp->m_colorCube
|
pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
|
||||||
[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -1588,6 +1587,7 @@ wxImage::wxImage( const wxBitmap &bitmap )
|
|||||||
|
|
||||||
#include <Xm/Xm.h>
|
#include <Xm/Xm.h>
|
||||||
#include "wx/utils.h"
|
#include "wx/utils.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
wxBitmap wxImage::ConvertToBitmap() const
|
wxBitmap wxImage::ConvertToBitmap() const
|
||||||
{
|
{
|
||||||
@@ -1630,8 +1630,6 @@ wxBitmap wxImage::ConvertToBitmap() const
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// bitmap.SetDepth( bpp );
|
|
||||||
|
|
||||||
// Retrieve depth info
|
// Retrieve depth info
|
||||||
|
|
||||||
XVisualInfo vinfo_template;
|
XVisualInfo vinfo_template;
|
||||||
@@ -1650,6 +1648,8 @@ wxBitmap wxImage::ConvertToBitmap() const
|
|||||||
return wxNullBitmap;
|
return wxNullBitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
XFree( vi );
|
||||||
|
|
||||||
if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
|
if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
|
||||||
if (bpp < 8) bpp = 8;
|
if (bpp < 8) bpp = 8;
|
||||||
|
|
||||||
@@ -1674,6 +1674,15 @@ wxBitmap wxImage::ConvertToBitmap() const
|
|||||||
int b_mask = GetMaskBlue();
|
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();
|
unsigned char* data = GetData();
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@@ -1702,8 +1711,8 @@ wxBitmap wxImage::ConvertToBitmap() const
|
|||||||
{
|
{
|
||||||
case 8:
|
case 8:
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
int pixel = -1;
|
int pixel = -1;
|
||||||
|
/*
|
||||||
if (wxTheApp->m_colorCube)
|
if (wxTheApp->m_colorCube)
|
||||||
{
|
{
|
||||||
pixel = wxTheApp->m_colorCube
|
pixel = wxTheApp->m_colorCube
|
||||||
@@ -1711,23 +1720,20 @@ wxBitmap wxImage::ConvertToBitmap() const
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
GdkColormap *cmap = gtk_widget_get_default_colormap();
|
*/
|
||||||
GdkColor *colors = cmap->colors;
|
|
||||||
int max = 3 * (65536);
|
int max = 3 * (65536);
|
||||||
|
for (int i = 0; i < 256; i++)
|
||||||
for (int i = 0; i < cmap->size; i++)
|
|
||||||
{
|
{
|
||||||
int rdiff = (r << 8) - colors[i].red;
|
int rdiff = (r << 8) - colors[i].red;
|
||||||
int gdiff = (g << 8) - colors[i].green;
|
int gdiff = (g << 8) - colors[i].green;
|
||||||
int bdiff = (b << 8) - colors[i].blue;
|
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; }
|
if (sum < max) { pixel = i; max = sum; }
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
}
|
}
|
||||||
|
|
||||||
gdk_image_put_pixel( data_image, x, y, pixel );
|
|
||||||
*/
|
*/
|
||||||
|
XPutPixel( data_image, x, y, pixel );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 15:
|
case 15:
|
||||||
@@ -1791,6 +1797,121 @@ wxBitmap wxImage::ConvertToBitmap() const
|
|||||||
|
|
||||||
wxImage::wxImage( const wxBitmap &bitmap )
|
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
|
#endif
|
||||||
|
Reference in New Issue
Block a user