blob: 8d0d68f71dc2f2878e7aca5dc04f3f7f370926c3 [file] [log] [blame]
Guido van Rossum0317a471992-10-26 13:40:15 +00001
2/* imageopmodule - Various operations on pictures */
3
4#ifdef sun
5#define signed
6#endif
7
Roger E. Massea141f8a1996-12-20 20:50:39 +00008#include "Python.h"
Guido van Rossum0317a471992-10-26 13:40:15 +00009
Guido van Rossum69011961998-04-23 20:23:00 +000010#if SIZEOF_INT == 4
11typedef int Py_Int32;
12typedef unsigned int Py_UInt32;
13#else
14#if SIZEOF_LONG == 4
15typedef long Py_Int32;
16typedef unsigned long Py_UInt32;
17#else
18#error "No 4-byte integral type"
19#endif
20#endif
21
Guido van Rossum0317a471992-10-26 13:40:15 +000022#define CHARP(cp, xmax, x, y) ((char *)(cp+y*xmax+x))
Jack Jansen76c79e91996-01-22 14:55:15 +000023#define SHORTP(cp, xmax, x, y) ((short *)(cp+2*(y*xmax+x)))
Guido van Rossum69011961998-04-23 20:23:00 +000024#define LONGP(cp, xmax, x, y) ((Py_Int32 *)(cp+4*(y*xmax+x)))
Guido van Rossum0317a471992-10-26 13:40:15 +000025
Roger E. Massea141f8a1996-12-20 20:50:39 +000026static PyObject *ImageopError;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +000027static PyObject *ImageopDict;
28
Benjamin Peterson8d77d442008-09-30 01:31:49 +000029/**
30 * Check a coordonnate, make sure that (0 < value).
31 * Return 0 on error.
32 */
33static int
34check_coordonnate(int value, const char* name)
35{
36 if ( 0 < value)
37 return 1;
38 PyErr_Format(PyExc_ValueError, "%s value is negative or nul", name);
39 return 0;
40}
41
42/**
43 * Check integer overflow to make sure that product == x*y*size.
44 * Return 0 on error.
45 */
46static int
47check_multiply_size(int product, int x, const char* xname, int y, const char* yname, int size)
48{
49 if ( !check_coordonnate(x, xname) )
50 return 0;
51 if ( !check_coordonnate(y, yname) )
52 return 0;
53 if ( size == (product / y) / x )
54 return 1;
55 PyErr_SetString(ImageopError, "String has incorrect length");
56 return 0;
57}
58
59/**
60 * Check integer overflow to make sure that product == x*y.
61 * Return 0 on error.
62 */
63static int
64check_multiply(int product, int x, int y)
65{
66 return check_multiply_size(product, x, "x", y, "y", 1);
67}
68
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +000069/* If this function returns true (the default if anything goes wrong), we're
70 behaving in a backward-compatible way with respect to how multi-byte pixels
71 are stored in the strings. The code in this module was originally written
72 for an SGI which is a big-endian system, and so the old code assumed that
73 4-byte integers hold the R, G, and B values in a particular order.
74 However, on little-endian systems the order is reversed, and so not
75 actually compatible with what gl.lrectwrite and imgfile expect.
76 (gl.lrectwrite and imgfile are also SGI-specific, however, it is
77 conceivable that the data handled here comes from or goes to an SGI or that
78 it is otherwise used in the expectation that the byte order in the strings
79 is as specified.)
80
81 The function returns the value of the module variable
82 "backward_compatible", or 1 if the variable does not exist or is not an
83 int.
84 */
85
86static int
87imageop_backward_compatible(void)
88{
89 static PyObject *bcos;
90 PyObject *bco;
91 long rc;
92
93 if (ImageopDict == NULL) /* "cannot happen" */
94 return 1;
95 if (bcos == NULL) {
96 /* cache string object for future use */
Gregory P. Smithdd96db62008-06-09 04:58:54 +000097 bcos = PyString_FromString("backward_compatible");
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +000098 if (bcos == NULL)
99 return 1;
100 }
101 bco = PyDict_GetItem(ImageopDict, bcos);
102 if (bco == NULL)
103 return 1;
104 if (!PyInt_Check(bco))
105 return 1;
106 rc = PyInt_AsLong(bco);
107 if (PyErr_Occurred()) {
108 /* not an integer, or too large, or something */
109 PyErr_Clear();
110 rc = 1;
111 }
112 return rc != 0; /* convert to values 0, 1 */
113}
114
Roger E. Massea141f8a1996-12-20 20:50:39 +0000115static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000116imageop_crop(PyObject *self, PyObject *args)
Guido van Rossum0317a471992-10-26 13:40:15 +0000117{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000118 char *cp, *ncp;
119 short *nsp;
Guido van Rossum69011961998-04-23 20:23:00 +0000120 Py_Int32 *nlp;
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000121 int len, size, x, y, newx1, newx2, newy1, newy2, nlen;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000122 int ix, iy, xstep, ystep;
123 PyObject *rv;
Guido van Rossum0317a471992-10-26 13:40:15 +0000124
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000125 if ( !PyArg_ParseTuple(args, "s#iiiiiii", &cp, &len, &size, &x, &y,
Roger E. Massea141f8a1996-12-20 20:50:39 +0000126 &newx1, &newy1, &newx2, &newy2) )
127 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000128
Roger E. Massea141f8a1996-12-20 20:50:39 +0000129 if ( size != 1 && size != 2 && size != 4 ) {
130 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
131 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +0000132 }
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000133 if ( !check_multiply_size(len, x, "x", y, "y", size) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000134 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000135
Roger E. Massea141f8a1996-12-20 20:50:39 +0000136 xstep = (newx1 < newx2)? 1 : -1;
137 ystep = (newy1 < newy2)? 1 : -1;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000138
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000139 nlen = (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000140 if ( !check_multiply_size(nlen, abs(newx2-newx1)+1, "abs(newx2-newx1)+1", abs(newy2-newy1)+1, "abs(newy2-newy1)+1", size) )
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000141 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000142 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000143 if ( rv == 0 )
144 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000145 ncp = (char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000146 nsp = (short *)ncp;
Guido van Rossum69011961998-04-23 20:23:00 +0000147 nlp = (Py_Int32 *)ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000148 newy2 += ystep;
149 newx2 += xstep;
150 for( iy = newy1; iy != newy2; iy+=ystep ) {
151 for ( ix = newx1; ix != newx2; ix+=xstep ) {
152 if ( iy < 0 || iy >= y || ix < 0 || ix >= x ) {
153 if ( size == 1 )
154 *ncp++ = 0;
155 else
156 *nlp++ = 0;
157 } else {
158 if ( size == 1 )
159 *ncp++ = *CHARP(cp, x, ix, iy);
160 else if ( size == 2 )
161 *nsp++ = *SHORTP(cp, x, ix, iy);
162 else
163 *nlp++ = *LONGP(cp, x, ix, iy);
164 }
165 }
166 }
167 return rv;
Guido van Rossum0317a471992-10-26 13:40:15 +0000168}
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000169
Roger E. Massea141f8a1996-12-20 20:50:39 +0000170static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000171imageop_scale(PyObject *self, PyObject *args)
Guido van Rossum0317a471992-10-26 13:40:15 +0000172{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000173 char *cp, *ncp;
174 short *nsp;
Guido van Rossum69011961998-04-23 20:23:00 +0000175 Py_Int32 *nlp;
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000176 int len, size, x, y, newx, newy, nlen;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000177 int ix, iy;
178 int oix, oiy;
179 PyObject *rv;
Guido van Rossum0317a471992-10-26 13:40:15 +0000180
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000181 if ( !PyArg_ParseTuple(args, "s#iiiii",
Roger E. Massea141f8a1996-12-20 20:50:39 +0000182 &cp, &len, &size, &x, &y, &newx, &newy) )
183 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000184
Roger E. Massea141f8a1996-12-20 20:50:39 +0000185 if ( size != 1 && size != 2 && size != 4 ) {
186 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
187 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +0000188 }
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000189 if ( !check_multiply_size(len, x, "x", y, "y", size) )
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000190 return 0;
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000191 nlen = newx*newy*size;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000192 if ( !check_multiply_size(nlen, newx, "newx", newy, "newy", size) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000193 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000194
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000195 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000196 if ( rv == 0 )
197 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000198 ncp = (char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000199 nsp = (short *)ncp;
Guido van Rossum69011961998-04-23 20:23:00 +0000200 nlp = (Py_Int32 *)ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000201 for( iy = 0; iy < newy; iy++ ) {
202 for ( ix = 0; ix < newx; ix++ ) {
203 oix = ix * x / newx;
204 oiy = iy * y / newy;
205 if ( size == 1 )
206 *ncp++ = *CHARP(cp, x, oix, oiy);
207 else if ( size == 2 )
208 *nsp++ = *SHORTP(cp, x, oix, oiy);
209 else
210 *nlp++ = *LONGP(cp, x, oix, oiy);
211 }
212 }
213 return rv;
Guido van Rossum0317a471992-10-26 13:40:15 +0000214}
215
Jack Jansend26b4581993-01-22 15:34:43 +0000216/* Note: this routine can use a bit of optimizing */
217
Roger E. Massea141f8a1996-12-20 20:50:39 +0000218static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000219imageop_tovideo(PyObject *self, PyObject *args)
Jack Jansend26b4581993-01-22 15:34:43 +0000220{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000221 int maxx, maxy, x, y, len;
222 int i;
223 unsigned char *cp, *ncp;
224 int width;
225 PyObject *rv;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000226
227
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000228 if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &width, &maxx, &maxy) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000229 return 0;
Jack Jansend26b4581993-01-22 15:34:43 +0000230
Roger E. Massea141f8a1996-12-20 20:50:39 +0000231 if ( width != 1 && width != 4 ) {
232 PyErr_SetString(ImageopError, "Size should be 1 or 4");
233 return 0;
234 }
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000235 if ( !check_multiply_size(len, maxx, "max", maxy, "maxy", width) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000236 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000237
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000238 rv = PyString_FromStringAndSize(NULL, len);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000239 if ( rv == 0 )
240 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000241 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansend26b4581993-01-22 15:34:43 +0000242
Roger E. Massea141f8a1996-12-20 20:50:39 +0000243 if ( width == 1 ) {
244 memcpy(ncp, cp, maxx); /* Copy first line */
245 ncp += maxx;
246 for (y=1; y<maxy; y++) { /* Interpolate other lines */
247 for(x=0; x<maxx; x++) {
248 i = y*maxx + x;
249 *ncp++ = ((int)cp[i] + (int)cp[i-maxx]) >> 1;
250 }
251 }
252 } else {
253 memcpy(ncp, cp, maxx*4); /* Copy first line */
254 ncp += maxx*4;
255 for (y=1; y<maxy; y++) { /* Interpolate other lines */
256 for(x=0; x<maxx; x++) {
257 i = (y*maxx + x)*4 + 1;
258 *ncp++ = 0; /* Skip alfa comp */
259 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
260 i++;
261 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
262 i++;
263 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
264 }
265 }
Jack Jansend26b4581993-01-22 15:34:43 +0000266 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000267 return rv;
Jack Jansend26b4581993-01-22 15:34:43 +0000268}
269
Roger E. Massea141f8a1996-12-20 20:50:39 +0000270static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000271imageop_grey2mono(PyObject *self, PyObject *args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000272{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000273 int tres, x, y, len;
274 unsigned char *cp, *ncp;
275 unsigned char ovalue;
276 PyObject *rv;
277 int i, bit;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000278
279
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000280 if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &x, &y, &tres) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000281 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000282
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000283 if ( !check_multiply(len, x, y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000284 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000285
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000286 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000287 if ( rv == 0 )
288 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000289 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000290
291 bit = 0x80;
292 ovalue = 0;
293 for ( i=0; i < len; i++ ) {
294 if ( (int)cp[i] > tres )
295 ovalue |= bit;
296 bit >>= 1;
297 if ( bit == 0 ) {
298 *ncp++ = ovalue;
299 bit = 0x80;
300 ovalue = 0;
301 }
302 }
303 if ( bit != 0x80 )
304 *ncp++ = ovalue;
305 return rv;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000306}
307
Roger E. Massea141f8a1996-12-20 20:50:39 +0000308static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000309imageop_grey2grey4(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000310{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000311 int x, y, len;
312 unsigned char *cp, *ncp;
313 unsigned char ovalue;
314 PyObject *rv;
315 int i;
316 int pos;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000317
318
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000319 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000320 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000321
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000322 if ( !check_multiply(len, x, y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000323 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000324
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000325 rv = PyString_FromStringAndSize(NULL, (len+1)/2);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000326 if ( rv == 0 )
327 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000328 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000329 pos = 0;
330 ovalue = 0;
331 for ( i=0; i < len; i++ ) {
332 ovalue |= ((int)cp[i] & 0xf0) >> pos;
333 pos += 4;
334 if ( pos == 8 ) {
335 *ncp++ = ovalue;
336 ovalue = 0;
337 pos = 0;
338 }
339 }
340 if ( pos != 0 )
341 *ncp++ = ovalue;
342 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000343}
344
Roger E. Massea141f8a1996-12-20 20:50:39 +0000345static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000346imageop_grey2grey2(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000347{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000348 int x, y, len;
349 unsigned char *cp, *ncp;
350 unsigned char ovalue;
351 PyObject *rv;
352 int i;
353 int pos;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000354
355
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000356 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000357 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000358
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000359 if ( !check_multiply(len, x, y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000360 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000361
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000362 rv = PyString_FromStringAndSize(NULL, (len+3)/4);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000363 if ( rv == 0 )
364 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000365 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000366 pos = 0;
367 ovalue = 0;
368 for ( i=0; i < len; i++ ) {
369 ovalue |= ((int)cp[i] & 0xc0) >> pos;
370 pos += 2;
371 if ( pos == 8 ) {
372 *ncp++ = ovalue;
373 ovalue = 0;
374 pos = 0;
375 }
376 }
377 if ( pos != 0 )
378 *ncp++ = ovalue;
379 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000380}
381
Roger E. Massea141f8a1996-12-20 20:50:39 +0000382static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000383imageop_dither2mono(PyObject *self, PyObject *args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000384{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000385 int sum, x, y, len;
386 unsigned char *cp, *ncp;
387 unsigned char ovalue;
388 PyObject *rv;
389 int i, bit;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000390
391
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000392 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000393 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000394
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000395 if ( !check_multiply(len, x, y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000396 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000397
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000398 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000399 if ( rv == 0 )
400 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000401 ncp = (unsigned char *)PyString_AsString(rv);
Guido van Rossum5f59d601992-12-14 16:59:51 +0000402
Roger E. Massea141f8a1996-12-20 20:50:39 +0000403 bit = 0x80;
404 ovalue = 0;
405 sum = 0;
406 for ( i=0; i < len; i++ ) {
407 sum += cp[i];
408 if ( sum >= 256 ) {
409 sum -= 256;
410 ovalue |= bit;
411 }
412 bit >>= 1;
413 if ( bit == 0 ) {
414 *ncp++ = ovalue;
415 bit = 0x80;
416 ovalue = 0;
417 }
Guido van Rossum5f59d601992-12-14 16:59:51 +0000418 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000419 if ( bit != 0x80 )
420 *ncp++ = ovalue;
421 return rv;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000422}
423
Roger E. Massea141f8a1996-12-20 20:50:39 +0000424static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000425imageop_dither2grey2(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000426{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000427 int x, y, len;
428 unsigned char *cp, *ncp;
429 unsigned char ovalue;
430 PyObject *rv;
431 int i;
432 int pos;
433 int sum = 0, nvalue;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000434
435
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000436 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000437 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000438
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000439 if ( !check_multiply(len, x, y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000440 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000441
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000442 rv = PyString_FromStringAndSize(NULL, (len+3)/4);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000443 if ( rv == 0 )
444 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000445 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000446 pos = 1;
447 ovalue = 0;
448 for ( i=0; i < len; i++ ) {
449 sum += cp[i];
450 nvalue = sum & 0x180;
451 sum -= nvalue;
452 ovalue |= nvalue >> pos;
453 pos += 2;
454 if ( pos == 9 ) {
455 *ncp++ = ovalue;
456 ovalue = 0;
457 pos = 1;
458 }
459 }
460 if ( pos != 0 )
461 *ncp++ = ovalue;
462 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000463}
464
Roger E. Massea141f8a1996-12-20 20:50:39 +0000465static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000466imageop_mono2grey(PyObject *self, PyObject *args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000467{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000468 int v0, v1, x, y, len, nlen;
469 unsigned char *cp, *ncp;
470 PyObject *rv;
471 int i, bit;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000472
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000473 if ( !PyArg_ParseTuple(args, "s#iiii", &cp, &len, &x, &y, &v0, &v1) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000474 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000475
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000476 nlen = x*y;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000477 if ( !check_multiply(nlen, x, y) )
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000478 return 0;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000479 if ( (nlen+7)/8 != len ) {
480 PyErr_SetString(ImageopError, "String has incorrect length");
481 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000482 }
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000483
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000484 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000485 if ( rv == 0 )
486 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000487 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000488
489 bit = 0x80;
490 for ( i=0; i < nlen; i++ ) {
491 if ( *cp & bit )
492 *ncp++ = v1;
493 else
494 *ncp++ = v0;
495 bit >>= 1;
496 if ( bit == 0 ) {
497 bit = 0x80;
498 cp++;
499 }
500 }
501 return rv;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000502}
503
Roger E. Massea141f8a1996-12-20 20:50:39 +0000504static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000505imageop_grey22grey(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000506{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000507 int x, y, len, nlen;
508 unsigned char *cp, *ncp;
509 PyObject *rv;
510 int i, pos, value = 0, nvalue;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000511
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000512 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000513 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000514
Roger E. Massea141f8a1996-12-20 20:50:39 +0000515 nlen = x*y;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000516 if ( !check_multiply(nlen, x, y) ) {
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000517 return 0;
518 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000519 if ( (nlen+3)/4 != len ) {
520 PyErr_SetString(ImageopError, "String has incorrect length");
521 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000522 }
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000523
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000524 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000525 if ( rv == 0 )
526 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000527 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000528
529 pos = 0;
530 for ( i=0; i < nlen; i++ ) {
531 if ( pos == 0 ) {
532 value = *cp++;
533 pos = 8;
534 }
535 pos -= 2;
536 nvalue = (value >> pos) & 0x03;
537 *ncp++ = nvalue | (nvalue << 2) |
538 (nvalue << 4) | (nvalue << 6);
539 }
540 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000541}
542
Roger E. Massea141f8a1996-12-20 20:50:39 +0000543static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000544imageop_grey42grey(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000545{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000546 int x, y, len, nlen;
547 unsigned char *cp, *ncp;
548 PyObject *rv;
549 int i, pos, value = 0, nvalue;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000550
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000551 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000552 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000553
Roger E. Massea141f8a1996-12-20 20:50:39 +0000554 nlen = x*y;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000555 if ( !check_multiply(nlen, x, y) )
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000556 return 0;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000557 if ( (nlen+1)/2 != len ) {
558 PyErr_SetString(ImageopError, "String has incorrect length");
559 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000560 }
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000561
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000562 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000563 if ( rv == 0 )
564 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000565 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000566
567 pos = 0;
568 for ( i=0; i < nlen; i++ ) {
569 if ( pos == 0 ) {
570 value = *cp++;
571 pos = 8;
572 }
573 pos -= 4;
574 nvalue = (value >> pos) & 0x0f;
575 *ncp++ = nvalue | (nvalue << 4);
576 }
577 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000578}
579
Roger E. Massea141f8a1996-12-20 20:50:39 +0000580static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000581imageop_rgb2rgb8(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000582{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000583 int x, y, len, nlen;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000584 unsigned char *cp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000585 unsigned char *ncp;
586 PyObject *rv;
587 int i, r, g, b;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000588 int backward_compatible = imageop_backward_compatible();
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000589
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000590 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000591 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000592
Amaury Forgeot d'Arc7cfe7ea2008-11-18 22:19:37 +0000593 if ( !check_multiply_size(len, x, "x", y, "y", 4) )
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000594 return 0;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000595 nlen = x*y;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000596 if ( !check_multiply(nlen, x, y) )
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000597 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000598
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000599 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000600 if ( rv == 0 )
601 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000602 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000603
Roger E. Massea141f8a1996-12-20 20:50:39 +0000604 for ( i=0; i < nlen; i++ ) {
605 /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000606 if (backward_compatible) {
607 Py_UInt32 value = * (Py_UInt32 *) cp;
608 cp += 4;
609 r = (int) ((value & 0xff) / 255. * 7. + .5);
610 g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
611 b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
612 } else {
613 cp++; /* skip alpha channel */
614 b = (int) (*cp++ / 255. * 3. + .5);
615 g = (int) (*cp++ / 255. * 7. + .5);
616 r = (int) (*cp++ / 255. * 7. + .5);
617 }
618 *ncp++ = (unsigned char)((r<<5) | (b<<3) | g);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000619 }
620 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000621}
622
Roger E. Massea141f8a1996-12-20 20:50:39 +0000623static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000624imageop_rgb82rgb(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000625{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000626 int x, y, len, nlen;
627 unsigned char *cp;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000628 unsigned char *ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000629 PyObject *rv;
630 int i, r, g, b;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000631 unsigned char value;
632 int backward_compatible = imageop_backward_compatible();
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000633
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000634 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000635 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000636
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000637 if ( !check_multiply(len, x, y) )
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000638 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000639 nlen = x*y*4;
640 if ( !check_multiply_size(nlen, x, "x", y, "y", 4) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000641 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000642
643 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000644 if ( rv == 0 )
645 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000646 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000647
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000648 for ( i=0; i < len; i++ ) {
Roger E. Massea141f8a1996-12-20 20:50:39 +0000649 /* Bits in source: RRRBBGGG
650 ** Red and Green are multiplied by 36.5, Blue by 85
651 */
652 value = *cp++;
653 r = (value >> 5) & 7;
654 g = (value ) & 7;
655 b = (value >> 3) & 3;
656 r = (r<<5) | (r<<3) | (r>>1);
657 g = (g<<5) | (g<<3) | (g>>1);
658 b = (b<<6) | (b<<4) | (b<<2) | b;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000659 if (backward_compatible) {
660 Py_UInt32 nvalue = r | (g<<8) | (b<<16);
661 * (Py_UInt32 *) ncp = nvalue;
662 ncp += 4;
663 } else {
664 *ncp++ = 0;
665 *ncp++ = b;
666 *ncp++ = g;
667 *ncp++ = r;
668 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000669 }
670 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000671}
672
Roger E. Massea141f8a1996-12-20 20:50:39 +0000673static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000674imageop_rgb2grey(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000675{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000676 int x, y, len, nlen;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000677 unsigned char *cp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000678 unsigned char *ncp;
679 PyObject *rv;
680 int i, r, g, b;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000681 int nvalue;
682 int backward_compatible = imageop_backward_compatible();
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000683
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000684 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000685 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000686
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000687 if ( !check_multiply_size(len, x, "x", y, "y", 4) )
688 return 0;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000689 nlen = x*y;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000690 if ( !check_multiply(nlen, x, y) )
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000691 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000692
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000693 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000694 if ( rv == 0 )
695 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000696 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000697
Roger E. Massea141f8a1996-12-20 20:50:39 +0000698 for ( i=0; i < nlen; i++ ) {
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000699 if (backward_compatible) {
700 Py_UInt32 value = * (Py_UInt32 *) cp;
701 cp += 4;
702 r = (int) ((value & 0xff) / 255. * 7. + .5);
703 g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
704 b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
705 } else {
706 cp++; /* skip alpha channel */
707 b = *cp++;
708 g = *cp++;
709 r = *cp++;
710 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000711 nvalue = (int)(0.30*r + 0.59*g + 0.11*b);
712 if ( nvalue > 255 ) nvalue = 255;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000713 *ncp++ = (unsigned char)nvalue;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000714 }
715 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000716}
717
Roger E. Massea141f8a1996-12-20 20:50:39 +0000718static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000719imageop_grey2rgb(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000720{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000721 int x, y, len, nlen;
722 unsigned char *cp;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000723 unsigned char *ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000724 PyObject *rv;
725 int i;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000726 unsigned char value;
727 int backward_compatible = imageop_backward_compatible();
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000728
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000729 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000730 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000731
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000732 if ( !check_multiply(len, x, y) )
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000733 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000734 nlen = x*y*4;
735 if ( !check_multiply_size(nlen, x, "x", y, "y", 4) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000736 return 0;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000737
738 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000739 if ( rv == 0 )
740 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000741 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000742
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000743 for ( i=0; i < len; i++ ) {
Roger E. Massea141f8a1996-12-20 20:50:39 +0000744 value = *cp++;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000745 if (backward_compatible) {
746 * (Py_UInt32 *) ncp = (Py_UInt32) value | ((Py_UInt32) value << 8 ) | ((Py_UInt32) value << 16);
747 ncp += 4;
748 } else {
749 *ncp++ = 0;
750 *ncp++ = value;
751 *ncp++ = value;
752 *ncp++ = value;
753 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000754 }
755 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000756}
757
Roger E. Massea141f8a1996-12-20 20:50:39 +0000758static PyMethodDef imageop_methods[] = {
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000759 { "crop", imageop_crop, METH_VARARGS },
760 { "scale", imageop_scale, METH_VARARGS },
761 { "grey2mono", imageop_grey2mono, METH_VARARGS },
762 { "grey2grey2", imageop_grey2grey2, METH_VARARGS },
763 { "grey2grey4", imageop_grey2grey4, METH_VARARGS },
764 { "dither2mono", imageop_dither2mono, METH_VARARGS },
765 { "dither2grey2", imageop_dither2grey2, METH_VARARGS },
766 { "mono2grey", imageop_mono2grey, METH_VARARGS },
767 { "grey22grey", imageop_grey22grey, METH_VARARGS },
768 { "grey42grey", imageop_grey42grey, METH_VARARGS },
769 { "tovideo", imageop_tovideo, METH_VARARGS },
770 { "rgb2rgb8", imageop_rgb2rgb8, METH_VARARGS },
771 { "rgb82rgb", imageop_rgb82rgb, METH_VARARGS },
772 { "rgb2grey", imageop_rgb2grey, METH_VARARGS },
773 { "grey2rgb", imageop_grey2rgb, METH_VARARGS },
Roger E. Massea141f8a1996-12-20 20:50:39 +0000774 { 0, 0 }
Guido van Rossum0317a471992-10-26 13:40:15 +0000775};
776
777
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000778PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000779initimageop(void)
Guido van Rossum0317a471992-10-26 13:40:15 +0000780{
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000781 PyObject *m;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000782
Brett Cannon42bfa902008-05-12 00:08:34 +0000783 if (PyErr_WarnPy3k("the imageop module has been removed in "
784 "Python 3.0", 2) < 0)
785 return;
Benjamin Peterson8d77d442008-09-30 01:31:49 +0000786
Roger E. Massea141f8a1996-12-20 20:50:39 +0000787 m = Py_InitModule("imageop", imageop_methods);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000788 if (m == NULL)
789 return;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000790 ImageopDict = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000791 ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
792 if (ImageopError != NULL)
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000793 PyDict_SetItemString(ImageopDict, "error", ImageopError);
Guido van Rossum0317a471992-10-26 13:40:15 +0000794}