Merge pull request #14059 from alalek:imgcodecs_disable_jasper

pull/14074/head^2
Alexander Alekhin 6 years ago
commit 19e8121a38
  1. 6
      modules/imgcodecs/CMakeLists.txt
  2. 50
      modules/imgcodecs/src/grfmt_jpeg2000.cpp
  3. 2
      modules/imgcodecs/test/test_grfmt.cpp
  4. 2
      modules/imgcodecs/test/test_read_write.cpp

@ -48,6 +48,9 @@ endif()
if(HAVE_JASPER) if(HAVE_JASPER)
ocv_include_directories(${JASPER_INCLUDE_DIR}) ocv_include_directories(${JASPER_INCLUDE_DIR})
list(APPEND GRFMT_LIBS ${JASPER_LIBRARIES}) list(APPEND GRFMT_LIBS ${JASPER_LIBRARIES})
if(OPENCV_IO_FORCE_JASPER)
add_definitions(-DOPENCV_IMGCODECS_FORCE_JASPER=1)
endif()
endif() endif()
if(HAVE_OPENEXR) if(HAVE_OPENEXR)
@ -143,4 +146,7 @@ if(NOT BUILD_opencv_world)
endif() endif()
ocv_add_accuracy_tests() ocv_add_accuracy_tests()
if(TARGET opencv_test_imgcodecs AND HAVE_JASPER AND "$ENV{OPENCV_IO_ENABLE_JASPER}")
ocv_target_compile_definitions(opencv_test_imgcodecs PRIVATE OPENCV_IMGCODECS_ENABLE_JASPER_TESTS=1)
endif()
ocv_add_perf_tests() ocv_add_perf_tests()

