Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/3.4' into merge-3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Mar 15, 2019
2 parents 17ad33c + 2c3f02c commit 26087e2
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 129 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/libtiff/tif_config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
#endif

/* Number of bits in a file offset, on hosts where this is settable. */
#define _FILE_OFFSET_BITS @FILE_OFFSET_BITS@
//disabled for OpenCV CMakeLists.txt: #define _FILE_OFFSET_BITS @FILE_OFFSET_BITS@

/* Define to `__inline__' or `__inline' if that's what the C compiler
calls it, or to nothing if 'inline' is not supported under any name. */
Expand Down
33 changes: 27 additions & 6 deletions cmake/OpenCVCompilerOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,24 @@ if(CV_GCC OR CV_CLANG)
string(REPLACE "-ffunction-sections" "" ${flags} "${${flags}}")
string(REPLACE "-fdata-sections" "" ${flags} "${${flags}}")
endforeach()
elseif(NOT ((IOS OR ANDROID) AND NOT BUILD_SHARED_LIBS) AND NOT MSVC)
# Remove unreferenced functions: function level linking
add_extra_compiler_option(-ffunction-sections)
add_extra_compiler_option(-fdata-sections)
if(NOT APPLE AND NOT OPENCV_SKIP_GC_SECTIONS)
set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} -Wl,--gc-sections")
else()
if(MSVC)
# TODO: Clang/C2 is not supported
elseif(((IOS OR ANDROID) AND NOT BUILD_SHARED_LIBS) AND NOT OPENCV_FORCE_FUNCTIONS_SECTIONS)
# don't create separate sections for functions/data, reduce package size
else()
# Remove unreferenced functions: function level linking
add_extra_compiler_option(-ffunction-sections)
add_extra_compiler_option(-fdata-sections)
if(NOT OPENCV_SKIP_GC_SECTIONS)
if(APPLE)
set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} -Wl,-dead_strip")
set(OPENCV_EXTRA_SHARED_LINKER_FLAGS "${OPENCV_EXTRA_SHARED_LINKER_FLAGS} -Wl,-dead_strip")
else()
set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set(OPENCV_EXTRA_SHARED_LINKER_FLAGS "${OPENCV_EXTRA_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
endif()
endif()
endif()
endif()

Expand Down Expand Up @@ -281,6 +293,15 @@ if((CV_GCC OR CV_CLANG)
add_extra_compiler_option(-fvisibility-inlines-hidden)
endif()

# workaround gcc bug for aligned ld/st
# https://github.com/opencv/opencv/issues/13211
if((PPC64LE AND NOT CMAKE_CROSSCOMPILING) OR OPENCV_FORCE_COMPILER_CHECK_VSX_ALIGNED)
ocv_check_runtime_flag("${CPU_BASELINE_FLAGS}" "OPENCV_CHECK_VSX_ALIGNED" "${OpenCV_SOURCE_DIR}/cmake/checks/runtime/cpu_vsx_aligned.cpp")
if(NOT OPENCV_CHECK_VSX_ALIGNED)
add_extra_compiler_option_force(-DCV_COMPILER_VSX_BROKEN_ALIGNED)
endif()
endif()

# combine all "extra" options
if(NOT OPENCV_SKIP_EXTRA_COMPILER_FLAGS)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")
Expand Down
38 changes: 38 additions & 0 deletions cmake/OpenCVUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,44 @@ macro(ocv_check_flag_support lang flag varname base_options)
ocv_check_compiler_flag("${_lang}" "${base_options} ${flag}" ${${varname}} ${ARGN})
endmacro()

macro(ocv_check_runtime_flag flag result)
set(_fname "${ARGN}")
if(NOT DEFINED ${result})
file(RELATIVE_PATH _rname "${CMAKE_SOURCE_DIR}" "${_fname}")
message(STATUS "Performing Runtime Test ${result} (check file: ${_rname})")
try_run(exec_return compile_result
"${CMAKE_BINARY_DIR}"
"${_fname}"
CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}" # CMP0056 do this on new CMake
COMPILE_DEFINITIONS "${flag}"
OUTPUT_VARIABLE OUTPUT)

if(${compile_result})
if(exec_return EQUAL 0)
set(${result} 1 CACHE INTERNAL "Runtime Test ${result}")
message(STATUS "Performing Runtime Test ${result} - Success")
else()
message(STATUS "Performing Runtime Test ${result} - Failed(${exec_return})")
set(${result} 0 CACHE INTERNAL "Runtime Test ${result}")
endif()
else()
set(${result} 0 CACHE INTERNAL "Runtime Test ${result}")
message(STATUS "Performing Runtime Test ${result} - Compiling Failed")
endif()

if(NOT ${result})
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Runtime Test failed:\n"
" source file: '${_fname}'\n"
" check option: '${flag}'\n"
" exec return: ${exec_return}\n"
"===== BUILD AND RUNTIME LOG =====\n"
"${OUTPUT}\n"
"===== END =====\n\n")
endif()
endif()
endmacro()

