-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathPoint.c
More file actions
313 lines (286 loc) · 9.72 KB
/
Copy pathPoint.c
File metadata and controls
313 lines (286 loc) · 9.72 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* 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;
// Note: all point handlers must fill the entirety of `imOut`,
// as it is allocated dirty by `ImagingPoint`.
/**
* Contract: imIn is read-only and imOut must be a distinct image.
*/
static void
im_point_8_8(Imaging imOut, Imaging imIn, im_point_context *context) {
/* 8-bit source, 8-bit destination */
UINT8 *table = (UINT8 *)context->table;
// Invariant over the loop.
int xsize = imIn->xsize, ysize = imIn->ysize;
for (int y = 0; y < ysize; y++) {
// restrict safe: imIn is read-only, imOut is a fresh allocation.
UINT8 *restrict in = imIn->image8[y];
UINT8 *restrict out = imOut->image8[y];
for (int x = 0; x < xsize; x++) {
out[x] = table[in[x]];
}
}
}
/**
* Contract: imIn is read-only and imOut must be a distinct image.
*/
static void
im_point_2x8_2x8(Imaging imOut, Imaging imIn, im_point_context *context) {
/* 2x8-bit source, 2x8-bit destination */
UINT8 *table = (UINT8 *)context->table;
// Invariant over the loop.
int xsize = imIn->xsize, ysize = imIn->ysize;
for (int y = 0; y < ysize; y++) {
// restrict safe: imIn is read-only, imOut is a fresh allocation.
UINT8 *restrict in = (UINT8 *)imIn->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (int x = 0; x < xsize; x++) {
out[0] = table[in[0]];
out[1] = 0;
out[2] = 0;
out[3] = table[in[3] + 256];
in += 4;
out += 4;
}
}
}
/**
* Contract: imIn is read-only and imOut must be a distinct image.
*/
static void
im_point_3x8_3x8(Imaging imOut, Imaging imIn, im_point_context *context) {
/* 3x8-bit source, 3x8-bit destination */
UINT8 *table = (UINT8 *)context->table;
// Invariant over the loop.
int xsize = imIn->xsize, ysize = imIn->ysize;
for (int y = 0; y < ysize; y++) {
// restrict safe: imIn is read-only, imOut is a fresh allocation.
UINT8 *restrict in = (UINT8 *)imIn->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (int x = 0; x < xsize; x++) {
out[0] = table[in[0]];
out[1] = table[in[1] + 256];
out[2] = table[in[2] + 512];
out[3] = 0;
in += 4;
out += 4;
}
}
}
/**
* Contract: imIn is read-only and imOut must be a distinct image.
*/
static void
im_point_4x8_4x8(Imaging imOut, Imaging imIn, im_point_context *context) {
/* 4x8-bit source, 4x8-bit destination */
UINT8 *table = (UINT8 *)context->table;
// Invariant over the loop.
int xsize = imIn->xsize, ysize = imIn->ysize;
for (int y = 0; y < ysize; y++) {
// restrict safe: imIn is read-only, imOut is a fresh allocation.
UINT8 *restrict in = (UINT8 *)imIn->image[y];
UINT8 *restrict out = (UINT8 *)imOut->image[y];
for (int x = 0; x < 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;
}
}
}
/**
* Contract: imIn is read-only and imOut must be a distinct image.
*/
static void
im_point_8_32(Imaging imOut, Imaging imIn, im_point_context *context) {
/* 8-bit source, 32-bit destination */
char *table = (char *)context->table;
// Invariant over the loop.
int xsize = imIn->xsize, ysize = imIn->ysize;
for (int y = 0; y < ysize; y++) {
// restrict safe: imIn is read-only, imOut is a fresh allocation.
UINT8 *restrict in = imIn->image8[y];
INT32 *restrict out = imOut->image32[y];
for (int x = 0; x < xsize; x++) {
memcpy(out + x, table + in[x] * sizeof(INT32), sizeof(INT32));
}
}
}
/**
* Contract: imIn is read-only and imOut must be a distinct image.
*/
static void
im_point_32_8(Imaging imOut, Imaging imIn, im_point_context *context) {
/* 32-bit source, 8-bit destination */
UINT8 *table = (UINT8 *)context->table;
// Invariant over the loop.
int xsize = imIn->xsize, ysize = imIn->ysize;
for (int y = 0; y < ysize; y++) {
// restrict safe: imIn is read-only, imOut is a fresh allocation.
INT32 *restrict in = imIn->image32[y];
UINT8 *restrict out = imOut->image8[y];
for (int x = 0; x < 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, ModeID mode, const void *table) {
/* lookup table transform */
ImagingSectionCookie cookie;
Imaging imOut;
im_point_context context;
if (!imIn) {
return (Imaging)ImagingError_ModeError();
}
if (mode == IMAGING_MODE_UNKNOWN) {
mode = imIn->mode;
}
if (imIn->type != IMAGING_TYPE_UINT8) {
if (imIn->type != IMAGING_TYPE_INT32 || mode != IMAGING_MODE_L) {
goto mode_mismatch;
}
} else if (!imIn->image8 && imIn->mode != mode) {
goto mode_mismatch;
}
imOut = ImagingNewDirty(mode, imIn->xsize, imIn->ysize);
if (!imOut) {
return NULL;
}
ImagingCopyPalette(imOut, imIn);
ImagingSectionEnter(&cookie);
context.table = table;
if (imIn->type == IMAGING_TYPE_UINT8) {
if (imIn->bands == imOut->bands && imIn->type == imOut->type) {
switch (imIn->bands) {
case 1:
im_point_8_8(imOut, imIn, &context);
break;
case 2:
im_point_2x8_2x8(imOut, imIn, &context);
break;
case 3:
im_point_3x8_3x8(imOut, imIn, &context);
break;
case 4:
im_point_4x8_4x8(imOut, imIn, &context);
break;
default:
/* this cannot really happen */
im_point_8_8(imOut, imIn, &context);
break;
}
} else {
im_point_8_32(imOut, imIn, &context);
}
} else {
im_point_32_8(imOut, imIn, &context);
}
ImagingSectionLeave(&cookie);
return imOut;
mode_mismatch:
return (Imaging)ImagingError_ValueError(
"point operation not supported for this mode"
);
}
/**
* Apply an affine (scale/offset) transform to every pixel of imIn,
* returning a newly allocated result.
*
* Contract: imIn is read-only.
*/
Imaging
ImagingPointTransform(Imaging imIn, double scale, double offset) {
/* scale/offset transform */
ImagingSectionCookie cookie;
Imaging imOut;
if (!imIn || (imIn->mode != IMAGING_MODE_I && imIn->mode != IMAGING_MODE_I_16 &&
imIn->mode != IMAGING_MODE_F)) {
return (Imaging)ImagingError_ModeError();
}
// Invariant over the loops.
int xsize = imIn->xsize, ysize = imIn->ysize;
imOut = ImagingNewDirty(imIn->mode, xsize, ysize);
if (!imOut) {
return NULL;
}
switch (imIn->type) {
case IMAGING_TYPE_INT32:
ImagingSectionEnter(&cookie);
for (int y = 0; y < ysize; y++) {
// restrict safe: imIn is read-only, imOut is a fresh allocation.
INT32 *restrict in = imIn->image32[y];
INT32 *restrict out = imOut->image32[y];
/* FIXME: add clipping? */
for (int x = 0; x < xsize; x++) {
out[x] = in[x] * scale + offset;
}
}
ImagingSectionLeave(&cookie);
break;
case IMAGING_TYPE_FLOAT32:
ImagingSectionEnter(&cookie);
for (int y = 0; y < ysize; y++) {
// restrict safe: imIn is read-only, imOut is a fresh allocation.
FLOAT32 *restrict in = (FLOAT32 *)imIn->image32[y];
FLOAT32 *restrict out = (FLOAT32 *)imOut->image32[y];
for (int x = 0; x < xsize; x++) {
out[x] = in[x] * scale + offset;
}
}
ImagingSectionLeave(&cookie);
break;
case IMAGING_TYPE_SPECIAL:
if (imIn->mode == IMAGING_MODE_I_16) {
ImagingSectionEnter(&cookie);
for (int y = 0; y < ysize; y++) {
// restrict safe: imIn is read-only, imOut is a fresh allocation.
char *restrict in = imIn->image[y];
char *restrict out = imOut->image[y];
/* FIXME: add clipping? */
for (int x = 0; x < 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;
}