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

Add JPEG2000 compression flag.

This commit is contained in:
Gregor Mittag
2019-02-16 15:05:31 +01:00
parent a1ef612662
commit d71977b4dd
2 changed files with 20 additions and 3 deletions
+18 -2
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 );
}