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
|
// ==========================================================
// Bitmap conversion routines
//
// 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 "FreeImage.h"
#include "Utilities.h"
// ----------------------------------------------------------
// smart convert X to RGBF
// ----------------------------------------------------------
FIBITMAP * DLL_CALLCONV
FreeImage_ConvertToRGBF(FIBITMAP *dib) {
FIBITMAP *src = NULL;
FIBITMAP *dst = NULL;
if(!FreeImage_HasPixels(dib)) return NULL;
const FREE_IMAGE_TYPE src_type = FreeImage_GetImageType(dib);
// check for allowed conversions
switch(src_type) {
case FIT_BITMAP:
{
// allow conversion from 24- and 32-bit
const FREE_IMAGE_COLOR_TYPE color_type = FreeImage_GetColorType(dib);
if((color_type != FIC_RGB) && (color_type != FIC_RGBALPHA)) {
src = FreeImage_ConvertTo24Bits(dib);
if(!src) return NULL;
} else {
src = dib;
}
break;
}
case FIT_UINT16:
// allow conversion from 16-bit
src = dib;
break;
case FIT_RGB16:
// allow conversion from 48-bit RGB
src = dib;
break;
case FIT_RGBA16:
// allow conversion from 64-bit RGBA (ignore the alpha channel)
src = dib;
break;
case FIT_FLOAT:
// allow conversion from 32-bit float
src = dib;
break;
case FIT_RGBAF:
// allow conversion from 128-bit RGBAF
src = dib;
break;
case FIT_RGBF:
// RGBF type : clone the src
return FreeImage_Clone(dib);
break;
default:
return NULL;
}
// allocate dst image
const unsigned width = FreeImage_GetWidth(src);
const unsigned height = FreeImage_GetHeight(src);
dst = FreeImage_AllocateT(FIT_RGBF, width, height);
if(!dst) {
if(src != dib) {
FreeImage_Unload(src);
}
return NULL;
}
// copy metadata from src to dst
FreeImage_CloneMetadata(dst, src);
// convert from src type to RGBF
const unsigned src_pitch = FreeImage_GetPitch(src);
const unsigned dst_pitch = FreeImage_GetPitch(dst);
switch(src_type) {
case FIT_BITMAP:
{
// calculate the number of bytes per pixel (3 for 24-bit or 4 for 32-bit)
const unsigned bytespp = FreeImage_GetLine(src) / FreeImage_GetWidth(src);
const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
for(unsigned y = 0; y < height; y++) {
const BYTE *src_pixel = (BYTE*)src_bits;
FIRGBF *dst_pixel = (FIRGBF*)dst_bits;
for(unsigned x = 0; x < width; x++) {
// convert and scale to the range [0..1]
dst_pixel->red = (float)(src_pixel[FI_RGBA_RED]) / 255.0F;
dst_pixel->green = (float)(src_pixel[FI_RGBA_GREEN]) / 255.0F;
dst_pixel->blue = (float)(src_pixel[FI_RGBA_BLUE]) / 255.0F;
src_pixel += bytespp;
dst_pixel ++;
}
src_bits += src_pitch;
dst_bits += dst_pitch;
}
}
break;
case FIT_UINT16:
{
const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
for(unsigned y = 0; y < height; y++) {
const WORD *src_pixel = (WORD*)src_bits;
FIRGBF *dst_pixel = (FIRGBF*)dst_bits;
for(unsigned x = 0; x < width; x++) {
// convert and scale to the range [0..1]
const float dst_value = (float)src_pixel[x] / 65535.0F;
dst_pixel[x].red = dst_value;
dst_pixel[x].green = dst_value;
dst_pixel[x].blue = dst_value;
}
src_bits += src_pitch;
dst_bits += dst_pitch;
}
}
break;
case FIT_RGB16:
{
const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
for(unsigned y = 0; y < height; y++) {
const FIRGB16 *src_pixel = (FIRGB16*) src_bits;
FIRGBF *dst_pixel = (FIRGBF*) dst_bits;
for(unsigned x = 0; x < width; x++) {
// convert and scale to the range [0..1]
dst_pixel[x].red = (float)(src_pixel[x].red) / 65535.0F;
dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.0F;
dst_pixel[x].blue = (float)(src_pixel[x].blue) / 65535.0F;
}
src_bits += src_pitch;
dst_bits += dst_pitch;
}
}
break;
case FIT_RGBA16:
{
const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
for(unsigned y = 0; y < height; y++) {
const FIRGBA16 *src_pixel = (FIRGBA16*) src_bits;
FIRGBF *dst_pixel = (FIRGBF*) dst_bits;
for(unsigned x = 0; x < width; x++) {
// convert and scale to the range [0..1]
dst_pixel[x].red = (float)(src_pixel[x].red) / 65535.0F;
dst_pixel[x].green = (float)(src_pixel[x].green) / 65535.0F;
dst_pixel[x].blue = (float)(src_pixel[x].blue) / 65535.0F;
}
src_bits += src_pitch;
dst_bits += dst_pitch;
}
}
break;
case FIT_FLOAT:
{
const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
for(unsigned y = 0; y < height; y++) {
const float *src_pixel = (float*) src_bits;
FIRGBF *dst_pixel = (FIRGBF*) dst_bits;
for(unsigned x = 0; x < width; x++) {
// convert by copying greyscale channel to each R, G, B channels
// assume float values are in [0..1]
const float value = CLAMP(src_pixel[x], 0.0F, 1.0F);
dst_pixel[x].red = value;
dst_pixel[x].green = value;
dst_pixel[x].blue = value;
}
src_bits += src_pitch;
dst_bits += dst_pitch;
}
}
break;
case FIT_RGBAF:
{
const BYTE *src_bits = (BYTE*)FreeImage_GetBits(src);
BYTE *dst_bits = (BYTE*)FreeImage_GetBits(dst);
for(unsigned y = 0; y < height; y++) {
const FIRGBAF *src_pixel = (FIRGBAF*) src_bits;
FIRGBF *dst_pixel = (FIRGBF*) dst_bits;
for(unsigned x = 0; x < width; x++) {
// convert and skip alpha channel
dst_pixel[x].red = CLAMP(src_pixel[x].red, 0.0F, 1.0F);
dst_pixel[x].green = CLAMP(src_pixel[x].green, 0.0F, 1.0F);
dst_pixel[x].blue = CLAMP(src_pixel[x].blue, 0.0F, 1.0F);
}
src_bits += src_pitch;
dst_bits += dst_pitch;
}
}
break;
}
if(src != dib) {
FreeImage_Unload(src);
}
return dst;
}
|