summaryrefslogtreecommitdiff
path: root/im/include/im_math.h
blob: 8e9b3dd700a4e2f49c7c97740126d1f3f0abcd3b (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/** \file
 * \brief Math Utilities
 *
 * See Copyright Notice in im_lib.h
 */

#ifndef __IM_MATH_H
#define __IM_MATH_H

#include <math.h>
#include "im_util.h"

#ifdef IM_DEFMATHFLOAT
inline float acosf(float _X) {return ((float)acos((double)_X)); }
inline float asinf(float _X) {return ((float)asin((double)_X)); }
inline float atanf(float _X) {return ((float)atan((double)_X)); }
inline float atan2f(float _X, float _Y) {return ((float)atan2((double)_X, (double)_Y)); }
inline float ceilf(float _X) {return ((float)ceil((double)_X)); }
inline float cosf(float _X)  {return ((float)cos((double)_X)); }
inline float coshf(float _X) {return ((float)cosh((double)_X)); }
inline float expf(float _X) {return ((float)exp((double)_X)); }
inline float fabsf(float _X) {return ((float)fabs((double)_X)); }
inline float floorf(float _X) {return ((float)floor((double)_X)); }
inline float fmodf(float _X, float _Y) {return ((float)fmod((double)_X, (double)_Y)); }
inline float logf(float _X) {return ((float)log((double)_X)); }
inline float log10f(float _X) {return ((float)log10((double)_X)); }
inline float powf(float _X, float _Y) {return ((float)pow((double)_X, (double)_Y)); }
inline float sinf(float _X) {return ((float)sin((double)_X)); }
inline float sinhf(float _X) {return ((float)sinh((double)_X)); }
inline float sqrtf(float _X) {return ((float)sqrt((double)_X)); }
inline float tanf(float _X) {return ((float)tan((double)_X)); }
inline float tanhf(float _X) {return ((float)tanh((double)_X)); }
#endif

/** \defgroup math Math Utilities
 * \par
 * When converting between continuous and discrete use: \n
 * Continuous = Discrete + 0.5         [Reconstruction/Interpolation] \n
 * Discrete = Round(Continuous - 0.5)  [Sampling/Quantization] \n
 * \par
 * Notice that must check 0-max limits when converting from Continuous to Discrete.
 * \par
 * When converting between discrete and discrete use: \n
 * integer src_size, dst_len, src_i, dst_i            \n
 * real factor = (real)(dst_size)/(real)(src_size)    \n
 * dst_i = Round(factor*(src_i + 0.5) - 0.5)
 * \par
 * See \ref im_math.h
 * \ingroup util */


/** Round a real to the nearest integer.
 * \ingroup math */
inline int imRound(float x) 
{
  return (int)(x < 0? x-0.5f: x+0.5f);
}
inline int imRound(double x) 
{
  return (int)(x < 0? x-0.5: x+0.5);
}

/** Converts between two discrete grids.
 * factor is "dst_size/src_size".
 * \ingroup math */
inline int imResample(int x, float factor)
{
  float xr = factor*(x + 0.5f) - 0.5f;
  return (int)(xr < 0? xr-0.5f: xr+0.5f);  /* Round */
}

/** Does Zero Order Decimation (Mean).
 * \ingroup math */
template <class T, class TU>
inline T imZeroOrderDecimation(int width, int height, T *map, float xl, float yl, float box_width, float box_height, TU Dummy)
{
  int x0,x1,y0,y1;
  (void)Dummy;

  x0 = (int)floor(xl - box_width/2.0 - 0.5) + 1;
  y0 = (int)floor(yl - box_height/2.0 - 0.5) + 1;
  x1 = (int)floor(xl + box_width/2.0 - 0.5);
  y1 = (int)floor(yl + box_height/2.0 - 0.5);

  if (x0 == x1) x1++;
  if (y0 == y1) y1++;

  x0 = x0<0? 0: x0>width-1? width-1: x0;
  y0 = y0<0? 0: y0>height-1? height-1: y0;
  x1 = x1<0? 0: x1>width-1? width-1: x1;
  y1 = y1<0? 0: y1>height-1? height-1: y1;

  TU Value;
  int Count = 0;

  Value = 0;

  for (int y = y0; y <= y1; y++)
  {
    for (int x = x0; x <= x1; x++)
    {
      Value += map[y*width+x];
      Count++;
    }
  }

  if (Count == 0)
  {
    Value = 0;
    return (T)Value;
  }

  return (T)(Value/(float)Count);
}

/** Does Bilinear Decimation.
 * \ingroup math */
template <class T, class TU>
inline T imBilinearDecimation(int width, int height, T *map, float xl, float yl, float box_width, float box_height, TU Dummy)
{
  int x0,x1,y0,y1;
  (void)Dummy;

  x0 = (int)floor(xl - box_width/2.0 - 0.5) + 1;
  y0 = (int)floor(yl - box_height/2.0 - 0.5) + 1;
  x1 = (int)floor(xl + box_width/2.0 - 0.5);
  y1 = (int)floor(yl + box_height/2.0 - 0.5);

  if (x0 == x1) x1++;
  if (y0 == y1) y1++;

  x0 = x0<0? 0: x0>width-1? width-1: x0;
  y0 = y0<0? 0: y0>height-1? height-1: y0;
  x1 = x1<0? 0: x1>width-1? width-1: x1;
  y1 = y1<0? 0: y1>height-1? height-1: y1;

  TU Value, LineValue;
  float LineNorm, Norm, dxr, dyr;

  Value = 0;
  Norm = 0;

  for (int y = y0; y <= y1; y++)
  {
    dyr = yl - (y+0.5f);
    if (dyr < 0) dyr *= -1;

    LineValue = 0;
    LineNorm = 0;

    for (int x = x0; x <= x1; x++)
    {
      dxr = xl - (x+0.5f);
      if (dxr < 0) dxr *= -1;

      LineValue += map[y*width+x] * dxr;
      LineNorm += dxr;
    }

    Value += LineValue * dyr;
    Norm += dyr * LineNorm;
  }

  if (Norm == 0)
  {
    Value = 0;
    return (T)Value;
  }

  return (T)(Value/Norm);
}

/** Does Zero Order Interpolation (Nearest Neighborhood).
 * \ingroup math */
template <class T>
inline T imZeroOrderInterpolation(int width, int height, T *map, float xl, float yl)
{
  int x0 = imRound(xl-0.5f);
  int y0 = imRound(yl-0.5f);
  x0 = x0<0? 0: x0>width-1? width-1: x0;
  y0 = y0<0? 0: y0>height-1? height-1: y0;
  return map[y0*width + x0];
}

/** Does Bilinear Interpolation.
 * \ingroup math */
template <class T>
inline T imBilinearInterpolation(int width, int height, T *map, float xl, float yl)
{
  int x0, y0, x1, y1;
  float t, u;

  if (xl < 0.5)
  {
    x1 = x0 = 0; 
    t = 0;
  }
  else if (xl > width-0.5)
  {
    x1 = x0 = width-1;
    t = 0;
  }
  else
  {
    x0 = (int)(xl-0.5f);
    x1 = x0+1;
    t = xl - (x0+0.5f);
  }

  if (yl < 0.5)
  {
    y1 = y0 = 0; 
    u = 0;
  }
  else if (yl > height-0.5)
  {
    y1 = y0 = height-1;
    u = 0;
  }
  else
  {
    y0 = (int)(yl-0.5f);
    y1 = y0+1;
    u = yl - (y0+0.5f);
  }

  T fll = map[y0*width + x0];
  T fhl = map[y0*width + x1];
  T flh = map[y1*width + x0];
  T fhh = map[y1*width + x1];

  return (T)((fhh - flh - fhl + fll) * u * t +
                         (fhl - fll) * t +
                         (flh - fll) * u +
                                fll);
}

/** Does Bicubic Interpolation.
 * \ingroup math */
template <class T, class TU>
inline T imBicubicInterpolation(int width, int height, T *map, float xl, float yl, TU Dummy)
{
  int X[4], Y[4];
  float t, u;
  (void)Dummy;

  if (xl > width-0.5)
  {
    X[3] = X[2] = X[1] = width-1;
    X[0] = X[1]-1;
    t = 0;
  }
  else
  {
    X[1] = (int)(xl-0.5f);
    if (X[1] < 0) X[1] = 0;

    X[0] = X[1]-1;
    X[2] = X[1]+1;
    X[3] = X[1]+2;

    if (X[0] < 0) X[0] = 0;
    if (X[3] > width-1) X[3] = width-1;

    t = xl - (X[1]+0.5f);
  }

  if (yl > height-0.5)
  {
    Y[3] = Y[2] = Y[1] = height-1;
    Y[0] = Y[1]-1;
    u = 0;
  }
  else
  {
    Y[1] = (int)(yl-0.5f);
    if (Y[1] < 0) Y[1] = 0;

    Y[0] = Y[1]-1;
    Y[2] = Y[1]+1;
    Y[3] = Y[1]+2;

    if (Y[0] < 0) Y[0] = 0;
    if (Y[3] > height-1) Y[3] = height-1;

    u = yl - (Y[1]+0.5f);
  }

  float CX[4], CY[4];

  // Optimize calculations
  {
    float c, c2, c3;

#define C0 (-c3 + 2.0f*c2 - c)
#define C1 ( c3 - 2.0f*c2 + 1.0f)
#define C2 (-c3 + c2 + c)
#define C3 ( c3 - c2)

    c = t;
    c2 = c*c; c3 = c2*c;
    CX[0] = C0; CX[1] = C1; CX[2] = C2; CX[3] = C3;

    c = u;
    c2 = c*c; c3 = c2*c;
    CY[0] = C0; CY[1] = C1; CY[2] = C2; CY[3] = C3;

#undef C0
#undef C1
#undef C2
#undef C3
  }

  TU LineValue, Value;
  float LineNorm, Norm;

  Value = 0;
  Norm = 0;

  for (int y = 0; y < 4; y++)
  {
    LineValue = 0;
    LineNorm = 0;

    for (int x = 0; x < 4; x++)
    {
      LineValue += map[Y[y]*width+X[x]] * CX[x];
      LineNorm += CX[x];
    }

    Value += LineValue * CY[y];
    Norm += CY[y] * LineNorm;
  }

  if (Norm == 0)
  {
    Value = 0;
    return (T)Value;
  }

  Value = (Value/Norm);

  int size = sizeof(T); 
  if (size == 1)
    return (T)(Value<=(TU)0? (TU)0: Value<=(TU)255? Value: (TU)255);
  else
    return (T)(Value);
}

/** Calculates minimum and maximum values.
 * \ingroup math */
template <class T> 
inline void imMinMax(const T *map, int count, T& min, T& max)
{
  min = *map++;
  max = min;
  for (int i = 1; i < count; i++)
  {
    T value = *map++;

    if (value > max)
      max = value;
    else if (value < min)
      min = value;
  }
}

#endif