X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/185d7c3ee4c6e4a9ddaf811ae38e57d68a0fdff3..5dacf03c57128529219145bafe9cee07d4348e85:/wxPython/src/image.i diff --git a/wxPython/src/image.i b/wxPython/src/image.i index f3155144f1..be25dd688d 100644 --- a/wxPython/src/image.i +++ b/wxPython/src/image.i @@ -15,6 +15,7 @@ %{ #include "helpers.h" +#include "pyistream.h" #include %} @@ -27,10 +28,37 @@ %import _defs.i %import misc.i %import gdi.i +%import streams.i //--------------------------------------------------------------------------- -class wxImageHandler { +class wxImageHistogram /* : public wxImageHistogramBase */ +{ +public: + wxImageHistogram(); + + // get the key in the histogram for the given RGB values + static unsigned long MakeKey(unsigned char r, + unsigned char g, + unsigned char b); + + // find first colour that is not used in the image and has higher + // RGB values than RGB(startR, startG, startB) + // + // returns true and puts this colour in r, g, b (each of which may be NULL) + // on success or returns false if there are no more free colours + bool FindFirstUnusedColour(unsigned char *OUTPUT, + unsigned char *OUTPUT, + unsigned char *OUTPUT, + unsigned char startR = 1, + unsigned char startG = 0, + unsigned char startB = 0 ) const; +}; + + +//--------------------------------------------------------------------------- + +class wxImageHandler : public wxObject { public: // wxImageHandler(); Abstract Base Class wxString GetName(); @@ -40,6 +68,10 @@ public: //bool LoadFile(wxImage* image, wxInputStream& stream); //bool SaveFile(wxImage* image, wxOutputStream& stream); + //virtual int GetImageCount( wxInputStream& stream ); + //bool CanRead( wxInputStream& stream ); + + bool CanRead( const wxString& name ); void SetName(const wxString& name); void SetExtension(const wxString& extension); @@ -66,6 +98,20 @@ public: wxBMPHandler(); }; +class wxICOHandler : public wxBMPHandler { +public: + wxICOHandler(); +}; + +class wxCURHandler : public wxICOHandler { +public: + wxCURHandler(); +}; + +class wxANIHandler : public wxCURHandler { +public: + wxANIHandler(); +}; class wxGIFHandler : public wxImageHandler { public: @@ -88,17 +134,19 @@ public: }; + //--------------------------------------------------------------------------- -class wxImage { +class wxImage : public wxObject { public: - wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY ); + wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ); ~wxImage(); - wxBitmap ConvertToBitmap(); void Create( int width, int height ); void Destroy(); + wxImage Scale( int width, int height ); + wxImage ShrinkBy( int xFactor , int yFactor ) const ; wxImage& Rescale(int width, int height); void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b ); @@ -106,12 +154,38 @@ public: unsigned char GetGreen( int x, int y ); unsigned char GetBlue( int x, int y ); - bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_PNG ); - %name(LoadMimeFile)bool LoadFile( const wxString& name, const wxString& mimetype ); + void SetAlpha(int x, int y, unsigned char alpha); + unsigned char GetAlpha(int x, int y); + bool HasAlpha(); + + // find first colour that is not used in the image and has higher + // RGB values than + bool FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT, + byte startR = 0, byte startG = 0, byte startB = 0 ) const; + + // Set image's mask to the area of 'mask' that has colour + bool SetMaskFromImage(const wxImage & mask, + byte mr, byte mg, byte mb); + +// void DoFloodFill (wxCoord x, wxCoord y, +// const wxBrush & fillBrush, +// const wxColour& testColour, +// int style = wxFLOOD_SURFACE, +// int LogicalFunction = wxCOPY /* currently unused */ ) ; + + static bool CanRead( const wxString& name ); + static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY ); + + bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 ); + %name(LoadMimeFile)bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 ); bool SaveFile( const wxString& name, int type ); %name(SaveMimeFile)bool SaveFile( const wxString& name, const wxString& mimetype ); + %name(CanReadStream) static bool CanRead( wxInputStream& stream ); + %name(LoadStream) bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 ); + %name(LoadMimeStream) bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 ); + bool Ok(); int GetWidth(); int GetHeight(); @@ -129,7 +203,6 @@ public: int len = self->GetWidth() * self->GetHeight() * 3; return PyString_FromStringAndSize((char*)data, len); } - void SetData(PyObject* data) { unsigned char* dataPtr; @@ -142,6 +215,75 @@ public: dataPtr = (unsigned char*) malloc(len); memcpy(dataPtr, PyString_AsString(data), len); self->SetData(dataPtr); + // wxImage takes ownership of dataPtr... + } + + + + PyObject* GetDataBuffer() { + unsigned char* data = self->GetData(); + int len = self->GetWidth() * self->GetHeight() * 3; + return PyBuffer_FromReadWriteMemory(data, len); + } + void SetDataBuffer(PyObject* data) { + unsigned char* buffer; + int size; + + if (!PyArg_Parse(data, "w#", &buffer, &size)) + return; + + if (size != self->GetWidth() * self->GetHeight() * 3) { + PyErr_SetString(PyExc_TypeError, "Incorrect buffer size"); + return; + } + self->SetData(buffer); + } + + + + PyObject* GetAlphaData() { + unsigned char* data = self->GetAlpha(); + if (! data) { + RETURN_NONE(); + } else { + int len = self->GetWidth() * self->GetHeight(); + return PyString_FromStringAndSize((char*)data, len); + } + } + void SetAlphaData(PyObject* data) { + unsigned char* dataPtr; + + if (! PyString_Check(data)) { + PyErr_SetString(PyExc_TypeError, "Expected string object"); + return /* NULL */ ; + } + + size_t len = self->GetWidth() * self->GetHeight(); + dataPtr = (unsigned char*) malloc(len); + memcpy(dataPtr, PyString_AsString(data), len); + self->SetAlpha(dataPtr); + // wxImage takes ownership of dataPtr... + } + + + + PyObject* GetAlphaBuffer() { + unsigned char* data = self->GetAlpha(); + int len = self->GetWidth() * self->GetHeight(); + return PyBuffer_FromReadWriteMemory(data, len); + } + void SetAlphaBuffer(PyObject* data) { + unsigned char* buffer; + int size; + + if (!PyArg_Parse(data, "w#", &buffer, &size)) + return; + + if (size != self->GetWidth() * self->GetHeight()) { + PyErr_SetString(PyExc_TypeError, "Incorrect buffer size"); + return; + } + self->SetAlpha(buffer); } } @@ -160,43 +302,125 @@ public: void Replace( unsigned char r1, unsigned char g1, unsigned char b1, unsigned char r2, unsigned char g2, unsigned char b2 ); + // convert to monochrome image ( will be replaced by white, everything else by black) + wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const; + + void SetOption(const wxString& name, const wxString& value); + %name(SetOptionInt)void SetOption(const wxString& name, int value); + wxString GetOption(const wxString& name) const; + int GetOptionInt(const wxString& name) const; + bool HasOption(const wxString& name) const; + unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ); - // TODO: unsigned long ComputeHistogram( wxHashTable &h ); + unsigned long ComputeHistogram( wxImageHistogram& h ); + + static void AddHandler( wxImageHandler *handler ); + static void InsertHandler( wxImageHandler *handler ); + static bool RemoveHandler( const wxString& name ); + static wxString GetImageExtWildcard(); + + + %addmethods { + wxBitmap ConvertToBitmap() { + wxBitmap bitmap(*self); + return bitmap; + } + wxBitmap ConvertToMonoBitmap( unsigned char red, + unsigned char green, + unsigned char blue ) { + wxImage mono = self->ConvertToMono( red, green, blue ); + wxBitmap bitmap( mono, 1 ); + return bitmap; + } + } + + %pragma(python) addtoclass = "def __nonzero__(self): return self.Ok()" }; + // Alternate constructors -%new wxImage* wxNullImage(); -%new wxImage* wxEmptyImage(int width, int height); -%new wxImage* wxImageFromMime(const wxString& name, const wxString& mimetype); +%new wxImage* wxEmptyImage(int width=0, int height=0, bool clear = TRUE); +%new wxImage* wxImageFromMime(const wxString& name, const wxString& mimetype, int index = -1); %new wxImage* wxImageFromBitmap(const wxBitmap &bitmap); +%new wxImage* wxImageFromData(int width, int height, unsigned char* data); +%new wxImage* wxImageFromStream(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1); +%new wxImage* wxImageFromStreamMime(wxInputStream& stream, const wxString& mimetype, int index = -1 ); + %{ - wxImage* wxNullImage() { - return new wxImage; + wxImage* wxEmptyImage(int width=0, int height=0, bool clear = TRUE) { + if (width > 0 && height > 0) + return new wxImage(width, height, clear); + else + return new wxImage; } - wxImage* wxEmptyImage(int width, int height) { - return new wxImage(width, height); - } - wxImage* wxImageFromMime(const wxString& name, const wxString& mimetype) { - return new wxImage(name, mimetype); + wxImage* wxImageFromMime(const wxString& name, const wxString& mimetype, int index) { + return new wxImage(name, mimetype, index); } + wxImage* wxImageFromBitmap(const wxBitmap &bitmap) { - return new wxImage(bitmap); + return new wxImage(bitmap.ConvertToImage()); } -%} -// Static Methods -void wxImage_AddHandler(wxImageHandler *handler); -%{ - void wxImage_AddHandler(wxImageHandler *handler) { - wxImage::AddHandler(handler); + + wxImage* wxImageFromData(int width, int height, unsigned char* data) { + // Copy the source data so the wxImage can clean it up later + unsigned char* copy = (unsigned char*)malloc(width*height*3); + if (copy == NULL) { + PyErr_NoMemory(); + return NULL; + } + memcpy(copy, data, width*height*3); + return new wxImage(width, height, copy, FALSE); + } + + + wxImage* wxImageFromStream(wxInputStream& stream, + long type = wxBITMAP_TYPE_ANY, int index = -1) { + return new wxImage(stream, type, index); + } + + + wxImage* wxImageFromStreamMime(wxInputStream& stream, + const wxString& mimetype, int index = -1 ) { + return new wxImage(stream, mimetype, index); } %} + + void wxInitAllImageHandlers(); + +%readonly +%{ +#if 0 +%} + +// See also wxPy_ReinitStockObjects in helpers.cpp +extern wxImage wxNullImage; + +%readwrite +%{ +#endif +%} + + + //--------------------------------------------------------------------------- +// This one is here to avoid circular imports + +%new wxBitmap* wxBitmapFromImage(const wxImage& img, int depth=-1); + +%{ + wxBitmap* wxBitmapFromImage(const wxImage& img, int depth=-1) { + return new wxBitmap(img, depth); + } + +%} + + //---------------------------------------------------------------------------