-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPFM.cpp
184 lines (147 loc) · 4.5 KB
/
PFM.cpp
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
//
// Copyright (C) Wojciech Jarosz <wjarosz@gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can
// be found in the LICENSE.txt file.
//
#include "PFM.h"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <cmath>
#include <cstdint>
using namespace std;
namespace
{
float reinterpretAsHostEndian(float f, bool bigEndian)
{
static_assert(sizeof(float) == sizeof(unsigned int), "Sizes must match");
const auto* uchar = (const unsigned char *) &f;
uint32_t i;
if (bigEndian)
i = (uchar[3]<<0) | (uchar[2]<<8) | (uchar[1]<<16) | (uchar[0]<<24);
else
i = (uchar[0]<<0) | (uchar[1]<<8) | (uchar[2]<<16) | (uchar[3]<<24);
float ret;
memcpy(&ret, &i, sizeof(float));
return ret;
}
} // end namespace
bool isPFMImage(const char *filename) noexcept
{
FILE *f = nullptr;
int numInputsRead = 0;
try
{
f = fopen(filename, "rb");
if (!f)
throw runtime_error("loadPFMImage: Error opening");
char buffer[1024];
numInputsRead = fscanf(f, "%2s\n", buffer);
if (numInputsRead != 1)
throw runtime_error("loadPFMImage: Could not read number of channels in header");
if (!(strcmp(buffer, "Pf") == 0 || strcmp(buffer, "PF") == 0))
throw runtime_error("loadPFMImage: Cannot deduce number of channels from header");
int width, height;
numInputsRead = fscanf(f, "%d%d", &width, &height);
if (numInputsRead != 2 || width <= 0 || height <= 0)
throw runtime_error("loadPFMImage: Invalid image width or height");
float scale;
numInputsRead = fscanf(f, "%f", &scale);
if (numInputsRead != 1)
throw runtime_error("loadPFMImage: Invalid file endianness. Big-Endian files not supported");
fclose(f);
return true;
}
catch (const exception & e)
{
fclose(f);
return false;
}
}
float * loadPFMImage(const char *filename, int *width, int *height, int *numChannels)
{
float * data = nullptr;
FILE *f = nullptr;
int numInputsRead = 0;
try
{
f = fopen(filename, "rb");
if (!f)
throw runtime_error("loadPFMImage: Error opening");
char buffer[1024];
numInputsRead = fscanf(f, "%2s\n", buffer);
if (numInputsRead != 1)
throw runtime_error("loadPFMImage: Could not read number of channels in header");
if (strcmp(buffer, "Pf") == 0)
*numChannels = 1;
else if (strcmp(buffer, "PF") == 0)
*numChannels = 3;
else
throw runtime_error("loadPFMImage: Cannot deduce number of channels from header");
numInputsRead = fscanf(f, "%d%d", width, height);
if (numInputsRead != 2 || *width <= 0 || *height <= 0)
throw runtime_error("loadPFMImage: Invalid image width or height");
float scale;
numInputsRead = fscanf(f, "%f", &scale);
if (numInputsRead != 1)
throw runtime_error("loadPFMImage: Invalid file endianness. Big-Endian files not supported");
bool bigEndian = scale > 0.0f;
data = new float[(*width) * (*height) * 3];
if (fread(data, 1, 1, f) != 1)
throw runtime_error("loadPFMImage: Unknown error");
size_t numFloats = static_cast<size_t>((*width) * (*height) * (*numChannels));
if (fread(data, sizeof(float), numFloats, f) != numFloats)
throw runtime_error("loadPFMImage: Could not read all pixel data");
// multiply data by scale factor
scale = fabsf(scale);
for (size_t i = 0; i < numFloats; ++i)
data[i] = scale*reinterpretAsHostEndian(data[i], bigEndian);
fclose(f);
return data;
}
catch (const runtime_error & e)
{
fclose(f);
delete [] data;
throw runtime_error(string(e.what()) + " in file '" + filename + "'");
}
}
bool writePFMImage(const char *filename, int width, int height, int numChannels, const float *data)
{
FILE *f = fopen(filename, "wb");
if (!f)
{
cerr << "writePFMImage: Error opening file '" << filename << "'" << endl;
return false;
}
fprintf(f, numChannels == 1 ? "Pf\n" : "PF\n");
fprintf(f, "%d %d\n", width, height);
// determine system endianness
bool littleEndian = false;
{
int n = 1;
// little endian if true
if (*(char *)&n == 1)
littleEndian = true;
}
fprintf(f, littleEndian ? "-1.0000000\n" : "1.0000000\n");
if (numChannels == 3 || numChannels == 1)
{
fwrite(&data[0], width * height * sizeof(float) * numChannels, 1, f);
}
else if (numChannels == 4)
{
for (int i = 0; i < width * height * 4; i += 4)
fwrite(&data[i], sizeof(float) * 3, 1, f);
}
else
{
fclose(f);
cerr << "writePFMImage: Unsupported number of channels "
<< numChannels << " when writing file '" << filename << "'" << endl;
return false;
}
fclose(f);
return true;
}