]> git.saurik.com Git - wxWidgets.git/blob - wxPython/src/_image.i
Optimized sizers to not call CalcMin more often than neccessary
[wxWidgets.git] / wxPython / src / _image.i
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: _image.i
3 // Purpose: SWIG definitions for wxImage and such
4 //
5 // Author: Robin Dunn
6 //
7 // Created: 25-Sept-2000
8 // RCS-ID: $Id$
9 // Copyright: (c) 2003 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 // Not a %module
14
15
16 //---------------------------------------------------------------------------
17
18 %{
19 #include "wx/wxPython/pyistream.h"
20 %}
21
22 //---------------------------------------------------------------------------
23 %newgroup
24
25
26 class wxImageHandler : public wxObject {
27 public:
28 // wxImageHandler(); Abstract Base Class
29 wxString GetName();
30 wxString GetExtension();
31 long GetType();
32 wxString GetMimeType();
33
34 //bool LoadFile(wxImage* image, wxInputStream& stream);
35 //bool SaveFile(wxImage* image, wxOutputStream& stream);
36 //virtual int GetImageCount( wxInputStream& stream );
37 //bool CanRead( wxInputStream& stream );
38
39 bool CanRead( const wxString& name );
40
41 void SetName(const wxString& name);
42 void SetExtension(const wxString& extension);
43 void SetType(long type);
44 void SetMimeType(const wxString& mimetype);
45 };
46
47
48 //---------------------------------------------------------------------------
49
50 class wxImageHistogram /* : public wxImageHistogramBase */
51 {
52 public:
53 wxImageHistogram();
54
55 DocStr(MakeKey, "Get the key in the histogram for the given RGB values", "");
56 static unsigned long MakeKey(unsigned char r,
57 unsigned char g,
58 unsigned char b);
59
60 DocDeclAStr(
61 bool, FindFirstUnusedColour(unsigned char *OUTPUT,
62 unsigned char *OUTPUT,
63 unsigned char *OUTPUT,
64 unsigned char startR = 1,
65 unsigned char startG = 0,
66 unsigned char startB = 0 ) const,
67 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
68 "Find first colour that is not used in the image and has higher RGB
69 values than startR, startG, startB. Returns a tuple consisting of a
70 success flag and rgb values.", "");
71 };
72
73
74 //---------------------------------------------------------------------------
75
76
77 class wxImage : public wxObject {
78 public:
79 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
80 ~wxImage();
81
82 // Alternate constructors
83 %name(ImageFromMime) wxImage(const wxString& name, const wxString& mimetype, int index = -1);
84 %name(ImageFromStream) wxImage(wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1);
85 %name(ImageFromStreamMime) wxImage(wxInputStream& stream, const wxString& mimetype, int index = -1 );
86 %extend {
87 %name(EmptyImage) wxImage(int width=0, int height=0, bool clear = True) {
88 if (width > 0 && height > 0)
89 return new wxImage(width, height, clear);
90 else
91 return new wxImage;
92 }
93
94 MustHaveApp(wxImage(const wxBitmap &bitmap));
95 %name(ImageFromBitmap) wxImage(const wxBitmap &bitmap) {
96 return new wxImage(bitmap.ConvertToImage());
97 }
98
99 %name(ImageFromData) wxImage(int width, int height, unsigned char* data) {
100 // Copy the source data so the wxImage can clean it up later
101 unsigned char* copy = (unsigned char*)malloc(width*height*3);
102 if (copy == NULL) {
103 PyErr_NoMemory();
104 return NULL;
105 }
106 memcpy(copy, data, width*height*3);
107 return new wxImage(width, height, copy, False);
108 }
109 }
110
111 void Create( int width, int height );
112 void Destroy();
113
114 wxImage Scale( int width, int height );
115 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
116 wxImage& Rescale(int width, int height);
117
118 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
119 unsigned char GetRed( int x, int y );
120 unsigned char GetGreen( int x, int y );
121 unsigned char GetBlue( int x, int y );
122
123 void SetAlpha(int x, int y, unsigned char alpha);
124 unsigned char GetAlpha(int x, int y);
125 bool HasAlpha();
126
127 // find first colour that is not used in the image and has higher
128 // RGB values than <startR,startG,startB>
129 DocDeclAStr(
130 bool, FindFirstUnusedColour( byte *OUTPUT, byte *OUTPUT, byte *OUTPUT,
131 byte startR = 0, byte startG = 0, byte startB = 0 ) const,
132 "FindFirstUnusedColour(int startR=1, int startG=0, int startB=0) -> (success, r, g, b)",
133 "Find first colour that is not used in the image and has higher RGB
134 values than startR, startG, startB. Returns a tuple consisting of a
135 success flag and rgb values.", "");
136
137
138 // Set image's mask to the area of 'mask' that has <mr,mg,mb> colour
139 bool SetMaskFromImage(const wxImage & mask,
140 byte mr, byte mg, byte mb);
141
142 // void DoFloodFill (wxCoord x, wxCoord y,
143 // const wxBrush & fillBrush,
144 // const wxColour& testColour,
145 // int style = wxFLOOD_SURFACE,
146 // int LogicalFunction = wxCOPY /* currently unused */ ) ;
147
148 static bool CanRead( const wxString& name );
149 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
150
151 bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
152 %name(LoadMimeFile)bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
153
154 bool SaveFile( const wxString& name, int type );
155 %name(SaveMimeFile)bool SaveFile( const wxString& name, const wxString& mimetype );
156
157 %name(CanReadStream) static bool CanRead( wxInputStream& stream );
158 %name(LoadStream) bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
159 %name(LoadMimeStream) bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
160
161 bool Ok();
162 int GetWidth();
163 int GetHeight();
164
165 %extend {
166 wxSize GetSize() {
167 wxSize size(self->GetWidth(), self->GetHeight());
168 return size;
169 }
170 }
171
172 wxImage GetSubImage(const wxRect& rect);
173 wxImage Copy();
174 void Paste( const wxImage &image, int x, int y );
175
176 //unsigned char *GetData();
177 //void SetData( unsigned char *data );
178
179 %extend {
180 PyObject* GetData() {
181 unsigned char* data = self->GetData();
182 int len = self->GetWidth() * self->GetHeight() * 3;
183 PyObject* rv;
184 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len));
185 return rv;
186 }
187 void SetData(PyObject* data) {
188 unsigned char* dataPtr;
189
190 if (! PyString_Check(data)) {
191 PyErr_SetString(PyExc_TypeError, "Expected string object");
192 return /* NULL */ ;
193 }
194
195 size_t len = self->GetWidth() * self->GetHeight() * 3;
196 dataPtr = (unsigned char*) malloc(len);
197 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
198 self->SetData(dataPtr);
199 // wxImage takes ownership of dataPtr...
200 }
201
202
203
204 PyObject* GetDataBuffer() {
205 unsigned char* data = self->GetData();
206 int len = self->GetWidth() * self->GetHeight() * 3;
207 PyObject* rv;
208 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
209 return rv;
210 }
211 void SetDataBuffer(PyObject* data) {
212 unsigned char* buffer;
213 int size;
214
215 bool blocked = wxPyBeginBlockThreads();
216 if (!PyArg_Parse(data, "t#", &buffer, &size))
217 goto done;
218
219 if (size != self->GetWidth() * self->GetHeight() * 3) {
220 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
221 goto done;
222 }
223 self->SetData(buffer);
224 done:
225 wxPyEndBlockThreads(blocked);
226 }
227
228
229
230 PyObject* GetAlphaData() {
231 unsigned char* data = self->GetAlpha();
232 if (! data) {
233 RETURN_NONE();
234 } else {
235 int len = self->GetWidth() * self->GetHeight();
236 PyObject* rv;
237 wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) );
238 return rv;
239 }
240 }
241 void SetAlphaData(PyObject* data) {
242 unsigned char* dataPtr;
243
244 if (! PyString_Check(data)) {
245 PyErr_SetString(PyExc_TypeError, "Expected string object");
246 return /* NULL */ ;
247 }
248
249 size_t len = self->GetWidth() * self->GetHeight();
250 dataPtr = (unsigned char*) malloc(len);
251 wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) );
252 self->SetAlpha(dataPtr);
253 // wxImage takes ownership of dataPtr...
254 }
255
256
257
258 PyObject* GetAlphaBuffer() {
259 unsigned char* data = self->GetAlpha();
260 int len = self->GetWidth() * self->GetHeight();
261 PyObject* rv;
262 wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) );
263 return rv;
264 }
265 void SetAlphaBuffer(PyObject* data) {
266 unsigned char* buffer;
267 int size;
268
269 bool blocked = wxPyBeginBlockThreads();
270 if (!PyArg_Parse(data, "t#", &buffer, &size))
271 goto done;
272
273 if (size != self->GetWidth() * self->GetHeight()) {
274 PyErr_SetString(PyExc_TypeError, "Incorrect buffer size");
275 goto done;
276 }
277 self->SetAlpha(buffer);
278 done:
279 wxPyEndBlockThreads(blocked);
280 }
281 }
282
283 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
284 unsigned char GetMaskRed();
285 unsigned char GetMaskGreen();
286 unsigned char GetMaskBlue();
287 void SetMask( bool mask = True );
288 bool HasMask();
289
290 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
291 bool interpolating = True, wxPoint * offset_after_rotation = NULL) const ;
292 wxImage Rotate90( bool clockwise = True ) ;
293 wxImage Mirror( bool horizontally = True ) ;
294
295 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
296 unsigned char r2, unsigned char g2, unsigned char b2 );
297
298 // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
299 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
300
301 void SetOption(const wxString& name, const wxString& value);
302 %name(SetOptionInt)void SetOption(const wxString& name, int value);
303 wxString GetOption(const wxString& name) const;
304 int GetOptionInt(const wxString& name) const;
305 bool HasOption(const wxString& name) const;
306
307 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 );
308 unsigned long ComputeHistogram( wxImageHistogram& h );
309
310 static void AddHandler( wxImageHandler *handler );
311 static void InsertHandler( wxImageHandler *handler );
312 static bool RemoveHandler( const wxString& name );
313 static wxString GetImageExtWildcard();
314
315
316 MustHaveApp(ConvertToBitmap);
317 MustHaveApp(ConvertToMonoBitmap);
318
319 %extend {
320 wxBitmap ConvertToBitmap() {
321 wxBitmap bitmap(*self);
322 return bitmap;
323 }
324
325 wxBitmap ConvertToMonoBitmap( unsigned char red,
326 unsigned char green,
327 unsigned char blue ) {
328 wxImage mono = self->ConvertToMono( red, green, blue );
329 wxBitmap bitmap( mono, 1 );
330 return bitmap;
331 }
332 }
333
334 %pythoncode { def __nonzero__(self): return self.Ok() }
335 };
336
337
338
339 ///void wxInitAllImageHandlers();
340
341 %pythoncode {
342 def InitAllImageHandlers():
343 """
344 The former functionality of InitAllImageHanders is now done internal to
345 the _core_ extension module and so this function has become a simple NOP.
346 """
347 pass
348 }
349
350
351
352 // See also wxPy_ReinitStockObjects in helpers.cpp
353 %immutable;
354 const wxImage wxNullImage;
355 %mutable;
356
357 //---------------------------------------------------------------------------
358
359
360 MAKE_CONST_WXSTRING(IMAGE_OPTION_BMP_FORMAT);
361 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_X);
362 MAKE_CONST_WXSTRING(IMAGE_OPTION_CUR_HOTSPOT_Y);
363 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTION);
364 MAKE_CONST_WXSTRING(IMAGE_OPTION_RESOLUTIONUNIT);
365
366 enum
367 {
368 wxIMAGE_RESOLUTION_INCHES = 1,
369 wxIMAGE_RESOLUTION_CM = 2
370 };
371
372
373 enum
374 {
375 wxBMP_24BPP = 24, // default, do not need to set
376 //wxBMP_16BPP = 16, // wxQuantize can only do 236 colors?
377 wxBMP_8BPP = 8, // 8bpp, quantized colors
378 wxBMP_8BPP_GREY = 9, // 8bpp, rgb averaged to greys
379 wxBMP_8BPP_GRAY = wxBMP_8BPP_GREY,
380 wxBMP_8BPP_RED = 10, // 8bpp, red used as greyscale
381 wxBMP_8BPP_PALETTE = 11, // 8bpp, use the wxImage's palette
382 wxBMP_4BPP = 4, // 4bpp, quantized colors
383 wxBMP_1BPP = 1, // 1bpp, quantized "colors"
384 wxBMP_1BPP_BW = 2 // 1bpp, black & white from red
385 };
386
387
388 class wxBMPHandler : public wxImageHandler {
389 public:
390 wxBMPHandler();
391 };
392
393 class wxICOHandler : public wxBMPHandler {
394 public:
395 wxICOHandler();
396 };
397
398 class wxCURHandler : public wxICOHandler {
399 public:
400 wxCURHandler();
401 };
402
403 class wxANIHandler : public wxCURHandler {
404 public:
405 wxANIHandler();
406 };
407
408
409 //---------------------------------------------------------------------------
410
411 class wxPNGHandler : public wxImageHandler {
412 public:
413 wxPNGHandler();
414 };
415
416
417 class wxGIFHandler : public wxImageHandler {
418 public:
419 wxGIFHandler();
420 };
421
422
423 class wxPCXHandler : public wxImageHandler {
424 public:
425 wxPCXHandler();
426 };
427
428
429 class wxJPEGHandler : public wxImageHandler {
430 public:
431 wxJPEGHandler();
432 };
433
434
435 class wxPNMHandler : public wxImageHandler {
436 public:
437 wxPNMHandler();
438 };
439
440 class wxXPMHandler : public wxImageHandler {
441 public:
442 wxXPMHandler();
443 };
444
445 class wxTIFFHandler : public wxImageHandler {
446 public:
447 wxTIFFHandler();
448 };
449
450
451 #if wxUSE_IFF
452 class wxIFFHandler : public wxImageHandler {
453 public:
454 wxIFFHandler();
455 };
456 #endif
457
458 //---------------------------------------------------------------------------