1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

imgcodecs: ensure parameters are key-value pairs, fix HDR encoder

This commit is contained in:
Alexander Alekhin
2022-11-18 18:09:26 +00:00
parent d9f66413ee
commit f0df78b7e7
6 changed files with 119 additions and 19 deletions
+16 -2
View File
@@ -141,14 +141,28 @@ bool HdrEncoder::write( const Mat& input_img, const std::vector<int>& params )
if(img.depth() != CV_32F) {
img.convertTo(img, CV_32FC3, 1/255.0f);
}
CV_Assert(params.empty() || params[0] == HDR_NONE || params[0] == HDR_RLE);
int compression = IMWRITE_HDR_COMPRESSION_RLE;
for (size_t i = 0; i + 1 < params.size(); i += 2)
{
switch (params[i])
{
case IMWRITE_HDR_COMPRESSION:
compression = params[i + 1];
break;
default:
break;
}
}
CV_Check(compression, compression == IMWRITE_HDR_COMPRESSION_NONE || compression == IMWRITE_HDR_COMPRESSION_RLE, "");
FILE *fout = fopen(m_filename.c_str(), "wb");
if(!fout) {
return false;
}
RGBE_WriteHeader(fout, img.cols, img.rows, NULL);
if(params.empty() || params[0] == HDR_RLE) {
if (compression == IMWRITE_HDR_COMPRESSION_RLE) {
RGBE_WritePixels_RLE(fout, const_cast<float*>(img.ptr<float>()), img.cols, img.rows);
} else {
RGBE_WritePixels(fout, const_cast<float*>(img.ptr<float>()), img.cols * img.rows);
-6
View File
@@ -50,12 +50,6 @@
namespace cv
{
enum HdrCompression
{
HDR_NONE = 0,
HDR_RLE = 1
};
// Radiance rgbe (.hdr) reader
class HdrDecoder CV_FINAL : public BaseImageDecoder
{
+45 -3
View File
@@ -662,7 +662,7 @@ bool imreadmulti(const String& filename, std::vector<Mat>& mats, int flags)
}
static bool imwrite_( const String& filename, const std::vector<Mat>& img_vec,
const std::vector<int>& params, bool flipv )
const std::vector<int>& params_, bool flipv )
{
bool isMultiImg = img_vec.size() > 1;
std::vector<Mat> write_vec;
@@ -696,7 +696,27 @@ static bool imwrite_( const String& filename, const std::vector<Mat>& img_vec,
}
encoder->setDestination( filename );
CV_Assert(params.size() <= CV_IO_MAX_IMAGE_PARAMS*2);
#if CV_VERSION_MAJOR < 5 && defined(HAVE_IMGCODEC_HDR)
bool fixed = false;
std::vector<int> params_pair(2);
if (dynamic_cast<HdrEncoder*>(encoder.get()))
{
if (params_.size() == 1)
{
CV_LOG_WARNING(NULL, "imwrite() accepts key-value pair of parameters, but single value is passed. "
"HDR encoder behavior has been changed, please use IMWRITE_HDR_COMPRESSION key.");
params_pair[0] = IMWRITE_HDR_COMPRESSION;
params_pair[1] = params_[0];
fixed = true;
}
}
const std::vector<int>& params = fixed ? params_pair : params_;
#else
const std::vector<int>& params = params_;
#endif
CV_Check(params.size(), (params.size() & 1) == 0, "Encoding 'params' must be key-value pairs");
CV_CheckLE(params.size(), (size_t)(CV_IO_MAX_IMAGE_PARAMS*2), "");
bool code = false;
try
{
@@ -936,7 +956,7 @@ Mat imdecode( InputArray _buf, int flags, Mat* dst )
}
bool imencode( const String& ext, InputArray _image,
std::vector<uchar>& buf, const std::vector<int>& params )
std::vector<uchar>& buf, const std::vector<int>& params_ )
{
CV_TRACE_FUNCTION();
@@ -958,6 +978,28 @@ bool imencode( const String& ext, InputArray _image,
image = temp;
}
#if CV_VERSION_MAJOR < 5 && defined(HAVE_IMGCODEC_HDR)
bool fixed = false;
std::vector<int> params_pair(2);
if (dynamic_cast<HdrEncoder*>(encoder.get()))
{
if (params_.size() == 1)
{
CV_LOG_WARNING(NULL, "imwrite() accepts key-value pair of parameters, but single value is passed. "
"HDR encoder behavior has been changed, please use IMWRITE_HDR_COMPRESSION key.");
params_pair[0] = IMWRITE_HDR_COMPRESSION;
params_pair[1] = params_[0];
fixed = true;
}
}
const std::vector<int>& params = fixed ? params_pair : params_;
#else
const std::vector<int>& params = params_;
#endif
CV_Check(params.size(), (params.size() & 1) == 0, "Encoding 'params' must be key-value pairs");
CV_CheckLE(params.size(), (size_t)(CV_IO_MAX_IMAGE_PARAMS*2), "");
bool code;
if( encoder->setDestination(buf) )
{