~ubuntu-branches/ubuntu/wily/python-imaging/wily

« back to all changes in this revision

Viewing changes to .pc/git-updates.diff/_imagingmath.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (1.1.4)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130131204920-7tnuhqhlsqdza4c2
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Python Imaging Library
 
3
 *
 
4
 * a simple math add-on for the Python Imaging Library
 
5
 *
 
6
 * history:
 
7
 * 1999-02-15 fl   Created
 
8
 * 2005-05-05 fl   Simplified and cleaned up for PIL 1.1.6
 
9
 *
 
10
 * Copyright (c) 1999-2005 by Secret Labs AB
 
11
 * Copyright (c) 2005 by Fredrik Lundh
 
12
 *
 
13
 * See the README file for information on usage and redistribution.
 
14
 */
 
15
 
 
16
#include "Python.h"
 
17
 
 
18
#include "Imaging.h"
 
19
 
 
20
#include "math.h"
 
21
#include "float.h"
 
22
 
 
23
#define MAX_INT32 2147483647.0
 
24
#define MIN_INT32 -2147483648.0
 
25
 
 
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)))
 
31
#endif
 
32
 
 
33
#define UNOP(name, op, type)\
 
34
void name(Imaging out, Imaging im1)\
 
35
{\
 
36
    int x, y;\
 
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++) {\
 
41
            *p0 = op(type, *p1);\
 
42
            p0++; p1++;\
 
43
        }\
 
44
    }\
 
45
}
 
46
 
 
47
#define BINOP(name, op, type)\
 
48
void name(Imaging out, Imaging im1, Imaging im2)\
 
49
{\
 
50
    int x, y;\
 
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);\
 
57
            p0++; p1++; p2++;\
 
58
        }\
 
59
    }\
 
60
}
 
61
 
 
62
#define NEG(type, v1) -(v1)
 
63
#define INVERT(type, v1) ~(v1)
 
64
 
 
65
#define ADD(type, v1, v2) (v1)+(v2)
 
66
#define SUB(type, v1, v2) (v1)-(v2)
 
67
#define MUL(type, v1, v2) (v1)*(v2)
 
68
 
 
69
#define MIN(type, v1, v2) ((v1)<(v2))?(v1):(v2)
 
70
#define MAX(type, v1, v2) ((v1)>(v2))?(v1):(v2)
 
71
 
 
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)
 
77
 
 
78
#define ABS_I(type, v1) abs((v1))
 
79
#define ABS_F(type, v1) fabs((v1))
 
80
 
 
81
/* --------------------------------------------------------------------
 
82
 * some day, we should add FPE protection mechanisms.  see pyfpe.h for
 
83
 * details.
 
84
 *   
 
85
 * PyFPE_START_PROTECT("Error in foobar", return 0)
 
86
 * PyFPE_END_PROTECT(result)
 
87
 */
 
88
 
 
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
 
91
 
 
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
 
94
 
 
95
static int powi(int x, int y)
 
96
{
 
97
    double v = pow(x, y) + 0.5;
 
98
    if (errno == EDOM)
 
99
        return 0;
 
100
    if (v < MIN_INT32)
 
101
        v = MIN_INT32;
 
102
    else if (v > MAX_INT32)
 
103
        v = MAX_INT32;
 
104
    return (int) v;
 
105
}
 
106
 
 
107
#define POW_I(type, v1, v2) powi(v1, v2)
 
108
#define POW_F(type, v1, v2) powf(v1, v2) /* FIXME: EDOM handling */
 
109
 
 
110
#define DIFF_I(type, v1, v2) abs((v1)-(v2))
 
111
#define DIFF_F(type, v1, v2) fabs((v1)-(v2))
 
112
 
 
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)
 
119
 
 
120
UNOP(abs_I, ABS_I, INT32)
 
121
UNOP(neg_I, NEG, INT32)
 
122
 
 
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)
 
130
 
 
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)
 
137
 
 
138
BINOP(min_I, MIN, INT32)
 
139
BINOP(max_I, MAX, INT32)
 
140
 
 
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)
 
147
 
 
148
UNOP(abs_F, ABS_F, FLOAT32)
 
149
UNOP(neg_F, NEG, FLOAT32)
 
150
 
 
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)
 
158
 
 
159
BINOP(min_F, MIN, FLOAT32)
 
160
BINOP(max_F, MAX, FLOAT32)
 
161
 
 
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)
 
168
 
 
169
static PyObject *
 
170
_unop(PyObject* self, PyObject* args)
 
171
{
 
172
    Imaging out;
 
173
    Imaging im1;
 
174
    void (*unop)(Imaging, Imaging);
 
175
 
 
176
    long op, i0, i1;
 
177
    if (!PyArg_ParseTuple(args, "lll", &op, &i0, &i1))
 
178
        return NULL;
 
179
 
 
180
    out = (Imaging) i0;
 
181
    im1 = (Imaging) i1;
 
182
 
 
183
    unop = (void*) op;
 
184
    
 
185
    unop(out, im1);
 
186
 
 
187
    Py_INCREF(Py_None);
 
188
    return Py_None;
 
189
}
 
190
 
 
191
static PyObject *
 
192
_binop(PyObject* self, PyObject* args)
 
193
{
 
194
    Imaging out;
 
195
    Imaging im1;
 
196
    Imaging im2;
 
197
    void (*binop)(Imaging, Imaging, Imaging);
 
198
 
 
199
    long op, i0, i1, i2;
 
200
    if (!PyArg_ParseTuple(args, "llll", &op, &i0, &i1, &i2))
 
201
        return NULL;
 
202
 
 
203
    out = (Imaging) i0;
 
204
    im1 = (Imaging) i1;
 
205
    im2 = (Imaging) i2;
 
206
 
 
207
    binop = (void*) op;
 
208
    
 
209
    binop(out, im1, im2);
 
210
 
 
211
    Py_INCREF(Py_None);
 
212
    return Py_None;
 
213
}
 
214
 
 
215
static PyMethodDef _functions[] = {
 
216
    {"unop", _unop, 1},
 
217
    {"binop", _binop, 1},
 
218
    {NULL, NULL}
 
219
};
 
220
 
 
221
static void
 
222
install(PyObject *d, char* name, void* value)
 
223
{
 
224
    PyObject *v = PyInt_FromLong((long) value);
 
225
    if (!v || PyDict_SetItemString(d, name, v))
 
226
        PyErr_Clear();
 
227
    Py_XDECREF(v);
 
228
}
 
229
 
 
230
DL_EXPORT(void)
 
231
init_imagingmath(void)
 
232
{
 
233
    PyObject* m;
 
234
    PyObject* d;
 
235
 
 
236
    m = Py_InitModule("_imagingmath", _functions);
 
237
    d = PyModule_GetDict(m);
 
238
 
 
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);
 
250
 
 
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);
 
257
 
 
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);
 
264
 
 
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);
 
276
 
 
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);
 
283
 
 
284
}