]>
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 | |
d07d2bc9 | 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)", | |
d07d2bc9 RD |
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.", ""); | |
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 ); | |
86 | %extend { | |
dd9f7fea | 87 | %name(EmptyImage) wxImage(int width=0, int height=0, bool clear = True) { |
d14a1e28 RD |
88 | if (width > 0 && height > 0) |
89 | return new wxImage(width, height, clear); | |
90 | else | |
91 | return new wxImage; | |
92 | } | |
ab1f7d2a RD |
93 | |
94 | MustHaveApp(wxImage(const wxBitmap &bitmap)); | |
d14a1e28 RD |
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); | |
dd9f7fea | 107 | return new wxImage(width, height, copy, False); |
d14a1e28 RD |
108 | } |
109 | } | |
110 | ||
cf694132 RD |
111 | void Create( int width, int height ); |
112 | void Destroy(); | |
6c0168c9 | 113 | |
cf694132 | 114 | wxImage Scale( int width, int height ); |
9cbf6f6e | 115 | wxImage ShrinkBy( int xFactor , int yFactor ) const ; |
f6bcfd97 | 116 | wxImage& Rescale(int width, int height); |
cf694132 RD |
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 | ||
9cbf6f6e RD |
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 | |
68bc8549 | 128 | // RGB values than <startR,startG,startB> |
dd9f7fea RD |
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)", | |
d07d2bc9 RD |
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.", ""); | |
dd9f7fea | 136 | |
68bc8549 RD |
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 | ||
db0ff83e RD |
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 */ ) ; | |
68bc8549 | 147 | |
96bfd053 | 148 | static bool CanRead( const wxString& name ); |
b5a5d647 RD |
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 ); | |
cf694132 RD |
153 | |
154 | bool SaveFile( const wxString& name, int type ); | |
155 | %name(SaveMimeFile)bool SaveFile( const wxString& name, const wxString& mimetype ); | |
156 | ||
f74ff5ef RD |
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 | ||
cf694132 RD |
161 | bool Ok(); |
162 | int GetWidth(); | |
163 | int GetHeight(); | |
164 | ||
ba938c3d RD |
165 | %extend { |
166 | wxSize GetSize() { | |
167 | wxSize size(self->GetWidth(), self->GetHeight()); | |
168 | return size; | |
169 | } | |
170 | } | |
171 | ||
9d8bd15f | 172 | wxImage GetSubImage(const wxRect& rect); |
f6bcfd97 BP |
173 | wxImage Copy(); |
174 | void Paste( const wxImage &image, int x, int y ); | |
9d8bd15f | 175 | |
1dc2f865 RD |
176 | //unsigned char *GetData(); |
177 | //void SetData( unsigned char *data ); | |
178 | ||
d14a1e28 | 179 | %extend { |
9cbf6f6e | 180 | PyObject* GetData() { |
d55d6b3f RD |
181 | unsigned char* data = self->GetData(); |
182 | int len = self->GetWidth() * self->GetHeight() * 3; | |
d14a1e28 RD |
183 | PyObject* rv; |
184 | wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len)); | |
185 | return rv; | |
d55d6b3f | 186 | } |
9cbf6f6e RD |
187 | void SetData(PyObject* data) { |
188 | unsigned char* dataPtr; | |
d55d6b3f | 189 | |
9cbf6f6e RD |
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); | |
d14a1e28 | 197 | wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) ); |
9cbf6f6e RD |
198 | self->SetData(dataPtr); |
199 | // wxImage takes ownership of dataPtr... | |
200 | } | |
201 | ||
202 | ||
203 | ||
204 | PyObject* GetDataBuffer() { | |
1dc2f865 RD |
205 | unsigned char* data = self->GetData(); |
206 | int len = self->GetWidth() * self->GetHeight() * 3; | |
d14a1e28 RD |
207 | PyObject* rv; |
208 | wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) ); | |
209 | return rv; | |
1dc2f865 | 210 | } |
1e4a197e RD |
211 | void SetDataBuffer(PyObject* data) { |
212 | unsigned char* buffer; | |
213 | int size; | |
214 | ||
da32eb53 | 215 | bool blocked = wxPyBeginBlockThreads(); |
d14a1e28 RD |
216 | if (!PyArg_Parse(data, "t#", &buffer, &size)) |
217 | goto done; | |
1e4a197e RD |
218 | |
219 | if (size != self->GetWidth() * self->GetHeight() * 3) { | |
220 | PyErr_SetString(PyExc_TypeError, "Incorrect buffer size"); | |
d14a1e28 | 221 | goto done; |
1e4a197e | 222 | } |
1e4a197e | 223 | self->SetData(buffer); |
d14a1e28 | 224 | done: |
da32eb53 | 225 | wxPyEndBlockThreads(blocked); |
1e4a197e RD |
226 | } |
227 | ||
9cbf6f6e RD |
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(); | |
d14a1e28 RD |
236 | PyObject* rv; |
237 | wxPyBLOCK_THREADS( rv = PyString_FromStringAndSize((char*)data, len) ); | |
238 | return rv; | |
9cbf6f6e RD |
239 | } |
240 | } | |
241 | void SetAlphaData(PyObject* data) { | |
1dc2f865 RD |
242 | unsigned char* dataPtr; |
243 | ||
244 | if (! PyString_Check(data)) { | |
245 | PyErr_SetString(PyExc_TypeError, "Expected string object"); | |
246 | return /* NULL */ ; | |
247 | } | |
9d8bd15f | 248 | |
9cbf6f6e | 249 | size_t len = self->GetWidth() * self->GetHeight(); |
185d7c3e | 250 | dataPtr = (unsigned char*) malloc(len); |
d14a1e28 | 251 | wxPyBLOCK_THREADS( memcpy(dataPtr, PyString_AsString(data), len) ); |
9cbf6f6e | 252 | self->SetAlpha(dataPtr); |
3b5ccda1 | 253 | // wxImage takes ownership of dataPtr... |
1dc2f865 | 254 | } |
9cbf6f6e RD |
255 | |
256 | ||
257 | ||
258 | PyObject* GetAlphaBuffer() { | |
259 | unsigned char* data = self->GetAlpha(); | |
260 | int len = self->GetWidth() * self->GetHeight(); | |
d14a1e28 RD |
261 | PyObject* rv; |
262 | wxPyBLOCK_THREADS( rv = PyBuffer_FromReadWriteMemory(data, len) ); | |
263 | return rv; | |
9cbf6f6e RD |
264 | } |
265 | void SetAlphaBuffer(PyObject* data) { | |
266 | unsigned char* buffer; | |
267 | int size; | |
268 | ||
da32eb53 | 269 | bool blocked = wxPyBeginBlockThreads(); |
d14a1e28 RD |
270 | if (!PyArg_Parse(data, "t#", &buffer, &size)) |
271 | goto done; | |
9cbf6f6e RD |
272 | |
273 | if (size != self->GetWidth() * self->GetHeight()) { | |
274 | PyErr_SetString(PyExc_TypeError, "Incorrect buffer size"); | |
d14a1e28 | 275 | goto done; |
9cbf6f6e RD |
276 | } |
277 | self->SetAlpha(buffer); | |
d14a1e28 | 278 | done: |
da32eb53 | 279 | wxPyEndBlockThreads(blocked); |
9cbf6f6e | 280 | } |
1dc2f865 | 281 | } |
cf694132 RD |
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(); | |
dd9f7fea | 287 | void SetMask( bool mask = True ); |
cf694132 RD |
288 | bool HasMask(); |
289 | ||
f6bcfd97 | 290 | wxImage Rotate(double angle, const wxPoint & centre_of_rotation, |
dd9f7fea RD |
291 | bool interpolating = True, wxPoint * offset_after_rotation = NULL) const ; |
292 | wxImage Rotate90( bool clockwise = True ) ; | |
293 | wxImage Mirror( bool horizontally = True ) ; | |
f6bcfd97 BP |
294 | |
295 | void Replace( unsigned char r1, unsigned char g1, unsigned char b1, | |
296 | unsigned char r2, unsigned char g2, unsigned char b2 ); | |
297 | ||
6c0168c9 RD |
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 | ||
f6bcfd97 | 307 | unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ); |
9cbf6f6e | 308 | unsigned long ComputeHistogram( wxImageHistogram& h ); |
f6bcfd97 | 309 | |
96bfd053 RD |
310 | static void AddHandler( wxImageHandler *handler ); |
311 | static void InsertHandler( wxImageHandler *handler ); | |
312 | static bool RemoveHandler( const wxString& name ); | |
3ef86e32 | 313 | static wxString GetImageExtWildcard(); |
b96c7a38 RD |
314 | |
315 | ||
ab1f7d2a RD |
316 | MustHaveApp(ConvertToBitmap); |
317 | MustHaveApp(ConvertToMonoBitmap); | |
318 | ||
d14a1e28 | 319 | %extend { |
b96c7a38 RD |
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 | } | |
1fded56b | 333 | |
d14a1e28 | 334 | %pythoncode { def __nonzero__(self): return self.Ok() } |
cf694132 RD |
335 | }; |
336 | ||
926bb76c | 337 | |
f74ff5ef | 338 | |
a3150741 RD |
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 | ||
cf694132 | 350 | |
f74ff5ef | 351 | |
d14a1e28 RD |
352 | // See also wxPy_ReinitStockObjects in helpers.cpp |
353 | %immutable; | |
354 | const wxImage wxNullImage; | |
355 | %mutable; | |
cf694132 | 356 | |
d14a1e28 | 357 | //--------------------------------------------------------------------------- |
f74ff5ef | 358 | |
b5a5d647 | 359 | |
d14a1e28 RD |
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); | |
f74ff5ef | 365 | |
d14a1e28 RD |
366 | enum |
367 | { | |
368 | wxIMAGE_RESOLUTION_INCHES = 1, | |
369 | wxIMAGE_RESOLUTION_CM = 2 | |
370 | }; | |
f74ff5ef RD |
371 | |
372 | ||
d14a1e28 RD |
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 | }; | |
f74ff5ef RD |
386 | |
387 | ||
d14a1e28 RD |
388 | class wxBMPHandler : public wxImageHandler { |
389 | public: | |
390 | wxBMPHandler(); | |
391 | }; | |
cf694132 | 392 | |
d14a1e28 RD |
393 | class wxICOHandler : public wxBMPHandler { |
394 | public: | |
395 | wxICOHandler(); | |
396 | }; | |
f74ff5ef | 397 | |
d14a1e28 RD |
398 | class wxCURHandler : public wxICOHandler { |
399 | public: | |
400 | wxCURHandler(); | |
401 | }; | |
f74ff5ef | 402 | |
d14a1e28 RD |
403 | class wxANIHandler : public wxCURHandler { |
404 | public: | |
405 | wxANIHandler(); | |
406 | }; | |
06c0fba4 | 407 | |
0a651eb8 | 408 | |
d14a1e28 | 409 | //--------------------------------------------------------------------------- |
0a651eb8 | 410 | |
d14a1e28 RD |
411 | class wxPNGHandler : public wxImageHandler { |
412 | public: | |
413 | wxPNGHandler(); | |
414 | }; | |
0a651eb8 | 415 | |
0a651eb8 | 416 | |
d14a1e28 RD |
417 | class wxGIFHandler : public wxImageHandler { |
418 | public: | |
419 | wxGIFHandler(); | |
420 | }; | |
0a651eb8 RD |
421 | |
422 | ||
d14a1e28 RD |
423 | class wxPCXHandler : public wxImageHandler { |
424 | public: | |
425 | wxPCXHandler(); | |
426 | }; | |
926bb76c | 427 | |
926bb76c | 428 | |
d14a1e28 RD |
429 | class wxJPEGHandler : public wxImageHandler { |
430 | public: | |
431 | wxJPEGHandler(); | |
432 | }; | |
926bb76c | 433 | |
926bb76c | 434 | |
d14a1e28 RD |
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 | |
926bb76c | 457 | |
cf694132 | 458 | //--------------------------------------------------------------------------- |