Coverage Report

Created: 2026-12-01 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvips/libvips/arithmetic/divide.c
Line
Count
Source
1
/* Divide two images
2
 *
3
 * Copyright: 1990, N. Dessipris.
4
 *
5
 * Author: Nicos Dessipris
6
 * Written on: 02/05/1990
7
 * Modified on:
8
 * 29/4/93 JC
9
 *  - now works for partial images
10
 * 1/7/93 JC
11
 *  - adapted for partial v2
12
 *  - ANSIfied
13
 * 19/10/93 JC
14
 *  - coredump-inducing bug in complex*complex fixed
15
 * 13/12/93
16
 *  - char * short bug fixed
17
 * 12/6/95 JC
18
 *  - new im_multiply adapted to make new im_divide
19
 * 27/9/04
20
 *  - updated for 1 band $op n band image -> n band image case
21
 * 8/12/06
22
 *  - add liboil support
23
 * 18/8/08
24
 *  - revise upcasting system
25
 *  - add gtkdoc comments
26
 * 31/7/10
27
 *  - remove liboil support
28
 *  - avoid /0
29
 * 6/11/11
30
 *  - rewrite as a class
31
 * 22/2/12
32
 *  - avoid /0 for complex as well
33
 * 6/4/12
34
 *  - fixed switch cases
35
 *  - fixed int operands with <1 result
36
 */
37
38
/*
39
40
  Copyright (C) 1991-2005 The National Gallery
41
42
  This library is free software; you can redistribute it and/or
43
  modify it under the terms of the GNU Lesser General Public
44
  License as published by the Free Software Foundation; either
45
  version 2.1 of the License, or (at your option) any later version.
46
47
  This library 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 GNU
50
  Lesser General Public License for more details.
51
52
  You should have received a copy of the GNU Lesser General Public
53
  License along with this library; 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
/*
66
#define DEBUG
67
 */
68
69
#ifdef HAVE_CONFIG_H
70
#include <config.h>
71
#endif /*HAVE_CONFIG_H*/
72
#include <glib/gi18n-lib.h>
73
74
#include <stdio.h>
75
#include <stdlib.h>
76
#include <math.h>
77
78
#include <vips/vips.h>
79
80
#include "binary.h"
81
82
typedef VipsBinary VipsDivide;
83
typedef VipsBinaryClass VipsDivideClass;
84
85
2
G_DEFINE_TYPE(VipsDivide, vips_divide, VIPS_TYPE_BINARY);
86
2
87
2
#define CLOOP(TYPE) \
88
2
  { \
89
0
    TYPE *restrict left = (TYPE *) in[0]; \
90
0
    TYPE *restrict right = (TYPE *) in[1]; \
91
0
    TYPE *restrict q = (TYPE *) out; \
92
0
    int i; \
93
0
\
94
0
    for (i = 0; i < sz; i++) { \
95
0
      if (right[0] == 0.0 && \
96
0
        right[1] == 0.0) { \
97
0
        q[0] = 0.0; \
98
0
        q[1] = 0.0; \
99
0
      } \
100
0
      else if (fabs(right[0]) > fabs(right[1])) { \
101
0
        double a = right[1] / right[0]; \
102
0
        double b = right[0] + right[1] * a; \
103
0
\
104
0
        q[0] = (left[0] + left[1] * a) / b; \
105
0
        q[1] = (left[1] - left[0] * a) / b; \
106
0
      } \
107
0
      else { \
108
0
        double a = right[0] / right[1]; \
109
0
        double b = right[1] + right[0] * a; \
110
0
\
111
0
        q[0] = (left[0] * a + left[1]) / b; \
112
0
        q[1] = (left[1] * a - left[0]) / b; \
113
0
      } \
114
0
\
115
0
      left += 2; \
116
0
      right += 2; \
117
0
      q += 2; \
118
0
    } \
119
0
  }
120
121
/* Real divide. Cast in to OUT before divide so we work for float output.
122
 */
123
#define RLOOP(IN, OUT) \
124
195k
  { \
125
195k
    IN *restrict left = (IN *) in[0]; \
126
195k
    IN *restrict right = (IN *) in[1]; \
127
195k
    OUT *restrict q = (OUT *) out; \
128
195k
\
129
18.9M
    for (x = 0; x < sz; x++) \
130
18.7M
      q[x] = right[x] == 0 ? 0 : (OUT) left[x] / (OUT) right[x]; \
131
195k
  }
132
133
static void
134
vips_divide_buffer(VipsArithmetic *arithmetic,
135
  VipsPel *out, VipsPel **in, int width)
