/src/libvips/libvips/histogram/maplut.c
Line | Count | Source |
1 | | /* map though a LUT |
2 | | * |
3 | | * Modified: |
4 | | * 18/6/93 JC |
5 | | * - oops! im_incheck() added for LUT image |
6 | | * - some ANSIfication |
7 | | * 15/7/93 JC |
8 | | * - adapted for partial v2 |
9 | | * - ANSIfied |
10 | | * - now does complex LUTs too |
11 | | * 10/3/94 JC |
12 | | * - more helpful error messages, slight reformatting |
13 | | * 24/8/94 JC |
14 | | * - now allows non-uchar image input |
15 | | * 7/10/94 JC |
16 | | * - uses im_malloc(), IM_NEW() etc. |
17 | | * 13/3/95 JC |
18 | | * - now takes a private copy of LUT, so user can im_close() LUT image |
19 | | * after im_maplut() without fear of coredumps |
20 | | * 23/6/95 JC |
21 | | * - lut may now have many bands if image has just one band |
22 | | * 3/3/01 JC |
23 | | * - small speed ups |
24 | | * 30/6/04 |
25 | | * - heh, 1 band image + 3 band lut + >8bit output has been broken for 9 |
26 | | * years :-) |
27 | | * 7/11/07 |
28 | | * - new eval start/end system |
29 | | * 25/3/10 |
30 | | * - gtkdoc |
31 | | * - small cleanups |
32 | | * 5/7/13 |
33 | | * - convert to a class |
34 | | * 2/10/13 |
35 | | * - add --band arg, replacing im_tone_map() |
36 | | */ |
37 | | |
38 | | /* |
39 | | |
40 | | This file is part of VIPS. |
41 | | |
42 | | VIPS is free software; you can redistribute it and/or modify |
43 | | it under the terms of the GNU Lesser General Public License as published by |
44 | | the Free Software Foundation; either version 2 of the License, or |
45 | | (at your option) any later version. |
46 | | |
47 | | This program is distributed in the hope that it will be useful, |
48 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
49 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
50 | | GNU Lesser General Public License for more details. |
51 | | |
52 | | You should have received a copy of the GNU Lesser General Public License |
53 | | along with this program; if not, write to the Free Software |
54 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
55 | | 02110-1301 USA |
56 | | |
57 | | */ |
58 | | |
59 | | /* |
60 | | |
61 | | These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk |
62 | | |
63 | | */ |
64 | | |
65 | | #ifdef HAVE_CONFIG_H |
66 | | #include <config.h> |
67 | | #endif /*HAVE_CONFIG_H*/ |
68 | | #include <glib/gi18n-lib.h> |
69 | | |
70 | | #include <stdio.h> |
71 | | #include <stdlib.h> |
72 | | #include <string.h> |
73 | | |
74 | | #include <vips/vips.h> |
75 | | |
76 | | typedef struct _VipsMaplut { |
77 | | VipsOperation parent_instance; |
78 | | |
79 | | VipsImage *in; |
80 | | VipsImage *out; |
81 | | VipsImage *lut; |
82 | | int band; |
83 | | |
84 | | int fmt; /* LUT image BandFmt */ |
85 | | int nb; /* Number of bands in lut */ |
86 | | int es; /* VIPS_IMAGE_SIZEOF_ELEMENT() for lut image */ |
87 | | int sz; /* Number of elements in minor dimension */ |
88 | | int clp; /* Value we clip against */ |
89 | | VipsPel **table; /* Lut converted to 2d array */ |
90 | | int overflow; /* Number of overflows for non-uchar lut */ |
91 | | |
92 | | } VipsMaplut; |
93 | | |
94 | | typedef VipsOperationClass VipsMaplutClass; |
95 | | |
96 | 2 | G_DEFINE_TYPE(VipsMaplut, vips_maplut, VIPS_TYPE_OPERATION); |
97 | 2 | |
98 | 2 | static void |
99 | 2 | vips_maplut_preeval(VipsImage *image, VipsProgress *progress, |
100 | 2 | VipsMaplut *maplut) |
101 | 2 | { |
102 | 0 | maplut->overflow = 0; |
103 | 0 | } |
104 | | |
105 | | static void |
106 | | vips_maplut_posteval(VipsImage *image, VipsProgress *progress, |
107 | | VipsMaplut *maplut) |
108 | 0 | { |
109 | 0 | if (maplut->overflow) |
110 | 0 | g_warning("%d overflows detected", maplut->overflow); |
111 | 0 | } |
112 | | |
113 | | /* Our sequence value: the region this sequence is using, and local stats. |
114 | | */ |
115 | | typedef struct { |
116 | | VipsRegion *ir; /* Input region */ |
117 | | int overflow; /* Number of overflows */ |
118 | | } VipsMaplutSequence; |
119 | | |
120 | | /* Our start function. |
121 | | */ |
122 | | static void * |
123 | | vips_maplut_start(VipsImage *out, void *a, void *b) |
124 | 0 | { |
125 | 0 | VipsImage *in = (VipsImage *) a; |
126 | 0 | VipsMaplutSequence *seq; |
127 | |
|
128 | 0 | if (!(seq = VIPS_NEW(NULL, VipsMaplutSequence))) |
129 | 0 | return NULL; |
130 | | |
131 | | /* Init! |
132 | | */ |
133 | 0 | seq->ir = NULL; |
134 | 0 | seq->overflow = 0; |
135 | |
|
136 | 0 | if (!(seq->ir = vips_region_new(in))) |
137 | 0 | return NULL; |
138 | | |
139 | 0 | return seq; |
140 | 0 | } |
141 | | |
142 | | /* Map through n non-complex luts. |
143 | | */ |
144 | | #define loop(OUT) \ |
145 | 0 | { \ |
146 | 0 | int b = maplut->nb; \ |
147 | 0 | \ |
148 | 0 | for (y = to; y < bo; y++) { \ |
149 | 0 | for (z = 0; z < b; z++) { \ |
150 | 0 | VipsPel *p = VIPS_REGION_ADDR(ir, le, y); \ |
151 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y); \ |
152 | 0 | OUT *tlut = (OUT *) maplut->table[z]; \ |
153 | 0 | \ |
154 | 0 | for (x = z; x < ne; x += b) { \ |
155 | 0 | unsigned int index = p[x]; \ |
156 | 0 | \ |
157 | 0 | if (index > maplut->clp) { \ |
158 | 0 | index = maplut->clp; \ |
159 | 0 | seq->overflow++; \ |
160 | 0 | } \ |
161 | 0 | \ |
162 | 0 | q[x] = tlut[index]; \ |
163 | 0 | } \ |
164 | 0 | } \ |
165 | 0 | } \ |
166 | 0 | } |
167 | | |
168 | | /* Map through n complex luts. |
169 | | */ |
170 | | #define loopc(OUT) \ |
171 | 0 | { \ |
172 | 0 | int b = in->Bands; \ |
173 | 0 | \ |
174 | 0 | for (y = to; y < bo; y++) { \ |
175 | 0 | for (z = 0; z < b; z++) { \ |
176 | 0 | VipsPel *p = VIPS_REGION_ADDR(ir, le, y) + z; \ |
177 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y) + z * 2; \ |
178 | 0 | OUT *tlut = (OUT *) maplut->table[z]; \ |
179 | 0 | \ |
180 | 0 | for (x = 0; x < ne; x += b) { \ |
181 | 0 | unsigned int index = p[x]; \ |
182 | 0 | \ |
183 | 0 | if (index > maplut->clp) { \ |
184 | 0 | index = maplut->clp; \ |
185 | 0 | seq->overflow++; \ |
186 | 0 | } \ |
187 | 0 | \ |
188 | 0 | q[0] = tlut[index * 2]; \ |
189 | 0 | q[1] = tlut[index * 2 + 1]; \ |
190 | 0 | q += b * 2; \ |
191 | 0 | } \ |
192 | 0 | } \ |
193 | 0 | } \ |
194 | 0 | } |
195 | | |
196 | | #define loopg(IN, OUT) \ |
197 | 0 | { \ |
198 | 0 | int b = maplut->nb; \ |
199 | 0 | \ |
200 | 0 | for (y = to; y < bo; y++) { \ |
201 | 0 | for (z = 0; z < b; z++) { \ |
202 | 0 | IN *p = (IN *) VIPS_REGION_ADDR(ir, le, y); \ |
203 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y); \ |
204 | 0 | OUT *tlut = (OUT *) maplut->table[z]; \ |
205 | 0 | \ |
206 | 0 | for (x = z; x < ne; x += b) { \ |
207 | 0 | unsigned int index = p[x]; \ |
208 | 0 | \ |
209 | 0 | if (index > maplut->clp) { \ |
210 | 0 | index = maplut->clp; \ |
211 | 0 | seq->overflow++; \ |
212 | 0 | } \ |
213 | 0 | \ |
214 | 0 | q[x] = tlut[index]; \ |
215 | 0 | } \ |
216 | 0 | } \ |
217 | 0 | } \ |
218 | 0 | } |
219 | | |
220 | | #define loopcg(IN, OUT) \ |
221 | 0 | { \ |
222 | 0 | int b = in->Bands; \ |
223 | 0 | \ |
224 | 0 | for (y = to; y < bo; y++) { \ |
225 | 0 | for (z = 0; z < b; z++) { \ |
226 | 0 | IN *p = (IN *) VIPS_REGION_ADDR(ir, le, y) + z; \ |
227 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y) + z * 2; \ |
228 | 0 | OUT *tlut = (OUT *) maplut->table[z]; \ |
229 | 0 | \ |
230 | 0 | for (x = 0; x < ne; x += b) { \ |
231 | 0 | unsigned int index = p[x]; \ |
232 | 0 | \ |
233 | 0 | if (index > maplut->clp) { \ |
234 | 0 | index = maplut->clp; \ |
235 | 0 | seq->overflow++; \ |
236 | 0 | } \ |
237 | 0 | \ |
238 | 0 | q[0] = tlut[index * 2]; \ |
239 | 0 | q[1] = tlut[index * 2 + 1]; \ |
240 | 0 | \ |
241 | 0 | q += b * 2; \ |
242 | 0 | } \ |
243 | 0 | } \ |
244 | 0 | } \ |
245 | 0 | } |
246 | | |
247 | | /* Map image through one non-complex lut. |
248 | | */ |
249 | | #define loop1(OUT) \ |
250 | 0 | { \ |
251 | 0 | OUT *tlut = (OUT *) maplut->table[0]; \ |
252 | 0 | \ |
253 | 0 | for (y = to; y < bo; y++) { \ |
254 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y); \ |
255 | 0 | VipsPel *p = VIPS_REGION_ADDR(ir, le, y); \ |
256 | 0 | \ |
257 | 0 | for (x = 0; x < ne; x++) { \ |
258 | 0 | unsigned int index = p[x]; \ |
259 | 0 | \ |
260 | 0 | if (index > maplut->clp) { \ |
261 | 0 | index = maplut->clp; \ |
262 | 0 | seq->overflow++; \ |
263 | 0 | } \ |
264 | 0 | \ |
265 | 0 | q[x] = tlut[index]; \ |
266 | 0 | } \ |
267 | 0 | } \ |
268 | 0 | } |
269 | | |
270 | | /* Map image through one complex lut. |
271 | | */ |
272 | | #define loop1c(OUT) \ |
273 | 0 | { \ |
274 | 0 | OUT *tlut = (OUT *) maplut->table[0]; \ |
275 | 0 | \ |
276 | 0 | for (y = to; y < bo; y++) { \ |
277 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y); \ |
278 | 0 | VipsPel *p = VIPS_REGION_ADDR(ir, le, y); \ |
279 | 0 | \ |
280 | 0 | for (x = 0; x < ne; x++) { \ |
281 | 0 | unsigned int index = p[x]; \ |
282 | 0 | \ |
283 | 0 | if (index > maplut->clp) { \ |
284 | 0 | index = maplut->clp; \ |
285 | 0 | seq->overflow++; \ |
286 | 0 | } \ |
287 | 0 | \ |
288 | 0 | q[0] = tlut[index * 2]; \ |
289 | 0 | q[1] = tlut[index * 2 + 1]; \ |
290 | 0 | q += 2; \ |
291 | 0 | } \ |
292 | 0 | } \ |
293 | 0 | } |
294 | | |
295 | | /* As above, but the input image may be any unsigned integer type. We have to |
296 | | * index the lut carefully, and record the number of overflows we detect. |
297 | | */ |
298 | | #define loop1g(IN, OUT) \ |
299 | 0 | { \ |
300 | 0 | OUT *tlut = (OUT *) maplut->table[0]; \ |
301 | 0 | \ |
302 | 0 | for (y = to; y < bo; y++) { \ |
303 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y); \ |
304 | 0 | IN *p = (IN *) VIPS_REGION_ADDR(ir, le, y); \ |
305 | 0 | \ |
306 | 0 | for (x = 0; x < ne; x++) { \ |
307 | 0 | unsigned int index = p[x]; \ |
308 | 0 | \ |
309 | 0 | if (index > maplut->clp) { \ |
310 | 0 | index = maplut->clp; \ |
311 | 0 | seq->overflow++; \ |
312 | 0 | } \ |
313 | 0 | \ |
314 | 0 | q[x] = tlut[index]; \ |
315 | 0 | } \ |
316 | 0 | } \ |
317 | 0 | } |
318 | | |
319 | | #define loop1cg(IN, OUT) \ |
320 | 0 | { \ |
321 | 0 | OUT *tlut = (OUT *) maplut->table[0]; \ |
322 | 0 | \ |
323 | 0 | for (y = to; y < bo; y++) { \ |
324 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y); \ |
325 | 0 | IN *p = (IN *) VIPS_REGION_ADDR(ir, le, y); \ |
326 | 0 | \ |
327 | 0 | for (x = 0; x < ne; x++) { \ |
328 | 0 | unsigned int index = p[x]; \ |
329 | 0 | \ |
330 | 0 | if (index > maplut->clp) { \ |
331 | 0 | index = maplut->clp; \ |
332 | 0 | seq->overflow++; \ |
333 | 0 | } \ |
334 | 0 | \ |
335 | 0 | q[0] = tlut[index * 2]; \ |
336 | 0 | q[1] = tlut[index * 2 + 1]; \ |
337 | 0 | q += 2; \ |
338 | 0 | } \ |
339 | 0 | } \ |
340 | 0 | } |
341 | | |
342 | | /* Map 1-band image through a many-band non-complex lut. |
343 | | */ |
344 | | #define loop1m(OUT) \ |
345 | 0 | { \ |
346 | 0 | OUT **tlut = (OUT **) maplut->table; \ |
347 | 0 | \ |
348 | 0 | for (y = to; y < bo; y++) { \ |
349 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y); \ |
350 | 0 | VipsPel *p = VIPS_REGION_ADDR(ir, le, y); \ |
351 | 0 | \ |
352 | 0 | for (i = 0, x = 0; x < np; x++) { \ |
353 | 0 | unsigned int n = p[x]; \ |
354 | 0 | \ |
355 | 0 | if (n > maplut->clp) { \ |
356 | 0 | n = maplut->clp; \ |
357 | 0 | seq->overflow++; \ |
358 | 0 | } \ |
359 | 0 | \ |
360 | 0 | for (z = 0; z < maplut->nb; z++, i++) \ |
361 | 0 | q[i] = tlut[z][n]; \ |
362 | 0 | } \ |
363 | 0 | } \ |
364 | 0 | } |
365 | | |
366 | | /* Map 1-band image through many-band complex lut. |
367 | | */ |
368 | | #define loop1cm(OUT) \ |
369 | 0 | { \ |
370 | 0 | OUT **tlut = (OUT **) maplut->table; \ |
371 | 0 | \ |
372 | 0 | for (y = to; y < bo; y++) { \ |
373 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y); \ |
374 | 0 | VipsPel *p = VIPS_REGION_ADDR(ir, le, y); \ |
375 | 0 | \ |
376 | 0 | for (x = 0; x < np; x++) { \ |
377 | 0 | unsigned int n = p[x]; \ |
378 | 0 | \ |
379 | 0 | if (n > maplut->clp) { \ |
380 | 0 | n = maplut->clp; \ |
381 | 0 | seq->overflow++; \ |
382 | 0 | } \ |
383 | 0 | \ |
384 | 0 | for (z = 0; z < maplut->nb; z++) { \ |
385 | 0 | q[0] = tlut[z][n * 2]; \ |
386 | 0 | q[1] = tlut[z][n * 2 + 1]; \ |
387 | 0 | q += 2; \ |
388 | 0 | } \ |
389 | 0 | } \ |
390 | 0 | } \ |
391 | 0 | } |
392 | | |
393 | | /* Map 1-band uint or ushort image through a many-band non-complex LUT. |
394 | | */ |
395 | | #define loop1gm(IN, OUT) \ |
396 | 0 | { \ |
397 | 0 | OUT **tlut = (OUT **) maplut->table; \ |
398 | 0 | \ |
399 | 0 | for (y = to; y < bo; y++) { \ |
400 | 0 | IN *p = (IN *) VIPS_REGION_ADDR(ir, le, y); \ |
401 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y); \ |
402 | 0 | \ |
403 | 0 | for (i = 0, x = 0; x < np; x++) { \ |
404 | 0 | unsigned int n = p[x]; \ |
405 | 0 | \ |
406 | 0 | if (n > maplut->clp) { \ |
407 | 0 | n = maplut->clp; \ |
408 | 0 | seq->overflow++; \ |
409 | 0 | } \ |
410 | 0 | \ |
411 | 0 | for (z = 0; z < maplut->nb; z++, i++) \ |
412 | 0 | q[i] = tlut[z][n]; \ |
413 | 0 | } \ |
414 | 0 | } \ |
415 | 0 | } |
416 | | |
417 | | /* Map 1-band uint or ushort image through a many-band complex LUT. |
418 | | */ |
419 | | #define loop1cgm(IN, OUT) \ |
420 | 0 | { \ |
421 | 0 | OUT **tlut = (OUT **) maplut->table; \ |
422 | 0 | \ |
423 | 0 | for (y = to; y < bo; y++) { \ |
424 | 0 | IN *p = (IN *) VIPS_REGION_ADDR(ir, le, y); \ |
425 | 0 | OUT *q = (OUT *) VIPS_REGION_ADDR(out_region, le, y); \ |
426 | 0 | \ |
427 | 0 | for (x = 0; x < np; x++) { \ |
428 | 0 | unsigned int n = p[x]; \ |
429 | 0 | \ |
430 | 0 | if (n > maplut->clp) { \ |
431 | 0 | n = maplut->clp; \ |
432 | 0 | seq->overflow++; \ |
433 | 0 | } \ |
434 | 0 | \ |
435 | 0 | for (z = 0; z < maplut->nb; z++) { \ |
436 | 0 | q[0] = tlut[z][n * 2]; \ |
437 | 0 | q[1] = tlut[z][n * 2 + 1]; \ |
438 | 0 | q += 2; \ |
439 | 0 | } \ |
440 | 0 | } \ |
441 | 0 | } \ |
442 | 0 | } |
443 | | |
444 | | /* Switch for input types. Has to be uint type! |
445 | | */ |
446 | | #define inner_switch(UCHAR, GEN, OUT) \ |
447 | 0 | switch (ir->im->BandFmt) { \ |
448 | 0 | case VIPS_FORMAT_UCHAR: \ |
449 | 0 | UCHAR(OUT); \ |
450 | 0 | break; \ |
451 | 0 | case VIPS_FORMAT_USHORT: \ |
452 | 0 | GEN(unsigned short, OUT); \ |
453 | 0 | break; \ |
454 | 0 | case VIPS_FORMAT_UINT: \ |
455 | 0 | GEN(unsigned int, OUT); \ |
456 | 0 | break; \ |
457 | 0 | default: \ |
458 | 0 | g_assert_not_reached(); \ |
459 | 0 | } |
460 | | |
461 | | /* Switch for LUT types. One function for non-complex images, a |
462 | | * variant for complex ones. Another pair as well, in case the input is not |
463 | | * uchar. |
464 | | */ |
465 | | #define outer_switch(UCHAR_F, UCHAR_FC, GEN_F, GEN_FC) \ |
466 | 0 | switch (maplut->fmt) { \ |
467 | 0 | case VIPS_FORMAT_UCHAR: \ |
468 | 0 | inner_switch(UCHAR_F, GEN_F, unsigned char); \ |
469 | 0 | break; \ |
470 | 0 | case VIPS_FORMAT_CHAR: \ |
471 | 0 | inner_switch(UCHAR_F, GEN_F, char); \ |
472 | 0 | break; \ |
473 | 0 | case VIPS_FORMAT_USHORT: \ |
474 | 0 | inner_switch(UCHAR_F, GEN_F, unsigned short); \ |
475 | 0 | break; \ |
476 | 0 | case VIPS_FORMAT_SHORT: \ |
477 | 0 | inner_switch(UCHAR_F, GEN_F, short); \ |
478 | 0 | break; \ |
479 | 0 | case VIPS_FORMAT_UINT: \ |
480 | 0 | inner_switch(UCHAR_F, GEN_F, unsigned int); \ |
481 | 0 | break; \ |
482 | 0 | case VIPS_FORMAT_INT: \ |
483 | 0 | inner_switch(UCHAR_F, GEN_F, int); \ |
484 | 0 | break; \ |
485 | 0 | case VIPS_FORMAT_FLOAT: \ |
486 | 0 | inner_switch(UCHAR_F, GEN_F, float); \ |
487 | 0 | break; \ |
488 | 0 | case VIPS_FORMAT_DOUBLE: \ |
489 | 0 | inner_switch(UCHAR_F, GEN_F, double); \ |
490 | 0 | break; \ |
491 | 0 | case VIPS_FORMAT_COMPLEX: \ |
492 | 0 | inner_switch(UCHAR_FC, GEN_FC, float); \ |
493 | 0 | break; \ |
494 | 0 | case VIPS_FORMAT_DPCOMPLEX: \ |
495 | 0 | inner_switch(UCHAR_FC, GEN_FC, double); \ |
496 | 0 | break; \ |
497 | 0 | default: \ |
498 | 0 | g_assert_not_reached(); \ |
499 | 0 | } |
500 | | |
501 | | /* Do a map. |
502 | | */ |
503 | | static int |
504 | | vips_maplut_gen(VipsRegion *out_region, |
505 | | void *vseq, void *a, void *b, gboolean *stop) |
506 | 0 | { |
507 | 0 | VipsMaplutSequence *seq = (VipsMaplutSequence *) vseq; |
508 | 0 | VipsImage *in = (VipsImage *) a; |
509 | 0 | VipsMaplut *maplut = (VipsMaplut *) b; |
510 | 0 | VipsRegion *ir = seq->ir; |
511 | 0 | VipsRect *r = &out_region->valid; |
512 | 0 | int le = r->left; |
513 | 0 | int to = r->top; |
514 | 0 | int bo = VIPS_RECT_BOTTOM(r); |
515 | 0 | int np = r->width; /* Pels across region */ |
516 | 0 | int ne = VIPS_REGION_N_ELEMENTS(out_region); /* Number of elements */ |
517 | |
|
518 | 0 | int x, y, z, i; |
519 | |
|
520 | 0 | if (vips_region_prepare(ir, r)) |
521 | 0 | return -1; |
522 | | |
523 | | /* clang-format off */ |
524 | 0 | if (maplut->nb == 1) { |
525 | | /* One band lut. |
526 | | */ |
527 | 0 | outer_switch(loop1, loop1c, loop1g, loop1cg) |
528 | 0 | } |
529 | 0 | else |
530 | | /* Many band lut. |
531 | | */ |
532 | 0 | if (in->Bands == 1) |
533 | | /* ... but 1 band input. |
534 | | */ |
535 | 0 | outer_switch(loop1m, loop1cm, loop1gm, loop1cgm) |
536 | 0 | else |
537 | 0 | outer_switch(loop, loopc, loopg, loopcg) |
538 | | |
539 | 0 | return 0; |
540 | | /* clang-format on */ |
541 | 0 | } |
542 | | |
543 | | /* Destroy a sequence value. |
544 | | */ |
545 | | static int |
546 | | vips_maplut_stop(void *vseq, void *a, void *b) |
547 | 0 | { |
548 | 0 | VipsMaplutSequence *seq = (VipsMaplutSequence *) vseq; |
549 | 0 | VipsMaplut *maplut = (VipsMaplut *) b; |
550 | | |
551 | | /* Add to global stats. |
552 | | */ |
553 | 0 | maplut->overflow += seq->overflow; |
554 | |
|
555 | 0 | VIPS_UNREF(seq->ir); |
556 | 0 | VIPS_FREE(seq); |
557 | |
|
558 | 0 | return 0; |
559 | 0 | } |
560 | | |
561 | | /* Save a bit of typing. |
562 | | */ |
563 | | #define UC VIPS_FORMAT_UCHAR |
564 | | #define US VIPS_FORMAT_USHORT |
565 | | #define UI VIPS_FORMAT_UINT |
566 | | |
567 | | /* Type mapping: go to uchar / ushort / uint to make an index. |
568 | | */ |
569 | | static const VipsBandFormat bandfmt_maplut[10] = { |
570 | | /* Band format: UC C US S UI I F X D DX */ |
571 | | /* Promotion: */ UC, UC, US, US, UI, UI, UI, UI, UI, UI |
572 | | }; |
573 | | |
574 | | /* Repack lut into a set of band arrays. If we're just passing one band of the |
575 | | * image through the lut, put the identity function in the other bands. |
576 | | */ |
577 | | #define PACK_TABLE(TYPE) \ |
578 | 0 | { \ |
579 | 0 | TYPE *data = (TYPE *) lut->data; \ |
580 | 0 | int x, b; \ |
581 | 0 | \ |
582 | 0 | for (x = 0; x < maplut->sz; x++) \ |
583 | 0 | for (b = 0; b < maplut->nb; b++) { \ |
584 | 0 | TYPE *q = (TYPE *) maplut->table[b]; \ |
585 | 0 | \ |
586 | 0 | if (maplut->band >= 0 && \ |
587 | 0 | lut->Bands == 1) { \ |
588 | 0 | if (b == maplut->band) \ |
589 | 0 | q[x] = data[x]; \ |
590 | 0 | else \ |
591 | 0 | q[x] = x; \ |
592 | 0 | } \ |
593 | 0 | else \ |
594 | 0 | q[x] = data[x * lut->Bands + b]; \ |
595 | 0 | } \ |
596 | 0 | } |
597 | | |
598 | | #define PACK_TABLEC(TYPE) \ |
599 | 0 | { \ |
600 | 0 | TYPE *data = (TYPE *) lut->data; \ |
601 | 0 | int x, b; \ |
602 | 0 | \ |
603 | 0 | for (x = 0; x < maplut->sz; x++) \ |
604 | 0 | for (b = 0; b < maplut->nb; b++) { \ |
605 | 0 | TYPE *q = (TYPE *) maplut->table[b]; \ |
606 | 0 | \ |
607 | 0 | if (maplut->band >= 0 && \ |
608 | 0 | lut->Bands == 1) { \ |
609 | 0 | if (b == maplut->band) { \ |
610 | 0 | q[2 * x] = data[2 * x]; \ |
611 | 0 | q[2 * x + 1] = data[2 * x + 1]; \ |
612 | 0 | } \ |
613 | 0 | else { \ |
614 | 0 | q[2 * x] = x; \ |
615 | 0 | q[2 * x + 1] = 0; \ |
616 | 0 | } \ |
617 | 0 | } \ |
618 | 0 | else { \ |
619 | 0 | q[2 * x] = data[2 * (x * lut->Bands + b)]; \ |
620 | 0 | q[2 * x + 1] = \ |
621 | 0 | data[2 * (x * lut->Bands + b) + 1]; \ |
622 | 0 | } \ |
623 | 0 | } \ |
624 | 0 | } |
625 | | |
626 | | static int |
627 | | vips_maplut_build(VipsObject *object) |
628 | 0 | { |
629 | 0 | VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(object); |
630 | 0 | VipsMaplut *maplut = (VipsMaplut *) object; |
631 | 0 | VipsImage **t = (VipsImage **) vips_object_local_array(object, 2); |
632 | |
|
633 | 0 | VipsImage *in; |
634 | 0 | VipsImage *lut; |
635 | 0 | int i; |
636 | |
|
637 | 0 | g_object_set(object, "out", vips_image_new(), NULL); |
638 | |
|
639 | 0 | if (VIPS_OBJECT_CLASS(vips_maplut_parent_class)->build(object)) |
640 | 0 | return -1; |
641 | | |
642 | 0 | in = maplut->in; |
643 | 0 | lut = maplut->lut; |
644 | |
|
645 | 0 | if (vips_check_hist(class->nickname, lut) || |
646 | 0 | vips_check_uncoded(class->nickname, lut)) |
647 | 0 | return -1; |
648 | | |
649 | | /* Cast @in to u8/u16/u32 to make the index image. |
650 | | */ |
651 | 0 | if (vips_cast(in, &t[0], bandfmt_maplut[in->BandFmt], NULL)) |
652 | 0 | return -1; |
653 | 0 | in = t[0]; |
654 | |
|
655 | 0 | if (vips_check_uncoded(class->nickname, in) || |
656 | 0 | vips_check_bands_1orn(class->nickname, in, lut) || |
657 | 0 | vips_image_pio_input(in)) |
658 | 0 | return -1; |
659 | | |
660 | 0 | if (vips_image_pipelinev(maplut->out, |
661 | 0 | VIPS_DEMAND_STYLE_THINSTRIP, in, lut, NULL)) |
662 | 0 | return -1; |
663 | 0 | maplut->out->BandFmt = lut->BandFmt; |
664 | | |
665 | | /* Output has same number of bands as LUT, unless LUT has 1 band, in |
666 | | * which case output has same number of bands as input. |
667 | | */ |
668 | 0 | if (lut->Bands != 1) |
669 | 0 | maplut->out->Bands = lut->Bands; |
670 | | |
671 | | /* The Type comes from the image with many bands. A B_W index image, |
672 | | * for example, needs to become an RGB image when it goes through a |
673 | | * three-band LUT. |
674 | | */ |
675 | 0 | if (lut->Bands != 1) |
676 | 0 | maplut->out->Type = lut->Type; |
677 | | |
678 | | /* We can still set crazy interpretations -- for example, a many-band LUT |
679 | | * made with identity will be a histogram, but we don't want that for the |
680 | | * output image. |
681 | | * |
682 | | * Replace any crazy interpretations with the default. |
683 | | */ |
684 | 0 | maplut->out->Type = vips_image_guess_interpretation(maplut->out); |
685 | |
|
686 | 0 | g_signal_connect(in, "preeval", |
687 | 0 | G_CALLBACK(vips_maplut_preeval), maplut); |
688 | 0 | g_signal_connect(in, "posteval", |
689 | 0 | G_CALLBACK(vips_maplut_posteval), maplut); |
690 | | |
691 | | /* Make luts. We unpack the LUT image into a 2D C array to speed |
692 | | * processing. |
693 | | */ |
694 | 0 | if (!(t[1] = vips_image_copy_memory(lut))) |
695 | 0 | return -1; |
696 | 0 | lut = t[1]; |
697 | 0 | maplut->fmt = lut->BandFmt; |
698 | 0 | maplut->es = VIPS_IMAGE_SIZEOF_ELEMENT(lut); |
699 | 0 | maplut->sz = lut->Xsize * lut->Ysize; |
700 | 0 | maplut->clp = maplut->sz - 1; |
701 | | |
702 | | /* If @bands is >= 0, we need to expand the lut to the number of bands |
703 | | * in the input image. |
704 | | */ |
705 | 0 | if (maplut->band >= 0 && |
706 | 0 | lut->Bands == 1) |
707 | 0 | maplut->nb = in->Bands; |
708 | 0 | else |
709 | 0 | maplut->nb = lut->Bands; |
710 | | |
711 | | /* Attach tables. |
712 | | */ |
713 | 0 | if (!(maplut->table = VIPS_ARRAY(maplut, maplut->nb, VipsPel *))) |
714 | 0 | return -1; |
715 | 0 | for (i = 0; i < maplut->nb; i++) |
716 | 0 | if (!(maplut->table[i] = VIPS_ARRAY(maplut, |
717 | 0 | maplut->sz * maplut->es, VipsPel))) |
718 | 0 | return -1; |
719 | | |
720 | | /* Scan LUT and fill table. |
721 | | */ |
722 | 0 | switch (lut->BandFmt) { |
723 | 0 | case VIPS_FORMAT_UCHAR: |
724 | 0 | PACK_TABLE(unsigned char); |
725 | 0 | break; |
726 | 0 | case VIPS_FORMAT_CHAR: |
727 | 0 | PACK_TABLE(char); |
728 | 0 | break; |
729 | 0 | case VIPS_FORMAT_USHORT: |
730 | 0 | PACK_TABLE(unsigned short); |
731 | 0 | break; |
732 | 0 | case VIPS_FORMAT_SHORT: |
733 | 0 | PACK_TABLE(short); |
734 | 0 | break; |
735 | 0 | case VIPS_FORMAT_UINT: |
736 | 0 | PACK_TABLE(unsigned int); |
737 | 0 | break; |
738 | 0 | case VIPS_FORMAT_INT: |
739 | 0 | PACK_TABLE(int); |
740 | 0 | break; |
741 | 0 | case VIPS_FORMAT_FLOAT: |
742 | 0 | PACK_TABLE(float); |
743 | 0 | break; |
744 | 0 | case VIPS_FORMAT_DOUBLE: |
745 | 0 | PACK_TABLE(double); |
746 | 0 | break; |
747 | 0 | case VIPS_FORMAT_COMPLEX: |
748 | 0 | PACK_TABLEC(float); |
749 | 0 | break; |
750 | 0 | case VIPS_FORMAT_DPCOMPLEX: |
751 | 0 | PACK_TABLEC(double); |
752 | 0 | break; |
753 | 0 | default: |
754 | 0 | g_assert_not_reached(); |
755 | 0 | } |
756 | | |
757 | 0 | if (vips_image_generate(maplut->out, |
758 | 0 | vips_maplut_start, vips_maplut_gen, vips_maplut_stop, |
759 | 0 | in, maplut)) |
760 | 0 | return -1; |
761 | | |
762 | 0 | return 0; |
763 | 0 | } |
764 | | |
765 | | static void |
766 | | vips_maplut_class_init(VipsMaplutClass *class) |
767 | 1 | { |
768 | 1 | GObjectClass *gobject_class = G_OBJECT_CLASS(class); |
769 | 1 | VipsObjectClass *object_class = VIPS_OBJECT_CLASS(class); |
770 | 1 | VipsOperationClass *operation_class = VIPS_OPERATION_CLASS(class); |
771 | | |
772 | 1 | gobject_class->set_property = vips_object_set_property; |
773 | 1 | gobject_class->get_property = vips_object_get_property; |
774 | | |
775 | 1 | object_class->nickname = "maplut"; |
776 | 1 | object_class->description = _("map an image though a lut"); |
777 | 1 | object_class->build = vips_maplut_build; |
778 | | |
779 | 1 | operation_class->flags = VIPS_OPERATION_SEQUENTIAL; |
780 | | |
781 | 1 | VIPS_ARG_IMAGE(class, "in", 1, |
782 | 1 | _("Input"), |
783 | 1 | _("Input image"), |
784 | 1 | VIPS_ARGUMENT_REQUIRED_INPUT, |
785 | 1 | G_STRUCT_OFFSET(VipsMaplut, in)); |
786 | | |
787 | 1 | VIPS_ARG_IMAGE(class, "out", 2, |
788 | 1 | _("Output"), |
789 | 1 | _("Output image"), |
790 | 1 | VIPS_ARGUMENT_REQUIRED_OUTPUT, |
791 | 1 | G_STRUCT_OFFSET(VipsMaplut, out)); |
792 | | |
793 | 1 | VIPS_ARG_IMAGE(class, "lut", 3, |
794 | 1 | _("LUT"), |
795 | 1 | _("Look-up table image"), |
796 | 1 | VIPS_ARGUMENT_REQUIRED_INPUT, |
797 | 1 | G_STRUCT_OFFSET(VipsMaplut, lut)); |
798 | | |
799 | 1 | VIPS_ARG_INT(class, "band", 4, |
800 | 1 | _("Band"), |
801 | 1 | _("Apply one-band lut to this band of in"), |
802 | 1 | VIPS_ARGUMENT_OPTIONAL_INPUT, |
803 | 1 | G_STRUCT_OFFSET(VipsMaplut, band), |
804 | 1 | -1, 10000, -1); |
805 | 1 | } |
806 | | |
807 | | static void |
808 | | vips_maplut_init(VipsMaplut *maplut) |
809 | 0 | { |
810 | 0 | maplut->band = -1; |
811 | 0 | } |
812 | | |
813 | | /** |
814 | | * vips_maplut: (method) |
815 | | * @in: input image |
816 | | * @out: (out): output image |
817 | | * @lut: look-up table |
818 | | * @...: `NULL`-terminated list of optional named arguments |
819 | | * |
820 | | * Map an image through another image acting as a LUT (Look Up Table). |
821 | | * The lut may have any type and the output image will be that type. |
822 | | * |
823 | | * The input image will be cast to one of the unsigned integer types, that is, |
824 | | * [enum@Vips.BandFormat.UCHAR], [enum@Vips.BandFormat.USHORT] or [enum@Vips.BandFormat.UINT]. |
825 | | * |
826 | | * If @lut is too small for the input type (for example, if @in is |
827 | | * [enum@Vips.BandFormat.UCHAR] but @lut only has 100 elements), the lut is padded out |
828 | | * by copying the last element. Overflows are reported at the end of |
829 | | * computation. |
830 | | * If @lut is too large, extra values are ignored. |
831 | | * |
832 | | * If @lut has one band and @band is -1 (the default), then all bands of @in |
833 | | * pass through @lut. If @band is >= 0, then just that band of @in passes |
834 | | * through @lut and other bands are just copied. |
835 | | * |
836 | | * If @lut |
837 | | * has same number of bands as @in, then each band is mapped |
838 | | * separately. If @in has one band, then @lut may have many bands and |
839 | | * the output will have the same number of bands as @lut. |
840 | | * |
841 | | * ::: tip "Optional arguments" |
842 | | * * @band: `gint`, apply one-band @lut to this band of @in |
843 | | * |
844 | | * ::: seealso |
845 | | * [method@Image.hist_find], [ctor@Image.identity]. |
846 | | * |
847 | | * Returns: 0 on success, -1 on error |
848 | | */ |
849 | | int |
850 | | vips_maplut(VipsImage *in, VipsImage **out, VipsImage *lut, ...) |
851 | 0 | { |
852 | 0 | va_list ap; |
853 | 0 | int result; |
854 | |
|
855 | 0 | va_start(ap, lut); |
856 | 0 | result = vips_call_split("maplut", ap, in, out, lut); |
857 | 0 | va_end(ap); |
858 | |
|
859 | 0 | return result; |
860 | 0 | } |