Updated for 2.1a3
[python/dscho.git] / Modules / imageop.c
blob586133bfed21d25c88a15a018645066f152b293b
2 /* imageopmodule - Various operations on pictures */
4 #ifdef sun
5 #define signed
6 #endif
8 #include "Python.h"
10 #if SIZEOF_INT == 4
11 typedef int Py_Int32;
12 typedef unsigned int Py_UInt32;
13 #else
14 #if SIZEOF_LONG == 4
15 typedef long Py_Int32;
16 typedef unsigned long Py_UInt32;
17 #else
18 #error "No 4-byte integral type"
19 #endif
20 #endif
22 #define CHARP(cp, xmax, x, y) ((char *)(cp+y*xmax+x))
23 #define SHORTP(cp, xmax, x, y) ((short *)(cp+2*(y*xmax+x)))
24 #define LONGP(cp, xmax, x, y) ((Py_Int32 *)(cp+4*(y*xmax+x)))
26 static PyObject *ImageopError;
28 static PyObject *
29 imageop_crop(PyObject *self, PyObject *args)
31 char *cp, *ncp;
32 short *nsp;
33 Py_Int32 *nlp;
34 int len, size, x, y, newx1, newx2, newy1, newy2;
35 int ix, iy, xstep, ystep;
36 PyObject *rv;
38 if ( !PyArg_Parse(args, "(s#iiiiiii)", &cp, &len, &size, &x, &y,
39 &newx1, &newy1, &newx2, &newy2) )
40 return 0;
42 if ( size != 1 && size != 2 && size != 4 ) {
43 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
44 return 0;
46 if ( len != size*x*y ) {
47 PyErr_SetString(ImageopError, "String has incorrect length");
48 return 0;
50 xstep = (newx1 < newx2)? 1 : -1;
51 ystep = (newy1 < newy2)? 1 : -1;
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;
59 nlp = (Py_Int32 *)ncp;
60 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);
79 return rv;
82 static PyObject *
83 imageop_scale(PyObject *self, PyObject *args)
85 char *cp, *ncp;
86 short *nsp;
87 Py_Int32 *nlp;
88 int len, size, x, y, newx, newy;
89 int ix, iy;
90 int oix, oiy;
91 PyObject *rv;
93 if ( !PyArg_Parse(args, "(s#iiiii)",
94 &cp, &len, &size, &x, &y, &newx, &newy) )
95 return 0;
97 if ( size != 1 && size != 2 && size != 4 ) {
98 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
99 return 0;
101 if ( len != size*x*y ) {
102 PyErr_SetString(ImageopError, "String has incorrect length");
103 return 0;
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;
111 nlp = (Py_Int32 *)ncp;
112 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);
124 return rv;
127 /* Note: this routine can use a bit of optimizing */
129 static PyObject *
130 imageop_tovideo(PyObject *self, PyObject *args)
132 int maxx, maxy, x, y, len;
133 int i;
134 unsigned char *cp, *ncp;
135 int width;
136 PyObject *rv;
139 if ( !PyArg_Parse(args, "(s#iii)", &cp, &len, &width, &maxx, &maxy) )
140 return 0;
142 if ( width != 1 && width != 4 ) {
143 PyErr_SetString(ImageopError, "Size should be 1 or 4");
144 return 0;
146 if ( maxx*maxy*width != len ) {
147 PyErr_SetString(ImageopError, "String has incorrect length");
148 return 0;
151 rv = PyString_FromStringAndSize(NULL, len);
152 if ( rv == 0 )
153 return 0;
154 ncp = (unsigned char *)PyString_AsString(rv);
156 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;
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;
180 return rv;
183 static PyObject *
184 imageop_grey2mono(PyObject *self, PyObject *args)
186 int tres, x, y, len;
187 unsigned char *cp, *ncp;
188 unsigned char ovalue;
189 PyObject *rv;
190 int i, bit;
193 if ( !PyArg_Parse(args, "(s#iii)", &cp, &len, &x, &y, &tres) )
194 return 0;
196 if ( x*y != len ) {
197 PyErr_SetString(ImageopError, "String has incorrect length");
198 return 0;
201 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
202 if ( rv == 0 )
203 return 0;
204 ncp = (unsigned char *)PyString_AsString(rv);
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;
218 if ( bit != 0x80 )
219 *ncp++ = ovalue;
220 return rv;
223 static PyObject *
224 imageop_grey2grey4(PyObject *self, PyObject *args)
226 int x, y, len;
227 unsigned char *cp, *ncp;
228 unsigned char ovalue;
229 PyObject *rv;
230 int i;
231 int pos;
234 if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
235 return 0;
237 if ( x*y != len ) {
238 PyErr_SetString(ImageopError, "String has incorrect length");
239 return 0;
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;
257 if ( pos != 0 )
258 *ncp++ = ovalue;
259 return rv;
262 static PyObject *
263 imageop_grey2grey2(PyObject *self, PyObject *args)
265 int x, y, len;
266 unsigned char *cp, *ncp;
267 unsigned char ovalue;
268 PyObject *rv;
269 int i;
270 int pos;
273 if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
274 return 0;
276 if ( x*y != len ) {
277 PyErr_SetString(ImageopError, "String has incorrect length");
278 return 0;
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;
296 if ( pos != 0 )
297 *ncp++ = ovalue;
298 return rv;
301 static PyObject *
302 imageop_dither2mono(PyObject *self, PyObject *args)
304 int sum, x, y, len;
305 unsigned char *cp, *ncp;
306 unsigned char ovalue;
307 PyObject *rv;
308 int i, bit;
311 if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
312 return 0;
314 if ( x*y != len ) {
315 PyErr_SetString(ImageopError, "String has incorrect length");
316 return 0;
319 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
320 if ( rv == 0 )
321 return 0;
322 ncp = (unsigned char *)PyString_AsString(rv);
324 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;
333 bit >>= 1;
334 if ( bit == 0 ) {
335 *ncp++ = ovalue;
336 bit = 0x80;
337 ovalue = 0;
340 if ( bit != 0x80 )
341 *ncp++ = ovalue;
342 return rv;
345 static PyObject *
346 imageop_dither2grey2(PyObject *self, PyObject *args)
348 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;
357 if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
358 return 0;
360 if ( x*y != len ) {
361 PyErr_SetString(ImageopError, "String has incorrect length");
362 return 0;
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;
383 if ( pos != 0 )
384 *ncp++ = ovalue;
385 return rv;
388 static PyObject *
389 imageop_mono2grey(PyObject *self, PyObject *args)
391 int v0, v1, x, y, len, nlen;
392 unsigned char *cp, *ncp;
393 PyObject *rv;
394 int i, bit;
396 if ( !PyArg_Parse(args, "(s#iiii)", &cp, &len, &x, &y, &v0, &v1) )
397 return 0;
399 nlen = x*y;
400 if ( (nlen+7)/8 != len ) {
401 PyErr_SetString(ImageopError, "String has incorrect length");
402 return 0;
405 rv = PyString_FromStringAndSize(NULL, nlen);
406 if ( rv == 0 )
407 return 0;
408 ncp = (unsigned char *)PyString_AsString(rv);
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++;
422 return rv;
425 static PyObject *
426 imageop_grey22grey(PyObject *self, PyObject *args)
428 int x, y, len, nlen;
429 unsigned char *cp, *ncp;
430 PyObject *rv;
431 int i, pos, value = 0, nvalue;
433 if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
434 return 0;
436 nlen = x*y;
437 if ( (nlen+3)/4 != len ) {
438 PyErr_SetString(ImageopError, "String has incorrect length");
439 return 0;
442 rv = PyString_FromStringAndSize(NULL, nlen);
443 if ( rv == 0 )
444 return 0;
445 ncp = (unsigned char *)PyString_AsString(rv);
447 pos = 0;
448 for ( i=0; i < nlen; i++ ) {
449 if ( pos == 0 ) {
450 value = *cp++;
451 pos = 8;
453 pos -= 2;
454 nvalue = (value >> pos) & 0x03;
455 *ncp++ = nvalue | (nvalue << 2) |
456 (nvalue << 4) | (nvalue << 6);
458 return rv;
461 static PyObject *
462 imageop_grey42grey(PyObject *self, PyObject *args)
464 int x, y, len, nlen;
465 unsigned char *cp, *ncp;
466 PyObject *rv;
467 int i, pos, value = 0, nvalue;
469 if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
470 return 0;
472 nlen = x*y;
473 if ( (nlen+1)/2 != len ) {
474 PyErr_SetString(ImageopError, "String has incorrect length");
475 return 0;
478 rv = PyString_FromStringAndSize(NULL, nlen);
479 if ( rv == 0 )
480 return 0;
481 ncp = (unsigned char *)PyString_AsString(rv);
483 pos = 0;
484 for ( i=0; i < nlen; i++ ) {
485 if ( pos == 0 ) {
486 value = *cp++;
487 pos = 8;
489 pos -= 4;
490 nvalue = (value >> pos) & 0x0f;
491 *ncp++ = nvalue | (nvalue << 4);
493 return rv;
496 static PyObject *
497 imageop_rgb2rgb8(PyObject *self, PyObject *args)
499 int x, y, len, nlen;
500 Py_UInt32 *cp;
501 unsigned char *ncp;
502 PyObject *rv;
503 int i, r, g, b;
504 Py_UInt32 value, nvalue;
506 if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
507 return 0;
509 nlen = x*y;
510 if ( nlen*4 != len ) {
511 PyErr_SetString(ImageopError, "String has incorrect length");
512 return 0;
515 rv = PyString_FromStringAndSize(NULL, nlen);
516 if ( rv == 0 )
517 return 0;
518 ncp = (unsigned char *)PyString_AsString(rv);
520 for ( i=0; i < nlen; i++ ) {
521 /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */
522 value = *cp++;
523 #if 0
524 r = (value >> 5) & 7;
525 g = (value >> 13) & 7;
526 b = (value >> 22) & 3;
527 #else
528 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);
531 #endif
532 nvalue = (r<<5) | (b<<3) | g;
533 *ncp++ = (unsigned char)nvalue;
535 return rv;
538 static PyObject *
539 imageop_rgb82rgb(PyObject *self, PyObject *args)
541 int x, y, len, nlen;
542 unsigned char *cp;
543 Py_UInt32 *ncp;
544 PyObject *rv;
545 int i, r, g, b;
546 Py_UInt32 value, nvalue;
548 if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
549 return 0;
551 nlen = x*y;
552 if ( nlen != len ) {
553 PyErr_SetString(ImageopError, "String has incorrect length");
554 return 0;
557 rv = PyString_FromStringAndSize(NULL, nlen*4);
558 if ( rv == 0 )
559 return 0;
560 ncp = (Py_UInt32 *)PyString_AsString(rv);
562 for ( i=0; i < nlen; i++ ) {
563 /* Bits in source: RRRBBGGG
564 ** Red and Green are multiplied by 36.5, Blue by 85
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;
576 return rv;
579 static PyObject *
580 imageop_rgb2grey(PyObject *self, PyObject *args)
582 int x, y, len, nlen;
583 Py_UInt32 *cp;
584 unsigned char *ncp;
585 PyObject *rv;
586 int i, r, g, b;
587 Py_UInt32 value, nvalue;
589 if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
590 return 0;
592 nlen = x*y;
593 if ( nlen*4 != len ) {
594 PyErr_SetString(ImageopError, "String has incorrect length");
595 return 0;
598 rv = PyString_FromStringAndSize(NULL, nlen);
599 if ( rv == 0 )
600 return 0;
601 ncp = (unsigned char *)PyString_AsString(rv);
603 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;
610 *ncp++ = (unsigned char)nvalue;
612 return rv;
615 static PyObject *
616 imageop_grey2rgb(PyObject *self, PyObject *args)
618 int x, y, len, nlen;
619 unsigned char *cp;
620 Py_UInt32 *ncp;
621 PyObject *rv;
622 int i;
623 Py_UInt32 value;
625 if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) )
626 return 0;
628 nlen = x*y;
629 if ( nlen != len ) {
630 PyErr_SetString(ImageopError, "String has incorrect length");
631 return 0;
634 rv = PyString_FromStringAndSize(NULL, nlen*4);
635 if ( rv == 0 )
636 return 0;
637 ncp = (Py_UInt32 *)PyString_AsString(rv);
639 for ( i=0; i < nlen; i++ ) {
640 value = *cp++;
641 *ncp++ = value | (value << 8 ) | (value << 16);
643 return rv;
647 static object *
648 imageop_mul(object *self, object *args)
650 char *cp, *ncp;
651 int len, size, x, y;
652 object *rv;
653 int i;
655 if ( !getargs(args, "(s#iii)", &cp, &len, &size, &x, &y) )
656 return 0;
658 if ( size != 1 && size != 4 ) {
659 err_setstr(ImageopError, "Size should be 1 or 4");
660 return 0;
662 if ( len != size*x*y ) {
663 err_setstr(ImageopError, "String has incorrect length");
664 return 0;
667 rv = newsizedstringobject(NULL, XXXX);
668 if ( rv == 0 )
669 return 0;
670 ncp = (char *)getstringvalue(rv);
673 for ( i=0; i < len; i += size ) {
675 return rv;
679 static PyMethodDef imageop_methods[] = {
680 { "crop", imageop_crop },
681 { "scale", imageop_scale },
682 { "grey2mono", imageop_grey2mono },
683 { "grey2grey2", imageop_grey2grey2 },
684 { "grey2grey4", imageop_grey2grey4 },
685 { "dither2mono", imageop_dither2mono },
686 { "dither2grey2", imageop_dither2grey2 },
687 { "mono2grey", imageop_mono2grey },
688 { "grey22grey", imageop_grey22grey },
689 { "grey42grey", imageop_grey42grey },
690 { "tovideo", imageop_tovideo },
691 { "rgb2rgb8", imageop_rgb2rgb8 },
692 { "rgb82rgb", imageop_rgb82rgb },
693 { "rgb2grey", imageop_rgb2grey },
694 { "grey2rgb", imageop_grey2rgb },
695 { 0, 0 }
699 DL_EXPORT(void)
700 initimageop(void)
702 PyObject *m, *d;
703 m = Py_InitModule("imageop", imageop_methods);
704 d = PyModule_GetDict(m);
705 ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
706 if (ImageopError != NULL)
707 PyDict_SetItemString(d, "error", ImageopError);