aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py3/libImaging/Point.c
blob: 8883578cbffe9a78ff123b84f0d261ef748cbc29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * The Python Imaging Library
 * $Id$
 *
 * point (pixel) translation
 *
 * history:
 * 1995-11-27 fl   Created
 * 1996-03-31 fl   Fixed colour support
 * 1996-08-13 fl   Support 8-bit to "1" thresholding
 * 1997-05-31 fl   Added floating point transform
 * 1998-07-02 fl   Added integer point transform
 * 1998-07-17 fl   Support L to anything lookup
 * 2004-12-18 fl   Refactored; added I to L lookup
 *
 * Copyright (c) 1997-2004 by Secret Labs AB.
 * Copyright (c) 1995-2004 by Fredrik Lundh.
 *
 * See the README file for information on usage and redistribution.
 */

#include "Imaging.h"

typedef struct {
    const void *table;
} im_point_context;

static void
im_point_8_8(Imaging imOut, Imaging imIn, im_point_context *context) {
    int x, y;
    /* 8-bit source, 8-bit destination */
    UINT8 *table = (UINT8 *)context->table;
    for (y = 0; y < imIn->ysize; y++) {
        UINT8 *in = imIn->image8[y];
        UINT8 *out = imOut->image8[y];
        for (x = 0; x < imIn->xsize; x++) {
            out[x] = table[in[x]];
        }
    }
}

static void
im_point_2x8_2x8(Imaging imOut, Imaging imIn, im_point_context *context) {
    int x, y;
    /* 2x8-bit source, 2x8-bit destination */
    UINT8 *table = (UINT8 *)context->table;
    for (y = 0; y < imIn->ysize; y++) {
        UINT8 *in = (UINT8 *)imIn->image[y];
        UINT8 *out = (UINT8 *)imOut->image[y];
        for (x = 0; x < imIn->xsize; x++) {
            out[0] = table[in[0]];
            out[3] = table[in[3] + 256];
            in += 4;
            out += 4;
        }
    }
}

static void
im_point_3x8_3x8(Imaging imOut, Imaging imIn, im_point_context *context) {
    int x, y;
    /* 3x8-bit source, 3x8-bit destination */
    UINT8 *table = (UINT8 *)context->table;
    for (y = 0; y < imIn->ysize; y++) {
        UINT8 *in = (UINT8 *)imIn->image[y];
        UINT8 *out = (UINT8 *)imOut->image[y];
        for (x = 0; x < imIn->xsize; x++) {
            out[0] = table[in[0]];
            out[1] = table[in[1] + 256];
            out[2] = table[in[2] + 512];
            in += 4;
            out += 4;
        }
    }
}

static void
im_point_4x8_4x8(Imaging imOut, Imaging imIn, im_point_context *context) {
    int x, y;
    /* 4x8-bit source, 4x8-bit destination */
    UINT8 *table = (UINT8 *)context->table;
    for (y = 0; y < imIn->ysize; y++) {
        UINT8 *in = (UINT8 *)imIn->image[y];
        UINT8 *out = (UINT8 *)imOut->image[y];
        for (x = 0; x < imIn->xsize; x++) {
            out[0] = table[in[0]];
            out[1] = table[in[1] + 256];
            out[2] = table[in[2] + 512];
            out[3] = table[in[3] + 768];
            in += 4;
            out += 4;
        }
    }
}

static void
im_point_8_32(Imaging imOut, Imaging imIn, im_point_context *context) {
    int x, y;
    /* 8-bit source, 32-bit destination */
    char *table = (char *)context->table;
    for (y = 0; y < imIn->ysize; y++) {
        UINT8 *in = imIn->image8[y];
        INT32 *out = imOut->image32[y];
        for (x = 0; x < imIn->xsize; x++) {
            memcpy(out + x, table + in[x] * sizeof(INT32), sizeof(INT32));
        }
    }
}

static void
im_point_32_8(Imaging imOut, Imaging imIn, im_point_context *context) {
    int x, y;
    /* 32-bit source, 8-bit destination */
    UINT8 *table = (UINT8 *)context->table;
    for (y = 0; y < imIn->ysize; y++) {
        INT32 *in = imIn->image32[y];
        UINT8 *out = imOut->image8[y];
        for (x = 0; x < imIn->xsize; x++) {
            int v = in[x];
            if (v < 0) {
                v = 0;
            } else if (v > 65535) {
                v = 65535;
            }
            out[x] = table[v];
        }
    }
}

