-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPFMLoader.cpp
More file actions
143 lines (116 loc) · 3.52 KB
/
PFMLoader.cpp
File metadata and controls
143 lines (116 loc) · 3.52 KB
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
#include "PFMLoader.h"
#include <stdexcept>
// define this to 1 if you are running on a big-endian machine (e.g. PPC Macs)
// #if WORDS_BIGENDIAN
namespace
{
inline void
byteSwapFloat(float& f)
{
union {float f; unsigned char b[4];} u1, u2;
u1.f = f;
u2.b[0] = u1.b[3];
u2.b[1] = u1.b[2];
u2.b[2] = u1.b[1];
u2.b[3] = u1.b[0];
f = u2.f;
}
#if WORDS_BIGENDIAN
// big endian system, need to swap bytes to convert into
// external little-endian representation
inline void littleEndianFloat(float& f) {byteSwapFloat (f);}
inline void bigEndianFloat(float) {}
#else
// little endian, no need to swap
inline void littleEndianFloat(float) {}
inline void bigEndianFloat(float& f) {byteSwapFloat(f);}
#endif
}
Vector3*
readPFMImage(const char * filename, int * width, int * height)
{
FILE *infile = 0;
float *lineBuffer = 0;
Vector3 *img = 0;
char junk;
try
{
infile = fopen(filename, "rb");
if (!infile)
throw std::runtime_error("cannot open file.");
int a = fgetc(infile);
int b = fgetc(infile);
fgetc(infile);
if ((a != 'P') || ((b != 'F') && (b != 'f')))
throw std::runtime_error("not a PFM image file.");
b = (b == 'F'); // 'F' = RGB, 'f' = monochrome
fscanf(infile, "%d %d%c", width, height, &junk);
if ((*width <= 0) || (*height <= 0))
throw std::runtime_error("invalid width or height.");
float scaleFactor;
fscanf(infile, "%f%c", &scaleFactor, &junk);
img = new Vector3[*width * *height];
a = *width * (b ? 3 : 1);
lineBuffer = new float[a];
for (int y = 0; y < *height; ++y)
{
Vector3 *cur = &img[y * *width];
if (fread(lineBuffer, sizeof(float), a, infile) != (size_t) a)
throw std::runtime_error("cannot read pixel data.");
float *temp = lineBuffer;
for (int x = 0; x < *width; x++)
{
if (b)
{ // color
(*cur)[0] = *temp++;
(*cur)[1] = *temp++;
(*cur)[2] = *temp++;
if (scaleFactor > 0.0)
{
bigEndianFloat((*cur)[0]);
bigEndianFloat((*cur)[1]);
bigEndianFloat((*cur)[2]);
}
else
{
littleEndianFloat((*cur)[0]);
littleEndianFloat((*cur)[1]);
littleEndianFloat((*cur)[2]);
}
}
else
{ // black and white
float c = *temp++;
if (scaleFactor > 0.0)
bigEndianFloat (c);
else
littleEndianFloat (c);
(*cur)[0] = (*cur)[1] = (*cur)[2] = c;
}
cur++;
}
}
delete [] lineBuffer;
fclose(infile);
return img;
}
catch (const std::exception &e)
{
printf("Unable to read image file \"%s\": %s",
filename, e.what());
delete [] lineBuffer;
delete [] img;
if (infile)
fclose (infile);
return 0;
}
catch (...)
{
printf("Unable to read image file \"%s\".", filename);
delete [] lineBuffer;
delete [] img;
if (infile)
fclose (infile);
return 0;
}
}