Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* imageopmodule - Various operations on pictures */ |
| 12 | |
| 13 | #ifdef sun |
| 14 | #define signed |
| 15 | #endif |
| 16 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 17 | #include "Python.h" |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 19 | #if SIZEOF_INT == 4 |
| 20 | typedef int Py_Int32; |
| 21 | typedef unsigned int Py_UInt32; |
| 22 | #else |
| 23 | #if SIZEOF_LONG == 4 |
| 24 | typedef long Py_Int32; |
| 25 | typedef unsigned long Py_UInt32; |
| 26 | #else |
| 27 | #error "No 4-byte integral type" |
| 28 | #endif |
| 29 | #endif |
| 30 | |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 31 | #define CHARP(cp, xmax, x, y) ((char *)(cp+y*xmax+x)) |
Jack Jansen | 76c79e9 | 1996-01-22 14:55:15 +0000 | [diff] [blame] | 32 | #define SHORTP(cp, xmax, x, y) ((short *)(cp+2*(y*xmax+x))) |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 33 | #define LONGP(cp, xmax, x, y) ((Py_Int32 *)(cp+4*(y*xmax+x))) |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 34 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 35 | static PyObject *ImageopError; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 36 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 37 | static PyObject * |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 38 | imageop_crop(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 39 | PyObject *self; |
| 40 | PyObject *args; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 41 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 42 | char *cp, *ncp; |
| 43 | short *nsp; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 44 | Py_Int32 *nlp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 45 | int len, size, x, y, newx1, newx2, newy1, newy2; |
| 46 | int ix, iy, xstep, ystep; |
| 47 | PyObject *rv; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 48 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 49 | if ( !PyArg_Parse(args, "(s#iiiiiii)", &cp, &len, &size, &x, &y, |
| 50 | &newx1, &newy1, &newx2, &newy2) ) |
| 51 | return 0; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 52 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 53 | if ( size != 1 && size != 2 && size != 4 ) { |
| 54 | PyErr_SetString(ImageopError, "Size should be 1, 2 or 4"); |
| 55 | return 0; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 56 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 57 | if ( len != size*x*y ) { |
| 58 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 59 | return 0; |
| 60 | } |
| 61 | xstep = (newx1 < newx2)? 1 : -1; |
| 62 | ystep = (newy1 < newy2)? 1 : -1; |
| 63 | |
| 64 | rv = PyString_FromStringAndSize(NULL, |
| 65 | (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size); |
| 66 | if ( rv == 0 ) |
| 67 | return 0; |
| 68 | ncp = (char *)PyString_AsString(rv); |
| 69 | nsp = (short *)ncp; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 70 | nlp = (Py_Int32 *)ncp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 71 | newy2 += ystep; |
| 72 | newx2 += xstep; |
| 73 | for( iy = newy1; iy != newy2; iy+=ystep ) { |
| 74 | for ( ix = newx1; ix != newx2; ix+=xstep ) { |
| 75 | if ( iy < 0 || iy >= y || ix < 0 || ix >= x ) { |
| 76 | if ( size == 1 ) |
| 77 | *ncp++ = 0; |
| 78 | else |
| 79 | *nlp++ = 0; |
| 80 | } else { |
| 81 | if ( size == 1 ) |
| 82 | *ncp++ = *CHARP(cp, x, ix, iy); |
| 83 | else if ( size == 2 ) |
| 84 | *nsp++ = *SHORTP(cp, x, ix, iy); |
| 85 | else |
| 86 | *nlp++ = *LONGP(cp, x, ix, iy); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | return rv; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 93 | static PyObject * |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 94 | imageop_scale(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 95 | PyObject *self; |
| 96 | PyObject *args; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 97 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 98 | char *cp, *ncp; |
| 99 | short *nsp; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 100 | Py_Int32 *nlp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 101 | int len, size, x, y, newx, newy; |
| 102 | int ix, iy; |
| 103 | int oix, oiy; |
| 104 | PyObject *rv; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 105 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 106 | if ( !PyArg_Parse(args, "(s#iiiii)", |
| 107 | &cp, &len, &size, &x, &y, &newx, &newy) ) |
| 108 | return 0; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 109 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 110 | if ( size != 1 && size != 2 && size != 4 ) { |
| 111 | PyErr_SetString(ImageopError, "Size should be 1, 2 or 4"); |
| 112 | return 0; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 113 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 114 | if ( len != size*x*y ) { |
| 115 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | rv = PyString_FromStringAndSize(NULL, newx*newy*size); |
| 120 | if ( rv == 0 ) |
| 121 | return 0; |
| 122 | ncp = (char *)PyString_AsString(rv); |
| 123 | nsp = (short *)ncp; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 124 | nlp = (Py_Int32 *)ncp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 125 | for( iy = 0; iy < newy; iy++ ) { |
| 126 | for ( ix = 0; ix < newx; ix++ ) { |
| 127 | oix = ix * x / newx; |
| 128 | oiy = iy * y / newy; |
| 129 | if ( size == 1 ) |
| 130 | *ncp++ = *CHARP(cp, x, oix, oiy); |
| 131 | else if ( size == 2 ) |
| 132 | *nsp++ = *SHORTP(cp, x, oix, oiy); |
| 133 | else |
| 134 | *nlp++ = *LONGP(cp, x, oix, oiy); |
| 135 | } |
| 136 | } |
| 137 | return rv; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 140 | /* Note: this routine can use a bit of optimizing */ |
| 141 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 142 | static PyObject * |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 143 | imageop_tovideo(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 144 | PyObject *self; |
| 145 | PyObject *args; |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 146 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 147 | int maxx, maxy, x, y, len; |
| 148 | int i; |
| 149 | unsigned char *cp, *ncp; |
| 150 | int width; |
| 151 | PyObject *rv; |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 152 | |
| 153 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 154 | if ( !PyArg_Parse(args, "(s#iii)", &cp, &len, &width, &maxx, &maxy) ) |
| 155 | return 0; |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 156 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 157 | if ( width != 1 && width != 4 ) { |
| 158 | PyErr_SetString(ImageopError, "Size should be 1 or 4"); |
| 159 | return 0; |
| 160 | } |
| 161 | if ( maxx*maxy*width != len ) { |
| 162 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 163 | return 0; |
| 164 | } |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 165 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 166 | rv = PyString_FromStringAndSize(NULL, len); |
| 167 | if ( rv == 0 ) |
| 168 | return 0; |
| 169 | ncp = (unsigned char *)PyString_AsString(rv); |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 170 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 171 | if ( width == 1 ) { |
| 172 | memcpy(ncp, cp, maxx); /* Copy first line */ |
| 173 | ncp += maxx; |
| 174 | for (y=1; y<maxy; y++) { /* Interpolate other lines */ |
| 175 | for(x=0; x<maxx; x++) { |
| 176 | i = y*maxx + x; |
| 177 | *ncp++ = ((int)cp[i] + (int)cp[i-maxx]) >> 1; |
| 178 | } |
| 179 | } |
| 180 | } else { |
| 181 | memcpy(ncp, cp, maxx*4); /* Copy first line */ |
| 182 | ncp += maxx*4; |
| 183 | for (y=1; y<maxy; y++) { /* Interpolate other lines */ |
| 184 | for(x=0; x<maxx; x++) { |
| 185 | i = (y*maxx + x)*4 + 1; |
| 186 | *ncp++ = 0; /* Skip alfa comp */ |
| 187 | *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1; |
| 188 | i++; |
| 189 | *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1; |
| 190 | i++; |
| 191 | *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1; |
| 192 | } |
| 193 | } |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 194 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 195 | return rv; |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 198 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 199 | imageop_grey2mono(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 200 | PyObject *self; |
| 201 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 202 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 203 | int tres, x, y, len; |
| 204 | unsigned char *cp, *ncp; |
| 205 | unsigned char ovalue; |
| 206 | PyObject *rv; |
| 207 | int i, bit; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 208 | |
| 209 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 210 | if ( !PyArg_Parse(args, "(s#iii)", &cp, &len, &x, &y, &tres) ) |
| 211 | return 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 212 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 213 | if ( x*y != len ) { |
| 214 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 215 | return 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 216 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 217 | |
| 218 | rv = PyString_FromStringAndSize(NULL, (len+7)/8); |
| 219 | if ( rv == 0 ) |
| 220 | return 0; |
| 221 | ncp = (unsigned char *)PyString_AsString(rv); |
| 222 | |
| 223 | bit = 0x80; |
| 224 | ovalue = 0; |
| 225 | for ( i=0; i < len; i++ ) { |
| 226 | if ( (int)cp[i] > tres ) |
| 227 | ovalue |= bit; |
| 228 | bit >>= 1; |
| 229 | if ( bit == 0 ) { |
| 230 | *ncp++ = ovalue; |
| 231 | bit = 0x80; |
| 232 | ovalue = 0; |
| 233 | } |
| 234 | } |
| 235 | if ( bit != 0x80 ) |
| 236 | *ncp++ = ovalue; |
| 237 | return rv; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 240 | static PyObject * |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 241 | imageop_grey2grey4(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 242 | PyObject *self; |
| 243 | PyObject *args; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 244 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 245 | int x, y, len; |
| 246 | unsigned char *cp, *ncp; |
| 247 | unsigned char ovalue; |
| 248 | PyObject *rv; |
| 249 | int i; |
| 250 | int pos; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 251 | |
| 252 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 253 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) ) |
| 254 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 255 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 256 | if ( x*y != len ) { |
| 257 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 258 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 259 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 260 | |
| 261 | rv = PyString_FromStringAndSize(NULL, (len+1)/2); |
| 262 | if ( rv == 0 ) |
| 263 | return 0; |
| 264 | ncp = (unsigned char *)PyString_AsString(rv); |
| 265 | pos = 0; |
| 266 | ovalue = 0; |
| 267 | for ( i=0; i < len; i++ ) { |
| 268 | ovalue |= ((int)cp[i] & 0xf0) >> pos; |
| 269 | pos += 4; |
| 270 | if ( pos == 8 ) { |
| 271 | *ncp++ = ovalue; |
| 272 | ovalue = 0; |
| 273 | pos = 0; |
| 274 | } |
| 275 | } |
| 276 | if ( pos != 0 ) |
| 277 | *ncp++ = ovalue; |
| 278 | return rv; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 281 | static PyObject * |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 282 | imageop_grey2grey2(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 283 | PyObject *self; |
| 284 | PyObject *args; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 285 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 286 | int x, y, len; |
| 287 | unsigned char *cp, *ncp; |
| 288 | unsigned char ovalue; |
| 289 | PyObject *rv; |
| 290 | int i; |
| 291 | int pos; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 292 | |
| 293 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 294 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) ) |
| 295 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 296 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 297 | if ( x*y != len ) { |
| 298 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 299 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 300 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 301 | |
| 302 | rv = PyString_FromStringAndSize(NULL, (len+3)/4); |
| 303 | if ( rv == 0 ) |
| 304 | return 0; |
| 305 | ncp = (unsigned char *)PyString_AsString(rv); |
| 306 | pos = 0; |
| 307 | ovalue = 0; |
| 308 | for ( i=0; i < len; i++ ) { |
| 309 | ovalue |= ((int)cp[i] & 0xc0) >> pos; |
| 310 | pos += 2; |
| 311 | if ( pos == 8 ) { |
| 312 | *ncp++ = ovalue; |
| 313 | ovalue = 0; |
| 314 | pos = 0; |
| 315 | } |
| 316 | } |
| 317 | if ( pos != 0 ) |
| 318 | *ncp++ = ovalue; |
| 319 | return rv; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 322 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 323 | imageop_dither2mono(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 324 | PyObject *self; |
| 325 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 326 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 327 | int sum, x, y, len; |
| 328 | unsigned char *cp, *ncp; |
| 329 | unsigned char ovalue; |
| 330 | PyObject *rv; |
| 331 | int i, bit; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 332 | |
| 333 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 334 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) ) |
| 335 | return 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 336 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 337 | if ( x*y != len ) { |
| 338 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 339 | return 0; |
| 340 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 341 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 342 | rv = PyString_FromStringAndSize(NULL, (len+7)/8); |
| 343 | if ( rv == 0 ) |
| 344 | return 0; |
| 345 | ncp = (unsigned char *)PyString_AsString(rv); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 346 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 347 | bit = 0x80; |
| 348 | ovalue = 0; |
| 349 | sum = 0; |
| 350 | for ( i=0; i < len; i++ ) { |
| 351 | sum += cp[i]; |
| 352 | if ( sum >= 256 ) { |
| 353 | sum -= 256; |
| 354 | ovalue |= bit; |
| 355 | } |
| 356 | bit >>= 1; |
| 357 | if ( bit == 0 ) { |
| 358 | *ncp++ = ovalue; |
| 359 | bit = 0x80; |
| 360 | ovalue = 0; |
| 361 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 362 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 363 | if ( bit != 0x80 ) |
| 364 | *ncp++ = ovalue; |
| 365 | return rv; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 368 | static PyObject * |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 369 | imageop_dither2grey2(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 370 | PyObject *self; |
| 371 | PyObject *args; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 372 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 373 | int x, y, len; |
| 374 | unsigned char *cp, *ncp; |
| 375 | unsigned char ovalue; |
| 376 | PyObject *rv; |
| 377 | int i; |
| 378 | int pos; |
| 379 | int sum = 0, nvalue; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 380 | |
| 381 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 382 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) ) |
| 383 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 384 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 385 | if ( x*y != len ) { |
| 386 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 387 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 388 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 389 | |
| 390 | rv = PyString_FromStringAndSize(NULL, (len+3)/4); |
| 391 | if ( rv == 0 ) |
| 392 | return 0; |
| 393 | ncp = (unsigned char *)PyString_AsString(rv); |
| 394 | pos = 1; |
| 395 | ovalue = 0; |
| 396 | for ( i=0; i < len; i++ ) { |
| 397 | sum += cp[i]; |
| 398 | nvalue = sum & 0x180; |
| 399 | sum -= nvalue; |
| 400 | ovalue |= nvalue >> pos; |
| 401 | pos += 2; |
| 402 | if ( pos == 9 ) { |
| 403 | *ncp++ = ovalue; |
| 404 | ovalue = 0; |
| 405 | pos = 1; |
| 406 | } |
| 407 | } |
| 408 | if ( pos != 0 ) |
| 409 | *ncp++ = ovalue; |
| 410 | return rv; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 413 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 414 | imageop_mono2grey(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 415 | PyObject *self; |
| 416 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 417 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 418 | int v0, v1, x, y, len, nlen; |
| 419 | unsigned char *cp, *ncp; |
| 420 | PyObject *rv; |
| 421 | int i, bit; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 422 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 423 | if ( !PyArg_Parse(args, "(s#iiii)", &cp, &len, &x, &y, &v0, &v1) ) |
| 424 | return 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 425 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 426 | nlen = x*y; |
| 427 | if ( (nlen+7)/8 != len ) { |
| 428 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 429 | return 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 430 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 431 | |
| 432 | rv = PyString_FromStringAndSize(NULL, nlen); |
| 433 | if ( rv == 0 ) |
| 434 | return 0; |
| 435 | ncp = (unsigned char *)PyString_AsString(rv); |
| 436 | |
| 437 | bit = 0x80; |
| 438 | for ( i=0; i < nlen; i++ ) { |
| 439 | if ( *cp & bit ) |
| 440 | *ncp++ = v1; |
| 441 | else |
| 442 | *ncp++ = v0; |
| 443 | bit >>= 1; |
| 444 | if ( bit == 0 ) { |
| 445 | bit = 0x80; |
| 446 | cp++; |
| 447 | } |
| 448 | } |
| 449 | return rv; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 452 | static PyObject * |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 453 | imageop_grey22grey(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 454 | PyObject *self; |
| 455 | PyObject *args; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 456 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 457 | int x, y, len, nlen; |
| 458 | unsigned char *cp, *ncp; |
| 459 | PyObject *rv; |
| 460 | int i, pos, value = 0, nvalue; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 461 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 462 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) ) |
| 463 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 464 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 465 | nlen = x*y; |
| 466 | if ( (nlen+3)/4 != len ) { |
| 467 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 468 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 469 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 470 | |
| 471 | rv = PyString_FromStringAndSize(NULL, nlen); |
| 472 | if ( rv == 0 ) |
| 473 | return 0; |
| 474 | ncp = (unsigned char *)PyString_AsString(rv); |
| 475 | |
| 476 | pos = 0; |
| 477 | for ( i=0; i < nlen; i++ ) { |
| 478 | if ( pos == 0 ) { |
| 479 | value = *cp++; |
| 480 | pos = 8; |
| 481 | } |
| 482 | pos -= 2; |
| 483 | nvalue = (value >> pos) & 0x03; |
| 484 | *ncp++ = nvalue | (nvalue << 2) | |
| 485 | (nvalue << 4) | (nvalue << 6); |
| 486 | } |
| 487 | return rv; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 490 | static PyObject * |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 491 | imageop_grey42grey(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 492 | PyObject *self; |
| 493 | PyObject *args; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 494 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 495 | int x, y, len, nlen; |
| 496 | unsigned char *cp, *ncp; |
| 497 | PyObject *rv; |
| 498 | int i, pos, value = 0, nvalue; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 499 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 500 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) ) |
| 501 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 502 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 503 | nlen = x*y; |
| 504 | if ( (nlen+1)/2 != len ) { |
| 505 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 506 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 507 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 508 | |
| 509 | rv = PyString_FromStringAndSize(NULL, nlen); |
| 510 | if ( rv == 0 ) |
| 511 | return 0; |
| 512 | ncp = (unsigned char *)PyString_AsString(rv); |
| 513 | |
| 514 | pos = 0; |
| 515 | for ( i=0; i < nlen; i++ ) { |
| 516 | if ( pos == 0 ) { |
| 517 | value = *cp++; |
| 518 | pos = 8; |
| 519 | } |
| 520 | pos -= 4; |
| 521 | nvalue = (value >> pos) & 0x0f; |
| 522 | *ncp++ = nvalue | (nvalue << 4); |
| 523 | } |
| 524 | return rv; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 527 | static PyObject * |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 528 | imageop_rgb2rgb8(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 529 | PyObject *self; |
| 530 | PyObject *args; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 531 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 532 | int x, y, len, nlen; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 533 | Py_UInt32 *cp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 534 | unsigned char *ncp; |
| 535 | PyObject *rv; |
| 536 | int i, r, g, b; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 537 | Py_UInt32 value, nvalue; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 538 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 539 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) ) |
| 540 | return 0; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 541 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 542 | nlen = x*y; |
| 543 | if ( nlen*4 != len ) { |
| 544 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 545 | return 0; |
| 546 | } |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 547 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 548 | rv = PyString_FromStringAndSize(NULL, nlen); |
| 549 | if ( rv == 0 ) |
| 550 | return 0; |
| 551 | ncp = (unsigned char *)PyString_AsString(rv); |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 552 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 553 | for ( i=0; i < nlen; i++ ) { |
| 554 | /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */ |
| 555 | value = *cp++; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 556 | #if 0 |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 557 | r = (value >> 5) & 7; |
| 558 | g = (value >> 13) & 7; |
| 559 | b = (value >> 22) & 3; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 560 | #else |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 561 | r = (int) ((value & 0xff) / 255. * 7. + .5); |
| 562 | g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5); |
| 563 | b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 564 | #endif |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 565 | nvalue = (r<<5) | (b<<3) | g; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 566 | *ncp++ = (unsigned char)nvalue; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 567 | } |
| 568 | return rv; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 571 | static PyObject * |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 572 | imageop_rgb82rgb(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 573 | PyObject *self; |
| 574 | PyObject *args; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 575 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 576 | int x, y, len, nlen; |
| 577 | unsigned char *cp; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 578 | Py_UInt32 *ncp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 579 | PyObject *rv; |
| 580 | int i, r, g, b; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 581 | Py_UInt32 value, nvalue; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 582 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 583 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) ) |
| 584 | return 0; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 585 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 586 | nlen = x*y; |
| 587 | if ( nlen != len ) { |
| 588 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 589 | return 0; |
| 590 | } |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 591 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 592 | rv = PyString_FromStringAndSize(NULL, nlen*4); |
| 593 | if ( rv == 0 ) |
| 594 | return 0; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 595 | ncp = (Py_UInt32 *)PyString_AsString(rv); |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 596 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 597 | for ( i=0; i < nlen; i++ ) { |
| 598 | /* Bits in source: RRRBBGGG |
| 599 | ** Red and Green are multiplied by 36.5, Blue by 85 |
| 600 | */ |
| 601 | value = *cp++; |
| 602 | r = (value >> 5) & 7; |
| 603 | g = (value ) & 7; |
| 604 | b = (value >> 3) & 3; |
| 605 | r = (r<<5) | (r<<3) | (r>>1); |
| 606 | g = (g<<5) | (g<<3) | (g>>1); |
| 607 | b = (b<<6) | (b<<4) | (b<<2) | b; |
| 608 | nvalue = r | (g<<8) | (b<<16); |
| 609 | *ncp++ = nvalue; |
| 610 | } |
| 611 | return rv; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 614 | static PyObject * |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 615 | imageop_rgb2grey(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 616 | PyObject *self; |
| 617 | PyObject *args; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 618 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 619 | int x, y, len, nlen; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 620 | Py_UInt32 *cp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 621 | unsigned char *ncp; |
| 622 | PyObject *rv; |
| 623 | int i, r, g, b; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 624 | Py_UInt32 value, nvalue; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 625 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 626 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) ) |
| 627 | return 0; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 628 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 629 | nlen = x*y; |
| 630 | if ( nlen*4 != len ) { |
| 631 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 632 | return 0; |
| 633 | } |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 634 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 635 | rv = PyString_FromStringAndSize(NULL, nlen); |
| 636 | if ( rv == 0 ) |
| 637 | return 0; |
| 638 | ncp = (unsigned char *)PyString_AsString(rv); |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 639 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 640 | for ( i=0; i < nlen; i++ ) { |
| 641 | value = *cp++; |
| 642 | r = (value ) & 0xff; |
| 643 | g = (value >> 8) & 0xff; |
| 644 | b = (value >> 16) & 0xff; |
| 645 | nvalue = (int)(0.30*r + 0.59*g + 0.11*b); |
| 646 | if ( nvalue > 255 ) nvalue = 255; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 647 | *ncp++ = (unsigned char)nvalue; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 648 | } |
| 649 | return rv; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 652 | static PyObject * |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 653 | imageop_grey2rgb(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 654 | PyObject *self; |
| 655 | PyObject *args; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 656 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 657 | int x, y, len, nlen; |
| 658 | unsigned char *cp; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 659 | Py_UInt32 *ncp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 660 | PyObject *rv; |
| 661 | int i; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 662 | Py_UInt32 value; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 663 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 664 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &x, &y) ) |
| 665 | return 0; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 666 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 667 | nlen = x*y; |
| 668 | if ( nlen != len ) { |
| 669 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 670 | return 0; |
| 671 | } |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 672 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 673 | rv = PyString_FromStringAndSize(NULL, nlen*4); |
| 674 | if ( rv == 0 ) |
| 675 | return 0; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 676 | ncp = (Py_UInt32 *)PyString_AsString(rv); |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 677 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 678 | for ( i=0; i < nlen; i++ ) { |
| 679 | value = *cp++; |
| 680 | *ncp++ = value | (value << 8 ) | (value << 16); |
| 681 | } |
| 682 | return rv; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 685 | /* |
| 686 | static object * |
| 687 | imageop_mul(self, args) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 688 | object *self; |
| 689 | object *args; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 690 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 691 | char *cp, *ncp; |
| 692 | int len, size, x, y; |
| 693 | object *rv; |
| 694 | int i; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 695 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 696 | if ( !getargs(args, "(s#iii)", &cp, &len, &size, &x, &y) ) |
| 697 | return 0; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 698 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 699 | if ( size != 1 && size != 4 ) { |
| 700 | err_setstr(ImageopError, "Size should be 1 or 4"); |
| 701 | return 0; |
| 702 | } |
| 703 | if ( len != size*x*y ) { |
| 704 | err_setstr(ImageopError, "String has incorrect length"); |
| 705 | return 0; |
| 706 | } |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 707 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 708 | rv = newsizedstringobject(NULL, XXXX); |
| 709 | if ( rv == 0 ) |
| 710 | return 0; |
| 711 | ncp = (char *)getstringvalue(rv); |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 712 | |
| 713 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 714 | for ( i=0; i < len; i += size ) { |
| 715 | } |
| 716 | return rv; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 717 | } |
| 718 | */ |
| 719 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 720 | static PyMethodDef imageop_methods[] = { |
| 721 | { "crop", imageop_crop }, |
| 722 | { "scale", imageop_scale }, |
| 723 | { "grey2mono", imageop_grey2mono }, |
| 724 | { "grey2grey2", imageop_grey2grey2 }, |
| 725 | { "grey2grey4", imageop_grey2grey4 }, |
| 726 | { "dither2mono", imageop_dither2mono }, |
| 727 | { "dither2grey2", imageop_dither2grey2 }, |
| 728 | { "mono2grey", imageop_mono2grey }, |
| 729 | { "grey22grey", imageop_grey22grey }, |
| 730 | { "grey42grey", imageop_grey42grey }, |
| 731 | { "tovideo", imageop_tovideo }, |
| 732 | { "rgb2rgb8", imageop_rgb2rgb8 }, |
| 733 | { "rgb82rgb", imageop_rgb82rgb }, |
| 734 | { "rgb2grey", imageop_rgb2grey }, |
| 735 | { "grey2rgb", imageop_grey2rgb }, |
| 736 | { 0, 0 } |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 737 | }; |
| 738 | |
| 739 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 740 | DL_EXPORT(void) |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 741 | initimageop() |
| 742 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 743 | PyObject *m, *d; |
| 744 | m = Py_InitModule("imageop", imageop_methods); |
| 745 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 746 | ImageopError = PyErr_NewException("imageop.error", NULL, NULL); |
| 747 | if (ImageopError != NULL) |
| 748 | PyDict_SetItemString(d, "error", ImageopError); |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 749 | } |