@ -44,6 +44,9 @@
#ifdef HAVE_JASPER #ifdef HAVE_JASPER
#include <opencv2/core/utils/configuration.private.hpp>
#include <opencv2/core/utils/logger.hpp>
#include "grfmt_jpeg2000.hpp" #include "grfmt_jpeg2000.hpp"
#include "opencv2/imgproc.hpp" #include "opencv2/imgproc.hpp"
@ -70,7 +73,36 @@ struct JasperInitializer
~JasperInitializer() { jas_cleanup(); } ~JasperInitializer() { jas_cleanup(); }
}; };
static JasperInitializer initialize_jasper; static JasperInitializer& _initJasper()
{
static JasperInitializer initialize_jasper;
return initialize_jasper;
}
static bool isJasperEnabled()
{
static const bool PARAM_ENABLE_JASPER = utils::getConfigurationParameterBool("OPENCV_IO_ENABLE_JASPER",
#ifdef OPENCV_IMGCODECS_FORCE_JASPER
true
#else
false
#endif
);
return PARAM_ENABLE_JASPER;
}
static JasperInitializer& initJasper()
{
if (isJasperEnabled())
{
return _initJasper();
}
else
{
const char* message = "imgcodecs: Jasper (JPEG-2000) codec is disabled. You can enable it via 'OPENCV_IO_ENABLE_JASPER' option. Refer for details and cautions here: https://github.com/opencv/opencv/issues/14058";
CV_LOG_WARNING(NULL, message);
CV_Error(Error::StsNotImplemented, message);
}
}
/////////////////////// Jpeg2KDecoder /////////////////// /////////////////////// Jpeg2KDecoder ///////////////////
@ -90,6 +122,7 @@ Jpeg2KDecoder::~Jpeg2KDecoder()
ImageDecoder Jpeg2KDecoder::newDecoder() const ImageDecoder Jpeg2KDecoder::newDecoder() const
{ {
initJasper();
return makePtr<Jpeg2KDecoder>(); return makePtr<Jpeg2KDecoder>();
} }
@ -97,12 +130,14 @@ void Jpeg2KDecoder::close()
{ {
if( m_stream ) if( m_stream )
{ {
CV_Assert(isJasperEnabled());
jas_stream_close( (jas_stream_t*)m_stream ); jas_stream_close( (jas_stream_t*)m_stream );
m_stream = 0; m_stream = 0;
} }
if( m_image ) if( m_image )
{ {
CV_Assert(isJasperEnabled());
jas_image_destroy( (jas_image_t*)m_image ); jas_image_destroy( (jas_image_t*)m_image );
m_image = 0; m_image = 0;
} }
@ -111,6 +146,7 @@ void Jpeg2KDecoder::close()
bool Jpeg2KDecoder::readHeader() bool Jpeg2KDecoder::readHeader()
{ {
CV_Assert(isJasperEnabled());
bool result = false; bool result = false;
close(); close();
@ -177,6 +213,8 @@ static void Jpeg2KDecoder_close(Jpeg2KDecoder* ptr)
bool Jpeg2KDecoder::readData( Mat& img ) bool Jpeg2KDecoder::readData( Mat& img )
{ {
CV_Assert(isJasperEnabled());
Ptr<Jpeg2KDecoder> close_this(this, Jpeg2KDecoder_close); Ptr<Jpeg2KDecoder> close_this(this, Jpeg2KDecoder_close);
bool result = false; bool result = false;
bool color = img.channels() > 1; bool color = img.channels() > 1;
@ -319,6 +357,8 @@ bool Jpeg2KDecoder::readComponent8u( uchar *data, void *_buffer,
int step, int cmpt, int step, int cmpt,
int maxval, int offset, int ncmpts ) int maxval, int offset, int ncmpts )
{ {
CV_Assert(isJasperEnabled());
jas_matrix_t* buffer = (jas_matrix_t*)_buffer; jas_matrix_t* buffer = (jas_matrix_t*)_buffer;
jas_image_t* image = (jas_image_t*)m_image; jas_image_t* image = (jas_image_t*)m_image;
int xstart = jas_image_cmpttlx( image, cmpt ); int xstart = jas_image_cmpttlx( image, cmpt );
@ -383,6 +423,8 @@ bool Jpeg2KDecoder::readComponent16u( unsigned short *data, void *_buffer,
int step, int cmpt, int step, int cmpt,
int maxval, int offset, int ncmpts ) int maxval, int offset, int ncmpts )
{ {
CV_Assert(isJasperEnabled());
jas_matrix_t* buffer = (jas_matrix_t*)_buffer; jas_matrix_t* buffer = (jas_matrix_t*)_buffer;
jas_image_t* image = (jas_image_t*)m_image; jas_image_t* image = (jas_image_t*)m_image;
int xstart = jas_image_cmpttlx( image, cmpt ); int xstart = jas_image_cmpttlx( image, cmpt );
@ -458,6 +500,7 @@ Jpeg2KEncoder::~Jpeg2KEncoder()
ImageEncoder Jpeg2KEncoder::newEncoder() const ImageEncoder Jpeg2KEncoder::newEncoder() const
{ {
initJasper();
return makePtr<Jpeg2KEncoder>(); return makePtr<Jpeg2KEncoder>();
} }
@ -469,6 +512,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>& )
{ {
CV_Assert(isJasperEnabled());
int width = _img.cols, height = _img.rows; int width = _img.cols, height = _img.rows;
int depth = _img.depth(), channels = _img.channels(); int depth = _img.depth(), channels = _img.channels();
depth = depth == CV_8U ? 8 : 16; depth = depth == CV_8U ? 8 : 16;
@ -525,6 +569,8 @@ bool Jpeg2KEncoder::write( const Mat& _img, const std::vector<int>& )
bool Jpeg2KEncoder::writeComponent8u( void *__img, const Mat& _img ) bool Jpeg2KEncoder::writeComponent8u( void *__img, const Mat& _img )
{ {
CV_Assert(isJasperEnabled());
jas_image_t* img = (jas_image_t*)__img; jas_image_t* img = (jas_image_t*)__img;
int w = _img.cols, h = _img.rows, ncmpts = _img.channels(); int w = _img.cols, h = _img.rows, ncmpts = _img.channels();
jas_matrix_t *row = jas_matrix_create( 1, w ); jas_matrix_t *row = jas_matrix_create( 1, w );
@ -549,6 +595,8 @@ bool Jpeg2KEncoder::writeComponent8u( void *__img, const Mat& _img )
bool Jpeg2KEncoder::writeComponent16u( void *__img, const Mat& _img ) bool Jpeg2KEncoder::writeComponent16u( void *__img, const Mat& _img )
{ {
CV_Assert(isJasperEnabled());
jas_image_t* img = (jas_image_t*)__img; jas_image_t* img = (jas_image_t*)__img;
int w = _img.cols, h = _img.rows, ncmpts = _img.channels(); int w = _img.cols, h = _img.rows, ncmpts = _img.channels();
jas_matrix_t *row = jas_matrix_create( 1, w ); jas_matrix_t *row = jas_matrix_create( 1, w );

@ -71,7 +71,7 @@ TEST_P(Imgcodecs_FileMode, regression)
const string all_images[] = const string all_images[] =
{ {
#ifdef HAVE_JASPER #if defined(HAVE_JASPER) && defined(OPENCV_IMGCODECS_ENABLE_JASPER_TESTS)
"readwrite/Rome.jp2", "readwrite/Rome.jp2",
"readwrite/Bretagne2.jp2", "readwrite/Bretagne2.jp2",
"readwrite/Bretagne2.jp2", "readwrite/Bretagne2.jp2",

@ -105,7 +105,7 @@ const string exts[] = {
#ifdef HAVE_JPEG #ifdef HAVE_JPEG
"jpg", "jpg",
#endif #endif
#ifdef HAVE_JASPER #if defined(HAVE_JASPER) && defined(OPENCV_IMGCODECS_ENABLE_JASPER_TESTS)
"jp2", "jp2",
#endif #endif
#if 0 /*defined HAVE_OPENEXR && !defined __APPLE__*/ #if 0 /*defined HAVE_OPENEXR && !defined __APPLE__*/

Loading…
Cancel
Save