/src/libvips/libvips/create/buildlut.c
Line | Count | Source |
1 | | /* Build a LUT from a set of x/y points. |
2 | | * |
3 | | * Written on: 26/9/06 |
4 | | * - from im_invertlut() |
5 | | * 9/10/06 |
6 | | * - don't output x values |
7 | | * 18/3/09 |
8 | | * - saner limit and rounding behaviour |
9 | | * 30/3/09 |
10 | | * - argh, fixed again |
11 | | * 22/6/09 |
12 | | * - more fixes for tables that don't start at zero (thanks Jack) |
13 | | * 23/3/10 |
14 | | * - gtkdoc |
15 | | * 2/7/13 |
16 | | * - convert to a class |
17 | | * 10/12/13 |
18 | | * - be more forgiving about x vales not quite integers |
19 | | */ |
20 | | |
21 | | /* |
22 | | |
23 | | This file is part of VIPS. |
24 | | |
25 | | VIPS is free software; you can redistribute it and/or modify |
26 | | it under the terms of the GNU Lesser General Public License as published by |
27 | | the Free Software Foundation; either version 2 of the License, or |
28 | | (at your option) any later version. |
29 | | |
30 | | This program is distributed in the hope that it will be useful, |
31 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
32 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
33 | | GNU Lesser General Public License for more details. |
34 | | |
35 | | You should have received a copy of the GNU Lesser General Public License |
36 | | along with this program; if not, write to the Free Software |
37 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
38 | | 02110-1301 USA |
39 | | |
40 | | */ |
41 | | |
42 | | /* |
43 | | |
44 | | These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk |
45 | | |
46 | | */ |
47 | | |
48 | | /* |
49 | | #define DEBUG |
50 | | */ |
51 | | |
52 | | #ifdef HAVE_CONFIG_H |
53 | | #include <config.h> |
54 | | #endif /*HAVE_CONFIG_H*/ |
55 | | #include <glib/gi18n-lib.h> |
56 | | |
57 | | #include <stdio.h> |
58 | | #include <stdlib.h> |
59 | | #include <math.h> |
60 | | |
61 | | #include <vips/vips.h> |
62 | | |
63 | | #include "pcreate.h" |
64 | | |
65 | | typedef struct _VipsBuildlut { |
66 | | VipsCreate parent_instance; |
67 | | |
68 | | /* Input image. |
69 | | */ |
70 | | VipsImage *in; |
71 | | |
72 | | /* .. and cast to a matrix. |
73 | | */ |
74 | | VipsImage *mat; |
75 | | |
76 | | int xlow; /* Index 0 in output is this x */ |
77 | | int lut_size; /* Number of output elements to generate */ |
78 | | double **data; /* Matrix row pointers */ |
79 | | double *buf; /* Output buffer */ |
80 | | } VipsBuildlut; |
81 | | |
82 | | typedef VipsCreateClass VipsBuildlutClass; |
83 | | |
84 | 40 | G_DEFINE_TYPE(VipsBuildlut, vips_buildlut, VIPS_TYPE_CREATE); |
85 | 40 | |
86 | 40 | static void |
87 | 40 | vips_buildlut_dispose(GObject *gobject) |
88 | 46 | { |
89 | 46 | VipsBuildlut *lut = (VipsBuildlut *) gobject; |
90 | | |
91 | 46 | VIPS_FREE(lut->data); |
92 | 46 | VIPS_FREE(lut->buf); |
93 | 46 | VIPS_UNREF(lut->mat); |
94 | | |
95 | 46 | G_OBJECT_CLASS(vips_buildlut_parent_class)->dispose(gobject); |
96 | 46 | } |
97 | | |
98 | | /* Use this to sort our input rows by the first column. |
99 | | */ |
100 | | static int |
101 | | vips_buildlut_compare(const void *a, const void *b) |
102 | 2.63k | { |
103 | 2.63k | double **r1 = (double **) a; |
104 | 2.63k | double **r2 = (double **) b; |
105 | | |
106 | 2.63k | double diff = r1[0][0] - r2[0][0]; |
107 | | |
108 | 2.63k | if (diff > 0) |
109 | 933 | return 1; |
110 | 1.70k | else if (diff == 0) |
111 | 1.06k | return 0; |
112 | 633 | else |
113 | 633 | return -1; |
114 | 2.63k | } |
115 | | |
116 | | static int |
117 | | vips_buildlut_build_init(VipsBuildlut *lut) |
118 | 43 | { |
119 | 43 | VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(lut); |
120 | | |
121 | 43 | double xlow, xhigh; |
122 | | |
123 | | /* Need xlow and xhigh to get the size of the LUT we build. |
124 | | */ |
125 | 43 | xlow = xhigh = rint(*VIPS_MATRIX(lut->mat, 0, 0)); |
126 | 799 | for (int y = 0; y < lut->mat->Ysize; y++) { |
127 | 756 | double v = *VIPS_MATRIX(lut->mat, 0, y); |
128 | | |
129 | | /* Allow for being a bit off. |
130 | | */ |
131 | 756 | if (isnan(v) || fabs(v - rint(v)) > 0.001) { |
132 | 0 | vips_error(class->nickname, |
133 | 0 | _("x value row %d not an int"), y); |
134 | 0 | return -1; |
135 | 0 | } |
136 | | |
137 | 756 | v = rint(v); |
138 | | |
139 | 756 | if (v < xlow) |
140 | 39 | xlow = v; |
141 | 756 | if (v > xhigh) |
142 | 33 | xhigh = v; |
143 | 756 | } |
144 | | |
145 | 43 | if (xlow < INT_MIN || |
146 | 43 | xhigh > INT_MAX) { |
147 | 0 | vips_error(class->nickname, "%s", _("x range too large")); |
148 | 0 | return -1; |
149 | 0 | } |
150 | 43 | lut->xlow = (int) xlow; |
151 | | |
152 | 43 | lut->lut_size = (int) (xhigh - xlow + 1); |
153 | 43 | if (lut->lut_size < 1 || |
154 | 43 | lut->lut_size > 100000 || |
155 | 43 | lut->mat->Xsize > 512) { |
156 | 0 | vips_error(class->nickname, "%s", _("bad lut size")); |
157 | 0 | return -1; |
158 | 0 | } |
159 | | |
160 | 43 | if (!(lut->data = VIPS_ARRAY(NULL, lut->mat->Ysize, double *))) |
161 | 0 | return -1; |
162 | 799 | for (int y = 0; y < lut->mat->Ysize; y++) |
163 | 756 | lut->data[y] = VIPS_MATRIX(lut->mat, 0, y); |
164 | | |
165 | 43 | if (!(lut->buf = VIPS_ARRAY(NULL, |
166 | 43 | lut->lut_size * (lut->mat->Xsize - 1), double))) |
167 | 2 | return -1; |
168 | | |
169 | | /* Sort by 1st column in input. |
170 | | */ |
171 | 41 | qsort(lut->data, lut->mat->Ysize, |
172 | 41 | sizeof(double *), vips_buildlut_compare); |
173 | | |
174 | | #ifdef DEBUG |
175 | | printf("Input table, sorted by 1st column\n"); |
176 | | for (int y = 0; y < lut->mat->Ysize; y++) { |
177 | | printf("%.4d ", y); |
178 | | |
179 | | for (int x = 0; x < lut->mat->Xsize; x++) |
180 | | printf("%.9f ", lut->data[y][x]); |
181 | | |
182 | | printf("\n"); |
183 | | } |
184 | | #endif /*DEBUG*/ |
185 | | |
186 | 41 | return 0; |
187 | 43 | } |
188 | | |
189 | | static int |
190 | | vips_buildlut_build_create(VipsBuildlut *lut) |
191 | 41 | { |
192 | 41 | const int xlow = lut->xlow; |
193 | 41 | const VipsImage *mat = lut->mat; |
194 | 41 | const int xsize = mat->Xsize; |
195 | 41 | const int ysize = mat->Ysize; |
196 | 41 | const int bands = xsize - 1; |
197 | | |
198 | | /* Do each output channel separately. |
199 | | */ |
200 | 551 | for (int b = 0; b < bands; b++) { |
201 | 11.3k | for (int i = 0; i < ysize - 1; i++) { |
202 | 10.8k | const int x1 = rint(lut->data[i][0]); |
203 | 10.8k | const int x2 = rint(lut->data[i + 1][0]); |
204 | 10.8k | const int dx = x2 - x1; |
205 | 10.8k | const double y1 = lut->data[i][b + 1]; |
206 | 10.8k | const double y2 = lut->data[i + 1][b + 1]; |
207 | 10.8k | const double dy = y2 - y1; |
208 | | |
209 | 37.1k | for (int x = 0; x < dx; x++) |
210 | 26.3k | lut->buf[b + (x + x1 - xlow) * bands] = y1 + x * dy / dx; |
211 | 10.8k | } |
212 | | |
213 | | /* We are inclusive: pop the final value in by hand. |
214 | | */ |
215 | 510 | lut->buf[b + (lut->lut_size - 1) * bands] = lut->data[ysize - 1][b + 1]; |
216 | 510 | } |
217 | | |
218 | 41 | return 0; |
219 | 41 | } |
220 | | |
221 | | static int |
222 | | vips_buildlut_build(VipsObject *object) |
223 | 44 | { |
224 | 44 | VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(object); |
225 | 44 | VipsCreate *create = VIPS_CREATE(object); |
226 | 44 | VipsBuildlut *lut = (VipsBuildlut *) object; |
227 | | |
228 | 44 | if (VIPS_OBJECT_CLASS(vips_buildlut_parent_class)->build(object)) |
229 | 0 | return -1; |
230 | | |
231 | 44 | if (vips_check_matrix(class->nickname, lut->in, &lut->mat)) |
232 | 1 | return -1; |
233 | | |
234 | 43 | if (vips_buildlut_build_init(lut) || |
235 | 41 | vips_buildlut_build_create(lut)) |
236 | 2 | return -1; |
237 | | |
238 | 41 | vips_image_init_fields(create->out, |
239 | 41 | lut->lut_size, 1, lut->mat->Xsize - 1, |
240 | 41 | VIPS_FORMAT_DOUBLE, VIPS_CODING_NONE, |
241 | 41 | VIPS_INTERPRETATION_HISTOGRAM, 1.0, 1.0); |
242 | 41 | if (vips_image_write_line(create->out, 0, (VipsPel *) lut->buf)) |
243 | 0 | return -1; |
244 | | |
245 | 41 | return 0; |
246 | 41 | } |
247 | | |
248 | | static void |
249 | | vips_buildlut_class_init(VipsBuildlutClass *class) |
250 | 20 | { |
251 | 20 | GObjectClass *gobject_class = G_OBJECT_CLASS(class); |
252 | 20 | VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class); |
253 | | |
254 | 20 | gobject_class->dispose = vips_buildlut_dispose; |
255 | 20 | gobject_class->set_property = vips_object_set_property; |
256 | 20 | gobject_class->get_property = vips_object_get_property; |
257 | | |
258 | 20 | vobject_class->nickname = "buildlut"; |
259 | 20 | vobject_class->description = _("build a look-up table"); |
260 | 20 | vobject_class->build = vips_buildlut_build; |
261 | | |
262 | 20 | VIPS_ARG_IMAGE(class, "in", 0, |
263 | 20 | _("Input"), |
264 | 20 | _("Matrix of XY coordinates"), |
265 | 20 | VIPS_ARGUMENT_REQUIRED_INPUT, |
266 | 20 | G_STRUCT_OFFSET(VipsBuildlut, in)); |
267 | 20 | } |
268 | | |
269 | | static void |
270 | | vips_buildlut_init(VipsBuildlut *lut) |
271 | 46 | { |
272 | 46 | } |
273 | | |
274 | | /** |
275 | | * vips_buildlut: (method) |
276 | | * @in: input matrix |
277 | | * @out: (out): output image |
278 | | * @...: `NULL`-terminated list of optional named arguments |
279 | | * |
280 | | * This operation builds a lookup table from a set of points. Intermediate |
281 | | * values are generated by piecewise linear interpolation. The lookup table is |
282 | | * always of type [enum@Vips.BandFormat.DOUBLE], use [method@Image.cast] to |
283 | | * change it to the type you need. |
284 | | * |
285 | | * For example, consider this 2 x 2 matrix of (x, y) coordinates: |
286 | | * |
287 | | * ``` |
288 | | * 2 2 |
289 | | * 0 0 |
290 | | * 255 100 |
291 | | * ``` |
292 | | * |
293 | | * We then generate a 1 x 256 element LUT like this: |
294 | | * |
295 | | * | Index | Value | |
296 | | * |-------|-------| |
297 | | * | 0 | 0 | |
298 | | * | 1 | 0.4 | |
299 | | * | etc. | 0.4 | |
300 | | * | 255 | 100 | |
301 | | |
302 | | * This is then written as the output image, with the left column giving the |
303 | | * index in the image to place the value. |
304 | | * |
305 | | * The (x, y) points don't need to be sorted: we do that. You can have |
306 | | * several Ys, each becomes a band in the output LUT. You don't need to |
307 | | * start at zero, any integer will do, including negatives. |
308 | | * |
309 | | * ::: seealso |
310 | | * [ctor@Image.identity], [method@Image.invertlut], [method@Image.cast], |
311 | | * [method@Image.maplut]. |
312 | | * |
313 | | * Returns: 0 on success, -1 on error |
314 | | */ |
315 | | int |
316 | | vips_buildlut(VipsImage *in, VipsImage **out, ...) |
317 | 0 | { |
318 | 0 | va_list ap; |
319 | 0 | int result; |
320 | |
|
321 | 0 | va_start(ap, out); |
322 | 0 | result = vips_call_split("buildlut", ap, in, out); |
323 | 0 | va_end(ap); |
324 | |
|
325 | 0 | return result; |
326 | 0 | } |