Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 1 | |
| 2 | /* imageopmodule - Various operations on pictures */ |
| 3 | |
| 4 | #ifdef sun |
| 5 | #define signed |
| 6 | #endif |
| 7 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 8 | #include "Python.h" |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 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 |
| 21 | |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 22 | #define CHARP(cp, xmax, x, y) ((char *)(cp+y*xmax+x)) |
Jack Jansen | 76c79e9 | 1996-01-22 14:55:15 +0000 | [diff] [blame] | 23 | #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] | 24 | #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] | 25 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 26 | static PyObject *ImageopError; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 27 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 28 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 29 | imageop_crop(PyObject *self, PyObject *args) |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 30 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 31 | char *cp, *ncp; |
| 32 | short *nsp; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 33 | Py_Int32 *nlp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 34 | int len, size, x, y, newx1, newx2, newy1, newy2; |
| 35 | int ix, iy, xstep, ystep; |
| 36 | PyObject *rv; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 37 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 38 | if ( !PyArg_ParseTuple(args, "s#iiiiiii", &cp, &len, &size, &x, &y, |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 39 | &newx1, &newy1, &newx2, &newy2) ) |
| 40 | return 0; |
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 | if ( size != 1 && size != 2 && size != 4 ) { |
| 43 | PyErr_SetString(ImageopError, "Size should be 1, 2 or 4"); |
| 44 | return 0; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 45 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 46 | 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 Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 59 | nlp = (Py_Int32 *)ncp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 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); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | return rv; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 82 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 83 | imageop_scale(PyObject *self, PyObject *args) |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 84 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 85 | char *cp, *ncp; |
| 86 | short *nsp; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 87 | Py_Int32 *nlp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 88 | int len, size, x, y, newx, newy; |
| 89 | int ix, iy; |
| 90 | int oix, oiy; |
| 91 | PyObject *rv; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 92 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 93 | if ( !PyArg_ParseTuple(args, "s#iiiii", |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 94 | &cp, &len, &size, &x, &y, &newx, &newy) ) |
| 95 | return 0; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 96 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 97 | if ( size != 1 && size != 2 && size != 4 ) { |
| 98 | PyErr_SetString(ImageopError, "Size should be 1, 2 or 4"); |
| 99 | return 0; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 100 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 101 | 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 Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 111 | nlp = (Py_Int32 *)ncp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 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); |
| 122 | } |
| 123 | } |
| 124 | return rv; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 127 | /* Note: this routine can use a bit of optimizing */ |
| 128 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 129 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 130 | imageop_tovideo(PyObject *self, PyObject *args) |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 131 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 132 | int maxx, maxy, x, y, len; |
| 133 | int i; |
| 134 | unsigned char *cp, *ncp; |
| 135 | int width; |
| 136 | PyObject *rv; |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 137 | |
| 138 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 139 | if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &width, &maxx, &maxy) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 140 | return 0; |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 141 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 142 | 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 Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 150 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 151 | rv = PyString_FromStringAndSize(NULL, len); |
| 152 | if ( rv == 0 ) |
| 153 | return 0; |
| 154 | ncp = (unsigned char *)PyString_AsString(rv); |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 155 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 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; |
| 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 Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 179 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 180 | return rv; |
Jack Jansen | d26b458 | 1993-01-22 15:34:43 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 183 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 184 | imageop_grey2mono(PyObject *self, PyObject *args) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 185 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 186 | int tres, x, y, len; |
| 187 | unsigned char *cp, *ncp; |
| 188 | unsigned char ovalue; |
| 189 | PyObject *rv; |
| 190 | int i, bit; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 191 | |
| 192 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 193 | if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &x, &y, &tres) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 194 | return 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 195 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 196 | if ( x*y != len ) { |
| 197 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 198 | return 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 199 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 200 | |
| 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 Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 223 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 224 | imageop_grey2grey4(PyObject *self, PyObject *args) |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 225 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 226 | int x, y, len; |
| 227 | unsigned char *cp, *ncp; |
| 228 | unsigned char ovalue; |
| 229 | PyObject *rv; |
| 230 | int i; |
| 231 | int pos; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 232 | |
| 233 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 234 | if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 235 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 236 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 237 | if ( x*y != len ) { |
| 238 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 239 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 240 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 241 | |
| 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 Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 262 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 263 | imageop_grey2grey2(PyObject *self, PyObject *args) |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 264 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 265 | int x, y, len; |
| 266 | unsigned char *cp, *ncp; |
| 267 | unsigned char ovalue; |
| 268 | PyObject *rv; |
| 269 | int i; |
| 270 | int pos; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 271 | |
| 272 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 273 | if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 274 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 275 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 276 | if ( x*y != len ) { |
| 277 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 278 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 279 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 280 | |
| 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 Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 301 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 302 | imageop_dither2mono(PyObject *self, PyObject *args) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 303 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 304 | int sum, x, y, len; |
| 305 | unsigned char *cp, *ncp; |
| 306 | unsigned char ovalue; |
| 307 | PyObject *rv; |
| 308 | int i, bit; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 309 | |
| 310 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 311 | if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 312 | return 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 313 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 314 | if ( x*y != len ) { |
| 315 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 316 | return 0; |
| 317 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 318 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 319 | rv = PyString_FromStringAndSize(NULL, (len+7)/8); |
| 320 | if ( rv == 0 ) |
| 321 | return 0; |
| 322 | ncp = (unsigned char *)PyString_AsString(rv); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 323 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 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; |
| 332 | } |
| 333 | bit >>= 1; |
| 334 | if ( bit == 0 ) { |
| 335 | *ncp++ = ovalue; |
| 336 | bit = 0x80; |
| 337 | ovalue = 0; |
| 338 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 339 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 340 | if ( bit != 0x80 ) |
| 341 | *ncp++ = ovalue; |
| 342 | return rv; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 345 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 346 | imageop_dither2grey2(PyObject *self, PyObject *args) |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 347 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 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; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 355 | |
| 356 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 357 | if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 358 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 359 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 360 | if ( x*y != len ) { |
| 361 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 362 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 363 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 364 | |
| 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 Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 388 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 389 | imageop_mono2grey(PyObject *self, PyObject *args) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 390 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 391 | int v0, v1, x, y, len, nlen; |
| 392 | unsigned char *cp, *ncp; |
| 393 | PyObject *rv; |
| 394 | int i, bit; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 395 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 396 | if ( !PyArg_ParseTuple(args, "s#iiii", &cp, &len, &x, &y, &v0, &v1) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 397 | return 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 398 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 399 | nlen = x*y; |
| 400 | if ( (nlen+7)/8 != len ) { |
| 401 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 402 | return 0; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 403 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 404 | |
| 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 Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 425 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 426 | imageop_grey22grey(PyObject *self, PyObject *args) |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 427 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 428 | int x, y, len, nlen; |
| 429 | unsigned char *cp, *ncp; |
| 430 | PyObject *rv; |
| 431 | int i, pos, value = 0, nvalue; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 432 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 433 | if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 434 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 435 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 436 | nlen = x*y; |
| 437 | if ( (nlen+3)/4 != len ) { |
| 438 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 439 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 440 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 441 | |
| 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 Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 461 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 462 | imageop_grey42grey(PyObject *self, PyObject *args) |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 463 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 464 | int x, y, len, nlen; |
| 465 | unsigned char *cp, *ncp; |
| 466 | PyObject *rv; |
| 467 | int i, pos, value = 0, nvalue; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 468 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 469 | if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 470 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 471 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 472 | nlen = x*y; |
| 473 | if ( (nlen+1)/2 != len ) { |
| 474 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 475 | return 0; |
Jack Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 476 | } |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 477 | |
| 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 Jansen | de3adf9 | 1992-12-22 14:05:55 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 496 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 497 | imageop_rgb2rgb8(PyObject *self, PyObject *args) |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 498 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 499 | int x, y, len, nlen; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 500 | Py_UInt32 *cp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 501 | unsigned char *ncp; |
| 502 | PyObject *rv; |
| 503 | int i, r, g, b; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 504 | Py_UInt32 value, nvalue; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 505 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 506 | if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 507 | return 0; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 508 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 509 | nlen = x*y; |
| 510 | if ( nlen*4 != len ) { |
| 511 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 512 | return 0; |
| 513 | } |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 514 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 515 | rv = PyString_FromStringAndSize(NULL, nlen); |
| 516 | if ( rv == 0 ) |
| 517 | return 0; |
| 518 | ncp = (unsigned char *)PyString_AsString(rv); |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 519 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 520 | for ( i=0; i < nlen; i++ ) { |
| 521 | /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */ |
| 522 | value = *cp++; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 523 | #if 0 |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 524 | r = (value >> 5) & 7; |
| 525 | g = (value >> 13) & 7; |
| 526 | b = (value >> 22) & 3; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 527 | #else |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 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); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 531 | #endif |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 532 | nvalue = (r<<5) | (b<<3) | g; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 533 | *ncp++ = (unsigned char)nvalue; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 534 | } |
| 535 | return rv; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 538 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 539 | imageop_rgb82rgb(PyObject *self, PyObject *args) |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 540 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 541 | int x, y, len, nlen; |
| 542 | unsigned char *cp; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 543 | Py_UInt32 *ncp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 544 | PyObject *rv; |
| 545 | int i, r, g, b; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 546 | Py_UInt32 value, nvalue; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 547 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 548 | if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 549 | return 0; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 550 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 551 | nlen = x*y; |
| 552 | if ( nlen != len ) { |
| 553 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 554 | return 0; |
| 555 | } |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 556 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 557 | rv = PyString_FromStringAndSize(NULL, nlen*4); |
| 558 | if ( rv == 0 ) |
| 559 | return 0; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 560 | ncp = (Py_UInt32 *)PyString_AsString(rv); |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 561 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 562 | 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 Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 579 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 580 | imageop_rgb2grey(PyObject *self, PyObject *args) |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 581 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 582 | int x, y, len, nlen; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 583 | Py_UInt32 *cp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 584 | unsigned char *ncp; |
| 585 | PyObject *rv; |
| 586 | int i, r, g, b; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 587 | Py_UInt32 value, nvalue; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 588 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 589 | if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 590 | return 0; |
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 | nlen = x*y; |
| 593 | if ( nlen*4 != len ) { |
| 594 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 595 | return 0; |
| 596 | } |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 597 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 598 | rv = PyString_FromStringAndSize(NULL, nlen); |
| 599 | if ( rv == 0 ) |
| 600 | return 0; |
| 601 | ncp = (unsigned char *)PyString_AsString(rv); |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 602 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 610 | *ncp++ = (unsigned char)nvalue; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 611 | } |
| 612 | return rv; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 615 | static PyObject * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 616 | imageop_grey2rgb(PyObject *self, PyObject *args) |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 617 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 618 | int x, y, len, nlen; |
| 619 | unsigned char *cp; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 620 | Py_UInt32 *ncp; |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 621 | PyObject *rv; |
| 622 | int i; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 623 | Py_UInt32 value; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 624 | |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 625 | if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 626 | return 0; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 627 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 628 | nlen = x*y; |
| 629 | if ( nlen != len ) { |
| 630 | PyErr_SetString(ImageopError, "String has incorrect length"); |
| 631 | return 0; |
| 632 | } |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 633 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 634 | rv = PyString_FromStringAndSize(NULL, nlen*4); |
| 635 | if ( rv == 0 ) |
| 636 | return 0; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 637 | ncp = (Py_UInt32 *)PyString_AsString(rv); |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 638 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 639 | for ( i=0; i < nlen; i++ ) { |
| 640 | value = *cp++; |
| 641 | *ncp++ = value | (value << 8 ) | (value << 16); |
| 642 | } |
| 643 | return rv; |
Jack Jansen | 4fada9c | 1993-02-19 15:51:41 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 646 | /* |
| 647 | static object * |
Peter Schneider-Kamp | fe74263 | 2000-07-10 09:55:32 +0000 | [diff] [blame] | 648 | imageop_mul(object *self, object *args) |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 649 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 650 | char *cp, *ncp; |
| 651 | int len, size, x, y; |
| 652 | object *rv; |
| 653 | int i; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 654 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 655 | if ( !getargs(args, "(s#iii)", &cp, &len, &size, &x, &y) ) |
| 656 | return 0; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 657 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 658 | 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 Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 666 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 667 | rv = newsizedstringobject(NULL, XXXX); |
| 668 | if ( rv == 0 ) |
| 669 | return 0; |
| 670 | ncp = (char *)getstringvalue(rv); |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 671 | |
| 672 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 673 | for ( i=0; i < len; i += size ) { |
| 674 | } |
| 675 | return rv; |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 676 | } |
| 677 | */ |
| 678 | |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 679 | static PyMethodDef imageop_methods[] = { |
Neal Norwitz | b0aaec5 | 2002-04-02 18:26:33 +0000 | [diff] [blame] | 680 | { "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. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 695 | { 0, 0 } |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 699 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 700 | initimageop(void) |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 701 | { |
Roger E. Masse | a141f8a | 1996-12-20 20:50:39 +0000 | [diff] [blame] | 702 | PyObject *m, *d; |
| 703 | m = Py_InitModule("imageop", imageop_methods); |
| 704 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 705 | ImageopError = PyErr_NewException("imageop.error", NULL, NULL); |
| 706 | if (ImageopError != NULL) |
| 707 | PyDict_SetItemString(d, "error", ImageopError); |
Guido van Rossum | 0317a47 | 1992-10-26 13:40:15 +0000 | [diff] [blame] | 708 | } |