diff --git a/modules/imgcodecs/include/opencv2/imgcodecs.hpp b/modules/imgcodecs/include/opencv2/imgcodecs.hpp index b326001882..dd70d16da4 100644 --- a/modules/imgcodecs/include/opencv2/imgcodecs.hpp +++ b/modules/imgcodecs/include/opencv2/imgcodecs.hpp @@ -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 { diff --git a/modules/imgcodecs/src/grfmt_jpeg2000.cpp b/modules/imgcodecs/src/grfmt_jpeg2000.cpp index be280e285a..18d8904aa9 100644 --- a/modules/imgcodecs/src/grfmt_jpeg2000.cpp +++ b/modules/imgcodecs/src/grfmt_jpeg2000.cpp @@ -43,6 +43,7 @@ #include "precomp.hpp" #ifdef HAVE_JASPER +#include #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& ) +bool Jpeg2KEncoder::write( const Mat& _img, const std::vector& 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& ) 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& ) 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 ); }