Coverage Report

Created: 2026-12-01 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvips/libvips/create/invertlut.c
Line
Count
Source
1
/* invert a lut
2
 *
3
 * Written on: 5/6/01
4
 * Modified on :
5
 *
6
 * 7/7/03 JC
7
 *  - generate image rather than doublemask (arrg)
8
 * 23/3/10
9
 *  - gtkdoc
10
 * 23/5/13
11
 *  - fix 1 high input matrices
12
 *  - fix file output
13
 * 4/9/13
14
 *  - convert to a class
15
 */
16
17
/*
18
19
  This file is part of VIPS.
20
21
  VIPS is free software; you can redistribute it and/or modify
22
  it under the terms of the GNU Lesser General Public License as published by
23
  the Free Software Foundation; either version 2 of the License, or
24
  (at your option) any later version.
25
26
  This program is distributed in the hope that it will be useful,
27
  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
  GNU Lesser General Public License for more details.
30
31
  You should have received a copy of the GNU Lesser General Public License
32
  along with this program; if not, write to the Free Software
33
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34
  02110-1301  USA
35
36
 */
37
38
/*
39
40
  These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
41
42
 */
43
44
#ifdef HAVE_CONFIG_H
45
#include <config.h>
46
#endif /*HAVE_CONFIG_H*/
47
#include <glib/gi18n-lib.h>
48
49
#include <stdio.h>
50
#include <stdlib.h>
51
#include <math.h>
52
53
#include <vips/vips.h>
54
55
#include "pcreate.h"
56
57
/*
58
#define DEBUG
59
 */
60
61
/* Our state.
62
 */
63
typedef struct _VipsInvertlut {
64
  VipsCreate parent_instance;
65
66
  /* Input image.
67
   */
68
  VipsImage *in;
69
70
  /* .. and cast to a matrix.
71
   */
72
  VipsImage *mat;
73
74
  int size; /* Number of output elements to generate */
75
76
  double **data; /* Rows of unpacked matrix */
77
  double *buf;   /* Output buffer */
78
} VipsInvertlut;
79
80
typedef VipsCreateClass VipsInvertlutClass;
81
82
2
G_DEFINE_TYPE(VipsInvertlut, vips_invertlut, VIPS_TYPE_CREATE);
83
2
84
2
static void
85
2
vips_invertlut_dispose(GObject *gobject)
86
2
{
87
0
  VipsInvertlut *lut = (VipsInvertlut *) gobject;
88
89
0
  VIPS_FREE(lut->data);
90
0
  VIPS_FREE(lut->buf);
91
0
  VIPS_UNREF(lut->mat);
92
93
0
  G_OBJECT_CLASS(vips_invertlut_parent_class)->dispose(gobject);
94
0
}
95
96
/* Use this to sort our input rows by the first column.
97
 */
