Merge pull request #12192 from pasbi:pfm

* created new decoder and encoder for PFM

pfm file format stores binary RGB or grayscale float images.

* added test for pfm codec

* replaced large with short licence header

* little/big-endian-check is now compile time

* fixed width/height confusion, improved big/little endian recognition, fixed scaling issue and Improved signature check

* adapted tests to handle float images well

* removed data-dependency: lena.pfm

the lena image is now loaded is pam and converted to pfm.

* fixed bug in endianess detection macro

* Added endianess detection for android and win

* removed fancy endianess detection

endianess detection will be implemented in cmake scripts soonish.

* fixed minor warnings

* fixed stupid elif defined bug

* silenced some implicit cast warnings

* replaced std::to_string with std::stringstream solution

std::to_string variant did not build on android.

* replaced new endianess macros with existing ones

* improved readability
This commit is contained in:
pasbi 2018-08-13 12:14:12 +02:00 committed by Alexander Alekhin
parent 4eb2966559
commit 9f5f64e14e
8 changed files with 376 additions and 2 deletions

View File

@ -282,6 +282,7 @@ OCV_OPTION(WITH_PROTOBUF "Enable libprotobuf" ON
OCV_OPTION(WITH_IMGCODEC_HDR "Include HDR support" ON)
OCV_OPTION(WITH_IMGCODEC_SUNRASTER "Include SUNRASTER support" ON)
OCV_OPTION(WITH_IMGCODEC_PXM "Include PNM (PBM,PGM,PPM) and PAM formats support" ON)
OCV_OPTION(WITH_IMGCODEC_PFM "Include PFM formats support" ON)
# OpenCV build components
# ===================================================
@ -1227,6 +1228,10 @@ if(WITH_IMGCODEC_PXM OR DEFINED HAVE_IMGCODEC_PXM)
status(" PXM:" HAVE_IMGCODEC_PXM THEN "YES" ELSE "NO")
endif()
if(WITH_IMGCODEC_PFM OR DEFINED HAVE_IMGCODEC_PFM)
status(" PFM:" HAVE_IMGCODEC_PFM THEN "YES" ELSE "NO")
endif()
# ========================== VIDEO IO ==========================
status("")
status(" Video I/O:")

View File

@ -268,3 +268,8 @@ if(WITH_IMGCODEC_PXM)
elseif(DEFINED WITH_IMGCODEC_PXM)
set(HAVE_IMGCODEC_PXM OFF)
endif()
if(WITH_IMGCODEC_PFM)
set(HAVE_IMGCODEC_PFM ON)
elseif(DEFINED WITH_IMGCODEC_PFM)
set(HAVE_IMGCODEC_PFM OFF)
endif()

View File

@ -72,6 +72,10 @@ if(HAVE_IMGCODEC_PXM)
add_definitions(-DHAVE_IMGCODEC_PXM)
endif()
if (HAVE_IMGCODEC_PFM)
add_definitions(-DHAVE_IMGCODEC_PFM)
endif()
file(GLOB grfmt_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/grfmt*.hpp)
file(GLOB grfmt_srcs ${CMAKE_CURRENT_LIST_DIR}/src/grfmt*.cpp)

View File

@ -0,0 +1,262 @@
// 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 endianess;
// negative scale means little endianess.
#ifdef WORDS_BIGENDIAN
return scale < 0.0;
#else
return scale >= 0.0;
#endif
}
void swap_endianess(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 enogh 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_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_endianess(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
{
return CV_MAT_DEPTH(depth) == CV_32F || CV_MAT_DEPTH(depth) == CV_8U;
}
bool PFMEncoder::write(const Mat& img, const std::vector<int>& params)
{
(void) 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

View File

@ -0,0 +1,57 @@
// 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.
#ifndef _GRFMT_PFM_H_
#define _GRFMT_PFM_H_
#include "grfmt_base.hpp"
#include "bitstrm.hpp"
#ifdef HAVE_IMGCODEC_PFM
namespace cv
{
class PFMDecoder CV_FINAL : public BaseImageDecoder
{
public:
PFMDecoder();
virtual ~PFMDecoder() CV_OVERRIDE;
bool readData( Mat& img ) CV_OVERRIDE;
bool readHeader() CV_OVERRIDE;
void close();
size_t signatureLength() const CV_OVERRIDE;
bool checkSignature( const String& signature ) const CV_OVERRIDE;
ImageDecoder newDecoder() const CV_OVERRIDE
{
return makePtr<PFMDecoder>();
}
private:
RLByteStream m_strm;
double m_scale_factor;
bool m_swap_byte_order;
};
class PFMEncoder CV_FINAL : public BaseImageEncoder
{
public:
PFMEncoder();
virtual ~PFMEncoder() CV_OVERRIDE;
bool isFormatSupported( int depth ) const CV_OVERRIDE;
bool write( const Mat& img, const std::vector<int>& params ) CV_OVERRIDE;
ImageEncoder newEncoder() const CV_OVERRIDE
{
return makePtr<PFMEncoder>();
}
};
}
#endif // HAVE_IMGCODEC_PXM
#endif/*_GRFMT_PFM_H_*/

View File

@ -47,6 +47,7 @@
#include "grfmt_sunras.hpp"
#include "grfmt_jpeg.hpp"
#include "grfmt_pxm.hpp"
#include "grfmt_pfm.hpp"
#include "grfmt_tiff.hpp"
#include "grfmt_png.hpp"
#include "grfmt_jpeg2000.hpp"

View File

@ -156,6 +156,10 @@ struct ImageCodecInitializer
decoders.push_back( makePtr<PAMDecoder>() );
encoders.push_back( makePtr<PAMEncoder>() );
#endif
#ifdef HAVE_IMGCODEC_PFM
decoders.push_back( makePtr<PFMDecoder>() );
encoders.push_back( makePtr<PFMEncoder>() );
#endif
#ifdef HAVE_TIFF
decoders.push_back( makePtr<TiffDecoder>() );
encoders.push_back( makePtr<TiffEncoder>() );

View File

@ -158,6 +158,7 @@ TEST_P(Imgcodecs_ExtSize, write_imageseq)
Mat img_gt(size, CV_MAKETYPE(CV_8U, cn), Scalar::all(0));
circle(img_gt, center, radius, Scalar::all(255));
#if 1
if (ext == ".pbm" || ext == ".pgm" || ext == ".ppm")
{
@ -172,6 +173,7 @@ TEST_P(Imgcodecs_ExtSize, write_imageseq)
EXPECT_EQ(img.type(), img.type());
EXPECT_EQ(cn, img.channels());
if (ext == ".jpg")
{
// JPEG format does not provide 100% accuracy
@ -181,14 +183,21 @@ TEST_P(Imgcodecs_ExtSize, write_imageseq)
EXPECT_LT(n, expected);
EXPECT_PRED_FORMAT2(cvtest::MatComparator(10, 0), img, img_gt);
}
else if (ext == ".pfm")
{
img_gt.convertTo(img_gt, CV_MAKETYPE(CV_32F, img.channels()));
double n = cvtest::norm(img, img_gt, NORM_L2);
EXPECT_LT(n, 1.);
EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), img, img_gt);
}
else
{
double n = cvtest::norm(img, img_gt, NORM_L2);
EXPECT_LT(n, 1.);
EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), img, img_gt);
}
#if 0
std::cout << filename << std::endl;
imshow("loaded", img);
waitKey(0);
#else
@ -214,7 +223,10 @@ const string all_exts[] =
".ppm",
".pgm",
".pbm",
".pnm"
".pnm",
#endif
#ifdef HAVE_IMGCODEC_PFM
".pfm",
#endif
};
@ -337,6 +349,30 @@ TEST(Imgcodecs_Pam, read_write)
}
#endif
#ifdef HAVE_IMGCODEC_PFM
TEST(Imgcodecs_Pfm, read_write)
{
Mat img = imread(findDataFile("readwrite/lena.pam"));
ASSERT_FALSE(img.empty());
img.convertTo(img, CV_32F, 1/255.0f);
std::vector<int> params;
string writefile = cv::tempfile(".pfm");
EXPECT_NO_THROW(cv::imwrite(writefile, img, params));
cv::Mat reread = cv::imread(writefile, IMREAD_UNCHANGED);
string writefile_no_param = cv::tempfile(".pfm");
EXPECT_NO_THROW(cv::imwrite(writefile_no_param, img));
cv::Mat reread_no_param = cv::imread(writefile_no_param, IMREAD_UNCHANGED);
EXPECT_EQ(0, cvtest::norm(reread, reread_no_param, NORM_INF));
EXPECT_EQ(0, cvtest::norm(img, reread, NORM_INF));
EXPECT_EQ(0, remove(writefile.c_str()));
EXPECT_EQ(0, remove(writefile_no_param.c_str()));
}
#endif
TEST(Imgcodecs, write_parameter_type)
{
cv::Mat m(10, 10, CV_8UC1, cv::Scalar::all(0));