136
195k
{
137
195k
  VipsImage *im = arithmetic->ready[0];
138
195k
  const int sz = width * vips_image_get_bands(im);
139
140
195k
  int x;
141
142
  /* Keep types here in sync with vips_divide_format_table[]
143
   * below.
144
   */
145
195k
  switch (vips_image_get_format(im)) {
146
0
  case VIPS_FORMAT_CHAR:
147
0
    RLOOP(signed char, float);
148
0
    break;
149
0
  case VIPS_FORMAT_UCHAR:
150
0
    RLOOP(unsigned char, float);
151
0
    break;
152
0
  case VIPS_FORMAT_SHORT:
153
0
    RLOOP(signed short, float);
154
0
    break;
155
0
  case VIPS_FORMAT_USHORT:
156
0
    RLOOP(unsigned short, float);
157
0
    break;
158
0
  case VIPS_FORMAT_INT:
159
0
    RLOOP(signed int, float);
160
0
    break;
161
0
  case VIPS_FORMAT_UINT:
162
0
    RLOOP(unsigned int, float);
163
0
    break;
164
195k
  case VIPS_FORMAT_FLOAT:
165
195k
    RLOOP(float, float);
166
195k
    break;
167
0
  case VIPS_FORMAT_DOUBLE:
168
0
    RLOOP(double, double);
169
0
    break;
170
0
  case VIPS_FORMAT_COMPLEX:
171
0
    CLOOP(float);
172
0
    break;
173
0
  case VIPS_FORMAT_DPCOMPLEX:
174
0
    CLOOP(double);
175
0
    break;
176
177
0
  default:
178
0
    g_assert_not_reached();
179
195k
  }
180
195k
}
181
182
/* Save a bit of typing.
183
 */
184
#define UC VIPS_FORMAT_UCHAR
185
#define C VIPS_FORMAT_CHAR
186
#define US VIPS_FORMAT_USHORT
187
#define S VIPS_FORMAT_SHORT
188
#define UI VIPS_FORMAT_UINT
189
#define I VIPS_FORMAT_INT
190
#define F VIPS_FORMAT_FLOAT
191
#define X VIPS_FORMAT_COMPLEX
192
#define D VIPS_FORMAT_DOUBLE
193
#define DX VIPS_FORMAT_DPCOMPLEX
194
195
/* Type promotion for division. Sign and value preserving. Make sure
196
 * these match the case statement in divide_buffer() above.
197
 */
198
static VipsBandFormat vips_divide_format_table[10] = {
199
  /* Band format:  UC C  US S  UI I  F  X  D  DX */
200
  /* Promotion: */ F, F, F, F, F, F, F, X, D, DX
201
};
202
203
static void
204
vips_divide_class_init(VipsDivideClass *class)
205
1
{
206
1
  VipsObjectClass *object_class = (VipsObjectClass *) class;
207
1
  VipsArithmeticClass *aclass = VIPS_ARITHMETIC_CLASS(class);
208
209
1
  object_class->nickname = "divide";
210
1
  object_class->description = _("divide two images");
211
212
1
  aclass->process_line = vips_divide_buffer;
213
214
1
  vips_arithmetic_set_format_table(aclass, vips_divide_format_table);
215
1
}
216
217
static void
218
vips_divide_init(VipsDivide *divide)
219
25.1k
{
220
25.1k
}
221
222
/**
223
 * vips_divide: (method)
224
 * @left: input image
225
 * @right: input image
226
 * @out: (out): output image
227
 * @...: `NULL`-terminated list of optional named arguments
228
 *
229
 * This operation calculates @in1 / @in2 and writes the result to @out. If any
230
 * pixels in @in2 are zero, the corresponding pixel in @out is also zero.
231
 *
232
 * If the images differ in size, the smaller image is enlarged to match the
233
 * larger by adding zero pixels along the bottom and right.
234
 *
235
 * If the number of bands differs, one of the images
236
 * must have one band. In this case, an n-band image is formed from the
237
 * one-band image by joining n copies of the one-band image together, and then
238
 * the two n-band images are operated upon.
239
 *
240
 * The two input images are cast up to the smallest common format (see table
241
 * Smallest common format in
242
 * [arithmetic](libvips-arithmetic.html)), then the
243
 * following table is used to determine the output type:
244
 *
245
 * ## [method@Image.divide] type promotion
246
 *
247
 * | input type     | output type    |
248
 * |----------------|----------------|
249
 * | uchar          | float          |
250
 * | char           | float          |
251
 * | ushort         | float          |
252
 * | short          | float          |
253
 * | uint           | float          |
254
 * | int            | float          |
255
 * | float          | float          |
256
 * | double         | double         |
257
 * | complex        | complex        |
258
 * | double complex | double complex |
259
 *
260
 * In other words, the output type is just large enough to hold the whole
261
 * range of possible values.
262
 *
263
 * ::: seealso
264
 *     [method@Image.multiply], [method@Image.linear], [method@Image.pow].
265
 *
266
 * Returns: 0 on success, -1 on error
267
 */
268
int
269
vips_divide(VipsImage *left, VipsImage *right, VipsImage **out, ...)
270
25.1k
{
271
25.1k
  va_list ap;
272
25.1k
  int result;
273
274
25.1k
  va_start(ap, out);
275
25.1k
  result = vips_call_split("divide", ap, left, right, out);
276
25.1k
  va_end(ap);
277
278
25.1k
  return result;
279
25.1k
}