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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/** \file
* \brief Image Data Type Conversion
*
* See Copyright Notice in im_lib.h
* $Id: im_converttype.cpp,v 1.1 2008/10/17 06:10:16 scuri Exp $
*/
#include "im.h"
#include "im_util.h"
#include "im_complex.h"
#include "im_image.h"
#include "im_convert.h"
#include "im_color.h"
#include "im_counter.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <memory.h>
/* IMPORTANT: leave template functions not "static"
because of some weird compiler bizarre errors.
Report on AIX C++.
*/
/* if gamma is applied then factor contains two conversions
one for applying gamma,
and other for normal destiny conversion to dst_min-dst_max range.
because gamma(0) = 0
For EXP: gamma(x) = (e^(g*x))-1
For LOG: gamma(x) = log((g*x)+1)
because gamma(1) = 1
gfactor = exp(g)-1
gfactor = log(g+1)
*/
inline float iGammaFactor(float range, float gamma)
{
if (gamma == 0)
return range;
else if (gamma < 0)
return range/float(log((-gamma) + 1));
else
return range/float(exp(gamma) - 1);
}
inline float iGammaFunc(float factor, float min, float gamma, float value)
{
// Here 0<value<1 (always)
if (gamma != 0)
{
if (gamma < 0)
value = log(value*(-gamma) + 1);
else
value = exp(value*gamma) - 1;
}
return factor*value + min;
}
inline int iIntMin()
{
return (int)(-8388608.0f);
}
inline int iIntMax()
{
return (int)(+8388607.0f);
}
/// Generic Abssolute
template <class T>
inline T iAbs(const T& v)
{
if (v <= 0)
return -1*v;
return v;
}
template <class T>
inline void iDataTypeIntMax(T& max)
{
int size_of = sizeof(T);
int data_type = (size_of == 1)? IM_BYTE: (size_of == 2)? IM_USHORT: IM_INT;
max = (T)imColorMax(data_type);
}
template <class T>
inline void iMinMaxAbs(int count, const T *map, T& min, T& max, int abssolute)
{
if (abssolute)
min = iAbs(*map++);
else
min = *map++;
max = min;
for (int i = 1; i < count; i++)
{
T value;
if (abssolute)
value = iAbs(*map++);
else
value = *map++;
if (value > max)
max = value;
else if (value < min)
min = value;
}
if (min == max)
{
max = min + 1;
if (min != 0)
min = min - 1;
}
}
template <class SRCT, class DSTT>
int iCopy(int count, const SRCT *src_map, DSTT *dst_map)
{
for (int i = 0; i < count; i++)
{
*dst_map++ = (DSTT)(*src_map++);
}
return IM_ERR_NONE;
}
template <class SRCT, class DSTT>
int iCopyCrop(int count, const SRCT *src_map, DSTT *dst_map, int abssolute)
{
SRCT value;
DSTT dst_max;
iDataTypeIntMax(dst_max);
for (int i = 0; i < count; i++)
{
if (abssolute)
value = iAbs(*src_map++);
else
value = *src_map++;
if (value > dst_max)
value = (SRCT)dst_max;
if (!(value >= 0))
value = 0;
*dst_map++ = (DSTT)(value);
}
return IM_ERR_NONE;
}
template <class SRCT>
int iPromote2Cpx(int count, const SRCT *src_map, imcfloat *dst_map)
{
for (int i = 0; i < count; i++)
{
dst_map->real = (float)(*src_map++);
dst_map++;
}
return IM_ERR_NONE;
}
template <class SRCT, class DSTT>
int iConvertInt2Int(int count, const SRCT *src_map, DSTT *dst_map, int abssolute, int cast_mode, int counter)
{
SRCT min, max;
if (cast_mode == IM_CAST_MINMAX)
{
iMinMaxAbs(count, src_map, min, max, abssolute);
if (min >= 0 && max <= 255)
{
min = 0;
max = 255;
}
}
else
{
min = 0;
iDataTypeIntMax(max);
}
DSTT dst_max;
iDataTypeIntMax(dst_max);
float factor = ((float)dst_max + 1.0f) / ((float)max - (float)min + 1.0f);
for (int i = 0; i < count; i++)
{
SRCT value;
if (abssolute)
value = iAbs(*src_map++);
else
value = *src_map++;
if (value >= max)
*dst_map++ = dst_max;
else if (value <= min)
*dst_map++ = 0;
else
*dst_map++ = (DSTT)imResample(value - min, factor);
if (!imCounterInc(counter))
return IM_ERR_COUNTER;
}
return IM_ERR_NONE;
}
template <class SRCT>
int iPromoteInt2Real(int count, const SRCT *src_map, float *dst_map, float gamma, int abssolute, int cast_mode, int counter)
{
SRCT min, max;
if (cast_mode == IM_CAST_MINMAX)
{
iMinMaxAbs(count, src_map, min, max, abssolute);
if (min >= 0 && max <= 255)
{
min = 0;
max = 255;
}
}
else
{
min = 0;
iDataTypeIntMax(max);
if (max == 16777215 && !abssolute) /* IM_INT */
{
min = (SRCT)iIntMin();
max = (SRCT)iIntMax();
}
}
float range = float(max - min + 1);
float dst_min = 0.0f;
float dst_max = 1.0f;
int size_of = sizeof(SRCT);
if (size_of == 4 && !abssolute)
{
dst_min = -0.5f;
dst_max = +0.5f;
}
gamma = -gamma; // gamma is inverted here, because we are promoting int2real
float factor = iGammaFactor(1.0f, gamma);
for (int i = 0; i < count; i++)
{
float fvalue;
if (abssolute)
fvalue = (iAbs(*src_map++) - min + 0.5f)/range;
else
fvalue = (*src_map++ - min + 0.5f)/range;
// Now 0 <= value <= 1 (if min-max are correct)
if (fvalue >= 1)
*dst_map++ = dst_max;
else if (fvalue <= 0)
*dst_map++ = dst_min;
else
*dst_map++ = iGammaFunc(factor, dst_min, gamma, fvalue);
if (!imCounterInc(counter))
return IM_ERR_COUNTER;
}
return IM_ERR_NONE;
}
template <class DSTT>
int iDemoteReal2Int(int count, const float *src_map, DSTT *dst_map, float gamma, int abssolute, int cast_mode, int counter)
{
float min, max;
DSTT dst_min = 0, dst_max;
iDataTypeIntMax(dst_max);
if (dst_max == 16777215 && !abssolute) /* IM_INT */
{
dst_min = (DSTT)iIntMin();
dst_max = (DSTT)iIntMax();
}
if (cast_mode == IM_CAST_MINMAX)
iMinMaxAbs(count, src_map, min, max, abssolute);
else
{
min = 0;
max = 1;
}
int dst_range = dst_max - dst_min + 1;
float range = max - min;
float factor = iGammaFactor((float)dst_range, gamma);
for (int i = 0; i < count; i++)
{
float value;
if (abssolute)
value = ((float)iAbs(*src_map++) - min)/range;
else
value = (*src_map++ - min)/range;
// Now 0 <= value <= 1 (if min-max are correct)
if (value >= 1)
*dst_map++ = dst_max;
else if (value <= 0)
*dst_map++ = dst_min;
else
{
value = iGammaFunc(factor, (float)dst_min, gamma, value);
if (value >= dst_max)
*dst_map++ = dst_max;
else if (value <= dst_min)
*dst_map++ = dst_min;
else
*dst_map++ = (DSTT)imRound(value - 0.5f);
}
if (!imCounterInc(counter))
return IM_ERR_COUNTER;
}
return IM_ERR_NONE;
}
int iDemoteCpx2Real(int count, const imcfloat* src_map, float *dst_map, int cpx2real)
{
float (*CpxCnv)(const imcfloat& cpx) = NULL;
switch(cpx2real)
{
case IM_CPX_REAL: CpxCnv = cpxreal; break;
case IM_CPX_IMAG: CpxCnv = cpximag; break;
case IM_CPX_MAG: CpxCnv = cpxmag; break;
case IM_CPX_PHASE: CpxCnv = cpxphase; break;
}
for (int i = 0; i < count; i++)
{
*dst_map++ = CpxCnv(*src_map++);
}
return IM_ERR_NONE;
}
template <class DSTT>
int iDemoteCpx2Int(int count, const imcfloat* src_map, DSTT *dst_map, int cpx2real, float gamma, int abssolute, int cast_mode, int counter)
{
float* real_map = (float*)malloc(count*sizeof(float));
if (!real_map) return IM_ERR_MEM;
iDemoteCpx2Real(count, src_map, real_map, cpx2real);
if (iDemoteReal2Int(count, real_map, dst_map, gamma, abssolute, cast_mode, counter) != IM_ERR_NONE)
{
free(real_map);
return IM_ERR_COUNTER;
}
free(real_map);
return IM_ERR_NONE;
}
template <class SRCT>
int iPromoteInt2Cpx(int count, const SRCT* src_map, imcfloat *dst_map, float gamma, int abssolute, int cast_mode, int counter)
{
float* real_map = (float*)malloc(count*sizeof(float));
if (!real_map) return IM_ERR_MEM;
if (iPromoteInt2Real(count, src_map, real_map, gamma, abssolute, cast_mode, counter) != IM_ERR_NONE)
{
free(real_map);
return IM_ERR_COUNTER;
}
iPromote2Cpx(count, real_map, dst_map);
free(real_map);
return IM_ERR_NONE;
}
int imConvertDataType(const imImage* src_image, imImage* dst_image, int cpx2real, float gamma, int abssolute, int cast_mode)
{
assert(src_image);
assert(dst_image);
if (!imImageMatchColorSpace(src_image, dst_image))
return IM_ERR_DATA;
if (src_image->data_type == dst_image->data_type)
return IM_ERR_DATA;
int total_count = src_image->depth * src_image->count;
int ret = IM_ERR_DATA;
int counter = imCounterBegin("Convert Data Type");
char msg[50];
sprintf(msg, "Converting to %s...", imDataTypeName(dst_image->data_type));
imCounterTotal(counter, total_count, msg);
switch(src_image->data_type)
{
case IM_BYTE:
switch(dst_image->data_type)
{
case IM_USHORT:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopy(total_count, (const imbyte*)src_image->data[0], (imushort*)dst_image->data[0]);
else
ret = iConvertInt2Int(total_count, (const imbyte*)src_image->data[0], (imushort*)dst_image->data[0], abssolute, cast_mode, counter);
break;
case IM_INT:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopy(total_count, (const imbyte*)src_image->data[0], (int*)dst_image->data[0]);
else
ret = iConvertInt2Int(total_count, (const imbyte*)src_image->data[0], (int*)dst_image->data[0], abssolute, cast_mode, counter);
break;
case IM_FLOAT:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopy(total_count, (const imbyte*)src_image->data[0], (float*)dst_image->data[0]);
else
ret = iPromoteInt2Real(total_count, (const imbyte*)src_image->data[0], (float*)dst_image->data[0], gamma, abssolute, cast_mode, counter);
break;
case IM_CFLOAT:
if (cast_mode == IM_CAST_DIRECT)
ret = iPromote2Cpx(total_count, (const imbyte*)src_image->data[0], (imcfloat*)dst_image->data[0]);
else
ret = iPromoteInt2Cpx(total_count, (const imbyte*)src_image->data[0], (imcfloat*)dst_image->data[0], gamma, abssolute, cast_mode, counter);
break;
}
break;
case IM_USHORT:
switch(dst_image->data_type)
{
case IM_BYTE:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopyCrop(total_count, (const imushort*)src_image->data[0], (imbyte*)dst_image->data[0], abssolute);
else
ret = iConvertInt2Int(total_count, (const imushort*)src_image->data[0], (imbyte*)dst_image->data[0], abssolute, cast_mode, counter);
break;
case IM_INT:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopy(total_count, (const imushort*)src_image->data[0], (int*)dst_image->data[0]);
else
ret = iConvertInt2Int(total_count, (const imushort*)src_image->data[0], (int*)dst_image->data[0], abssolute, cast_mode, counter);
break;
case IM_FLOAT:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopy(total_count, (const imushort*)src_image->data[0], (float*)dst_image->data[0]);
else
ret = iPromoteInt2Real(total_count, (const imushort*)src_image->data[0], (float*)dst_image->data[0], gamma, abssolute, cast_mode, counter);
break;
case IM_CFLOAT:
if (cast_mode == IM_CAST_DIRECT)
ret = iPromote2Cpx(total_count, (const imushort*)src_image->data[0], (imcfloat*)dst_image->data[0]);
else
ret = iPromoteInt2Cpx(total_count, (const imushort*)src_image->data[0], (imcfloat*)dst_image->data[0], gamma, abssolute, cast_mode, counter);
break;
}
break;
case IM_INT:
switch(dst_image->data_type)
{
case IM_BYTE:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopyCrop(total_count, (const int*)src_image->data[0], (imbyte*)dst_image->data[0], abssolute);
else
ret = iConvertInt2Int(total_count, (const int*)src_image->data[0], (imbyte*)dst_image->data[0], abssolute, cast_mode, counter);
break;
case IM_USHORT:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopyCrop(total_count, (const int*)src_image->data[0], (imushort*)dst_image->data[0], abssolute);
else
ret = iConvertInt2Int(total_count, (const int*)src_image->data[0], (imushort*)dst_image->data[0], abssolute, cast_mode, counter);
break;
case IM_FLOAT:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopy(total_count, (const int*)src_image->data[0], (float*)dst_image->data[0]);
else
ret = iPromoteInt2Real(total_count, (const int*)src_image->data[0], (float*)dst_image->data[0], gamma, abssolute, cast_mode, counter);
break;
case IM_CFLOAT:
if (cast_mode == IM_CAST_DIRECT)
ret = iPromote2Cpx(total_count, (const int*)src_image->data[0], (imcfloat*)dst_image->data[0]);
else
ret = iPromoteInt2Cpx(total_count, (const int*)src_image->data[0], (imcfloat*)dst_image->data[0], gamma, abssolute, cast_mode, counter);
break;
}
break;
case IM_FLOAT:
switch(dst_image->data_type)
{
case IM_BYTE:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopyCrop(total_count, (const float*)src_image->data[0], (imbyte*)dst_image->data[0], abssolute);
else
ret = iDemoteReal2Int(total_count, (const float*)src_image->data[0], (imbyte*)dst_image->data[0], gamma, abssolute, cast_mode, counter);
break;
case IM_USHORT:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopyCrop(total_count, (const float*)src_image->data[0], (imushort*)dst_image->data[0], abssolute);
else
ret = iDemoteReal2Int(total_count, (const float*)src_image->data[0], (imushort*)dst_image->data[0], gamma, abssolute, cast_mode, counter);
break;
case IM_INT:
if (cast_mode == IM_CAST_DIRECT)
ret = iCopy(total_count, (const float*)src_image->data[0], (int*)dst_image->data[0]);
else
ret = iDemoteReal2Int(total_count, (const float*)src_image->data[0], (int*)dst_image->data[0], gamma, abssolute, cast_mode, counter);
break;
case IM_CFLOAT:
ret = iPromote2Cpx(total_count, (const float*)src_image->data[0], (imcfloat*)dst_image->data[0]);
break;
}
break;
case IM_CFLOAT:
switch(dst_image->data_type)
{
case IM_BYTE:
ret = iDemoteCpx2Int(total_count, (const imcfloat*)src_image->data[0], (imbyte*)dst_image->data[0], cpx2real, gamma, abssolute, cast_mode, counter);
break;
case IM_USHORT:
ret = iDemoteCpx2Int(total_count, (const imcfloat*)src_image->data[0], (imushort*)dst_image->data[0], cpx2real, gamma, abssolute, cast_mode, counter);
break;
case IM_INT:
ret = iDemoteCpx2Int(total_count, (const imcfloat*)src_image->data[0], (int*)dst_image->data[0], cpx2real, gamma, abssolute, cast_mode, counter);
break;
case IM_FLOAT:
ret = iDemoteCpx2Real(total_count, (const imcfloat*)src_image->data[0], (float*)dst_image->data[0], cpx2real);
break;
}
break;
}
imCounterEnd(counter);
return ret;
}
|