98
static int
99
compare(const void *a, const void *b)
100
0
{
101
0
  double **r1 = (double **) a;
102
0
  double **r2 = (double **) b;
103
104
0
  double diff = r1[0][0] - r2[0][0];
105
106
0
  if (diff > 0)
107
0
    return 1;
108
0
  else if (diff == 0)
109
0
    return 0;
110
0
  else
111
0
    return -1;
112
0
}
113
114
static int
115
vips_invertlut_build_init(VipsInvertlut *lut)
116
0
{
117
0
  VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(lut);
118
119
0
  int x, y;
120
121
0
  if (!lut->mat ||
122
0
    lut->mat->Xsize < 2 ||
123
0
    lut->mat->Ysize < 1) {
124
0
    vips_error(class->nickname, "%s", _("bad input matrix"));
125
0
    return -1;
126
0
  }
127
0
  if (lut->size < 1 ||
128
0
    lut->size > 65536) {
129
0
    vips_error(class->nickname, "%s", _("bad size"));
130
0
    return -1;
131
0
  }
132
133
0
  if (!(lut->buf =
134
0
        VIPS_ARRAY(NULL, (size_t) lut->size * (lut->mat->Xsize - 1), double)))
135
0
    return -1;
136
137
0
  if (!(lut->data = VIPS_ARRAY(NULL, lut->mat->Ysize, double *)))
138
0
    return -1;
139
0
  for (y = 0; y < lut->mat->Ysize; y++)
140
0
    lut->data[y] = VIPS_MATRIX(lut->mat, 0, y);
141
142
  /* Sanity check for data range.
143
   */
144
0
  for (y = 0; y < lut->mat->Ysize; y++)
145
0
    for (x = 0; x < lut->mat->Xsize; x++)
146
0
      if (lut->data[y][x] > 1.0 ||
147
0
        lut->data[y][x] < 0.0) {
148
0
        vips_error(class->nickname,
149
0
          _("element (%d, %d) is %g, outside range [0,1]"),
150
0
          x, y, lut->data[y][x]);
151
0
        return -1;
152
0
      }
153
154
  /* Sort by 1st column in input.
155
   */
156
0
  qsort(lut->data, lut->mat->Ysize, sizeof(double *), compare);
157
158
#ifdef DEBUG
159
  printf("Input table, sorted by 1st column\n");
160
  for (y = 0; y < lut->mat->Ysize; y++) {
161
    printf("%.4d ", y);
162
163
    for (x = 0; x < lut->mat->Xsize; x++)
164
      printf("%.9f ", lut->data[y][x]);
165
166
    printf("\n");
167
  }
168
#endif /*DEBUG*/
169
170
0
  return 0;
171
0
}
172
173
static int
174
vips_invertlut_build_create(VipsInvertlut *lut)
175
0
{
176
0
  VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(lut);
177
178
0
  int bands = lut->mat->Xsize - 1;
179
0
  int height = lut->mat->Ysize;
180
181
0
  int b;
182
183
  /* Do each output channel separately.
184
   */
185
0
  for (b = 0; b < bands; b++) {
186
    /* The first and last lut positions we know real values for.
187
     */
188
0
    int first = lut->data[0][b + 1] * (lut->size - 1);
189
0
    int last = lut->data[height - 1][b + 1] * (lut->size - 1);
190
0
    if (first < 0 || first >= lut->size ||
191
0
      last < 0 || last >= lut->size) {
192
0
      vips_error(class->nickname, _("invalid input mask"));
193
0
      return -1;
194
0
    }
195
196
0
    int k;
197
198
    /* Extrapolate bottom and top segments to (0,0) and (1,1).
199
     */
200
0
    for (k = 0; k < first; k++) {
201
      /* Have this inside the loop to avoid /0 errors if
202
       * first == 0.
203
       */
204
0
      double fac = lut->data[0][0] / first;
205
206
0
      lut->buf[b + k * bands] = k * fac;
207
0
    }
208
209
0
    for (k = last; k < lut->size; k++) {
210
      /* Inside the loop to avoid /0 errors for last ==
211
       * (size - 1).
212
       */
213
0
      double fac = (1 - lut->data[height - 1][0]) /
214
0
        ((lut->size - 1) - last);
215
216
0
      lut->buf[b + k * bands] =
217
0
        lut->data[height - 1][0] + (k - last) * fac;
218
0
    }
219
220
    /* Interpolate the data sections.
221
     */
222
0
    for (k = first; k <= last; k++) {
223
      /* Where we're at in the [0,1] range.
224
       */
225
0
      double ki = (double) k / (lut->size - 1);
226
227
0
      double irange, orange;
228
0
      int j;
229
230
      /* Search for the lowest real value < ki. There may
231
       * not be one: if not, just use 0. Tiny error.
232
       */
233
0
      for (j = height - 1; j >= 0; j--)
234
0
        if (lut->data[j][b + 1] < ki)
235
0
          break;
236
0
      if (j == -1)
237
0
        j = 0;
238
239
0
      if (height > 1) {
240
        /* Interpolate k as being between row data[j] and row
241
         * data[j + 1].
242
         */
243
0
        irange = lut->data[j + 1][b + 1] - lut->data[j][b + 1];
244
0
        orange = lut->data[j + 1][0] - lut->data[j][0];
245
246
0
        lut->buf[b + k * bands] = lut->data[j][0] +
247
0
          orange * ((ki - lut->data[j][b + 1]) / irange);
248
0
      }
249
0
      else {
250
0
        lut->buf[b + k * bands] = lut->data[j][0];
251
0
      }
252
0
    }
253
0
  }
254
255
0
  return 0;
256
0
}
257
258
static int
259
vips_invertlut_build(VipsObject *object)
260
0
{
261
0
  VipsObjectClass *class = VIPS_OBJECT_GET_CLASS(object);
262
0
  VipsCreate *create = VIPS_CREATE(object);
263
0
  VipsInvertlut *lut = (VipsInvertlut *) object;
264
265
0
  if (VIPS_OBJECT_CLASS(vips_invertlut_parent_class)->build(object))
266
0
    return -1;
267
268
0
  if (vips_check_matrix(class->nickname, lut->in, &lut->mat))
269
0
    return -1;
270
271
0
  if (vips_invertlut_build_init(lut) ||
272
0
    vips_invertlut_build_create(lut))
273
0
    return -1;
274
275
0
  vips_image_init_fields(create->out,
276
0
    lut->size, 1, lut->mat->Xsize - 1,
277
0
    VIPS_FORMAT_DOUBLE, VIPS_CODING_NONE,
278
0
    VIPS_INTERPRETATION_HISTOGRAM, 1.0, 1.0);
279
0
  if (vips_image_write_line(create->out, 0, (VipsPel *) lut->buf))
280
0
    return -1;
281
282
0
  return 0;
283
0
}
284
285
static void
286
vips_invertlut_class_init(VipsInvertlutClass *class)
287
1
{
288
1
  GObjectClass *gobject_class = G_OBJECT_CLASS(class);
289
1
  VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class);