Imaging
ImagingPoint(Imaging imIn, const char *mode, const void *table) {
    /* lookup table transform */

    ImagingSectionCookie cookie;
    Imaging imOut;
    im_point_context context;
    void (*point)(Imaging imIn, Imaging imOut, im_point_context * context);

    if (!imIn) {
        return (Imaging)ImagingError_ModeError();
    }

    if (!mode) {
        mode = imIn->mode;
    }

    if (imIn->type != IMAGING_TYPE_UINT8) {
        if (imIn->type != IMAGING_TYPE_INT32 || strcmp(mode, "L") != 0) {
            goto mode_mismatch;
        }
    } else if (!imIn->image8 && strcmp(imIn->mode, mode) != 0) {
        goto mode_mismatch;
    }

    imOut = ImagingNew(mode, imIn->xsize, imIn->ysize);
    if (!imOut) {
        return NULL;
    }

    /* find appropriate handler */
    if (imIn->type == IMAGING_TYPE_UINT8) {
        if (imIn->bands == imOut->bands && imIn->type == imOut->type) {
            switch (imIn->bands) {
                case 1:
                    point = im_point_8_8;
                    break;
                case 2:
                    point = im_point_2x8_2x8;
                    break;
                case 3:
                    point = im_point_3x8_3x8;
                    break;
                case 4:
                    point = im_point_4x8_4x8;
                    break;
                default:
                    /* this cannot really happen */
                    point = im_point_8_8;
                    break;
            }
        } else {
            point = im_point_8_32;
        }
    } else {
        point = im_point_32_8;
    }

    ImagingCopyPalette(imOut, imIn);

    ImagingSectionEnter(&cookie);

    context.table = table;
    point(imOut, imIn, &context);

    ImagingSectionLeave(&cookie);

    return imOut;

mode_mismatch:
    return (Imaging)ImagingError_ValueError(
        "point operation not supported for this mode");
}

Imaging
ImagingPointTransform(Imaging imIn, double scale, double offset) {
    /* scale/offset transform */

    ImagingSectionCookie cookie;
    Imaging imOut;
    int x, y;

    if (!imIn || (strcmp(imIn->mode, "I") != 0 && strcmp(imIn->mode, "I;16") != 0 &&
                  strcmp(imIn->mode, "F") != 0)) {
        return (Imaging)ImagingError_ModeError();
    }

    imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize);
    if (!imOut) {
        return NULL;
    }

    switch (imIn->type) {
        case IMAGING_TYPE_INT32:
            ImagingSectionEnter(&cookie);
            for (y = 0; y < imIn->ysize; y++) {
                INT32 *in = imIn->image32[y];
                INT32 *out = imOut->image32[y];
                /* FIXME: add clipping? */
                for (x = 0; x < imIn->xsize; x++) {
                    out[x] = in[x] * scale + offset;
                }
            }
            ImagingSectionLeave(&cookie);
            break;
        case IMAGING_TYPE_FLOAT32:
            ImagingSectionEnter(&cookie);
            for (y = 0; y < imIn->ysize; y++) {
                FLOAT32 *in = (FLOAT32 *)imIn->image32[y];
                FLOAT32 *out = (FLOAT32 *)imOut->image32[y];
                for (x = 0; x < imIn->xsize; x++) {
                    out[x] = in[x] * scale + offset;
                }
            }
            ImagingSectionLeave(&cookie);
            break;
        case IMAGING_TYPE_SPECIAL:
            if (strcmp(imIn->mode, "I;16") == 0) {
                ImagingSectionEnter(&cookie);
                for (y = 0; y < imIn->ysize; y++) {
                    char *in = (char *)imIn->image[y];
                    char *out = (char *)imOut->image[y];
                    /* FIXME: add clipping? */
                    for (x = 0; x < imIn->xsize; x++) {
                        UINT16 v;
                        memcpy(&v, in + x * sizeof(v), sizeof(v));
                        v = v * scale + offset;
                        memcpy(out + x * sizeof(UINT16), &v, sizeof(v));
                    }
                }
                ImagingSectionLeave(&cookie);
                break;
            }
            /* FALL THROUGH */
        default:
            ImagingDelete(imOut);
            return (Imaging)ImagingError_ValueError("internal error");
    }

    return imOut;
}