blob: a2b69b6cdf3220d7e64b76173df29843489f2d64 [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
29/* If this function returns true (the default if anything goes wrong), we're
30 behaving in a backward-compatible way with respect to how multi-byte pixels
31 are stored in the strings. The code in this module was originally written
32 for an SGI which is a big-endian system, and so the old code assumed that
33 4-byte integers hold the R, G, and B values in a particular order.
34 However, on little-endian systems the order is reversed, and so not
35 actually compatible with what gl.lrectwrite and imgfile expect.
36 (gl.lrectwrite and imgfile are also SGI-specific, however, it is
37 conceivable that the data handled here comes from or goes to an SGI or that
38 it is otherwise used in the expectation that the byte order in the strings
39 is as specified.)
40
41 The function returns the value of the module variable
42 "backward_compatible", or 1 if the variable does not exist or is not an
43 int.
44 */
45
46static int
47imageop_backward_compatible(void)
48{
49 static PyObject *bcos;
50 PyObject *bco;
51 long rc;
52
53 if (ImageopDict == NULL) /* "cannot happen" */
54 return 1;
55 if (bcos == NULL) {
56 /* cache string object for future use */
Gregory P. Smithdd96db62008-06-09 04:58:54 +000057 bcos = PyString_FromString("backward_compatible");
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +000058 if (bcos == NULL)
59 return 1;
60 }
61 bco = PyDict_GetItem(ImageopDict, bcos);
62 if (bco == NULL)
63 return 1;
64 if (!PyInt_Check(bco))
65 return 1;
66 rc = PyInt_AsLong(bco);
67 if (PyErr_Occurred()) {
68 /* not an integer, or too large, or something */
69 PyErr_Clear();
70 rc = 1;
71 }
72 return rc != 0; /* convert to values 0, 1 */
73}
74
Roger E. Massea141f8a1996-12-20 20:50:39 +000075static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +000076imageop_crop(PyObject *self, PyObject *args)
Guido van Rossum0317a471992-10-26 13:40:15 +000077{
Roger E. Massea141f8a1996-12-20 20:50:39 +000078 char *cp, *ncp;
79 short *nsp;
Guido van Rossum69011961998-04-23 20:23:00 +000080 Py_Int32 *nlp;
Guido van Rossum93ebfb12008-08-19 21:02:04 +000081 int len, size, x, y, newx1, newx2, newy1, newy2, nlen;
Roger E. Massea141f8a1996-12-20 20:50:39 +000082 int ix, iy, xstep, ystep;
83 PyObject *rv;
Guido van Rossum0317a471992-10-26 13:40:15 +000084
Neal Norwitzb0aaec52002-04-02 18:26:33 +000085 if ( !PyArg_ParseTuple(args, "s#iiiiiii", &cp, &len, &size, &x, &y,
Roger E. Massea141f8a1996-12-20 20:50:39 +000086 &newx1, &newy1, &newx2, &newy2) )
87 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +000088
Roger E. Massea141f8a1996-12-20 20:50:39 +000089 if ( size != 1 && size != 2 && size != 4 ) {
90 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
91 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +000092 }
Guido van Rossum93ebfb12008-08-19 21:02:04 +000093 if (( len != size*x*y ) ||
94 ( size != ((len / x) / y) )) {
Roger E. Massea141f8a1996-12-20 20:50:39 +000095 PyErr_SetString(ImageopError, "String has incorrect length");
96 return 0;
97 }
98 xstep = (newx1 < newx2)? 1 : -1;
99 ystep = (newy1 < newy2)? 1 : -1;
100
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000101 nlen = (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size;
102 if ( size != ((nlen / (abs(newx2-newx1)+1)) / (abs(newy2-newy1)+1)) ) {
103 PyErr_SetString(ImageopError, "String has incorrect length");
104 return 0;
105 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000106 rv = PyString_FromStringAndSize(NULL,
Roger E. Massea141f8a1996-12-20 20:50:39 +0000107 (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size);
108 if ( rv == 0 )
109 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000110 ncp = (char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000111 nsp = (short *)ncp;
Guido van Rossum69011961998-04-23 20:23:00 +0000112 nlp = (Py_Int32 *)ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000113 newy2 += ystep;
114 newx2 += xstep;
115 for( iy = newy1; iy != newy2; iy+=ystep ) {
116 for ( ix = newx1; ix != newx2; ix+=xstep ) {
117 if ( iy < 0 || iy >= y || ix < 0 || ix >= x ) {
118 if ( size == 1 )
119 *ncp++ = 0;
120 else
121 *nlp++ = 0;
122 } else {
123 if ( size == 1 )
124 *ncp++ = *CHARP(cp, x, ix, iy);
125 else if ( size == 2 )
126 *nsp++ = *SHORTP(cp, x, ix, iy);
127 else
128 *nlp++ = *LONGP(cp, x, ix, iy);
129 }
130 }
131 }
132 return rv;
Guido van Rossum0317a471992-10-26 13:40:15 +0000133}
134
Roger E. Massea141f8a1996-12-20 20:50:39 +0000135static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000136imageop_scale(PyObject *self, PyObject *args)
Guido van Rossum0317a471992-10-26 13:40:15 +0000137{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000138 char *cp, *ncp;
139 short *nsp;
Guido van Rossum69011961998-04-23 20:23:00 +0000140 Py_Int32 *nlp;
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000141 int len, size, x, y, newx, newy, nlen;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000142 int ix, iy;
143 int oix, oiy;
144 PyObject *rv;
Guido van Rossum0317a471992-10-26 13:40:15 +0000145
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000146 if ( !PyArg_ParseTuple(args, "s#iiiii",
Roger E. Massea141f8a1996-12-20 20:50:39 +0000147 &cp, &len, &size, &x, &y, &newx, &newy) )
148 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +0000149
Roger E. Massea141f8a1996-12-20 20:50:39 +0000150 if ( size != 1 && size != 2 && size != 4 ) {
151 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
152 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +0000153 }
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000154 if ( ( len != size*x*y ) ||
155 ( size != ((len / x) / y) ) ) {
156 PyErr_SetString(ImageopError, "String has incorrect length");
157 return 0;
158 }
159 nlen = newx*newy*size;
160 if ( size != ((nlen / newx) / newy) ) {
Roger E. Massea141f8a1996-12-20 20:50:39 +0000161 PyErr_SetString(ImageopError, "String has incorrect length");
162 return 0;
163 }
164
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000165 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000166 if ( rv == 0 )
167 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000168 ncp = (char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000169 nsp = (short *)ncp;
Guido van Rossum69011961998-04-23 20:23:00 +0000170 nlp = (Py_Int32 *)ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000171 for( iy = 0; iy < newy; iy++ ) {
172 for ( ix = 0; ix < newx; ix++ ) {
173 oix = ix * x / newx;
174 oiy = iy * y / newy;
175 if ( size == 1 )
176 *ncp++ = *CHARP(cp, x, oix, oiy);
177 else if ( size == 2 )
178 *nsp++ = *SHORTP(cp, x, oix, oiy);
179 else
180 *nlp++ = *LONGP(cp, x, oix, oiy);
181 }
182 }
183 return rv;
Guido van Rossum0317a471992-10-26 13:40:15 +0000184}
185
Jack Jansend26b4581993-01-22 15:34:43 +0000186/* Note: this routine can use a bit of optimizing */
187
Roger E. Massea141f8a1996-12-20 20:50:39 +0000188static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000189imageop_tovideo(PyObject *self, PyObject *args)
Jack Jansend26b4581993-01-22 15:34:43 +0000190{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000191 int maxx, maxy, x, y, len;
192 int i;
193 unsigned char *cp, *ncp;
194 int width;
195 PyObject *rv;
Jack Jansend26b4581993-01-22 15:34:43 +0000196
197
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000198 if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &width, &maxx, &maxy) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000199 return 0;
Jack Jansend26b4581993-01-22 15:34:43 +0000200
Roger E. Massea141f8a1996-12-20 20:50:39 +0000201 if ( width != 1 && width != 4 ) {
202 PyErr_SetString(ImageopError, "Size should be 1 or 4");
203 return 0;
204 }
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000205 if ( ( maxx*maxy*width != len ) ||
206 ( maxx != ((len / maxy) / width) ) ) {
Roger E. Massea141f8a1996-12-20 20:50:39 +0000207 PyErr_SetString(ImageopError, "String has incorrect length");
208 return 0;
209 }
Jack Jansend26b4581993-01-22 15:34:43 +0000210
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000211 rv = PyString_FromStringAndSize(NULL, len);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000212 if ( rv == 0 )
213 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000214 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansend26b4581993-01-22 15:34:43 +0000215
Roger E. Massea141f8a1996-12-20 20:50:39 +0000216 if ( width == 1 ) {
217 memcpy(ncp, cp, maxx); /* Copy first line */
218 ncp += maxx;
219 for (y=1; y<maxy; y++) { /* Interpolate other lines */
220 for(x=0; x<maxx; x++) {
221 i = y*maxx + x;
222 *ncp++ = ((int)cp[i] + (int)cp[i-maxx]) >> 1;
223 }
224 }
225 } else {
226 memcpy(ncp, cp, maxx*4); /* Copy first line */
227 ncp += maxx*4;
228 for (y=1; y<maxy; y++) { /* Interpolate other lines */
229 for(x=0; x<maxx; x++) {
230 i = (y*maxx + x)*4 + 1;
231 *ncp++ = 0; /* Skip alfa comp */
232 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
233 i++;
234 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
235 i++;
236 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
237 }
238 }
Jack Jansend26b4581993-01-22 15:34:43 +0000239 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000240 return rv;
Jack Jansend26b4581993-01-22 15:34:43 +0000241}
242
Roger E. Massea141f8a1996-12-20 20:50:39 +0000243static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000244imageop_grey2mono(PyObject *self, PyObject *args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000245{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000246 int tres, x, y, len;
247 unsigned char *cp, *ncp;
248 unsigned char ovalue;
249 PyObject *rv;
250 int i, bit;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000251
252
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000253 if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &x, &y, &tres) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000254 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000255
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000256 if ( ( x*y != len ) ||
257 ( x != len / y ) ) {
Roger E. Massea141f8a1996-12-20 20:50:39 +0000258 PyErr_SetString(ImageopError, "String has incorrect length");
259 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000260 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000261
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000262 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000263 if ( rv == 0 )
264 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000265 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000266
267 bit = 0x80;
268 ovalue = 0;
269 for ( i=0; i < len; i++ ) {
270 if ( (int)cp[i] > tres )
271 ovalue |= bit;
272 bit >>= 1;
273 if ( bit == 0 ) {
274 *ncp++ = ovalue;
275 bit = 0x80;
276 ovalue = 0;
277 }
278 }
279 if ( bit != 0x80 )
280 *ncp++ = ovalue;
281 return rv;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000282}
283
Roger E. Massea141f8a1996-12-20 20:50:39 +0000284static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000285imageop_grey2grey4(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000286{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000287 int x, y, len;
288 unsigned char *cp, *ncp;
289 unsigned char ovalue;
290 PyObject *rv;
291 int i;
292 int pos;
Jack Jansende3adf91992-12-22 14:05:55 +0000293
294
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000295 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000296 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000297
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000298 if ( ( x*y != len ) ||
299 ( x != len / y ) ) {
Roger E. Massea141f8a1996-12-20 20:50:39 +0000300 PyErr_SetString(ImageopError, "String has incorrect length");
301 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000302 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000303
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000304 rv = PyString_FromStringAndSize(NULL, (len+1)/2);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000305 if ( rv == 0 )
306 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000307 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000308 pos = 0;
309 ovalue = 0;
310 for ( i=0; i < len; i++ ) {
311 ovalue |= ((int)cp[i] & 0xf0) >> pos;
312 pos += 4;
313 if ( pos == 8 ) {
314 *ncp++ = ovalue;
315 ovalue = 0;
316 pos = 0;
317 }
318 }
319 if ( pos != 0 )
320 *ncp++ = ovalue;
321 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000322}
323
Roger E. Massea141f8a1996-12-20 20:50:39 +0000324static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000325imageop_grey2grey2(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000326{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000327 int x, y, len;
328 unsigned char *cp, *ncp;
329 unsigned char ovalue;
330 PyObject *rv;
331 int i;
332 int pos;
Jack Jansende3adf91992-12-22 14:05:55 +0000333
334
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000335 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000336 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000337
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000338 if ( ( x*y != len ) ||
339 ( x != len / y ) ) {
Roger E. Massea141f8a1996-12-20 20:50:39 +0000340 PyErr_SetString(ImageopError, "String has incorrect length");
341 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000342 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000343
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000344 rv = PyString_FromStringAndSize(NULL, (len+3)/4);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000345 if ( rv == 0 )
346 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000347 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000348 pos = 0;
349 ovalue = 0;
350 for ( i=0; i < len; i++ ) {
351 ovalue |= ((int)cp[i] & 0xc0) >> pos;
352 pos += 2;
353 if ( pos == 8 ) {
354 *ncp++ = ovalue;
355 ovalue = 0;
356 pos = 0;
357 }
358 }
359 if ( pos != 0 )
360 *ncp++ = ovalue;
361 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000362}
363
Roger E. Massea141f8a1996-12-20 20:50:39 +0000364static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000365imageop_dither2mono(PyObject *self, PyObject *args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000366{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000367 int sum, x, y, len;
368 unsigned char *cp, *ncp;
369 unsigned char ovalue;
370 PyObject *rv;
371 int i, bit;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000372
373
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000374 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000375 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000376
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000377 if ( ( x*y != len ) ||
378 ( x != len / y ) ) {
Roger E. Massea141f8a1996-12-20 20:50:39 +0000379 PyErr_SetString(ImageopError, "String has incorrect length");
380 return 0;
381 }
Guido van Rossum5f59d601992-12-14 16:59:51 +0000382
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000383 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000384 if ( rv == 0 )
385 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000386 ncp = (unsigned char *)PyString_AsString(rv);
Guido van Rossum5f59d601992-12-14 16:59:51 +0000387
Roger E. Massea141f8a1996-12-20 20:50:39 +0000388 bit = 0x80;
389 ovalue = 0;
390 sum = 0;
391 for ( i=0; i < len; i++ ) {
392 sum += cp[i];
393 if ( sum >= 256 ) {
394 sum -= 256;
395 ovalue |= bit;
396 }
397 bit >>= 1;
398 if ( bit == 0 ) {
399 *ncp++ = ovalue;
400 bit = 0x80;
401 ovalue = 0;
402 }
Guido van Rossum5f59d601992-12-14 16:59:51 +0000403 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000404 if ( bit != 0x80 )
405 *ncp++ = ovalue;
406 return rv;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000407}
408
Roger E. Massea141f8a1996-12-20 20:50:39 +0000409static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000410imageop_dither2grey2(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000411{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000412 int x, y, len;
413 unsigned char *cp, *ncp;
414 unsigned char ovalue;
415 PyObject *rv;
416 int i;
417 int pos;
418 int sum = 0, nvalue;
Jack Jansende3adf91992-12-22 14:05:55 +0000419
420
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000421 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000422 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000423
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000424 if ( ( x*y != len ) ||
425 ( x != len / y ) ) {
Roger E. Massea141f8a1996-12-20 20:50:39 +0000426 PyErr_SetString(ImageopError, "String has incorrect length");
427 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000428 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000429
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000430 rv = PyString_FromStringAndSize(NULL, (len+3)/4);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000431 if ( rv == 0 )
432 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000433 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000434 pos = 1;
435 ovalue = 0;
436 for ( i=0; i < len; i++ ) {
437 sum += cp[i];
438 nvalue = sum & 0x180;
439 sum -= nvalue;
440 ovalue |= nvalue >> pos;
441 pos += 2;
442 if ( pos == 9 ) {
443 *ncp++ = ovalue;
444 ovalue = 0;
445 pos = 1;
446 }
447 }
448 if ( pos != 0 )
449 *ncp++ = ovalue;
450 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000451}
452
Roger E. Massea141f8a1996-12-20 20:50:39 +0000453static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000454imageop_mono2grey(PyObject *self, PyObject *args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000455{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000456 int v0, v1, x, y, len, nlen;
457 unsigned char *cp, *ncp;
458 PyObject *rv;
459 int i, bit;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000460
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000461 if ( !PyArg_ParseTuple(args, "s#iiii", &cp, &len, &x, &y, &v0, &v1) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000462 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000463
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000464 nlen = x*y;
465 if ( x != (nlen / y) ) {
466 PyErr_SetString(ImageopError, "String has incorrect length");
467 return 0;
468 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000469 if ( (nlen+7)/8 != len ) {
470 PyErr_SetString(ImageopError, "String has incorrect length");
471 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000472 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000473
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000474 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000475 if ( rv == 0 )
476 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000477 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000478
479 bit = 0x80;
480 for ( i=0; i < nlen; i++ ) {
481 if ( *cp & bit )
482 *ncp++ = v1;
483 else
484 *ncp++ = v0;
485 bit >>= 1;
486 if ( bit == 0 ) {
487 bit = 0x80;
488 cp++;
489 }
490 }
491 return rv;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000492}
493
Roger E. Massea141f8a1996-12-20 20:50:39 +0000494static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000495imageop_grey22grey(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000496{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000497 int x, y, len, nlen;
498 unsigned char *cp, *ncp;
499 PyObject *rv;
500 int i, pos, value = 0, nvalue;
Jack Jansende3adf91992-12-22 14:05:55 +0000501
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000502 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000503 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000504
Roger E. Massea141f8a1996-12-20 20:50:39 +0000505 nlen = x*y;
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000506 if ( x != (nlen / y) ) {
507 PyErr_SetString(ImageopError, "String has incorrect length");
508 return 0;
509 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000510 if ( (nlen+3)/4 != len ) {
511 PyErr_SetString(ImageopError, "String has incorrect length");
512 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000513 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000514
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000515 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000516 if ( rv == 0 )
517 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000518 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000519
520 pos = 0;
521 for ( i=0; i < nlen; i++ ) {
522 if ( pos == 0 ) {
523 value = *cp++;
524 pos = 8;
525 }
526 pos -= 2;
527 nvalue = (value >> pos) & 0x03;
528 *ncp++ = nvalue | (nvalue << 2) |
529 (nvalue << 4) | (nvalue << 6);
530 }
531 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000532}
533
Roger E. Massea141f8a1996-12-20 20:50:39 +0000534static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000535imageop_grey42grey(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000536{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000537 int x, y, len, nlen;
538 unsigned char *cp, *ncp;
539 PyObject *rv;
540 int i, pos, value = 0, nvalue;
Jack Jansende3adf91992-12-22 14:05:55 +0000541
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000542 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000543 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000544
Roger E. Massea141f8a1996-12-20 20:50:39 +0000545 nlen = x*y;
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000546 if ( x != (nlen / y) ) {
547 PyErr_SetString(ImageopError, "String has incorrect length");
548 return 0;
549 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000550 if ( (nlen+1)/2 != len ) {
551 PyErr_SetString(ImageopError, "String has incorrect length");
552 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000553 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000554
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000555 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000556 if ( rv == 0 )
557 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000558 ncp = (unsigned char *)PyString_AsString(rv);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000559
560 pos = 0;
561 for ( i=0; i < nlen; i++ ) {
562 if ( pos == 0 ) {
563 value = *cp++;
564 pos = 8;
565 }
566 pos -= 4;
567 nvalue = (value >> pos) & 0x0f;
568 *ncp++ = nvalue | (nvalue << 4);
569 }
570 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000571}
572
Roger E. Massea141f8a1996-12-20 20:50:39 +0000573static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000574imageop_rgb2rgb8(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000575{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000576 int x, y, len, nlen;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000577 unsigned char *cp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000578 unsigned char *ncp;
579 PyObject *rv;
580 int i, r, g, b;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000581 int backward_compatible = imageop_backward_compatible();
Jack Jansen4fada9c1993-02-19 15:51:41 +0000582
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000583 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000584 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000585
Roger E. Massea141f8a1996-12-20 20:50:39 +0000586 nlen = x*y;
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000587 if ( x != (nlen / y) ) {
588 PyErr_SetString(ImageopError, "String has incorrect length");
589 return 0;
590 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000591 if ( nlen*4 != len ) {
592 PyErr_SetString(ImageopError, "String has incorrect length");
593 return 0;
594 }
Jack Jansen4fada9c1993-02-19 15:51:41 +0000595
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000596 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000597 if ( rv == 0 )
598 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000599 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000600
Roger E. Massea141f8a1996-12-20 20:50:39 +0000601 for ( i=0; i < nlen; i++ ) {
602 /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000603 if (backward_compatible) {
604 Py_UInt32 value = * (Py_UInt32 *) cp;
605 cp += 4;
606 r = (int) ((value & 0xff) / 255. * 7. + .5);
607 g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
608 b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
609 } else {
610 cp++; /* skip alpha channel */
611 b = (int) (*cp++ / 255. * 3. + .5);
612 g = (int) (*cp++ / 255. * 7. + .5);
613 r = (int) (*cp++ / 255. * 7. + .5);
614 }
615 *ncp++ = (unsigned char)((r<<5) | (b<<3) | g);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000616 }
617 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000618}
619
Roger E. Massea141f8a1996-12-20 20:50:39 +0000620static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000621imageop_rgb82rgb(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000622{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000623 int x, y, len, nlen;
624 unsigned char *cp;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000625 unsigned char *ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000626 PyObject *rv;
627 int i, r, g, b;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000628 unsigned char value;
629 int backward_compatible = imageop_backward_compatible();
Jack Jansen4fada9c1993-02-19 15:51:41 +0000630
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000631 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000632 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000633
Roger E. Massea141f8a1996-12-20 20:50:39 +0000634 nlen = x*y;
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000635 if ( x != (nlen / y) ) {
636 PyErr_SetString(ImageopError, "String has incorrect length");
637 return 0;
638 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000639 if ( nlen != len ) {
640 PyErr_SetString(ImageopError, "String has incorrect length");
641 return 0;
642 }
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000643
644 if ( nlen / x != y || nlen > INT_MAX / 4) {
645 PyErr_SetString(ImageopError, "Image is too large");
646 return 0;
647 }
Jack Jansen4fada9c1993-02-19 15:51:41 +0000648
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000649 rv = PyString_FromStringAndSize(NULL, nlen*4);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000650 if ( rv == 0 )
651 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000652 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000653
Roger E. Massea141f8a1996-12-20 20:50:39 +0000654 for ( i=0; i < nlen; i++ ) {
655 /* Bits in source: RRRBBGGG
656 ** Red and Green are multiplied by 36.5, Blue by 85
657 */
658 value = *cp++;
659 r = (value >> 5) & 7;
660 g = (value ) & 7;
661 b = (value >> 3) & 3;
662 r = (r<<5) | (r<<3) | (r>>1);
663 g = (g<<5) | (g<<3) | (g>>1);
664 b = (b<<6) | (b<<4) | (b<<2) | b;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000665 if (backward_compatible) {
666 Py_UInt32 nvalue = r | (g<<8) | (b<<16);
667 * (Py_UInt32 *) ncp = nvalue;
668 ncp += 4;
669 } else {
670 *ncp++ = 0;
671 *ncp++ = b;
672 *ncp++ = g;
673 *ncp++ = r;
674 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000675 }
676 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000677}
678
Roger E. Massea141f8a1996-12-20 20:50:39 +0000679static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000680imageop_rgb2grey(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000681{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000682 int x, y, len, nlen;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000683 unsigned char *cp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000684 unsigned char *ncp;
685 PyObject *rv;
686 int i, r, g, b;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000687 int nvalue;
688 int backward_compatible = imageop_backward_compatible();
Jack Jansen4fada9c1993-02-19 15:51:41 +0000689
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000690 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000691 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000692
Roger E. Massea141f8a1996-12-20 20:50:39 +0000693 nlen = x*y;
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000694 if ( x != (nlen / y) ) {
695 PyErr_SetString(ImageopError, "String has incorrect length");
696 return 0;
697 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000698 if ( nlen*4 != len ) {
699 PyErr_SetString(ImageopError, "String has incorrect length");
700 return 0;
701 }
Jack Jansen4fada9c1993-02-19 15:51:41 +0000702
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000703 rv = PyString_FromStringAndSize(NULL, nlen);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000704 if ( rv == 0 )
705 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000706 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000707
Roger E. Massea141f8a1996-12-20 20:50:39 +0000708 for ( i=0; i < nlen; i++ ) {
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000709 if (backward_compatible) {
710 Py_UInt32 value = * (Py_UInt32 *) cp;
711 cp += 4;
712 r = (int) ((value & 0xff) / 255. * 7. + .5);
713 g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
714 b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
715 } else {
716 cp++; /* skip alpha channel */
717 b = *cp++;
718 g = *cp++;
719 r = *cp++;
720 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000721 nvalue = (int)(0.30*r + 0.59*g + 0.11*b);
722 if ( nvalue > 255 ) nvalue = 255;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000723 *ncp++ = (unsigned char)nvalue;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000724 }
725 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000726}
727
Roger E. Massea141f8a1996-12-20 20:50:39 +0000728static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000729imageop_grey2rgb(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000730{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000731 int x, y, len, nlen;
732 unsigned char *cp;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000733 unsigned char *ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000734 PyObject *rv;
735 int i;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000736 unsigned char value;
737 int backward_compatible = imageop_backward_compatible();
Jack Jansen4fada9c1993-02-19 15:51:41 +0000738
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000739 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000740 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000741
Roger E. Massea141f8a1996-12-20 20:50:39 +0000742 nlen = x*y;
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000743 if ( x != (nlen / y) ) {
744 PyErr_SetString(ImageopError, "String has incorrect length");
745 return 0;
746 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000747 if ( nlen != len ) {
748 PyErr_SetString(ImageopError, "String has incorrect length");
749 return 0;
750 }
Guido van Rossum93ebfb12008-08-19 21:02:04 +0000751
752 if ( nlen / x != y || nlen > INT_MAX / 4) {
753 PyErr_SetString(ImageopError, "Image is too large");
754 return 0;
755 }
Jack Jansen4fada9c1993-02-19 15:51:41 +0000756
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000757 rv = PyString_FromStringAndSize(NULL, nlen*4);
Roger E. Massea141f8a1996-12-20 20:50:39 +0000758 if ( rv == 0 )
759 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000760 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000761
Roger E. Massea141f8a1996-12-20 20:50:39 +0000762 for ( i=0; i < nlen; i++ ) {
763 value = *cp++;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000764 if (backward_compatible) {
765 * (Py_UInt32 *) ncp = (Py_UInt32) value | ((Py_UInt32) value << 8 ) | ((Py_UInt32) value << 16);
766 ncp += 4;
767 } else {
768 *ncp++ = 0;
769 *ncp++ = value;
770 *ncp++ = value;
771 *ncp++ = value;
772 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000773 }
774 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000775}
776
Guido van Rossum0317a471992-10-26 13:40:15 +0000777/*
778static object *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000779imageop_mul(object *self, object *args)
Guido van Rossum0317a471992-10-26 13:40:15 +0000780{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000781 char *cp, *ncp;
782 int len, size, x, y;
783 object *rv;
784 int i;
Guido van Rossum0317a471992-10-26 13:40:15 +0000785
Roger E. Massea141f8a1996-12-20 20:50:39 +0000786 if ( !getargs(args, "(s#iii)", &cp, &len, &size, &x, &y) )
787 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +0000788
Roger E. Massea141f8a1996-12-20 20:50:39 +0000789 if ( size != 1 && size != 4 ) {
790 err_setstr(ImageopError, "Size should be 1 or 4");
791 return 0;
792 }
793 if ( len != size*x*y ) {
794 err_setstr(ImageopError, "String has incorrect length");
795 return 0;
796 }
Guido van Rossum0317a471992-10-26 13:40:15 +0000797
Roger E. Massea141f8a1996-12-20 20:50:39 +0000798 rv = newsizedstringobject(NULL, XXXX);
799 if ( rv == 0 )
800 return 0;
801 ncp = (char *)getstringvalue(rv);
Guido van Rossum0317a471992-10-26 13:40:15 +0000802
803
Roger E. Massea141f8a1996-12-20 20:50:39 +0000804 for ( i=0; i < len; i += size ) {
805 }
806 return rv;
Guido van Rossum0317a471992-10-26 13:40:15 +0000807}
808*/
809
Roger E. Massea141f8a1996-12-20 20:50:39 +0000810static PyMethodDef imageop_methods[] = {
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000811 { "crop", imageop_crop, METH_VARARGS },
812 { "scale", imageop_scale, METH_VARARGS },
813 { "grey2mono", imageop_grey2mono, METH_VARARGS },
814 { "grey2grey2", imageop_grey2grey2, METH_VARARGS },
815 { "grey2grey4", imageop_grey2grey4, METH_VARARGS },
816 { "dither2mono", imageop_dither2mono, METH_VARARGS },
817 { "dither2grey2", imageop_dither2grey2, METH_VARARGS },
818 { "mono2grey", imageop_mono2grey, METH_VARARGS },
819 { "grey22grey", imageop_grey22grey, METH_VARARGS },
820 { "grey42grey", imageop_grey42grey, METH_VARARGS },
821 { "tovideo", imageop_tovideo, METH_VARARGS },
822 { "rgb2rgb8", imageop_rgb2rgb8, METH_VARARGS },
823 { "rgb82rgb", imageop_rgb82rgb, METH_VARARGS },
824 { "rgb2grey", imageop_rgb2grey, METH_VARARGS },
825 { "grey2rgb", imageop_grey2rgb, METH_VARARGS },
Roger E. Massea141f8a1996-12-20 20:50:39 +0000826 { 0, 0 }
Guido van Rossum0317a471992-10-26 13:40:15 +0000827};
828
829
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000830PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000831initimageop(void)
Guido van Rossum0317a471992-10-26 13:40:15 +0000832{
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000833 PyObject *m;
Brett Cannon42bfa902008-05-12 00:08:34 +0000834
835 if (PyErr_WarnPy3k("the imageop module has been removed in "
836 "Python 3.0", 2) < 0)
837 return;
838
Roger E. Massea141f8a1996-12-20 20:50:39 +0000839 m = Py_InitModule("imageop", imageop_methods);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000840 if (m == NULL)
841 return;
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000842 ImageopDict = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000843 ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
844 if (ImageopError != NULL)
Sjoerd Mullender7e6bbe12004-01-10 20:43:43 +0000845 PyDict_SetItemString(ImageopDict, "error", ImageopError);
Guido van Rossum0317a471992-10-26 13:40:15 +0000846}