35 void exportPFM(
const std::string& filename,
const bitmap<T>& data,
const std::array<ptrdiff_t,3>& channels = {0,1,2})
38 if(data.
width() == 0 || data.
height() == 0)
throw std::runtime_error(
"io::exportPFM: No data in bitmap");
41 std::ofstream ofs(filename.c_str(), std::ios::binary);
44 char header[3] = {
'P',
'F',
'\n'};
45 ofs.write(
static_cast<char *
>(header), 3);
48 const unsigned int num_channels = 3;
49 ofs << data.
width() <<
" " << data.
height() <<
'\n';
52 ofs <<
"-1.000000" <<
'\n';
56 auto buffer = std::make_unique<float[]>(
size);
58 auto buffer_ref = buffer.get();
60 for(
size_t x=0; x < data.
width(); x++, buffer_ref += 3)
65 for(
size_t c = 0; c < num_channels; c++)
66 buffer_ref[c] = (channels[c] >= 0 &&
size_t(channels[c]) < data_size) ? bbm::cast<float>(data_ref[channels[c]]) : 0.0f;
73 ofs.write(
reinterpret_cast<char*
>(buffer.get()),
size*
sizeof(
float));
91 template<
typename T,
size_t N=3>
92 void importPFM(
const std::string& filename,
bitmap<T>& data,
const std::array<ptrdiff_t,N>& channels = {0,1,2})
95 std::ifstream ifs(filename.c_str(), std::ios::binary);
99 ifs.read(
static_cast<char *
>(header), 3);
100 if(header[0] !=
'P')
throw std::runtime_error(
"io::importPFM: Not a recognized PFM format");
101 char type = header[1];
104 if(ifs.peek() ==
'#') ifs.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
107 size_t width, height;
108 ifs >> width >> height;
111 if(ifs.peek() ==
'#') ifs.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
117 bool bigEndian = (endianess > 0);
121 while(ifs.get() !=
'0') ifs.unget();
125 size_t num_channels = (type ==
'f') ? 1 : 3;
128 size_t size = width*height*num_channels;
129 auto buffer = std::make_unique<float[]>(
size);
130 ifs.read(
reinterpret_cast<char*
>(buffer.get()),
size*
sizeof(
float));
138 if(data.
width() != width || data.
height() != height)
142 auto buffer_ref = buffer.get();
143 for(
size_t y=height-1; y < height; y--)
144 for(
size_t x=0; x < width; x++, buffer_ref += num_channels)
149 for(
size_t c = 0; c < std::min(N, num_channels); c++)
150 if(channels[c] >= 0 && channels[c] < data_size)
151 data_ref[channels[c]] = buffer_ref[c];
Detect Endianess of processor and conversion methods.
size_t width() const
Definition: bitmap.h:86
size_t height() const
Definition: bitmap.h:87
void reshape(size_t width, size_t height)
reshape the bitmap (loss of old data)
Definition: bitmap.h:67
Extensions for STL iterators/ranges.
T big(const T &value)
Definition: Endian.h:80
T little(const T &value)
Definition: Endian.h:62
void exportPFM(const std::string &filename, const bitmap< T > &data, const std::array< ptrdiff_t, 3 > &channels={0, 1, 2})
Export a bitmap to a PFM.
Definition: pfm.h:35
void importPFM(const std::string &filename, bitmap< T > &data, const std::array< ptrdiff_t, N > &channels={0, 1, 2})
Import a bitmap from a PFM.
Definition: pfm.h:92
Definition: aggregatebsdf.h:29
size_t size(T &&t)
Definition: iterator_util.h:22
auto begin(T &&t)
Definition: iterator_util.h:29