Refine logic of parsing PNG version

Currently, if `PNG_FOUND`, cmake scripts will check include and parse
header while we can use `PNG_VERSION_STRING` conveniently. If
`BUILD_PNG`, parse version from `PNG_LIBPNG_VER_STRING` directly is more
convenient than parsing major, minor and patch and concatenate them.

The comment of png.h also supports this.
```
/* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */
```
https://github.com/glennrp/libpng/blob/libpng16/png.h#L287

This patch also modifies `ocv_parse_header_version` macro to receive
another parameter to make it more general.

The reason why changing `PNG_VERSION` to `PNG_VERSION_STRING` is to be
consistent with cmake's FindPNG.

This patch removes `HAVE_LIBPNG_PNG_H` variable because `PNG_INCLUDE_DIR`
is where to find png.h, etc according to
https://cmake.org/cmake/help/latest/module/FindPNG.html.

This patch also removes `PNG_PNG_INCLUDE_DIR` variable which is an
advanced variable used in cmake's FindPNG and is not used in opencv.
pull/24754/head
Letu Ren 11 months ago
parent 953dddd26b
commit 4546f40d8b
  1. 2
      CMakeLists.txt
  2. 18
      cmake/OpenCVFindLibsGrfmt.cmake
  3. 8
      cmake/OpenCVUtils.cmake
  4. 3
      cmake/templates/cvconfig.h.in
  5. 4
      modules/imgcodecs/src/grfmt_png.cpp

@ -1406,7 +1406,7 @@ if(WITH_SPNG)
status(" PNG:" "${SPNG_LIBRARY} (ver ${SPNG_VERSION})")
endif()
elseif(WITH_PNG OR HAVE_PNG)
status(" PNG:" PNG_FOUND THEN "${PNG_LIBRARY} (ver ${PNG_VERSION})" ELSE "build (ver ${PNG_VERSION})")
status(" PNG:" PNG_FOUND THEN "${PNG_LIBRARY} (ver ${PNG_VERSION_STRING})" ELSE "build (ver ${PNG_VERSION_STRING})")
endif()
if(WITH_TIFF OR HAVE_TIFF)

@ -34,7 +34,7 @@ if(NOT ZLIB_FOUND)
set(ZLIB_INCLUDE_DIRS ${ZLIB_INCLUDE_DIR})
set(ZLIB_LIBRARIES ${ZLIB_LIBRARY})
ocv_parse_header_version(ZLIB "${${ZLIB_LIBRARY}_SOURCE_DIR}/zlib.h")
ocv_parse_header_version(ZLIB "${${ZLIB_LIBRARY}_SOURCE_DIR}/zlib.h" ZLIB_VERSION)
endif()
# --- libavif (optional) ---
@ -273,31 +273,21 @@ if(NOT HAVE_SPNG AND WITH_PNG)
ocv_clear_vars(PNG_FOUND)
else()
ocv_clear_internal_cache_vars(PNG_LIBRARY PNG_INCLUDE_DIR)
include(FindPNG)
if(PNG_FOUND)
include(CheckIncludeFile)
check_include_file("${PNG_PNG_INCLUDE_DIR}/libpng/png.h" HAVE_LIBPNG_PNG_H)
if(HAVE_LIBPNG_PNG_H)
ocv_parse_header("${PNG_PNG_INCLUDE_DIR}/libpng/png.h" PNG_VERSION_LINES PNG_LIBPNG_VER_MAJOR PNG_LIBPNG_VER_MINOR PNG_LIBPNG_VER_RELEASE)
else()
ocv_parse_header("${PNG_PNG_INCLUDE_DIR}/png.h" PNG_VERSION_LINES PNG_LIBPNG_VER_MAJOR PNG_LIBPNG_VER_MINOR PNG_LIBPNG_VER_RELEASE)
endif()
endif()
find_package(PNG QUIET)
endif()
if(NOT PNG_FOUND)
ocv_clear_vars(PNG_LIBRARY PNG_LIBRARIES PNG_INCLUDE_DIR PNG_PNG_INCLUDE_DIR HAVE_LIBPNG_PNG_H PNG_DEFINITIONS)
ocv_clear_vars(PNG_LIBRARY PNG_LIBRARIES PNG_INCLUDE_DIR PNG_DEFINITIONS)
set(PNG_LIBRARY libpng CACHE INTERNAL "")
set(PNG_LIBRARIES ${PNG_LIBRARY})
add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libpng")
set(PNG_INCLUDE_DIR "${${PNG_LIBRARY}_SOURCE_DIR}" CACHE INTERNAL "")
set(PNG_DEFINITIONS "")
ocv_parse_header("${PNG_INCLUDE_DIR}/png.h" PNG_VERSION_LINES PNG_LIBPNG_VER_MAJOR PNG_LIBPNG_VER_MINOR PNG_LIBPNG_VER_RELEASE)
ocv_parse_header_version(PNG "${PNG_INCLUDE_DIR}/png.h" PNG_LIBPNG_VER_STRING)
endif()
set(HAVE_PNG YES)
set(PNG_VERSION "${PNG_LIBPNG_VER_MAJOR}.${PNG_LIBPNG_VER_MINOR}.${PNG_LIBPNG_VER_RELEASE}")
endif()

@ -1430,15 +1430,15 @@ macro(ocv_parse_header2 LIBNAME HDR_PATH VARNAME)
endif()
endmacro()
# set ${LIBNAME}_VERSION_STRING to ${LIBNAME}_VERSION without quotes
macro(ocv_parse_header_version LIBNAME HDR_PATH)
# set ${LIBNAME}_VERSION_STRING to ${LIBVER} without quotes
macro(ocv_parse_header_version LIBNAME HDR_PATH LIBVER)
ocv_clear_vars(${LIBNAME}_VERSION_STRING)
set(${LIBNAME}_H "")
if(EXISTS "${HDR_PATH}")
file(STRINGS "${HDR_PATH}" ${LIBNAME}_H REGEX "^#define[ \t]+${LIBNAME}_VERSION[ \t]+\"[^\"]*\".*$" LIMIT_COUNT 1)
file(STRINGS "${HDR_PATH}" ${LIBNAME}_H REGEX "^#define[ \t]+${LIBVER}[ \t]+\"[^\"]*\".*$" LIMIT_COUNT 1)
endif()
if(${LIBNAME}_H)
string(REGEX REPLACE "^.*[ \t]${LIBNAME}_VERSION[ \t]+\"([0-9\.]+)\"$" "\\1" ${LIBNAME}_VERSION_STRING "${${LIBNAME}_H}")
string(REGEX REPLACE "^.*[ \t]${LIBVER}[ \t]+\"([0-9\.]+)\"$" "\\1" ${LIBNAME}_VERSION_STRING "${${LIBNAME}_H}")
endif()
endmacro()

@ -78,9 +78,6 @@
/* IJG JPEG codec */
#cmakedefine HAVE_JPEG
/* libpng/png.h needs to be included */
#cmakedefine HAVE_LIBPNG_PNG_H
/* GDCM DICOM codec */
#cmakedefine HAVE_GDCM

@ -58,11 +58,7 @@
# define _FILE_OFFSET_BITS 0
#endif
#ifdef HAVE_LIBPNG_PNG_H
#include <libpng/png.h>
#else
#include <png.h>
#endif
#include <zlib.h>
#include "grfmt_png.hpp"

Loading…
Cancel
Save