# turns off warnings
macro(ocv_warnings_disable)
if(NOT ENABLE_NOISY_WARNINGS)
Expand Down
55 changes: 55 additions & 0 deletions cmake/checks/runtime/cpu_vsx_aligned.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// check sanity of vsx aligned ld/st
// https://github.com/opencv/opencv/issues/13211

#include <altivec.h>

#define vsx_ld vec_vsx_ld
#define vsx_st vec_vsx_st

template<typename T>
static void fill(T& d, int from = 0, int to = 16)
{
for (int i = from; i < to; i++)
d[i] = i;
}

template<typename T, typename Tvec>
static bool check_data(T& d, Tvec& v, int from = 0, int to = 16)
{
for (int i = from; i < to; i++)
{
if (d[i] != vec_extract(v, i))
return false;
}
return true;
}

int main()
{
unsigned char __attribute__ ((aligned (16))) rbuf[16];
unsigned char __attribute__ ((aligned (16))) wbuf[16];
__vector unsigned char a;

// 1- check aligned load and store
fill(rbuf);
a = vec_ld(0, rbuf);
if (!check_data(rbuf, a))
return 1;
vec_st(a, 0, wbuf);
if (!check_data(wbuf, a))
return 11;

// 2- check mixing aligned load and unaligned store
a = vec_ld(0, rbuf);
vsx_st(a, 0, wbuf);
if (!check_data(wbuf, a))
return 2;

// 3- check mixing unaligned load and aligned store
a = vsx_ld(0, rbuf);
vec_st(a, 0, wbuf);
if (!check_data(wbuf, a))
return 3;

return 0;
}
12 changes: 10 additions & 2 deletions modules/core/include/opencv2/core/hal/intrin_vsx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,16 @@ inline void v_store_low(_Tp* ptr, const _Tpvec& a) \
inline void v_store_high(_Tp* ptr, const _Tpvec& a) \
{ vec_st_h8(a.val, ptr); }

#define OPENCV_HAL_IMPL_VSX_LOADSTORE(_Tpvec, _Tp) \
OPENCV_HAL_IMPL_VSX_LOADSTORE_C(_Tpvec, _Tp, vsx_ld, vec_ld, vsx_st, vec_st)
// working around gcc bug for aligned ld/st
// if runtime check for vec_ld/st fail we failback to unaligned ld/st
// https://github.com/opencv/opencv/issues/13211
#ifdef CV_COMPILER_VSX_BROKEN_ALIGNED
#define OPENCV_HAL_IMPL_VSX_LOADSTORE(_Tpvec, _Tp) \
OPENCV_HAL_IMPL_VSX_LOADSTORE_C(_Tpvec, _Tp, vsx_ld, vsx_ld, vsx_st, vsx_st)
#else
#define OPENCV_HAL_IMPL_VSX_LOADSTORE(_Tpvec, _Tp) \
OPENCV_HAL_IMPL_VSX_LOADSTORE_C(_Tpvec, _Tp, vsx_ld, vec_ld, vsx_st, vec_st)
#endif

OPENCV_HAL_IMPL_VSX_LOADSTORE(v_uint8x16, uchar)
OPENCV_HAL_IMPL_VSX_LOADSTORE(v_int8x16, schar)
Expand Down
4 changes: 2 additions & 2 deletions modules/dnn/src/layers/region_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ class RegionLayerImpl CV_FINAL : public RegionLayer