290
291
1
  gobject_class->dispose = vips_invertlut_dispose;
292
1
  gobject_class->set_property = vips_object_set_property;
293
1
  gobject_class->get_property = vips_object_get_property;
294
295
1
  vobject_class->nickname = "invertlut";
296
1
  vobject_class->description = _("build an inverted look-up table");
297
1
  vobject_class->build = vips_invertlut_build;
298
299
1
  VIPS_ARG_IMAGE(class, "in", 0,
300
1
    _("Input"),
301
1
    _("Matrix of XY coordinates"),
302
1
    VIPS_ARGUMENT_REQUIRED_INPUT,
303
1
    G_STRUCT_OFFSET(VipsInvertlut, in));
304
305
1
  VIPS_ARG_INT(class, "size", 5,
306
1
    _("Size"),
307
1
    _("LUT size to generate"),
308
1
    VIPS_ARGUMENT_OPTIONAL_INPUT,
309
1
    G_STRUCT_OFFSET(VipsInvertlut, size),
310
1
    1, 1000000, 256);
311
1
}
312
313
static void
314
vips_invertlut_init(VipsInvertlut *lut)
315
0
{
316
0
  lut->size = 256;
317
0
}
318
319
/**
320
 * vips_invertlut: (method)
321
 * @in: input mask
322
 * @out: (out): output LUT
323
 * @...: `NULL`-terminated list of optional named arguments
324
 *
325
 * Given a mask of target values and real values, generate a LUT which
326
 * will map reals to targets.
327
 *
328
 * Handy for linearising images from measurements of a colour chart. All
329
 * values in [0,1]. Piecewise linear interpolation, extrapolate head and tail
330
 * to 0 and 1.
331
 *
332
 * Eg. input like this:
333
 *
334
 * ```
335
 * 4 3
336
 * 0.1 0.2 0.3 0.1
337
 * 0.2 0.4 0.4 0.2
338
 * 0.7 0.5 0.6 0.3
339
 * ```
340
 *
341
 * Means a patch with 10% reflectance produces an image with 20% in
342
 * channel 1, 30% in channel 2, and 10% in channel 3, and so on.
343
 *
344
 * Inputs don't need to be sorted (we do that). Generate any precision
345
 * LUT, default to 256 elements.
346
 *
347
 * It won't work too well for non-monotonic camera responses
348
 * (we should fix this). Interpolation is simple piecewise linear; we ought to
349
 * do something better really.
350
 *
351
 * ::: tip "Optional arguments"
352
 *     * @size: `gint`, generate this much
353
 *
354
 * ::: seealso
355
 *     [method@Image.buildlut].
356
 *
357
 * Returns: 0 on success, -1 on error
358
 */
359
int
360
vips_invertlut(VipsImage *in, VipsImage **out, ...)
361
0
{
362
0
  va_list ap;
363
0
  int result;
364
365
0
  va_start(ap, out);
366
0
  result = vips_call_split("invertlut", ap, in, out);
367
0
  va_end(ap);
368
369
0
  return result;
370
0
}