File libheif-fix-cmake.patch of Package libheif

From 1f085c9f55e2b388f8ca315c0e09cde317382a9d Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@cryptomilk.org>
Date: Thu, 9 Jan 2020 10:02:46 +0100
Subject: [PATCH 1/8] box: Fix size comparison
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

libheif/box.cc:517:26: error: comparison of integer expressions of
different signedness: ‘uint64_t’ {aka ‘long unsigned int’} and ‘long
int’ [-Werror=sign-compare]
  517 |   if (hdr.get_box_size() > std::numeric_limits<int64_t>::max()) {
      |       ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 libheif/box.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libheif/box.cc b/libheif/box.cc
index 350e2c0..0670ed2 100644
--- a/libheif/box.cc
+++ b/libheif/box.cc
@@ -514,7 +514,7 @@ Error Box::read(BitstreamRange& range, std::shared_ptr<heif::Box>* result)
 
   // Security check: make sure that box size does not exceed int64 size.
 
-  if (hdr.get_box_size() > std::numeric_limits<int64_t>::max()) {
+  if (hdr.get_box_size() > (uint64_t)std::numeric_limits<int64_t>::max()) {
     return Error(heif_error_Invalid_input,
                  heif_suberror_Invalid_box_size);
   }

From 10cfa4990e3c2e0e0560cb905bfba1c497b4d869 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@cryptomilk.org>
Date: Thu, 9 Jan 2020 10:13:51 +0100
Subject: [PATCH 2/8] encoder: Fix unused variable error
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

libheif/heif_encoder_x265.cc:635:7: error: unused variable ‘s’ [-Werror=unused-variable]
  635 |   int s = snprintf(ctu_buf,4,"%d",ctuSize);
      |       ^
---
 libheif/heif_encoder_x265.cc | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/libheif/heif_encoder_x265.cc b/libheif/heif_encoder_x265.cc
index 926ecdb..aa0e17b 100644
--- a/libheif/heif_encoder_x265.cc
+++ b/libheif/heif_encoder_x265.cc
@@ -598,7 +598,7 @@ static struct heif_error x265_encode_image(void* encoder_raw, const struct heif_
   // x265 cannot encode images smaller than one CTU size
   // https://bitbucket.org/multicoreware/x265/issues/475/x265-does-not-allow-image-sizes-smaller
   // -> use smaller CTU sizes for very small images
-  char ctu_buf[4];
+  const char *ctu = NULL;
   int ctuSize = 64;
 #if 0
   while  (heif_image_get_width(image, heif_channel_Y) < ctuSize ||
@@ -632,8 +632,25 @@ static struct heif_error x265_encode_image(void* encoder_raw, const struct heif_
   }
 #endif
 
-  int s = snprintf(ctu_buf,4,"%d",ctuSize);
-  assert(s<4);
+  // ctuSize should be a power of 2 in [16;64]
+  switch (ctuSize) {
+  case 64:
+      ctu = "64";
+      break;
+  case 32:
+      ctu = "32";
+      break;
+  case 16:
+      ctu = "16";
+      break;
+  default:
+    struct heif_error err = {
+      heif_error_Encoder_plugin_error,
+      heif_suberror_Invalid_parameter_value,
+      kError_unsupported_image_size
+    };
+    return err;
+  }
 
   // BPG uses CQP. It does not seem to be better though.
   //  param->rc.rateControlMode = X265_RC_CQP;
@@ -643,7 +660,7 @@ static struct heif_error x265_encode_image(void* encoder_raw, const struct heif_
   api->param_parse(param, "info", "0");
   api->param_parse(param, "limit-modes", "0");
   api->param_parse(param, "limit-refs", "0");
-  api->param_parse(param, "ctu", ctu_buf);
+  api->param_parse(param, "ctu", ctu);
   api->param_parse(param, "rskip", "0");
 
   api->param_parse(param, "rect", "1");

From c5befa12601e966b48138536a9a9cdf90e03a18f Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@cryptomilk.org>
Date: Thu, 9 Jan 2020 10:05:11 +0100
Subject: [PATCH 3/8] examples: Correctly cast value to uint8_t
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

examples/heif_enc.cc:652:39: warning: conversion from ‘int’ to ‘uint8_t’
    {aka ‘unsigned char’} may change value [-Wconversion]
  652 |         p_out[2*x + y*stride + 0] = (v>>8);
      |                                     ~~^~~~
examples/heif_enc.cc:653:40: warning: conversion from ‘int’ to ‘uint8_t’
    {aka ‘unsigned char’} may change value [-Wconversion]
  653 |         p_out[2*x + y*stride + 1] = (v & 0xFF);
      |                                     ~~~^~~~~~~
---
 examples/heif_enc.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/examples/heif_enc.cc b/examples/heif_enc.cc
index d2d68b6..8a54947 100644
--- a/examples/heif_enc.cc
+++ b/examples/heif_enc.cc
@@ -649,8 +649,8 @@ std::shared_ptr<heif_image> loadPNG(const char* filename, int output_bit_depth)
 
       for (uint32_t x = 0; x < nVal ; x++) {
         uint16_t v = (uint16_t)(((p[0]<<8) | p[1]) >> bdShift);
-        p_out[2*x + y*stride + 0] = (v>>8);
-        p_out[2*x + y*stride + 1] = (v & 0xFF);
+        p_out[2*x + y*stride + 0] = (uint8_t)(v >> 8);
+        p_out[2*x + y*stride + 1] = (uint8_t)(v & 0xFF);
         p += 2;
       }
     }

From be75661eb993bc9b0ac69f5650b7059b9a251cd3 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@cryptomilk.org>
Date: Mon, 13 Jan 2020 18:06:46 +0100
Subject: [PATCH 4/8] cmake: Correctly install heif_version.h

---
 CMakeLists.txt            | 3 ---
 gdk-pixbuf/CMakeLists.txt | 2 +-
 libheif/CMakeLists.txt    | 4 +++-
 3 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4d44eeb..35e218c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -12,9 +12,6 @@ include(GNUInstallDirs)
 # The version number.
 set (PACKAGE_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
 
-configure_file ("libheif/heif_version.h.in" "libheif/heif_version.h")
-
-
 # Check for unistd.h
 
 include (${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
diff --git a/gdk-pixbuf/CMakeLists.txt b/gdk-pixbuf/CMakeLists.txt
index 3a3cb3f..754588a 100644
--- a/gdk-pixbuf/CMakeLists.txt
+++ b/gdk-pixbuf/CMakeLists.txt
@@ -7,7 +7,7 @@ if(UNIX)
 
     add_library(pixbufloader-heif MODULE pixbufloader-heif.c)
 
-    target_include_directories(pixbufloader-heif PRIVATE ${GDKPIXBUF2_INCLUDE_DIRS})
+    target_include_directories(pixbufloader-heif PRIVATE ${GDKPIXBUF2_INCLUDE_DIRS} ${PROJECT_BINARY_DIR})
     target_link_libraries(pixbufloader-heif PUBLIC ${GDKPIXBUF2_LIBRARIES} ${LIBHEIF_LIBRARY_NAME})
 
     install(TARGETS pixbufloader-heif LIBRARY DESTINATION ${GDKPIXBUF2_MODULE_DIR})
diff --git a/libheif/CMakeLists.txt b/libheif/CMakeLists.txt
index 4c73774..0317038 100644
--- a/libheif/CMakeLists.txt
+++ b/libheif/CMakeLists.txt
@@ -6,6 +6,8 @@ if(UNIX)
   pkg_check_modules (X265 x265)
 endif()
 
+configure_file(heif_version.h.in ${CMAKE_CURRENT_BINARY_DIR}/heif_version.h)
+
 set(libheif_headers
     bitstream.h
     box.h
@@ -21,7 +23,7 @@ set(libheif_headers
     heif_limits.h
     heif_plugin.h
     logging.h
-    ${PROJECT_BINARY_DIR}/libheif/heif_version.h
+    ${CMAKE_CURRENT_BINARY_DIR}/heif_version.h
 )
 
 add_library(${PROJECT_NAME}

From 413f10df0ea4eec6dc1d895ae3c43da0e82d0b21 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@cryptomilk.org>
Date: Tue, 14 Jan 2020 08:52:50 +0100
Subject: [PATCH 5/8] cmake: Use libheif as the project name

We can't use heif or the install path will completely differ from the
autotools which would break builds.
---
 CMakeLists.txt            |  7 +-----
 examples/CMakeLists.txt   | 12 +++++-----
 gdk-pixbuf/CMakeLists.txt |  6 ++++-
 libheif/CMakeLists.txt    | 50 +++++++++++++++++++++------------------
 4 files changed, 39 insertions(+), 36 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 35e218c..71beab1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,9 +1,6 @@
 cmake_minimum_required (VERSION 3.3.2)
 
-project (heif
-    LANGUAGES C CXX
-    VERSION 1.6.1.0
-)
+project(libheif LANGUAGES C CXX VERSION 1.6.1.0)
 
 # https://cmake.org/cmake/help/v3.1/policy/CMP0054.html
 cmake_policy(SET CMP0054 NEW)
@@ -51,8 +48,6 @@ if (has_potentially_evaluated_expression)
   add_definitions(-Wno-error=potentially-evaluated-expression)
 endif()
 
-include_directories ("${PROJECT_SOURCE_DIR}")
-
 add_subdirectory (examples)
 add_subdirectory (libheif)
 add_subdirectory (gdk-pixbuf)
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index d5d4013..c5d30e5 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1,5 +1,5 @@
 # Needed to find libheif/heif_version.h while compiling the library
-include_directories(${PROJECT_BINARY_DIR})
+include_directories(${libheif_BINARY_DIR} ${libheif_SOURCE_DIR})
 
 set (heif_convert_sources
   encoder.cc
@@ -83,16 +83,16 @@ if(MSVC)
 endif()
 
 add_executable (heif-convert ${heif_convert_sources} ${getopt_sources})
-target_link_libraries (heif-convert ${PROJECT_NAME} ${additional_libraries})
+target_link_libraries (heif-convert heif ${additional_libraries})
 
 add_executable (heif-info ${heif_info_sources} ${getopt_sources})
-target_link_libraries (heif-info ${PROJECT_NAME})
+target_link_libraries (heif-info heif)
 
 add_executable (heif-enc ${heif_enc_sources} ${getopt_sources})
-target_link_libraries (heif-enc ${PROJECT_NAME} ${additional_libraries})
+target_link_libraries (heif-enc heif ${additional_libraries})
 
 add_executable (heif-test ${heif_test_sources} ${getopt_sources})
-target_link_libraries (heif-test ${PROJECT_NAME})
+target_link_libraries (heif-test heif)
 
 
 if(LIBPNG_FOUND)
@@ -105,5 +105,5 @@ if(LIBPNG_FOUND)
     )
 
   add_executable (heif-thumbnailer ${heif_thumbnailer_sources})
-  target_link_libraries (heif-thumbnailer ${PROJECT_NAME} ${LIBPNG_LIBRARIES})
+  target_link_libraries (heif-thumbnailer heif ${LIBPNG_LIBRARIES})
 endif()
diff --git a/gdk-pixbuf/CMakeLists.txt b/gdk-pixbuf/CMakeLists.txt
index 754588a..88dd5b6 100644
--- a/gdk-pixbuf/CMakeLists.txt
+++ b/gdk-pixbuf/CMakeLists.txt
@@ -7,7 +7,11 @@ if(UNIX)
 
     add_library(pixbufloader-heif MODULE pixbufloader-heif.c)
 
-    target_include_directories(pixbufloader-heif PRIVATE ${GDKPIXBUF2_INCLUDE_DIRS} ${PROJECT_BINARY_DIR})
+    target_include_directories(pixbufloader-heif
+                               PRIVATE
+                                   ${GDKPIXBUF2_INCLUDE_DIRS}
+                                   ${libheif_BINARY_DIR}
+                                   ${libheif_SOURCE_DIR})
     target_link_libraries(pixbufloader-heif PUBLIC ${GDKPIXBUF2_LIBRARIES} ${LIBHEIF_LIBRARY_NAME})
 
     install(TARGETS pixbufloader-heif LIBRARY DESTINATION ${GDKPIXBUF2_MODULE_DIR})
diff --git a/libheif/CMakeLists.txt b/libheif/CMakeLists.txt
index 0317038..83f63d6 100644
--- a/libheif/CMakeLists.txt
+++ b/libheif/CMakeLists.txt
@@ -26,7 +26,7 @@ set(libheif_headers
     ${CMAKE_CURRENT_BINARY_DIR}/heif_version.h
 )
 
-add_library(${PROJECT_NAME}
+add_library(heif
     bitstream.cc
     box.cc
     error.cc
@@ -42,27 +42,31 @@ add_library(${PROJECT_NAME}
 )
 
 # Needed to find libheif/heif_version.h while compiling the library
-target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_BINARY_DIR})
+target_include_directories(heif PRIVATE ${libheif_BINARY_DIR} ${libheif_SOURCE_DIR})
 
 # Propagate include/libheif to consume the headers from other projects
-target_include_directories(${PROJECT_NAME} PUBLIC
-    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}>
-    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
-    $<INSTALL_INTERFACE:include>
-)
-
-target_compile_definitions(${PROJECT_NAME}
-    PUBLIC 
-        LIBHEIF_EXPORTS
-        HAVE_VISIBILITY
-)
+target_include_directories(heif
+                           PUBLIC
+                               $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}>
+                               $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
+                               $<INSTALL_INTERFACE:include>)
+
+set_target_properties(heif
+                      PROPERTIES
+                          VERSION ${PROJECT_VERSION}
+                          SOVERSION ${PROJECT_VERSION_MAJOR})
+
+target_compile_definitions(heif
+                           PUBLIC
+                               LIBHEIF_EXPORTS
+                               HAVE_VISIBILITY)
 
 if(LIBDE265_FOUND)
-    target_compile_definitions(${PROJECT_NAME} PRIVATE HAVE_LIBDE265=1)
-    target_sources(${PROJECT_NAME} PRIVATE
-        heif_decoder_libde265.cc
-        heif_decoder_libde265.h
-    )
+    target_compile_definitions(heif PRIVATE HAVE_LIBDE265=1)
+    target_sources(heif
+                   PRIVATE
+                       heif_decoder_libde265.cc
+                       heif_decoder_libde265.h)
 
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBDE265_CFLAGS}")
 
@@ -70,14 +74,14 @@ if(LIBDE265_FOUND)
         set(LIBDE265_LINKDIR "-L${LIBDE265_LIBRARY_DIRS}")
     endif()
 
-    target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBDE265_LIBRARIES} ${LIBDE265_LINKDIR})
+    target_link_libraries(heif PRIVATE ${LIBDE265_LIBRARIES} ${LIBDE265_LINKDIR})
 
     install(FILES heif_decoder_libde265.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
 endif()
 
 if(X265_FOUND)
-    target_compile_definitions(${PROJECT_NAME} PRIVATE HAVE_X265=1)
-    target_sources(${PROJECT_NAME} PRIVATE
+    target_compile_definitions(heif PRIVATE HAVE_X265=1)
+    target_sources(heif PRIVATE
         heif_encoder_x265.cc
         heif_encoder_x265.h
     )
@@ -87,14 +91,14 @@ if(X265_FOUND)
         set(X265_LINKDIR "-L${X265_LIBRARY_DIRS}")
     endif()
 
-    target_link_libraries(${PROJECT_NAME} PRIVATE ${X265_LIBRARIES} ${X265_LINKDIR})
+    target_link_libraries(heif PRIVATE ${X265_LIBRARIES} ${X265_LINKDIR})
 
     install(FILES heif_encoder_x265.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
 endif()
 
 write_basic_package_version_file(${PROJECT_NAME}-config-version.cmake COMPATIBILITY ExactVersion)
 
-install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}-config
+install(TARGETS heif EXPORT ${PROJECT_NAME}-config
     RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
     LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
     ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}

From 416ddca07c47a678514c032dec835a7f323bf09f Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@cryptomilk.org>
Date: Mon, 13 Jan 2020 17:39:11 +0100
Subject: [PATCH 6/8] cmake: Use find_package for finding PkgConfig

---
 gdk-pixbuf/CMakeLists.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/gdk-pixbuf/CMakeLists.txt b/gdk-pixbuf/CMakeLists.txt
index 88dd5b6..e647ea4 100644
--- a/gdk-pixbuf/CMakeLists.txt
+++ b/gdk-pixbuf/CMakeLists.txt
@@ -1,6 +1,6 @@
 if(UNIX)
-  include (${CMAKE_ROOT}/Modules/FindPkgConfig.cmake)
-  pkg_check_modules (GDKPIXBUF2 gdk-pixbuf-2.0)
+  find_package(PkgConfig)
+  pkg_check_modules(GDKPIXBUF2 gdk-pixbuf-2.0)
 
   if(GDKPIXBUF2_FOUND)
     execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} gdk-pixbuf-2.0 --variable gdk_pixbuf_moduledir OUTPUT_VARIABLE GDKPIXBUF2_MODULE_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)

From 724f22dcbd03ba2391539a75f375466c9c57d607 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@cryptomilk.org>
Date: Mon, 13 Jan 2020 17:53:04 +0100
Subject: [PATCH 7/8] cmake: Create libheif pkgconfig file

---
 CMakeLists.txt         | 28 ++++++++++++++++++++++++++++
 libheif/CMakeLists.txt |  6 ------
 2 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 71beab1..73b53f7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -48,6 +48,34 @@ if (has_potentially_evaluated_expression)
   add_definitions(-Wno-error=potentially-evaluated-expression)
 endif()
 
+if (UNIX)
+  find_package(PkgConfig)
+  pkg_check_modules(LIBDE265 libde265)
+  pkg_check_modules(X265 x265)
+endif()
+
+# Create libheif pkgconfig file
+set(prefix ${CMAKE_INSTALL_PREFIX})
+set(exec_prefix ${CMAKE_INSTALL_PREFIX})
+set(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
+set(includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
+if (LIBDE265_FOUND)
+    set(have_libde265 yes)
+else()
+    set(have_libde265 no)
+endif()
+if (X265_FOUND)
+    set(have_x265 yes)
+else()
+    set(have_x265 no)
+endif()
+set(VERSION ${PROJECT_VERSION})
+
+configure_file(libheif.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libheif.pc @ONLY)
+
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libheif.pc
+        DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
+
 add_subdirectory (examples)
 add_subdirectory (libheif)
 add_subdirectory (gdk-pixbuf)
diff --git a/libheif/CMakeLists.txt b/libheif/CMakeLists.txt
index 83f63d6..874cb39 100644
--- a/libheif/CMakeLists.txt
+++ b/libheif/CMakeLists.txt
@@ -1,11 +1,5 @@
 include(CMakePackageConfigHelpers)
 
-if(UNIX)
-  include (${CMAKE_ROOT}/Modules/FindPkgConfig.cmake)
-  pkg_check_modules (LIBDE265 libde265)
-  pkg_check_modules (X265 x265)
-endif()
-
 configure_file(heif_version.h.in ${CMAKE_CURRENT_BINARY_DIR}/heif_version.h)
 
 set(libheif_headers

From 1079ad0d1e10930dc93c236fe75644ce0c90c78a Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@cryptomilk.org>
Date: Thu, 9 Jan 2020 10:11:44 +0100
Subject: [PATCH 8/8] cmake: Generate compile database

---
 CMakeLists.txt | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 73b53f7..f10efac 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -40,6 +40,9 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
 
 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
 
+# Create the compile command database for clang by default
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+
 option(BUILD_SHARED_LIBS "Build shared libraries" ON)
 
 include(CheckCXXCompilerFlag)