Merge pull request #13847 from ghellwig:jpeg2000-compression-flag

This commit is contained in:
Alexander Alekhin 2019-03-11 10:03:18 +00:00
commit 49093d6d38
2 changed files with 20 additions and 3 deletions

View File

@ -94,7 +94,8 @@ enum ImwriteFlags {
IMWRITE_PAM_TUPLETYPE = 128,//!< For PAM, sets the TUPLETYPE field to the corresponding string value that is defined for the format
IMWRITE_TIFF_RESUNIT = 256,//!< For TIFF, use to specify which DPI resolution unit to set; see libtiff documentation for valid values
IMWRITE_TIFF_XDPI = 257,//!< For TIFF, use to specify the X direction DPI
IMWRITE_TIFF_YDPI = 258 //!< For TIFF, use to specify the Y direction DPI
IMWRITE_TIFF_YDPI = 258, //!< For TIFF, use to specify the Y direction DPI
IMWRITE_JPEG2000_COMPRESSION_X1000 = 272 //!< For JPEG2000, use to specify the target compression rate (multiplied by 1000). The value can be from 0 to 1000. Default is 1000.
};
enum ImwriteEXRTypeFlags {

View File

@ -43,6 +43,7 @@
#include "precomp.hpp"
#ifdef HAVE_JASPER
#include <sstream>
#include "grfmt_jpeg2000.hpp"
#include "opencv2/imgproc.hpp"
@ -467,7 +468,7 @@ bool Jpeg2KEncoder::isFormatSupported( int depth ) const
}
bool Jpeg2KEncoder::write( const Mat& _img, const std::vector<int>& )
bool Jpeg2KEncoder::write( const Mat& _img, const std::vector<int>& params )
{
int width = _img.cols, height = _img.rows;
int depth = _img.depth(), channels = _img.channels();
@ -476,6 +477,18 @@ bool Jpeg2KEncoder::write( const Mat& _img, const std::vector<int>& )
if( channels > 3 || channels < 1 )
return false;
CV_Assert(params.size() % 2 == 0);
double target_compression_rate = 1.0;
for( size_t i = 0; i < params.size(); i += 2 )
{
switch(params[i])
{
case cv::IMWRITE_JPEG2000_COMPRESSION_X1000:
target_compression_rate = std::min(std::max(params[i+1], 0), 1000) / 1000.0;
break;
}
}
jas_image_cmptparm_t component_info[3];
for( int i = 0; i < channels; i++ )
{
@ -511,7 +524,10 @@ bool Jpeg2KEncoder::write( const Mat& _img, const std::vector<int>& )
jas_stream_t *stream = jas_stream_fopen( m_filename.c_str(), "wb" );
if( stream )
{
result = !jas_image_encode( img, stream, jas_image_strtofmt( (char*)"jp2" ), (char*)"" );
std::stringstream options;
options << "rate=" << target_compression_rate;
result = !jas_image_encode( img, stream, jas_image_strtofmt( (char*)"jp2" ), (char*)options.str().c_str() );
jas_stream_close( stream );
}