blob: 8c83cc3cf27d1bc1fd4ac784c390c57c32c42534 [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;
Guido van Rossum0317a471992-10-26 13:40:15 +000027
Roger E. Massea141f8a1996-12-20 20:50:39 +000028static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +000029imageop_crop(PyObject *self, PyObject *args)
Guido van Rossum0317a471992-10-26 13:40:15 +000030{
Roger E. Massea141f8a1996-12-20 20:50:39 +000031 char *cp, *ncp;
32 short *nsp;
Guido van Rossum69011961998-04-23 20:23:00 +000033 Py_Int32 *nlp;
Roger E. Massea141f8a1996-12-20 20:50:39 +000034 int len, size, x, y, newx1, newx2, newy1, newy2;
35 int ix, iy, xstep, ystep;
36 PyObject *rv;
Guido van Rossum0317a471992-10-26 13:40:15 +000037
Neal Norwitzb0aaec52002-04-02 18:26:33 +000038 if ( !PyArg_ParseTuple(args, "s#iiiiiii", &cp, &len, &size, &x, &y,
Roger E. Massea141f8a1996-12-20 20:50:39 +000039 &newx1, &newy1, &newx2, &newy2) )
40 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +000041
Roger E. Massea141f8a1996-12-20 20:50:39 +000042 if ( size != 1 && size != 2 && size != 4 ) {
43 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
44 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +000045 }
Roger E. Massea141f8a1996-12-20 20:50:39 +000046 if ( len != size*x*y ) {
47 PyErr_SetString(ImageopError, "String has incorrect length");
48 return 0;
49 }
50 xstep = (newx1 < newx2)? 1 : -1;
51 ystep = (newy1 < newy2)? 1 : -1;
52
53 rv = PyString_FromStringAndSize(NULL,
54 (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size);
55 if ( rv == 0 )
56 return 0;
57 ncp = (char *)PyString_AsString(rv);
58 nsp = (short *)ncp;
Guido van Rossum69011961998-04-23 20:23:00 +000059 nlp = (Py_Int32 *)ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +000060 newy2 += ystep;
61 newx2 += xstep;
62 for( iy = newy1; iy != newy2; iy+=ystep ) {
63 for ( ix = newx1; ix != newx2; ix+=xstep ) {
64 if ( iy < 0 || iy >= y || ix < 0 || ix >= x ) {
65 if ( size == 1 )
66 *ncp++ = 0;
67 else
68 *nlp++ = 0;
69 } else {
70 if ( size == 1 )
71 *ncp++ = *CHARP(cp, x, ix, iy);
72 else if ( size == 2 )
73 *nsp++ = *SHORTP(cp, x, ix, iy);
74 else
75 *nlp++ = *LONGP(cp, x, ix, iy);
76 }
77 }
78 }
79 return rv;
Guido van Rossum0317a471992-10-26 13:40:15 +000080}
81
Roger E. Massea141f8a1996-12-20 20:50:39 +000082static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +000083imageop_scale(PyObject *self, PyObject *args)
Guido van Rossum0317a471992-10-26 13:40:15 +000084{
Roger E. Massea141f8a1996-12-20 20:50:39 +000085 char *cp, *ncp;
86 short *nsp;
Guido van Rossum69011961998-04-23 20:23:00 +000087 Py_Int32 *nlp;
Roger E. Massea141f8a1996-12-20 20:50:39 +000088 int len, size, x, y, newx, newy;
89 int ix, iy;
90 int oix, oiy;
91 PyObject *rv;
Guido van Rossum0317a471992-10-26 13:40:15 +000092
Neal Norwitzb0aaec52002-04-02 18:26:33 +000093 if ( !PyArg_ParseTuple(args, "s#iiiii",
Roger E. Massea141f8a1996-12-20 20:50:39 +000094 &cp, &len, &size, &x, &y, &newx, &newy) )
95 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +000096
Roger E. Massea141f8a1996-12-20 20:50:39 +000097 if ( size != 1 && size != 2 && size != 4 ) {
98 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
99 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +0000100 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000101 if ( len != size*x*y ) {
102 PyErr_SetString(ImageopError, "String has incorrect length");
103 return 0;
104 }
105
106 rv = PyString_FromStringAndSize(NULL, newx*newy*size);
107 if ( rv == 0 )
108 return 0;
109 ncp = (char *)PyString_AsString(rv);
110 nsp = (short *)ncp;
Guido van Rossum69011961998-04-23 20:23:00 +0000111 nlp = (Py_Int32 *)ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000112 for( iy = 0; iy < newy; iy++ ) {
113 for ( ix = 0; ix < newx; ix++ ) {
114 oix = ix * x / newx;
115 oiy = iy * y / newy;
116 if ( size == 1 )
117 *ncp++ = *CHARP(cp, x, oix, oiy);
118 else if ( size == 2 )
119 *nsp++ = *SHORTP(cp, x, oix, oiy);
120 else
121 *nlp++ = *LONGP(cp, x, oix, oiy);
122 }
123 }
124 return rv;
Guido van Rossum0317a471992-10-26 13:40:15 +0000125}
126
Jack Jansend26b4581993-01-22 15:34:43 +0000127/* Note: this routine can use a bit of optimizing */
128
Roger E. Massea141f8a1996-12-20 20:50:39 +0000129static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000130imageop_tovideo(PyObject *self, PyObject *args)
Jack Jansend26b4581993-01-22 15:34:43 +0000131{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000132 int maxx, maxy, x, y, len;
133 int i;
134 unsigned char *cp, *ncp;
135 int width;
136 PyObject *rv;
Jack Jansend26b4581993-01-22 15:34:43 +0000137
138
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000139 if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &width, &maxx, &maxy) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000140 return 0;
Jack Jansend26b4581993-01-22 15:34:43 +0000141
Roger E. Massea141f8a1996-12-20 20:50:39 +0000142 if ( width != 1 && width != 4 ) {
143 PyErr_SetString(ImageopError, "Size should be 1 or 4");
144 return 0;
145 }
146 if ( maxx*maxy*width != len ) {
147 PyErr_SetString(ImageopError, "String has incorrect length");
148 return 0;
149 }
Jack Jansend26b4581993-01-22 15:34:43 +0000150
Roger E. Massea141f8a1996-12-20 20:50:39 +0000151 rv = PyString_FromStringAndSize(NULL, len);
152 if ( rv == 0 )
153 return 0;
154 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansend26b4581993-01-22 15:34:43 +0000155
Roger E. Massea141f8a1996-12-20 20:50:39 +0000156 if ( width == 1 ) {
157 memcpy(ncp, cp, maxx); /* Copy first line */
158 ncp += maxx;
159 for (y=1; y<maxy; y++) { /* Interpolate other lines */
160 for(x=0; x<maxx; x++) {
161 i = y*maxx + x;
162 *ncp++ = ((int)cp[i] + (int)cp[i-maxx]) >> 1;
163 }
164 }
165 } else {
166 memcpy(ncp, cp, maxx*4); /* Copy first line */
167 ncp += maxx*4;
168 for (y=1; y<maxy; y++) { /* Interpolate other lines */
169 for(x=0; x<maxx; x++) {
170 i = (y*maxx + x)*4 + 1;
171 *ncp++ = 0; /* Skip alfa comp */
172 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
173 i++;
174 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
175 i++;
176 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
177 }
178 }
Jack Jansend26b4581993-01-22 15:34:43 +0000179 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000180 return rv;
Jack Jansend26b4581993-01-22 15:34:43 +0000181}
182
Roger E. Massea141f8a1996-12-20 20:50:39 +0000183static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000184imageop_grey2mono(PyObject *self, PyObject *args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000185{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000186 int tres, x, y, len;
187 unsigned char *cp, *ncp;
188 unsigned char ovalue;
189 PyObject *rv;
190 int i, bit;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000191
192
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000193 if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &x, &y, &tres) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000194 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000195
Roger E. Massea141f8a1996-12-20 20:50:39 +0000196 if ( x*y != len ) {
197 PyErr_SetString(ImageopError, "String has incorrect length");
198 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000199 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000200
201 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
202 if ( rv == 0 )
203 return 0;
204 ncp = (unsigned char *)PyString_AsString(rv);
205
206 bit = 0x80;
207 ovalue = 0;
208 for ( i=0; i < len; i++ ) {
209 if ( (int)cp[i] > tres )
210 ovalue |= bit;
211 bit >>= 1;
212 if ( bit == 0 ) {
213 *ncp++ = ovalue;
214 bit = 0x80;
215 ovalue = 0;
216 }
217 }
218 if ( bit != 0x80 )
219 *ncp++ = ovalue;
220 return rv;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000221}
222
Roger E. Massea141f8a1996-12-20 20:50:39 +0000223static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000224imageop_grey2grey4(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000225{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000226 int x, y, len;
227 unsigned char *cp, *ncp;
228 unsigned char ovalue;
229 PyObject *rv;
230 int i;
231 int pos;
Jack Jansende3adf91992-12-22 14:05:55 +0000232
233
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000234 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000235 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000236
Roger E. Massea141f8a1996-12-20 20:50:39 +0000237 if ( x*y != len ) {
238 PyErr_SetString(ImageopError, "String has incorrect length");
239 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000240 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000241
242 rv = PyString_FromStringAndSize(NULL, (len+1)/2);
243 if ( rv == 0 )
244 return 0;
245 ncp = (unsigned char *)PyString_AsString(rv);
246 pos = 0;
247 ovalue = 0;
248 for ( i=0; i < len; i++ ) {
249 ovalue |= ((int)cp[i] & 0xf0) >> pos;
250 pos += 4;
251 if ( pos == 8 ) {
252 *ncp++ = ovalue;
253 ovalue = 0;
254 pos = 0;
255 }
256 }
257 if ( pos != 0 )
258 *ncp++ = ovalue;
259 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000260}
261
Roger E. Massea141f8a1996-12-20 20:50:39 +0000262static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000263imageop_grey2grey2(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000264{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000265 int x, y, len;
266 unsigned char *cp, *ncp;
267 unsigned char ovalue;
268 PyObject *rv;
269 int i;
270 int pos;
Jack Jansende3adf91992-12-22 14:05:55 +0000271
272
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000273 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000274 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000275
Roger E. Massea141f8a1996-12-20 20:50:39 +0000276 if ( x*y != len ) {
277 PyErr_SetString(ImageopError, "String has incorrect length");
278 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000279 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000280
281 rv = PyString_FromStringAndSize(NULL, (len+3)/4);
282 if ( rv == 0 )
283 return 0;
284 ncp = (unsigned char *)PyString_AsString(rv);
285 pos = 0;
286 ovalue = 0;
287 for ( i=0; i < len; i++ ) {
288 ovalue |= ((int)cp[i] & 0xc0) >> pos;
289 pos += 2;
290 if ( pos == 8 ) {
291 *ncp++ = ovalue;
292 ovalue = 0;
293 pos = 0;
294 }
295 }
296 if ( pos != 0 )
297 *ncp++ = ovalue;
298 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000299}
300
Roger E. Massea141f8a1996-12-20 20:50:39 +0000301static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000302imageop_dither2mono(PyObject *self, PyObject *args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000303{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000304 int sum, x, y, len;
305 unsigned char *cp, *ncp;
306 unsigned char ovalue;
307 PyObject *rv;
308 int i, bit;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000309
310
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000311 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000312 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000313
Roger E. Massea141f8a1996-12-20 20:50:39 +0000314 if ( x*y != len ) {
315 PyErr_SetString(ImageopError, "String has incorrect length");
316 return 0;
317 }
Guido van Rossum5f59d601992-12-14 16:59:51 +0000318
Roger E. Massea141f8a1996-12-20 20:50:39 +0000319 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
320 if ( rv == 0 )
321 return 0;
322 ncp = (unsigned char *)PyString_AsString(rv);
Guido van Rossum5f59d601992-12-14 16:59:51 +0000323
Roger E. Massea141f8a1996-12-20 20:50:39 +0000324 bit = 0x80;
325 ovalue = 0;
326 sum = 0;
327 for ( i=0; i < len; i++ ) {
328 sum += cp[i];
329 if ( sum >= 256 ) {
330 sum -= 256;
331 ovalue |= bit;
332 }
333 bit >>= 1;
334 if ( bit == 0 ) {
335 *ncp++ = ovalue;
336 bit = 0x80;
337 ovalue = 0;
338 }
Guido van Rossum5f59d601992-12-14 16:59:51 +0000339 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000340 if ( bit != 0x80 )
341 *ncp++ = ovalue;
342 return rv;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000343}
344
Roger E. Massea141f8a1996-12-20 20:50:39 +0000345static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000346imageop_dither2grey2(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;
354 int sum = 0, nvalue;
Jack Jansende3adf91992-12-22 14:05:55 +0000355
356
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000357 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000358 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000359
Roger E. Massea141f8a1996-12-20 20:50:39 +0000360 if ( x*y != len ) {
361 PyErr_SetString(ImageopError, "String has incorrect length");
362 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000363 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000364
365 rv = PyString_FromStringAndSize(NULL, (len+3)/4);
366 if ( rv == 0 )
367 return 0;
368 ncp = (unsigned char *)PyString_AsString(rv);
369 pos = 1;
370 ovalue = 0;
371 for ( i=0; i < len; i++ ) {
372 sum += cp[i];
373 nvalue = sum & 0x180;
374 sum -= nvalue;
375 ovalue |= nvalue >> pos;
376 pos += 2;
377 if ( pos == 9 ) {
378 *ncp++ = ovalue;
379 ovalue = 0;
380 pos = 1;
381 }
382 }
383 if ( pos != 0 )
384 *ncp++ = ovalue;
385 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000386}
387
Roger E. Massea141f8a1996-12-20 20:50:39 +0000388static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000389imageop_mono2grey(PyObject *self, PyObject *args)
Guido van Rossum5f59d601992-12-14 16:59:51 +0000390{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000391 int v0, v1, x, y, len, nlen;
392 unsigned char *cp, *ncp;
393 PyObject *rv;
394 int i, bit;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000395
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000396 if ( !PyArg_ParseTuple(args, "s#iiii", &cp, &len, &x, &y, &v0, &v1) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000397 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000398
Roger E. Massea141f8a1996-12-20 20:50:39 +0000399 nlen = x*y;
400 if ( (nlen+7)/8 != len ) {
401 PyErr_SetString(ImageopError, "String has incorrect length");
402 return 0;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000403 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000404
405 rv = PyString_FromStringAndSize(NULL, nlen);
406 if ( rv == 0 )
407 return 0;
408 ncp = (unsigned char *)PyString_AsString(rv);
409
410 bit = 0x80;
411 for ( i=0; i < nlen; i++ ) {
412 if ( *cp & bit )
413 *ncp++ = v1;
414 else
415 *ncp++ = v0;
416 bit >>= 1;
417 if ( bit == 0 ) {
418 bit = 0x80;
419 cp++;
420 }
421 }
422 return rv;
Guido van Rossum5f59d601992-12-14 16:59:51 +0000423}
424
Roger E. Massea141f8a1996-12-20 20:50:39 +0000425static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000426imageop_grey22grey(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000427{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000428 int x, y, len, nlen;
429 unsigned char *cp, *ncp;
430 PyObject *rv;
431 int i, pos, value = 0, nvalue;
Jack Jansende3adf91992-12-22 14:05:55 +0000432
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000433 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000434 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000435
Roger E. Massea141f8a1996-12-20 20:50:39 +0000436 nlen = x*y;
437 if ( (nlen+3)/4 != len ) {
438 PyErr_SetString(ImageopError, "String has incorrect length");
439 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000440 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000441
442 rv = PyString_FromStringAndSize(NULL, nlen);
443 if ( rv == 0 )
444 return 0;
445 ncp = (unsigned char *)PyString_AsString(rv);
446
447 pos = 0;
448 for ( i=0; i < nlen; i++ ) {
449 if ( pos == 0 ) {
450 value = *cp++;
451 pos = 8;
452 }
453 pos -= 2;
454 nvalue = (value >> pos) & 0x03;
455 *ncp++ = nvalue | (nvalue << 2) |
456 (nvalue << 4) | (nvalue << 6);
457 }
458 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000459}
460
Roger E. Massea141f8a1996-12-20 20:50:39 +0000461static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000462imageop_grey42grey(PyObject *self, PyObject *args)
Jack Jansende3adf91992-12-22 14:05:55 +0000463{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000464 int x, y, len, nlen;
465 unsigned char *cp, *ncp;
466 PyObject *rv;
467 int i, pos, value = 0, nvalue;
Jack Jansende3adf91992-12-22 14:05:55 +0000468
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000469 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000470 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000471
Roger E. Massea141f8a1996-12-20 20:50:39 +0000472 nlen = x*y;
473 if ( (nlen+1)/2 != len ) {
474 PyErr_SetString(ImageopError, "String has incorrect length");
475 return 0;
Jack Jansende3adf91992-12-22 14:05:55 +0000476 }
Roger E. Massea141f8a1996-12-20 20:50:39 +0000477
478 rv = PyString_FromStringAndSize(NULL, nlen);
479 if ( rv == 0 )
480 return 0;
481 ncp = (unsigned char *)PyString_AsString(rv);
482
483 pos = 0;
484 for ( i=0; i < nlen; i++ ) {
485 if ( pos == 0 ) {
486 value = *cp++;
487 pos = 8;
488 }
489 pos -= 4;
490 nvalue = (value >> pos) & 0x0f;
491 *ncp++ = nvalue | (nvalue << 4);
492 }
493 return rv;
Jack Jansende3adf91992-12-22 14:05:55 +0000494}
495
Roger E. Massea141f8a1996-12-20 20:50:39 +0000496static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000497imageop_rgb2rgb8(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000498{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000499 int x, y, len, nlen;
Guido van Rossum69011961998-04-23 20:23:00 +0000500 Py_UInt32 *cp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000501 unsigned char *ncp;
502 PyObject *rv;
503 int i, r, g, b;
Guido van Rossum69011961998-04-23 20:23:00 +0000504 Py_UInt32 value, nvalue;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000505
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000506 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000507 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000508
Roger E. Massea141f8a1996-12-20 20:50:39 +0000509 nlen = x*y;
510 if ( nlen*4 != len ) {
511 PyErr_SetString(ImageopError, "String has incorrect length");
512 return 0;
513 }
Jack Jansen4fada9c1993-02-19 15:51:41 +0000514
Roger E. Massea141f8a1996-12-20 20:50:39 +0000515 rv = PyString_FromStringAndSize(NULL, nlen);
516 if ( rv == 0 )
517 return 0;
518 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000519
Roger E. Massea141f8a1996-12-20 20:50:39 +0000520 for ( i=0; i < nlen; i++ ) {
521 /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */
522 value = *cp++;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000523#if 0
Roger E. Massea141f8a1996-12-20 20:50:39 +0000524 r = (value >> 5) & 7;
525 g = (value >> 13) & 7;
526 b = (value >> 22) & 3;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000527#else
Roger E. Massea141f8a1996-12-20 20:50:39 +0000528 r = (int) ((value & 0xff) / 255. * 7. + .5);
529 g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
530 b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000531#endif
Roger E. Massea141f8a1996-12-20 20:50:39 +0000532 nvalue = (r<<5) | (b<<3) | g;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000533 *ncp++ = (unsigned char)nvalue;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000534 }
535 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000536}
537
Roger E. Massea141f8a1996-12-20 20:50:39 +0000538static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000539imageop_rgb82rgb(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000540{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000541 int x, y, len, nlen;
542 unsigned char *cp;
Guido van Rossum69011961998-04-23 20:23:00 +0000543 Py_UInt32 *ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000544 PyObject *rv;
545 int i, r, g, b;
Guido van Rossum69011961998-04-23 20:23:00 +0000546 Py_UInt32 value, nvalue;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000547
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000548 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000549 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000550
Roger E. Massea141f8a1996-12-20 20:50:39 +0000551 nlen = x*y;
552 if ( nlen != len ) {
553 PyErr_SetString(ImageopError, "String has incorrect length");
554 return 0;
555 }
Jack Jansen4fada9c1993-02-19 15:51:41 +0000556
Roger E. Massea141f8a1996-12-20 20:50:39 +0000557 rv = PyString_FromStringAndSize(NULL, nlen*4);
558 if ( rv == 0 )
559 return 0;
Guido van Rossum69011961998-04-23 20:23:00 +0000560 ncp = (Py_UInt32 *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000561
Roger E. Massea141f8a1996-12-20 20:50:39 +0000562 for ( i=0; i < nlen; i++ ) {
563 /* Bits in source: RRRBBGGG
564 ** Red and Green are multiplied by 36.5, Blue by 85
565 */
566 value = *cp++;
567 r = (value >> 5) & 7;
568 g = (value ) & 7;
569 b = (value >> 3) & 3;
570 r = (r<<5) | (r<<3) | (r>>1);
571 g = (g<<5) | (g<<3) | (g>>1);
572 b = (b<<6) | (b<<4) | (b<<2) | b;
573 nvalue = r | (g<<8) | (b<<16);
574 *ncp++ = nvalue;
575 }
576 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000577}
578
Roger E. Massea141f8a1996-12-20 20:50:39 +0000579static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000580imageop_rgb2grey(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000581{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000582 int x, y, len, nlen;
Guido van Rossum69011961998-04-23 20:23:00 +0000583 Py_UInt32 *cp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000584 unsigned char *ncp;
585 PyObject *rv;
586 int i, r, g, b;
Guido van Rossum69011961998-04-23 20:23:00 +0000587 Py_UInt32 value, nvalue;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000588
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000589 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000590 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000591
Roger E. Massea141f8a1996-12-20 20:50:39 +0000592 nlen = x*y;
593 if ( nlen*4 != len ) {
594 PyErr_SetString(ImageopError, "String has incorrect length");
595 return 0;
596 }
Jack Jansen4fada9c1993-02-19 15:51:41 +0000597
Roger E. Massea141f8a1996-12-20 20:50:39 +0000598 rv = PyString_FromStringAndSize(NULL, nlen);
599 if ( rv == 0 )
600 return 0;
601 ncp = (unsigned char *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000602
Roger E. Massea141f8a1996-12-20 20:50:39 +0000603 for ( i=0; i < nlen; i++ ) {
604 value = *cp++;
605 r = (value ) & 0xff;
606 g = (value >> 8) & 0xff;
607 b = (value >> 16) & 0xff;
608 nvalue = (int)(0.30*r + 0.59*g + 0.11*b);
609 if ( nvalue > 255 ) nvalue = 255;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000610 *ncp++ = (unsigned char)nvalue;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000611 }
612 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000613}
614
Roger E. Massea141f8a1996-12-20 20:50:39 +0000615static PyObject *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000616imageop_grey2rgb(PyObject *self, PyObject *args)
Jack Jansen4fada9c1993-02-19 15:51:41 +0000617{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000618 int x, y, len, nlen;
619 unsigned char *cp;
Guido van Rossum69011961998-04-23 20:23:00 +0000620 Py_UInt32 *ncp;
Roger E. Massea141f8a1996-12-20 20:50:39 +0000621 PyObject *rv;
622 int i;
Guido van Rossum69011961998-04-23 20:23:00 +0000623 Py_UInt32 value;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000624
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000625 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
Roger E. Massea141f8a1996-12-20 20:50:39 +0000626 return 0;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000627
Roger E. Massea141f8a1996-12-20 20:50:39 +0000628 nlen = x*y;
629 if ( nlen != len ) {
630 PyErr_SetString(ImageopError, "String has incorrect length");
631 return 0;
632 }
Jack Jansen4fada9c1993-02-19 15:51:41 +0000633
Roger E. Massea141f8a1996-12-20 20:50:39 +0000634 rv = PyString_FromStringAndSize(NULL, nlen*4);
635 if ( rv == 0 )
636 return 0;
Guido van Rossum69011961998-04-23 20:23:00 +0000637 ncp = (Py_UInt32 *)PyString_AsString(rv);
Jack Jansen4fada9c1993-02-19 15:51:41 +0000638
Roger E. Massea141f8a1996-12-20 20:50:39 +0000639 for ( i=0; i < nlen; i++ ) {
640 value = *cp++;
641 *ncp++ = value | (value << 8 ) | (value << 16);
642 }
643 return rv;
Jack Jansen4fada9c1993-02-19 15:51:41 +0000644}
645
Guido van Rossum0317a471992-10-26 13:40:15 +0000646/*
647static object *
Peter Schneider-Kampfe742632000-07-10 09:55:32 +0000648imageop_mul(object *self, object *args)
Guido van Rossum0317a471992-10-26 13:40:15 +0000649{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000650 char *cp, *ncp;
651 int len, size, x, y;
652 object *rv;
653 int i;
Guido van Rossum0317a471992-10-26 13:40:15 +0000654
Roger E. Massea141f8a1996-12-20 20:50:39 +0000655 if ( !getargs(args, "(s#iii)", &cp, &len, &size, &x, &y) )
656 return 0;
Guido van Rossum0317a471992-10-26 13:40:15 +0000657
Roger E. Massea141f8a1996-12-20 20:50:39 +0000658 if ( size != 1 && size != 4 ) {
659 err_setstr(ImageopError, "Size should be 1 or 4");
660 return 0;
661 }
662 if ( len != size*x*y ) {
663 err_setstr(ImageopError, "String has incorrect length");
664 return 0;
665 }
Guido van Rossum0317a471992-10-26 13:40:15 +0000666
Roger E. Massea141f8a1996-12-20 20:50:39 +0000667 rv = newsizedstringobject(NULL, XXXX);
668 if ( rv == 0 )
669 return 0;
670 ncp = (char *)getstringvalue(rv);
Guido van Rossum0317a471992-10-26 13:40:15 +0000671
672
Roger E. Massea141f8a1996-12-20 20:50:39 +0000673 for ( i=0; i < len; i += size ) {
674 }
675 return rv;
Guido van Rossum0317a471992-10-26 13:40:15 +0000676}
677*/
678
Roger E. Massea141f8a1996-12-20 20:50:39 +0000679static PyMethodDef imageop_methods[] = {
Neal Norwitzb0aaec52002-04-02 18:26:33 +0000680 { "crop", imageop_crop, METH_VARARGS },
681 { "scale", imageop_scale, METH_VARARGS },
682 { "grey2mono", imageop_grey2mono, METH_VARARGS },
683 { "grey2grey2", imageop_grey2grey2, METH_VARARGS },
684 { "grey2grey4", imageop_grey2grey4, METH_VARARGS },
685 { "dither2mono", imageop_dither2mono, METH_VARARGS },
686 { "dither2grey2", imageop_dither2grey2, METH_VARARGS },
687 { "mono2grey", imageop_mono2grey, METH_VARARGS },
688 { "grey22grey", imageop_grey22grey, METH_VARARGS },
689 { "grey42grey", imageop_grey42grey, METH_VARARGS },
690 { "tovideo", imageop_tovideo, METH_VARARGS },
691 { "rgb2rgb8", imageop_rgb2rgb8, METH_VARARGS },
692 { "rgb82rgb", imageop_rgb82rgb, METH_VARARGS },
693 { "rgb2grey", imageop_rgb2grey, METH_VARARGS },
694 { "grey2rgb", imageop_grey2rgb, METH_VARARGS },
Roger E. Massea141f8a1996-12-20 20:50:39 +0000695 { 0, 0 }
Guido van Rossum0317a471992-10-26 13:40:15 +0000696};
697
698
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000699PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000700initimageop(void)
Guido van Rossum0317a471992-10-26 13:40:15 +0000701{
Roger E. Massea141f8a1996-12-20 20:50:39 +0000702 PyObject *m, *d;
703 m = Py_InitModule("imageop", imageop_methods);
704 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000705 ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
706 if (ImageopError != NULL)
707 PyDict_SetItemString(d, "error", ImageopError);
Guido van Rossum0317a471992-10-26 13:40:15 +0000708}