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
|
// ==========================================================
// PFM Loader and Writer
//
// 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"
// ==========================================================
// Internal functions
// ==========================================================
/** maximum size of a line in the header */
#define PFM_MAXLINE 256
/** Big endian / Little endian float conversion */
#define REVERSEBYTES(source, dest) \
{ \
char *j = (char *) source; \
char *dj = (char *) dest; \
dj[0] = j[3]; \
dj[1] = j[2]; \
dj[2] = j[1]; \
dj[3] = j[0]; \
}
/**
Get a line from a ASCII io stream
*/
static BOOL
pfm_get_line(FreeImageIO *io, fi_handle handle, char *buffer, int length) {
int i;
memset(buffer, 0, length);
for(i = 0; i < length; i++) {
if(!io->read_proc(&buffer[i], 1, 1, handle))
return FALSE;
if(buffer[i] == 0x0A)
break;
}
return (i < length) ? TRUE : FALSE;
}
/**
Get an integer value from the actual position pointed by handle
*/
static int
pfm_get_int(FreeImageIO *io, fi_handle handle) {
char c = 0;
BOOL firstchar;
// skip forward to start of next number
if(!io->read_proc(&c, 1, 1, handle)) throw FI_MSG_ERROR_PARSING;
while (1) {
// eat comments
if (c == '#') {
// if we're at a comment, read to end of line
firstchar = TRUE;
while (1) {
if(!io->read_proc(&c, 1, 1, handle)) throw FI_MSG_ERROR_PARSING;
if (firstchar && c == ' ') {
// loop off 1 sp after #
firstchar = FALSE;
} else if (c == '\n') {
break;
}
}
}
if (c >= '0' && c <='9') {
// we've found what we were looking for
break;
}
if(!io->read_proc(&c, 1, 1, handle)) throw FI_MSG_ERROR_PARSING;
}
// we're at the start of a number, continue until we hit a non-number
int i = 0;
while (1) {
i = (i * 10) + (c - '0');
if(!io->read_proc(&c, 1, 1, handle)) throw FI_MSG_ERROR_PARSING;
if (c < '0' || c > '9')
break;
}
return i;
}
// ==========================================================
// Plugin Interface
// ==========================================================
static int s_format_id;
// ==========================================================
// Plugin Implementation
// ==========================================================
static const char * DLL_CALLCONV
Format() {
return "PFM";
}
static const char * DLL_CALLCONV
Description() {
return "Portable floatmap";
}
static const char * DLL_CALLCONV
Extension() {
return "pfm";
}
static const char * DLL_CALLCONV
RegExpr() {
return NULL;
}
static const char * DLL_CALLCONV
MimeType() {
return "image/freeimage-pfm";
}
static BOOL DLL_CALLCONV
Validate(FreeImageIO *io, fi_handle handle) {
BYTE pfm_id1[] = { 0x50, 0x46 };
BYTE pfm_id2[] = { 0x50, 0x66 };
BYTE signature[2] = { 0, 0 };
io->read_proc(signature, 1, sizeof(pfm_id1), handle);
if (memcmp(pfm_id1, signature, sizeof(pfm_id1)) == 0)
return TRUE;
if (memcmp(pfm_id2, signature, sizeof(pfm_id2)) == 0)
return TRUE;
return FALSE;
}
static BOOL DLL_CALLCONV
SupportsExportDepth(int depth) {
return FALSE;
}
static BOOL DLL_CALLCONV
SupportsExportType(FREE_IMAGE_TYPE type) {
return (
(type == FIT_FLOAT) ||
(type == FIT_RGBF)
);
}
static BOOL DLL_CALLCONV
SupportsNoPixels() {
return TRUE;
}
// ----------------------------------------------------------
static FIBITMAP * DLL_CALLCONV
Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
char line_buffer[PFM_MAXLINE];
char id_one = 0, id_two = 0;
FIBITMAP *dib = NULL;
float *lineBuffer = NULL;
if (!handle) {
return NULL;
}
BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;
try {
FREE_IMAGE_TYPE image_type = FIT_UNKNOWN;
// Read the first two bytes of the file to determine the file format
// "PF" = color image
// "Pf" = greyscale image
io->read_proc(&id_one, 1, 1, handle);
io->read_proc(&id_two, 1, 1, handle);
if(id_one == 'P') {
if(id_two == 'F') {
image_type = FIT_RGBF;
} else if(id_two == 'f') {
image_type = FIT_FLOAT;
}
}
if(image_type == FIT_UNKNOWN) {
// signature error
throw FI_MSG_ERROR_MAGIC_NUMBER;
}
// Read the header information: width, height and the scale value
unsigned width = (unsigned) pfm_get_int(io, handle);
unsigned height = (unsigned) pfm_get_int(io, handle);
float scalefactor = 1;
BOOL bResult = pfm_get_line(io, handle, line_buffer, PFM_MAXLINE);
if(bResult) {
bResult = (sscanf(line_buffer, "%f", &scalefactor) == 1) ? TRUE : FALSE;
}
if(!bResult) {
throw "Read error: invalid PFM header";
}
// Create a new DIB
dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height);
if (dib == NULL) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
if(header_only) {
// header only mode
return dib;
}
// Read the image...
if(image_type == FIT_RGBF) {
const unsigned lineWidth = 3 * width;
lineBuffer = (float*)malloc(lineWidth * sizeof(float));
if(!lineBuffer) {
throw FI_MSG_ERROR_MEMORY;
}
for (unsigned y = 0; y < height; y++) {
FIRGBF *bits = (FIRGBF*)FreeImage_GetScanLine(dib, height - 1 - y);
if(io->read_proc(lineBuffer, sizeof(float), lineWidth, handle) != lineWidth) {
throw "Read error";
}
float *channel = lineBuffer;
if(scalefactor > 0) {
// MSB
for (unsigned x = 0; x < width; x++) {
REVERSEBYTES(channel++, &bits[x].red);
REVERSEBYTES(channel++, &bits[x].green);
REVERSEBYTES(channel++, &bits[x].blue);
}
} else {
// LSB
for (unsigned x = 0; x < width; x++) {
bits[x].red = *channel++;
bits[x].green = *channel++;
bits[x].blue = *channel++;
}
}
}
free(lineBuffer);
lineBuffer = NULL;
} else if(image_type == FIT_FLOAT) {
const unsigned lineWidth = width;
lineBuffer = (float*)malloc(lineWidth * sizeof(float));
if(!lineBuffer) {
throw FI_MSG_ERROR_MEMORY;
}
for (unsigned y = 0; y < height; y++) {
float *bits = (float*)FreeImage_GetScanLine(dib, height - 1 - y);
if(io->read_proc(lineBuffer, sizeof(float), lineWidth, handle) != lineWidth) {
throw "Read error";
}
float *channel = lineBuffer;
if(scalefactor > 0) {
// MSB - File is Big endian
for (unsigned x = 0; x < width; x++) {
REVERSEBYTES(channel++, &bits[x]);
}
} else {
// LSB - File is Little Endian
for (unsigned x = 0; x < width; x++) {
bits[x] = *channel++;
}
}
}
free(lineBuffer);
lineBuffer = NULL;
}
return dib;
} catch (const char *text) {
if(lineBuffer) free(lineBuffer);
if(dib) FreeImage_Unload(dib);
if(NULL != text) {
FreeImage_OutputMessageProc(s_format_id, text);
}
return NULL;
}
}
static BOOL DLL_CALLCONV
Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data) {
if(!dib || !handle) return FALSE;
FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
if((image_type != FIT_RGBF) && (image_type != FIT_FLOAT)) {
return FALSE;
}
unsigned width = FreeImage_GetWidth(dib);
unsigned height = FreeImage_GetHeight(dib);
unsigned lineWidth = FreeImage_GetLine(dib);
// save image as Little Endian
const float scalefactor = -1.0F;
char buffer[PFM_MAXLINE]; // temporary buffer whose size should be enough for what we need
// Find the appropriate magic number for this file type
char magic = 0;
switch(image_type) {
case FIT_RGBF:
magic = 'F'; // RGBF
break;
case FIT_FLOAT:
magic = 'f'; // float greyscale
break;
default:
return FALSE;
}
// Write the header info
sprintf(buffer, "P%c\n%d %d\n%f\n", magic, width, height, scalefactor);
io->write_proc(&buffer, (unsigned int)strlen(buffer), 1, handle);
// Write the image data
for (unsigned y = 0; y < height; y++) {
BYTE *bits = FreeImage_GetScanLine(dib, height - 1 - y);
io->write_proc(bits, 1, lineWidth, handle);
}
return TRUE;
}
// ==========================================================
// Init
// ==========================================================
void DLL_CALLCONV
InitPFM(Plugin *plugin, int format_id) {
s_format_id = format_id;
plugin->format_proc = Format;
plugin->description_proc = Description;
plugin->extension_proc = Extension;
plugin->regexpr_proc = RegExpr;
plugin->open_proc = NULL;
plugin->close_proc = NULL;
plugin->pagecount_proc = NULL;
plugin->pagecapability_proc = NULL;
plugin->load_proc = Load;
plugin->save_proc = Save;
plugin->validate_proc = Validate;
plugin->mime_proc = MimeType;
plugin->supports_export_bpp_proc = SupportsExportDepth;
plugin->supports_export_type_proc = SupportsExportType;
plugin->supports_icc_profiles_proc = NULL;
plugin->supports_no_pixels_proc = SupportsNoPixels;
}
|