File size: 6,458 Bytes
be94e5d |
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 |
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "precomp.hpp"
#include "utils.hpp"
#include "grfmt_pfm.hpp"
#include <iostream>
#ifdef HAVE_IMGCODEC_PFM
namespace {
static_assert(sizeof(float) == 4, "float must be 32 bit.");
bool is_byte_order_swapped(double scale)
{
// ".pfm" format file specifies that:
// positive scale means big endianness;
// negative scale means little endianness.
#ifdef WORDS_BIGENDIAN
return scale < 0.0;
#else
return scale >= 0.0;
#endif
}
void swap_endianness(uint32_t& ui)
{
static const uint32_t A(0x000000ffU);
static const uint32_t B(0x0000ff00U);
static const uint32_t C(0x00ff0000U);
static const uint32_t D(0xff000000U);
ui = ( (ui & A) << 24 )
| ( (ui & B) << 8 )
| ( (ui & C) >> 8 )
| ( (ui & D) >> 24 );
}
template<typename T> T atoT(const std::string& s);
template<> int atoT<int>(const std::string& s) { return std::atoi(s.c_str()); }
template<> double atoT<double>(const std::string& s) { return std::atof(s.c_str()); }
template<typename T>
T read_number(cv::RLByteStream& strm)
{
// should be enough to take string representation of any number
const size_t buffer_size = 2048;
std::vector<char> buffer(buffer_size, 0);
for (size_t i = 0; i < buffer_size; ++i) {
const int intc = strm.getByte();
CV_Assert(intc >= -128 && intc < 128);
char c = static_cast<char>(intc);
if (std::isspace(c)) {
break;
}
buffer[i] = c;
}
const std::string str(buffer.begin(), buffer.end());
return atoT<T>(str);
}
template<typename T> void write_anything(cv::WLByteStream& strm, const T& t)
{
std::ostringstream ss;
ss << t;
strm.putBytes(ss.str().c_str(), static_cast<int>(ss.str().size()));
}
}
namespace cv {
PFMDecoder::~PFMDecoder()
{
}
PFMDecoder::PFMDecoder() : m_scale_factor(0), m_swap_byte_order(false)
{
m_strm.close();
}
bool PFMDecoder::readHeader()
{
if (m_buf.empty()) {
if (!m_strm.open(m_filename)) {
return false;
}
} else {
if (!m_strm.open(m_buf)) {
return false;
}
}
if (m_strm.getByte() != 'P') {
CV_Error(Error::StsError, "Unexpected file type (expected P)");
}
switch (m_strm.getByte()) {
case 'f':
m_type = CV_32FC1;
break;
case 'F':
m_type = CV_32FC3;
break;
default:
CV_Error(Error::StsError, "Unexpected file type (expected `f` or `F`)");
}
if ('\n' != m_strm.getByte()) {
CV_Error(Error::StsError, "Unexpected header format (expected line break)");
}
m_width = read_number<int>(m_strm);
m_height = read_number<int>(m_strm);
m_scale_factor = read_number<double>(m_strm);
m_swap_byte_order = is_byte_order_swapped(m_scale_factor);
return true;
}
bool PFMDecoder::readData(Mat& mat)
{
if (!m_strm.isOpened()) {
CV_Error(Error::StsError, "Unexpected status in data stream");
}
Mat buffer(mat.size(), m_type);
for (int y = m_height - 1; y >= 0; --y) {
m_strm.getBytes(buffer.ptr(y), static_cast<int>(m_width * buffer.elemSize()));
if (is_byte_order_swapped(m_scale_factor)) {
for (int i = 0; i < m_width * buffer.channels(); ++i) {
static_assert( sizeof(uint32_t) == sizeof(float),
"uint32_t and float must have same size." );
swap_endianness(buffer.ptr<uint32_t>(y)[i]);
}
}
}
if (buffer.channels() == 3) {
cv::cvtColor(buffer, buffer, cv::COLOR_BGR2RGB);
}
CV_Assert(fabs(m_scale_factor) > 0.0f);
buffer *= 1.f / fabs(m_scale_factor);
buffer.convertTo(mat, mat.type());
return true;
}
size_t PFMDecoder::signatureLength() const
{
return 3;
}
bool PFMDecoder::checkSignature( const String& signature ) const
{
return signature.size() >= 3
&& signature[0] == 'P'
&& ( signature[1] == 'f' || signature[1] == 'F' )
&& isspace(signature[2]);
}
void PFMDecoder::close()
{
// noop
}
//////////////////////////////////////////////////////////////////////////////////////////
PFMEncoder::PFMEncoder()
{
m_description = "Portable image format - float (*.pfm)";
}
PFMEncoder::~PFMEncoder()
{
}
bool PFMEncoder::isFormatSupported(int depth) const
{
// any depth will be converted into 32-bit float.
CV_UNUSED(depth);
return true;
}
bool PFMEncoder::write(const Mat& img, const std::vector<int>& params)
{
CV_UNUSED(params);
WLByteStream strm;
if (m_buf) {
if (!strm.open(*m_buf)) {
return false;
} else {
m_buf->reserve(alignSize(256 + sizeof(float) * img.channels() * img.total(), 256));
}
} else if (!strm.open(m_filename)) {
return false;
}
Mat float_img;
strm.putByte('P');
switch (img.channels()) {
case 1:
strm.putByte('f');
img.convertTo(float_img, CV_32FC1);
break;
case 3:
strm.putByte('F');
img.convertTo(float_img, CV_32FC3);
break;
default:
CV_Error(Error::StsBadArg, "Expected 1 or 3 channel image.");
}
strm.putByte('\n');
write_anything(strm, float_img.cols);
strm.putByte(' ');
write_anything(strm, float_img.rows);
strm.putByte('\n');
#ifdef WORDS_BIGENDIAN
write_anything(strm, 1.0);
#else
write_anything(strm, -1.0);
#endif
strm.putByte('\n');
// Comments are not officially supported in this file format.
// write_anything(strm, "# Generated by OpenCV " CV_VERSION "\n");
for (int y = float_img.rows - 1; y >= 0; --y)
{
if (float_img.channels() == 3) {
const float* bgr_row = float_img.ptr<float>(y);
size_t row_size = float_img.cols * float_img.channels();
std::vector<float> rgb_row(row_size);
for (int x = 0; x < float_img.cols; ++x) {
rgb_row[x*3+0] = bgr_row[x*3+2];
rgb_row[x*3+1] = bgr_row[x*3+1];
rgb_row[x*3+2] = bgr_row[x*3+0];
}
strm.putBytes( reinterpret_cast<const uchar*>(rgb_row.data()),
static_cast<int>(sizeof(float) * row_size) );
} else if (float_img.channels() == 1) {
strm.putBytes(float_img.ptr(y), sizeof(float) * float_img.cols);
}
}
return true;
}
}
#endif // HAVE_IMGCODEC_PFM
|