2
* The Python Imaging Library
4
* a simple math add-on for the Python Imaging Library
7
* 1999-02-15 fl Created
8
* 2005-05-05 fl Simplified and cleaned up for PIL 1.1.6
10
* Copyright (c) 1999-2005 by Secret Labs AB
11
* Copyright (c) 2005 by Fredrik Lundh
13
* See the README file for information on usage and redistribution.
23
#define MAX_INT32 2147483647.0
24
#define MIN_INT32 -2147483648.0
26
#if defined(_MSC_VER) && _MSC_VER < 1500
27
/* python 2.1/2.2/2.3 = VC98 = VER 1200 */
28
/* python 2.4/2.5 = VS.NET 2003 = VER 1310 */
29
/* python 2.6 = VS 9.0 = VER 1500 */
30
#define powf(a, b) ((float) pow((double) (a), (double) (b)))
33
#define UNOP(name, op, type)\
34
void name(Imaging out, Imaging im1)\
37
for (y = 0; y < out->ysize; y++) {\
38
type* p0 = (type*) out->image[y];\
39
type* p1 = (type*) im1->image[y];\
40
for (x = 0; x < out->xsize; x++) {\
47
#define BINOP(name, op, type)\
48
void name(Imaging out, Imaging im1, Imaging im2)\
51
for (y = 0; y < out->ysize; y++) {\
52
type* p0 = (type*) out->image[y];\
53
type* p1 = (type*) im1->image[y];\
54
type* p2 = (type*) im2->image[y];\
55
for (x = 0; x < out->xsize; x++) {\
56
*p0 = op(type, *p1, *p2);\
62
#define NEG(type, v1) -(v1)
63
#define INVERT(type, v1) ~(v1)
65
#define ADD(type, v1, v2) (v1)+(v2)
66
#define SUB(type, v1, v2) (v1)-(v2)
67
#define MUL(type, v1, v2) (v1)*(v2)
69
#define MIN(type, v1, v2) ((v1)<(v2))?(v1):(v2)
70
#define MAX(type, v1, v2) ((v1)>(v2))?(v1):(v2)
72
#define AND(type, v1, v2) (v1)&(v2)
73
#define OR(type, v1, v2) (v1)|(v2)
74
#define XOR(type, v1, v2) (v1)^(v2)
75
#define LSHIFT(type, v1, v2) (v1)<<(v2)
76
#define RSHIFT(type, v1, v2) (v1)>>(v2)
78
#define ABS_I(type, v1) abs((v1))
79
#define ABS_F(type, v1) fabs((v1))
81
/* --------------------------------------------------------------------
82
* some day, we should add FPE protection mechanisms. see pyfpe.h for
85
* PyFPE_START_PROTECT("Error in foobar", return 0)
86
* PyFPE_END_PROTECT(result)
89
#define DIV_I(type, v1, v2) ((v2)!=0)?(v1)/(v2):0
90
#define DIV_F(type, v1, v2) ((v2)!=0.0F)?(v1)/(v2):0.0F
92
#define MOD_I(type, v1, v2) ((v2)!=0)?(v1)%(v2):0
93
#define MOD_F(type, v1, v2) ((v2)!=0.0F)?fmod((v1),(v2)):0.0F
95
static int powi(int x, int y)
97
double v = pow(x, y) + 0.5;
102
else if (v > MAX_INT32)
107
#define POW_I(type, v1, v2) powi(v1, v2)
108
#define POW_F(type, v1, v2) powf(v1, v2) /* FIXME: EDOM handling */
110
#define DIFF_I(type, v1, v2) abs((v1)-(v2))
111
#define DIFF_F(type, v1, v2) fabs((v1)-(v2))
113
#define EQ(type, v1, v2) (v1)==(v2)
114
#define NE(type, v1, v2) (v1)!=(v2)
115
#define LT(type, v1, v2) (v1)<(v2)
116
#define LE(type, v1, v2) (v1)<=(v2)
117
#define GT(type, v1, v2) (v1)>(v2)
118
#define GE(type, v1, v2) (v1)>=(v2)
120
UNOP(abs_I, ABS_I, INT32)
121
UNOP(neg_I, NEG, INT32)
123
BINOP(add_I, ADD, INT32)
124
BINOP(sub_I, SUB, INT32)
125
BINOP(mul_I, MUL, INT32)
126
BINOP(div_I, DIV_I, INT32)
127
BINOP(mod_I, MOD_I, INT32)
128
BINOP(pow_I, POW_I, INT32)
129
BINOP(diff_I, DIFF_I, INT32)
131
UNOP(invert_I, INVERT, INT32)
132
BINOP(and_I, AND, INT32)
133
BINOP(or_I, OR, INT32)
134
BINOP(xor_I, XOR, INT32)
135
BINOP(lshift_I, LSHIFT, INT32)
136
BINOP(rshift_I, RSHIFT, INT32)
138
BINOP(min_I, MIN, INT32)
139
BINOP(max_I, MAX, INT32)
141
BINOP(eq_I, EQ, INT32)
142
BINOP(ne_I, NE, INT32)
143
BINOP(lt_I, LT, INT32)
144
BINOP(le_I, LE, INT32)
145
BINOP(gt_I, GT, INT32)
146
BINOP(ge_I, GE, INT32)
148
UNOP(abs_F, ABS_F, FLOAT32)
149
UNOP(neg_F, NEG, FLOAT32)
151
BINOP(add_F, ADD, FLOAT32)
152
BINOP(sub_F, SUB, FLOAT32)
153
BINOP(mul_F, MUL, FLOAT32)
154
BINOP(div_F, DIV_F, FLOAT32)
155
BINOP(mod_F, MOD_F, FLOAT32)
156
BINOP(pow_F, POW_F, FLOAT32)
157
BINOP(diff_F, DIFF_F, FLOAT32)
159
BINOP(min_F, MIN, FLOAT32)
160
BINOP(max_F, MAX, FLOAT32)
162
BINOP(eq_F, EQ, FLOAT32)
163
BINOP(ne_F, NE, FLOAT32)
164
BINOP(lt_F, LT, FLOAT32)
165
BINOP(le_F, LE, FLOAT32)
166
BINOP(gt_F, GT, FLOAT32)
167
BINOP(ge_F, GE, FLOAT32)
170
_unop(PyObject* self, PyObject* args)
174
void (*unop)(Imaging, Imaging);
177
if (!PyArg_ParseTuple(args, "lll", &op, &i0, &i1))
192
_binop(PyObject* self, PyObject* args)
197
void (*binop)(Imaging, Imaging, Imaging);
200
if (!PyArg_ParseTuple(args, "llll", &op, &i0, &i1, &i2))
209
binop(out, im1, im2);
215
static PyMethodDef _functions[] = {
217
{"binop", _binop, 1},
222
install(PyObject *d, char* name, void* value)
224
PyObject *v = PyInt_FromLong((long) value);
225
if (!v || PyDict_SetItemString(d, name, v))
231
init_imagingmath(void)
236
m = Py_InitModule("_imagingmath", _functions);
237
d = PyModule_GetDict(m);
239
install(d, "abs_I", abs_I);
240
install(d, "neg_I", neg_I);
241
install(d, "add_I", add_I);
242
install(d, "sub_I", sub_I);
243
install(d, "diff_I", diff_I);
244
install(d, "mul_I", mul_I);
245
install(d, "div_I", div_I);
246
install(d, "mod_I", mod_I);
247
install(d, "min_I", min_I);
248
install(d, "max_I", max_I);
249
install(d, "pow_I", pow_I);
251
install(d, "invert_I", invert_I);
252
install(d, "and_I", and_I);
253
install(d, "or_I", or_I);
254
install(d, "xor_I", xor_I);
255
install(d, "lshift_I", lshift_I);
256
install(d, "rshift_I", rshift_I);
258
install(d, "eq_I", eq_I);
259
install(d, "ne_I", ne_I);
260
install(d, "lt_I", lt_I);
261
install(d, "le_I", le_I);
262
install(d, "gt_I", gt_I);
263
install(d, "ge_I", ge_I);
265
install(d, "abs_F", abs_F);
266
install(d, "neg_F", neg_F);
267
install(d, "add_F", add_F);
268
install(d, "sub_F", sub_F);
269
install(d, "diff_F", diff_F);
270
install(d, "mul_F", mul_F);
271
install(d, "div_F", div_F);
272
install(d, "mod_F", mod_F);
273
install(d, "min_F", min_F);
274
install(d, "max_F", max_F);
275
install(d, "pow_F", pow_F);
277
install(d, "eq_F", eq_F);
278
install(d, "ne_F", ne_F);
279
install(d, "lt_F", lt_F);
280
install(d, "le_F", le_F);
281
install(d, "gt_F", gt_F);
282
install(d, "ge_F", ge_F);