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