dstData[box_index + 0] = (x + logistic_activate(srcData[box_index + 0])) / cols;
dstData[box_index + 1] = (y + logistic_activate(srcData[box_index + 1])) / rows;
dstData[box_index + 2] = exp(srcData[box_index + 2]) * biasData[2 * a] / hNorm;
dstData[box_index + 3] = exp(srcData[box_index + 3]) * biasData[2 * a + 1] / wNorm;
dstData[box_index + 2] = exp(srcData[box_index + 2]) * biasData[2 * a] / wNorm;
dstData[box_index + 3] = exp(srcData[box_index + 3]) * biasData[2 * a + 1] / hNorm;

int class_index = index_sample_offset + index * cell_size + 5;
for (int j = 0; j < classes; ++j) {
Expand Down
6 changes: 6 additions & 0 deletions modules/imgcodecs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ endif()
if(HAVE_JASPER)
ocv_include_directories(${JASPER_INCLUDE_DIR})
list(APPEND GRFMT_LIBS ${JASPER_LIBRARIES})
if(OPENCV_IO_FORCE_JASPER)
add_definitions(-DOPENCV_IMGCODECS_FORCE_JASPER=1)
endif()
endif()

if(HAVE_OPENEXR)
Expand Down Expand Up @@ -148,4 +151,7 @@ if(NOT BUILD_opencv_world)
endif()

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()
50 changes: 49 additions & 1 deletion modules/imgcodecs/src/grfmt_jpeg2000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#ifdef HAVE_JASPER
#include <sstream>

#include <opencv2/core/utils/configuration.private.hpp>
#include <opencv2/core/utils/logger.hpp>

#include "grfmt_jpeg2000.hpp"
#include "opencv2/imgproc.hpp"

Expand All @@ -71,7 +74,36 @@ struct JasperInitializer
~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 ///////////////////
Expand All @@ -91,19 +123,22 @@ Jpeg2KDecoder::~Jpeg2KDecoder()

ImageDecoder Jpeg2KDecoder::newDecoder() const
{
initJasper();
return makePtr<Jpeg2KDecoder>();
}

void Jpeg2KDecoder::close()
{
if( m_stream )
{
CV_Assert(isJasperEnabled());
jas_stream_close( (jas_stream_t*)m_stream );
m_stream = 0;
}

if( m_image )
{
CV_Assert(isJasperEnabled());
jas_image_destroy( (jas_image_t*)m_image );
m_image = 0;
}
Expand All @@ -112,6 +147,7 @@ void Jpeg2KDecoder::close()

bool Jpeg2KDecoder::readHeader()
{
CV_Assert(isJasperEnabled());
bool result = false;

close();
Expand Down Expand Up @@ -178,6 +214,8 @@ static void Jpeg2KDecoder_close(Jpeg2KDecoder* ptr)

bool Jpeg2KDecoder::readData( Mat& img )
{
CV_Assert(isJasperEnabled());

Ptr<Jpeg2KDecoder> close_this(this, Jpeg2KDecoder_close);
bool result = false;
bool color = img.channels() > 1;
Expand Down Expand Up @@ -320,6 +358,8 @@ bool Jpeg2KDecoder::readComponent8u( uchar *data, void *_buffer,
int step, int cmpt,
int maxval, int offset, int ncmpts )
{
CV_Assert(isJasperEnabled());

jas_matrix_t* buffer = (jas_matrix_t*)_buffer;
jas_image_t* image = (jas_image_t*)m_image;
int xstart = jas_image_cmpttlx( image, cmpt );
Expand Down Expand Up @@ -384,6 +424,8 @@ bool Jpeg2KDecoder::readComponent16u( unsigned short *data, void *_buffer,
int step, int cmpt,
int maxval, int offset, int ncmpts )
{
CV_Assert(isJasperEnabled());

jas_matrix_t* buffer = (jas_matrix_t*)_buffer;
jas_image_t* image = (jas_image_t*)m_image;
int xstart = jas_image_cmpttlx( image, cmpt );
Expand Down Expand Up @@ -459,6 +501,7 @@ Jpeg2KEncoder::~Jpeg2KEncoder()

ImageEncoder Jpeg2KEncoder::newEncoder() const
{
initJasper();
return makePtr<Jpeg2KEncoder>();
}

Expand All @@ -470,6 +513,7 @@ bool Jpeg2KEncoder::isFormatSupported( int depth ) const

bool Jpeg2KEncoder::write( const Mat& _img, const std::vector<int>& params )
{
CV_Assert(isJasperEnabled());
int width = _img.cols, height = _img.rows;
int depth = _img.depth(), channels = _img.channels();
depth = depth == CV_8U ? 8 : 16;
Expand Down Expand Up @@ -541,6 +585,8 @@ bool Jpeg2KEncoder::write( const Mat& _img, const std::vector<int>& params )

bool Jpeg2KEncoder::writeComponent8u( void *__img, const Mat& _img )
{
CV_Assert(isJasperEnabled());

jas_image_t* img = (jas_image_t*)__img;
int w = _img.cols, h = _img.rows, ncmpts = _img.channels();
jas_matrix_t *row = jas_matrix_create( 1, w );
Expand All @@ -565,6 +611,8 @@ bool Jpeg2KEncoder::writeComponent8u( void *__img, const Mat& _img )

bool Jpeg2KEncoder::writeComponent16u( void *__img, const Mat& _img )
{
CV_Assert(isJasperEnabled());

jas_image_t* img = (jas_image_t*)__img;
int w = _img.cols, h = _img.rows, ncmpts = _img.channels();
jas_matrix_t *row = jas_matrix_create( 1, w );
Expand Down
2 changes: 1 addition & 1 deletion modules/imgcodecs/test/test_grfmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ TEST_P(Imgcodecs_FileMode, regression)

const string all_images[] =
{
#ifdef HAVE_JASPER
#if defined(HAVE_JASPER) && defined(OPENCV_IMGCODECS_ENABLE_JASPER_TESTS)
"readwrite/Rome.jp2",
"readwrite/Bretagne2.jp2",
"readwrite/Bretagne2.jp2",
Expand Down
2 changes: 1 addition & 1 deletion modules/imgcodecs/test/test_read_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const string exts[] = {
#ifdef HAVE_JPEG
"jpg",
#endif
#ifdef HAVE_JASPER
#if defined(HAVE_JASPER) && defined(OPENCV_IMGCODECS_ENABLE_JASPER_TESTS)
"jp2",
#endif
#if 0 /*defined HAVE_OPENEXR && !defined __APPLE__*/
Expand Down
2 changes: 1 addition & 1 deletion modules/ml/src/ann_mlp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ class ANN_MLPImpl CV_FINAL : public ANN_MLP
int count = inputs.rows;

int iter = -1, max_iter = termCrit.maxCount*count;
double epsilon = termCrit.epsilon*count;
double epsilon = (termCrit.type & CV_TERMCRIT_EPS) ? termCrit.epsilon*count : 0;

int l_count = layer_count();
int ivcount = layer_sizes[0];
Expand Down
12 changes: 8 additions & 4 deletions modules/python/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@ if(NOT OPENCV_SKIP_PYTHON_LOADER AND DEFINED OPENCV_PYTHON_INSTALL_PATH)
set(OPENCV_PYTHON_INSTALL_PATH_SETUPVARS "${OPENCV_PYTHON_INSTALL_PATH}" CACHE INTERNAL "")
endif()

if(NOT " ${PYTHON}" STREQUAL " PYTHON" AND DEFINED OPENCV_${PYTHON}_INSTALL_PATH)
set(__python_binary_install_path "${OPENCV_${PYTHON}_INSTALL_PATH}")
elseif(OPENCV_SKIP_PYTHON_LOADER AND DEFINED ${PYTHON}_PACKAGES_PATH)
set(__python_binary_install_path "${${PYTHON}_PACKAGES_PATH}")
if(OPENCV_SKIP_PYTHON_LOADER)
if(DEFINED OPENCV_${PYTHON}_INSTALL_PATH)
set(__python_binary_install_path "${OPENCV_${PYTHON}_INSTALL_PATH}")
elseif(DEFINED ${PYTHON}_PACKAGES_PATH)
set(__python_binary_install_path "${${PYTHON}_PACKAGES_PATH}")
else()
message(FATAL_ERROR "Specify 'OPENCV_${PYTHON}_INSTALL_PATH' variable")
endif()
else()
ocv_assert(DEFINED OPENCV_PYTHON_INSTALL_PATH)
set(__python_binary_install_path "${OPENCV_PYTHON_INSTALL_PATH}/${__python_loader_subdir}python-${${PYTHON}_VERSION_MAJOR}.${${PYTHON}_VERSION_MINOR}")
Expand Down
Loading

0 comments on commit 26087e2

Please sign in to comment.