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
|
// ==========================================================
// Channel processing support
//
// Design and implementation by
// - Hervé Drolon (drolon@infonie.fr)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
#include "../stdafx.h"
/** @brief Retrieves the red, green, blue or alpha channel of a BGR[A] image.
@param src Input image to be processed.
@param channel Color channel to extract
@return Returns the extracted channel if successful, returns NULL otherwise.
*/
FIBITMAP * DLL_CALLCONV
FreeImage_GetChannel(FIBITMAP *src, FREE_IMAGE_COLOR_CHANNEL channel) {
if(!FreeImage_HasPixels(src)) return NULL;
FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(src);
unsigned bpp = FreeImage_GetBPP(src);
// 24- or 32-bit
if(image_type == FIT_BITMAP && ((bpp == 24) || (bpp == 32))) {
int c;
// select the channel to extract
switch(channel) {
case FICC_BLUE:
c = FI_RGBA_BLUE;
break;
case FICC_GREEN:
c = FI_RGBA_GREEN;
break;
case FICC_RED:
c = FI_RGBA_RED;
break;
case FICC_ALPHA:
if(bpp != 32) return NULL;
c = FI_RGBA_ALPHA;
break;
default:
return NULL;
}
// allocate a 8-bit dib
unsigned width = FreeImage_GetWidth(src);
unsigned height = FreeImage_GetHeight(src);
FIBITMAP *dst = FreeImage_Allocate(width, height, 8) ;
if(!dst) return NULL;
// build a greyscale palette
RGBQUAD *pal = FreeImage_GetPalette(dst);
for(int i = 0; i < 256; i++) {
pal[i].rgbBlue = pal[i].rgbGreen = pal[i].rgbRed = (uint8_t)i;
}
// perform extraction
int bytespp = bpp / 8; // bytes / pixel
for(unsigned y = 0; y < height; y++) {
uint8_t *src_bits = FreeImage_GetScanLine(src, y);
uint8_t *dst_bits = FreeImage_GetScanLine(dst, y);
for(unsigned x = 0; x < width; x++) {
dst_bits[x] = src_bits[c];
src_bits += bytespp;
}
}
// copy metadata from src to dst
FreeImage_CloneMetadata(dst, src);
return dst;
}
// 48-bit RGB or 64-bit RGBA images
if((image_type == FIT_RGB16) || (image_type == FIT_RGBA16)) {
int c;
// select the channel to extract (always RGB[A])
switch(channel) {
case FICC_BLUE:
c = 2;
break;
case FICC_GREEN:
c = 1;
break;
case FICC_RED:
c = 0;
break;
case FICC_ALPHA:
if(bpp != 64) return NULL;
c = 3;
break;
default:
return NULL;
}
// allocate a greyscale dib
unsigned width = FreeImage_GetWidth(src);
unsigned height = FreeImage_GetHeight(src);
FIBITMAP *dst = FreeImage_AllocateT(FIT_UINT16, width, height) ;
if(!dst) return NULL;
// perform extraction
int bytespp = bpp / 16; // words / pixel
for(unsigned y = 0; y < height; y++) {
unsigned short *src_bits = (unsigned short*)FreeImage_GetScanLine(src, y);
unsigned short *dst_bits = (unsigned short*)FreeImage_GetScanLine(dst, y);
for(unsigned x = 0; x < width; x++) {
dst_bits[x] = src_bits[c];
src_bits += bytespp;
}
}
// copy metadata from src to dst
FreeImage_CloneMetadata(dst, src);
return dst;
}
// 96-bit RGBF or 128-bit RGBAF images
if((image_type == FIT_RGBF) || (image_type == FIT_RGBAF)) {
int c;
// select the channel to extract (always RGB[A])
switch(channel) {
case FICC_BLUE:
c = 2;
break;
case FICC_GREEN:
c = 1;
break;
case FICC_RED:
c = 0;
break;
case FICC_ALPHA:
if(bpp != 128) return NULL;
c = 3;
break;
default:
return NULL;
}
// allocate a greyscale dib
unsigned width = FreeImage_GetWidth(src);
unsigned height = FreeImage_GetHeight(src);
FIBITMAP *dst = FreeImage_AllocateT(FIT_FLOAT, width, height) ;
if(!dst) return NULL;
// perform extraction
int bytespp = bpp / 32; // floats / pixel
for(unsigned y = 0; y < height; y++) {
float *src_bits = (float*)FreeImage_GetScanLine(src, y);
float *dst_bits = (float*)FreeImage_GetScanLine(dst, y);
for(unsigned x = 0; x < width; x++) {
dst_bits[x] = src_bits[c];
src_bits += bytespp;
}
}
// copy metadata from src to dst
FreeImage_CloneMetadata(dst, src);
return dst;
}
return NULL;
}
/** @brief Insert a greyscale dib into a RGB[A] image.
Both src and dst must have the same width and height.
@param dst Image to modify (RGB or RGBA)
@param src Input greyscale image to insert
@param channel Color channel to modify
@return Returns TRUE if successful, FALSE otherwise.
*/
BOOL DLL_CALLCONV
FreeImage_SetChannel(FIBITMAP *dst, FIBITMAP *src, FREE_IMAGE_COLOR_CHANNEL channel) {
int c;
if(!FreeImage_HasPixels(src) || !FreeImage_HasPixels(dst)) return FALSE;
// src and dst images should have the same width and height
unsigned src_width = FreeImage_GetWidth(src);
unsigned src_height = FreeImage_GetHeight(src);
unsigned dst_width = FreeImage_GetWidth(dst);
unsigned dst_height = FreeImage_GetHeight(dst);
if((src_width != dst_width) || (src_height != dst_height))
return FALSE;
// src image should be grayscale, dst image should be RGB or RGBA
FREE_IMAGE_COLOR_TYPE src_type = FreeImage_GetColorType(src);
FREE_IMAGE_COLOR_TYPE dst_type = FreeImage_GetColorType(dst);
if((dst_type != FIC_RGB) && (dst_type != FIC_RGBALPHA) || (src_type != FIC_MINISBLACK)) {
return FALSE;
}
FREE_IMAGE_TYPE src_image_type = FreeImage_GetImageType(src);
FREE_IMAGE_TYPE dst_image_type = FreeImage_GetImageType(dst);
if((dst_image_type == FIT_BITMAP) && (src_image_type == FIT_BITMAP)) {
// src image should be grayscale, dst image should be 24- or 32-bit
unsigned src_bpp = FreeImage_GetBPP(src);
unsigned dst_bpp = FreeImage_GetBPP(dst);
if((src_bpp != 8) || (dst_bpp != 24) && (dst_bpp != 32))
return FALSE;
// select the channel to modify
switch(channel) {
case FICC_BLUE:
c = FI_RGBA_BLUE;
break;
case FICC_GREEN:
c = FI_RGBA_GREEN;
break;
case FICC_RED:
c = FI_RGBA_RED;
break;
case FICC_ALPHA:
if(dst_bpp != 32) return FALSE;
c = FI_RGBA_ALPHA;
break;
default:
return FALSE;
}
// perform insertion
int bytespp = dst_bpp / 8; // bytes / pixel
for(unsigned y = 0; y < dst_height; y++) {
uint8_t *src_bits = FreeImage_GetScanLine(src, y);
uint8_t *dst_bits = FreeImage_GetScanLine(dst, y);
for(unsigned x = 0; x < dst_width; x++) {
dst_bits[c] = src_bits[x];
dst_bits += bytespp;
}
}
return TRUE;
}
if(((dst_image_type == FIT_RGB16) || (dst_image_type == FIT_RGBA16)) && (src_image_type == FIT_UINT16)) {
// src image should be grayscale, dst image should be 48- or 64-bit
unsigned src_bpp = FreeImage_GetBPP(src);
unsigned dst_bpp = FreeImage_GetBPP(dst);
if((src_bpp != 16) || (dst_bpp != 48) && (dst_bpp != 64))
return FALSE;
// select the channel to modify (always RGB[A])
switch(channel) {
case FICC_BLUE:
c = 2;
break;
case FICC_GREEN:
c = 1;
break;
case FICC_RED:
c = 0;
break;
case FICC_ALPHA:
if(dst_bpp != 64) return FALSE;
c = 3;
break;
default:
return FALSE;
}
// perform insertion
int bytespp = dst_bpp / 16; // words / pixel
for(unsigned y = 0; y < dst_height; y++) {
unsigned short *src_bits = (unsigned short*)FreeImage_GetScanLine(src, y);
unsigned short *dst_bits = (unsigned short*)FreeImage_GetScanLine(dst, y);
for(unsigned x = 0; x < dst_width; x++) {
dst_bits[c] = src_bits[x];
dst_bits += bytespp;
}
}
return TRUE;
}
if(((dst_image_type == FIT_RGBF) || (dst_image_type == FIT_RGBAF)) && (src_image_type == FIT_FLOAT)) {
// src image should be grayscale, dst image should be 96- or 128-bit
unsigned src_bpp = FreeImage_GetBPP(src);
unsigned dst_bpp = FreeImage_GetBPP(dst);
if((src_bpp != 32) || (dst_bpp != 96) && (dst_bpp != 128))
return FALSE;
// select the channel to modify (always RGB[A])
switch(channel) {
case FICC_BLUE:
c = 2;
break;
case FICC_GREEN:
c = 1;
break;
case FICC_RED:
c = 0;
break;
case FICC_ALPHA:
if(dst_bpp != 128) return FALSE;
c = 3;
break;
default:
return FALSE;
}
// perform insertion
int bytespp = dst_bpp / 32; // floats / pixel
for(unsigned y = 0; y < dst_height; y++) {
float *src_bits = (float*)FreeImage_GetScanLine(src, y);
float *dst_bits = (float*)FreeImage_GetScanLine(dst, y);
for(unsigned x = 0; x < dst_width; x++) {
dst_bits[c] = src_bits[x];
dst_bits += bytespp;
}
}
return TRUE;
}
return FALSE;
}
/** @brief Retrieves the real part, imaginary part, magnitude or phase of a complex image.
@param src Input image to be processed.
@param channel Channel to extract
@return Returns the extracted channel if successful, returns NULL otherwise.
*/
FIBITMAP * DLL_CALLCONV
FreeImage_GetComplexChannel(FIBITMAP *src, FREE_IMAGE_COLOR_CHANNEL channel) {
unsigned x, y;
double mag, phase;
FICOMPLEX *src_bits = NULL;
double *dst_bits = NULL;
FIBITMAP *dst = NULL;
if(!FreeImage_HasPixels(src)) return NULL;
if(FreeImage_GetImageType(src) == FIT_COMPLEX) {
// allocate a dib of type FIT_DOUBLE
unsigned width = FreeImage_GetWidth(src);
unsigned height = FreeImage_GetHeight(src);
dst = FreeImage_AllocateT(FIT_DOUBLE, width, height) ;
if(!dst) return NULL;
// perform extraction
switch(channel) {
case FICC_REAL: // real part
for(y = 0; y < height; y++) {
src_bits = (FICOMPLEX *)FreeImage_GetScanLine(src, y);
dst_bits = (double *)FreeImage_GetScanLine(dst, y);
for(x = 0; x < width; x++) {
dst_bits[x] = src_bits[x].r;
}
}
break;
case FICC_IMAG: // imaginary part
for(y = 0; y < height; y++) {
src_bits = (FICOMPLEX *)FreeImage_GetScanLine(src, y);
dst_bits = (double *)FreeImage_GetScanLine(dst, y);
for(x = 0; x < width; x++) {
dst_bits[x] = src_bits[x].i;
}
}
break;
case FICC_MAG: // magnitude
for(y = 0; y < height; y++) {
src_bits = (FICOMPLEX *)FreeImage_GetScanLine(src, y);
dst_bits = (double *)FreeImage_GetScanLine(dst, y);
for(x = 0; x < width; x++) {
mag = src_bits[x].r * src_bits[x].r + src_bits[x].i * src_bits[x].i;
dst_bits[x] = sqrt(mag);
}
}
break;
case FICC_PHASE: // phase
for(y = 0; y < height; y++) {
src_bits = (FICOMPLEX *)FreeImage_GetScanLine(src, y);
dst_bits = (double *)FreeImage_GetScanLine(dst, y);
for(x = 0; x < width; x++) {
if((src_bits[x].r == 0) && (src_bits[x].i == 0)) {
phase = 0;
} else {
phase = atan2(src_bits[x].i, src_bits[x].r);
}
dst_bits[x] = phase;
}
}
break;
}
}
// copy metadata from src to dst
FreeImage_CloneMetadata(dst, src);
return dst;
}
/** @brief Set the real or imaginary part of a complex image.
Both src and dst must have the same width and height.
@param dst Image to modify (image of type FIT_COMPLEX)
@param src Input image of type FIT_DOUBLE
@param channel Channel to modify
@return Returns TRUE if successful, FALSE otherwise.
*/
BOOL DLL_CALLCONV
FreeImage_SetComplexChannel(FIBITMAP *dst, FIBITMAP *src, FREE_IMAGE_COLOR_CHANNEL channel) {
unsigned x, y;
double *src_bits = NULL;
FICOMPLEX *dst_bits = NULL;
if(!FreeImage_HasPixels(src) || !FreeImage_HasPixels(dst)) return FALSE;
// src image should be of type FIT_DOUBLE, dst image should be of type FIT_COMPLEX
const FREE_IMAGE_TYPE src_type = FreeImage_GetImageType(src);
const FREE_IMAGE_TYPE dst_type = FreeImage_GetImageType(dst);
if((src_type != FIT_DOUBLE) || (dst_type != FIT_COMPLEX))
return FALSE;
// src and dst images should have the same width and height
unsigned src_width = FreeImage_GetWidth(src);
unsigned src_height = FreeImage_GetHeight(src);
unsigned dst_width = FreeImage_GetWidth(dst);
unsigned dst_height = FreeImage_GetHeight(dst);
if((src_width != dst_width) || (src_height != dst_height))
return FALSE;
// select the channel to modify
switch(channel) {
case FICC_REAL: // real part
for(y = 0; y < dst_height; y++) {
src_bits = (double *)FreeImage_GetScanLine(src, y);
dst_bits = (FICOMPLEX *)FreeImage_GetScanLine(dst, y);
for(x = 0; x < dst_width; x++) {
dst_bits[x].r = src_bits[x];
}
}
break;
case FICC_IMAG: // imaginary part
for(y = 0; y < dst_height; y++) {
src_bits = (double *)FreeImage_GetScanLine(src, y);
dst_bits = (FICOMPLEX *)FreeImage_GetScanLine(dst, y);
for(x = 0; x < dst_width; x++) {
dst_bits[x].i = src_bits[x];
}
}
break;
}
return TRUE;
}
|