]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/src/image.i
added functions to read/write several elements at once (patch 754986)
[wxWidgets.git] / wxPython / src / image.i
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: image.i
3// Purpose: SWIG interface file for wxImage, wxImageHandler, etc.
4//
5// Author: Robin Dunn
6//
7// Created: 28-Apr-1999
8// RCS-ID: $Id$
9// Copyright: (c) 1998 by Total Control Software
10// Licence: wxWindows license
11/////////////////////////////////////////////////////////////////////////////
12
13
14%module image
15
16%{
17#include "helpers.h"
18#include "pyistream.h"
19#include <wx/image.h>
20%}
21
22//----------------------------------------------------------------------
23
24%include typemaps.i
25%include my_typemaps.i
26
27// Import some definitions of other classes, etc.
28%import _defs.i
29%import misc.i
30%import gdi.i
31%import streams.i
32
33//---------------------------------------------------------------------------
34
35class wxImageHistogram /* : public wxImageHistogramBase */
36{
37public:
38 wxImageHistogram();
39
40 // get the key in the histogram for the given RGB values
41 static unsigned long MakeKey(unsigned char r,
42 unsigned char g,
43 unsigned char b);
44
45 // find first colour that is not used in the image and has higher
46 // RGB values than RGB(startR, startG, startB)
47 //
48 // returns true and puts this colour in r, g, b (each of which may be NULL)
49 // on success or returns false if there are no more free colours
50 bool FindFirstUnusedColour(unsigned char *OUTPUT,
51 unsigned char *OUTPUT,
52 unsigned char *OUTPUT,
53 unsigned char startR = 1,
54 unsigned char startG = 0,
55 unsigned char startB = 0 ) const;
56};
57
58
59//---------------------------------------------------------------------------
60
61class wxImageHandler : public wxObject {
62public:
63 // wxImageHandler(); Abstract Base Class
64 wxString GetName();
65 wxString GetExtension();
66 long GetType();
67 wxString GetMimeType();
68
69 //bool LoadFile(wxImage* image, wxInputStream& stream);
70 //bool SaveFile(wxImage* image, wxOutputStream& stream);
71 //virtual int GetImageCount( wxInputStream& stream );
72 //bool CanRead( wxInputStream& stream );
73
74 bool CanRead( const wxString& name );
75
76 void SetName(const wxString& name);
77 void SetExtension(const wxString& extension);
78 void SetType(long type);
79 void SetMimeType(const wxString& mimetype);
80};
81
82//---------------------------------------------------------------------------
83
84class wxPNGHandler : public wxImageHandler {
85public:
86 wxPNGHandler();
87};
88
89
90class wxJPEGHandler : public wxImageHandler {
91public:
92 wxJPEGHandler();
93};
94
95
96class wxBMPHandler : public wxImageHandler {
97public:
98 wxBMPHandler();
99};
100
101class wxICOHandler : public wxBMPHandler {
102public:
103 wxICOHandler();
104};
105
106class wxCURHandler : public wxICOHandler {
107public:
108 wxCURHandler();
109};
110
111class wxANIHandler : public wxCURHandler {
112public:
113 wxANIHandler();
114};
115
116class wxGIFHandler : public wxImageHandler {
117public:
118 wxGIFHandler();
119};
120
121class wxPNMHandler : public wxImageHandler {
122public:
123 wxPNMHandler();
124};
125
126class wxPCXHandler : public wxImageHandler {
127public:
128 wxPCXHandler();
129};
130
131class wxTIFFHandler : public wxImageHandler {
132public:
133 wxTIFFHandler();
134};
135
136
137
138//---------------------------------------------------------------------------
139
140class wxImage : public wxObject {
141public:
142 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
143 ~wxImage();
144
145 void Create( int width, int height );
146 void Destroy();
147
148 wxImage Scale( int width, int height );
149 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
150 wxImage& Rescale(int width, int height);
151
152 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
153 unsigned char GetRed( int x, int y );
154 unsigned char GetGreen( int x, int y );
155 unsigned char GetBlue( int x, int y );
156
157 void SetAlpha(int x, int y, unsigned char alpha);
158 unsigned char GetAlpha(int x, int y);
159 bool HasAlpha();
160
161 // find first colour that is not used in the image and has higher
162 // RGB values than <startR,startG,startB>
163 bool FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
164 byte startR = 0, byte startG = 0, byte startB = 0 ) const;
165
166 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
167 bool SetMaskFromImage(const wxImage & mask,
168 byte mr, byte mg, byte mb);
169
170// void DoFloodFill (wxCoord x, wxCoord y,
171// const wxBrush & fillBrush,
172// const wxColour& testColour,
173// int style = wxFLOOD_SURFACE,
174// int LogicalFunction = wxCOPY /* currently unused */ ) ;
175
176 static bool CanRead( const wxString& name );
177 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
178
179 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
180 %name(LoadMimeFile)bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
181
182 bool SaveFile( const wxString& name, int type );
183 %name(SaveMimeFile)bool SaveFile( const wxString& name, const wxString& mimetype );
184
185 %name(CanReadStream) static bool CanRead( wxInputStream& stream );
186 %name(LoadStream) bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
187 %name(LoadMimeStream) bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
188
189 bool Ok();
190 int GetWidth();
191 int GetHeight();
192
193 wxImage GetSubImage(const wxRect& rect);
194 wxImage Copy();
195 void Paste( const wxImage &image, int x, int y );
196
197 //unsigned char *GetData();
198 //void SetData( unsigned char *data );
199
200 %addmethods {
201 PyObject* GetData() {
202 unsigned char* data = self->GetData();
203 int len = self->GetWidth() * self->GetHeight() * 3;
204 return PyString_FromStringAndSize((char*)data, len);
205 }
206 void SetData(PyObject* data) {
207 unsigned char* dataPtr;
208
209 if (! PyString_Check(data)) {
210 PyErr_SetString(PyExc_TypeError, "Expected string object");
211 return /* NULL */ ;
212 }
213
214 size_t len = self->GetWidth() * self->GetHeight() * 3;
215 dataPtr = (unsigned char*) malloc(len);
216 memcpy(dataPtr, PyString_AsString(data), len);
217 self->SetData(dataPtr);
218 // wxImage takes ownership of dataPtr...
219 }
220
221
222
223 PyObject* GetDataBuffer() {
224 unsigned char* data = self->GetData();
225 int len = self->GetWidth() * self->GetHeight() * 3;
226 return PyBuffer_FromReadWriteMemory(data, len);
227 }
228 void SetDataBuffer(PyObject* data) {
229 unsigned char* buffer;
230 int size;
231
232 if (!PyArg_Parse(data, "w#", &buffer, &size))
233 return;
234
235 if (size != self->GetWidth() * self->GetHeight() * 3) {
236 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
237 return;
238 }
239 self->SetData(buffer);
240 }
241
242
243
244 PyObject* GetAlphaData() {
245 unsigned char* data = self->GetAlpha();
246 if (! data) {
247 RETURN_NONE();
248 } else {
249 int len = self->GetWidth() * self->GetHeight();
250 return PyString_FromStringAndSize((char*)data, len);
251 }
252 }
253 void SetAlphaData(PyObject* data) {
254 unsigned char* dataPtr;
255
256 if (! PyString_Check(data)) {
257 PyErr_SetString(PyExc_TypeError, "Expected string object");
258 return /* NULL */ ;
259 }
260
261 size_t len = self->GetWidth() * self->GetHeight();
262 dataPtr = (unsigned char*) malloc(len);
263 memcpy(dataPtr, PyString_AsString(data), len);
264 self->SetAlpha(dataPtr);
265 // wxImage takes ownership of dataPtr...
266 }
267
268
269
270 PyObject* GetAlphaBuffer() {
271 unsigned char* data = self->GetAlpha();
272 int len = self->GetWidth() * self->GetHeight();
273 return PyBuffer_FromReadWriteMemory(data, len);
274 }
275 void SetAlphaBuffer(PyObject* data) {
276 unsigned char* buffer;
277 int size;
278
279 if (!PyArg_Parse(data, "w#", &buffer, &size))
280 return;
281
282 if (size != self->GetWidth() * self->GetHeight()) {
283 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
284 return;
285 }
286 self->SetAlpha(buffer);
287 }
288 }
289
290 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
291 unsigned char GetMaskRed();
292 unsigned char GetMaskGreen();
293 unsigned char GetMaskBlue();
294 void SetMask( bool mask = TRUE );
295 bool HasMask();
296
297 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
298 bool interpolating = TRUE, wxPoint * offset_after_rotation = NULL) const ;
299 wxImage Rotate90( bool clockwise = TRUE ) ;
300 wxImage Mirror( bool horizontally = TRUE ) ;
301
302 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
303 unsigned char r2, unsigned char g2, unsigned char b2 );
304
305 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
306 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
307
308 void SetOption(const wxString& name, const wxString& value);
309 %name(SetOptionInt)void SetOption(const wxString& name, int value);
310 wxString GetOption(const wxString& name) const;
311 int GetOptionInt(const wxString& name) const;
312 bool HasOption(const wxString& name) const;
313
314 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
315 unsigned long ComputeHistogram( wxImageHistogram& h );
316
317 static void AddHandler( wxImageHandler *handler );
318 static void InsertHandler( wxImageHandler *handler );
319 static bool RemoveHandler( const wxString& name );
320
321
322 %addmethods {
323 wxBitmap ConvertToBitmap() {
324 wxBitmap bitmap(*self);
325 return bitmap;
326 }
327
328 wxBitmap ConvertToMonoBitmap( unsigned char red,
329 unsigned char green,
330 unsigned char blue ) {
331 wxImage mono = self->ConvertToMono( red, green, blue );
332 wxBitmap bitmap( mono, 1 );
333 return bitmap;
334 }
335 }
336
337 %pragma(python) addtoclass = "def __nonzero__(self): return self.Ok()"
338};
339
340
341// Alternate constructors
342%new wxImage* wxEmptyImage(int width=0, int height=0);
343%new wxImage* wxImageFromMime(const wxString& name, const wxString& mimetype, int index = -1);
344%new wxImage* wxImageFromBitmap(const wxBitmap &bitmap);
345%new wxImage* wxImageFromData(int width, int height, unsigned char* data);
346%new wxImage* wxImageFromStream(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1);
347%new wxImage* wxImageFromStreamMime(wxInputStream& stream, const wxString& mimetype, int index = -1 );
348
349%{
350 wxImage* wxEmptyImage(int width=0, int height=0) {
351 if (width == 0 && height == 0)
352 return new wxImage;
353 else
354 return new wxImage(width, height);
355 }
356
357
358 wxImage* wxImageFromMime(const wxString& name, const wxString& mimetype, int index) {
359 return new wxImage(name, mimetype, index);
360 }
361
362
363 wxImage* wxImageFromBitmap(const wxBitmap &bitmap) {
364 return new wxImage(bitmap.ConvertToImage());
365 }
366
367
368 wxImage* wxImageFromData(int width, int height, unsigned char* data) {
369 // Copy the source data so the wxImage can clean it up later
370 unsigned char* copy = (unsigned char*)malloc(width*height*3);
371 if (copy == NULL) {
372 PyErr_NoMemory();
373 return NULL;
374 }
375 memcpy(copy, data, width*height*3);
376 return new wxImage(width, height, copy, FALSE);
377 }
378
379
380 wxImage* wxImageFromStream(wxInputStream& stream,
381 long type = wxBITMAP_TYPE_ANY, int index = -1) {
382 return new wxImage(stream, type, index);
383 }
384
385
386 wxImage* wxImageFromStreamMime(wxInputStream& stream,
387 const wxString& mimetype, int index = -1 ) {
388 return new wxImage(stream, mimetype, index);
389 }
390%}
391
392
393
394void wxInitAllImageHandlers();
395
396
397%readonly
398%{
399#if 0
400%}
401
402extern wxImage wxNullImage;
403
404%readwrite
405%{
406#endif
407%}
408
409
410
411//---------------------------------------------------------------------------
412// This one is here to avoid circular imports
413
414%new wxBitmap* wxBitmapFromImage(const wxImage& img, int depth=-1);
415
416%{
417 wxBitmap* wxBitmapFromImage(const wxImage& img, int depth=-1) {
418 return new wxBitmap(img, depth);
419 }
420
421%}
422
423
424//---------------------------------------------------------------------------