##
##  opencv.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_opkg 4.6.0
%define       V_dist 4.6.0

#   package information
Name:         opencv
Summary:      Computer Vision Library
URL:          http://opencv.org/
Vendor:       Intel, Willow Garage, Advanced Micro Devices, OpenCV Foundation, Itseez
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Graphics
License:      BSD
Version:      %{V_opkg}
Release:      20220616

#   package options
%option       with_contrib  no

#   list of sources
Source0:      https://github.com/Itseez/opencv/archive/%{V_dist}.tar.gz
Source1:      http://download.openpkg.org/components/versioned/opencv/opencv_contrib-%{V_dist}.tar.gz
Patch0:       opencv.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, gcc::with_cxx = yes
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  jpeg, png, zlib, tiff, webp
PreReq:       jpeg, png, zlib, tiff, webp
Provides:     OPENCV

%description
    OpenCV (Open Source Computer Vision Library) is an open source
    computer vision and machine learning software library. OpenCV
    was built to provide a common infrastructure for computer vision
    applications and to accelerate the use of machine perception in the
    commercial products. OpenCV makes it easy for businesses to utilize
    and modify the code. The library has more than 2500 optimized
    algorithms, which includes a comprehensive set of both classic and
    state-of-the-art computer vision and machine learning algorithms.
    These algorithms can be used to detect and recognize faces, identify
    objects, classify human actions in videos, track camera movements,
    track moving objects, extract 3D models of objects, produce 3D point
    clouds from stereo cameras, stitch images together to produce a
    high resolution image of an entire scene, find similar images from
    an image database, remove red eyes from images taken using flash,
    follow eye movements, recognize scenery and establish markers to
    overlay it with augmented reality, etc.

%track
    prog opencv = {
        version   = %{V_dist}
        url       = https://github.com/Itseez/opencv/releases
        regex     = /([34]\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q -n opencv-%{V_dist}
%if "%{with_contrib}" == "yes"
    %setup -q -n opencv-%{V_dist} -T -D -a 1
%endif
    %patch -p0

%build
%if "%{with_contrib}" == "yes"
    mkdir contrib
    for module in \
        bgsegm dpm fuzzy hfs img_hash line_descriptor optflow reg \
        rgbd saliency tracking ximgproc xobjdetect xphoto; do
        mv opencv_contrib-%{V_dist}/modules/$module contrib/
    done
%endif
    mkdir build
    cd build
    cmake \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_INSTALL_PREFIX="%{l_prefix}" \
        -DCMAKE_C_COMPILER="%{l_cc}" \
        -DCMAKE_C_FLAGS="%{l_cflags} %{l_cppflags}" \
        -DCMAKE_EXE_LINKER_FLAGS="%{l_ldflags}" \
        -DCMAKE_CXX_COMPILER="%{l_cxx}" \
        -DCMAKE_CXX_FLAGS="%{l_cxxflags}" \
        -DBUILD_SHARED_LIBS=OFF \
        -DBUILD_PERF_TESTS=OFF \
        -DBUILD_TESTS=OFF \
        -DBUILD_WITH_DEBUG_INFO=OFF \
        -DBUILD_EXAMPLES=OFF \
        -DINSTALL_TESTS=OFF \
        -DINSTALL_C_EXAMPLES=OFF \
        -DOPENCV_WARNINGS_ARE_ERRORS=OFF \
        -DENABLE_CXX11=ON \
        -DENABLE_PRECOMPILED_HEADERS=OFF \
        -DZLIB_INCLUDE_DIRS="%{l_prefix}/include" \
        -DZLIB_LIBRARY="%{l_prefix}/lib/libz.a" \
        -DJPEG_INCLUDE_DIRS="%{l_prefix}/include" \
        -DJPEG_LIBRARY="%{l_prefix}/lib/libjpeg.a" \
        -DTIFF_INCLUDE_DIRS="%{l_prefix}/include/tiff" \
        -DTIFF_LIBRARY="%{l_prefix}/lib/libtiff.a" \
        -DPNG_INCLUDE_DIRS="%{l_prefix}/include" \
        -DPNG_LIBRARY="%{l_prefix}/lib/libpng.a" \
        -DWEBP_INCLUDE_DIRS="%{l_prefix}/include" \
        -DWEBP_LIBRARY="%{l_prefix}/lib/libwebp.a" \
        -DPYTHON3_EXECUTABLE="%{l_prefix}/bin/python" \
        -DWITH_LAPACK=OFF \
        -DWITH_JASPER=OFF \
        -DWITH_JPEG=ON \
        -DWITH_PNG=ON \
        -DWITH_TIFF=ON \
        -DWITH_WEBP=ON \
        -DWITH_TBB=ON \
        -DWITH_FFMPEG=OFF \
        -DWITH_IPP=OFF \
        -DWITH_GTK=OFF \
        -DWITH_GSTREAMER=OFF \
        -DWITH_GPHOTO2=OFF \
        -DWITH_ITT=OFF \
        -DWITH_OPENEXR=OFF \
        -DWITH_PROTOBUF=OFF \
        -DWITH_EIGEN=OFF \
        -DBUILD_JAVA=OFF \
%if "%{with_contrib}" == "yes"
        -DOPENCV_EXTRA_MODULES_PATH="`pwd`/../contrib" \
%endif
        ..
    %{l_make} %{l_mflags}

%install
    (   cd build
        %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT
    ) || exit $?
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

