From 76d27a352c063341126e2b990636dacf3b65ca29 Mon Sep 17 00:00:00 2001 From: TheAssassin Date: Mon, 25 Jun 2018 17:37:21 +0200 Subject: [PATCH] Replace ImageMagick with CImg For now, it is known to support JPEG and PNG files. Fixes #3. --- .travis.yml | 3 +- cmake/Modules/FindCImg.cmake | 22 ++++++++++++++ src/core/CMakeLists.txt | 7 ++--- src/core/appdir.cpp | 59 ++++++++++++++++++------------------ 4 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 cmake/Modules/FindCImg.cmake diff --git a/.travis.yml b/.travis.yml index 65477d8..b1aef40 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,8 +7,7 @@ addons: packages: - libboost-regex1.55-dev - libboost-filesystem1.55-dev - - libmagick++-dev - - libmagic-dev + - cimg-dev - automake # required for patchelf install: diff --git a/cmake/Modules/FindCImg.cmake b/cmake/Modules/FindCImg.cmake new file mode 100644 index 0000000..b441bc3 --- /dev/null +++ b/cmake/Modules/FindCImg.cmake @@ -0,0 +1,22 @@ +# required for PNG imported target +cmake_minimum_required(VERSION 3.5) + +message(STATUS "Searching for CImg") + +find_path(CIMG_H_DIR + NAMES CImg.h + HINTS ${CMAKE_INSTALL_PREFIX} + PATH_SUFFIXES include include/linux +) + +if(NOT CIMG_H_DIR) + message(FATAL_ERROR "CImg.h not found") +endif() + +find_package(PNG REQUIRED) +find_package(JPEG REQUIRED) + +add_library(CImg INTERFACE IMPORTED) +set_property(TARGET CImg PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CIMG_H_DIR};${JPEG_INCLUDE_DIR}") +set_property(TARGET CImg PROPERTY INTERFACE_LINK_LIBRARIES "PNG::PNG;${JPEG_LIBRARIES}") +set_property(TARGET CImg PROPERTY INTERFACE_COMPILE_DEFINITIONS "cimg_display=0;cimg_use_png=1;cimg_use_jpeg=1") diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index aa003d5..e5defd2 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -7,10 +7,9 @@ file(GLOB HEADERS ${PROJECT_SOURCE_DIR}/include/linuxdeploy/core/*.h) find_package(Threads) -find_package(LibMagic) +find_package(LibMagic REQUIRED) -find_package(PkgConfig) -pkg_check_modules(magick++ REQUIRED IMPORTED_TARGET Magick++) +find_package(CImg REQUIRED) message(STATUS "Generating excludelist") execute_process( @@ -19,7 +18,7 @@ execute_process( ) add_library(linuxdeploy_core STATIC elf.cpp log.cpp appdir.cpp desktopfile.cpp ${HEADERS}) -target_link_libraries(linuxdeploy_core PUBLIC linuxdeploy_plugin linuxdeploy_util ${BOOST_LIBS} subprocess cpp-feather-ini-parser PkgConfig::magick++ libmagic_static ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(linuxdeploy_core PUBLIC linuxdeploy_plugin linuxdeploy_util ${BOOST_LIBS} subprocess cpp-feather-ini-parser CImg libmagic_static ${CMAKE_THREAD_LIBS_INIT}) target_include_directories(linuxdeploy_core PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(linuxdeploy_core PUBLIC ${PROJECT_SOURCE_DIR}/include) target_compile_definitions(linuxdeploy_core PUBLIC -DBOOST_NO_CXX11_SCOPED_ENUMS) diff --git a/src/core/appdir.cpp b/src/core/appdir.cpp index 7d6e32f..a8b060f 100644 --- a/src/core/appdir.cpp +++ b/src/core/appdir.cpp @@ -5,7 +5,7 @@ // library headers #include -#include +#include #include #include @@ -19,6 +19,7 @@ using namespace linuxdeploy::core; using namespace linuxdeploy::core::log; +using namespace cimg_library; namespace bf = boost::filesystem; namespace linuxdeploy { @@ -340,44 +341,42 @@ namespace linuxdeploy { if (util::strLower(path.filename().extension().string()) == ".svg") { resolution = "scalable"; } else { - Magick::Image image; - try { - image.read(path.string()); - } catch (const Magick::Exception& e) { - ldLog() << LD_ERROR << "Magick error: " << e.what() << std::endl; - return false; - } + CImg image(path.c_str()); - auto xRes = image.columns(); - auto yRes = image.rows(); + auto xRes = image.width(); + auto yRes = image.height(); - if (xRes != yRes) { - ldLog() << LD_WARNING << "x and y resolution of icon are not equal:" << path; - } + if (xRes != yRes) { + ldLog() << LD_WARNING << "x and y resolution of icon are not equal:" << path; + } - resolution = std::to_string(xRes) + "x" + std::to_string(yRes); + resolution = std::to_string(xRes) + "x" + std::to_string(yRes); - // otherwise, test resolution against "known good" values, and reject invalid ones - const auto knownResolutions = {8, 16, 20, 22, 24, 32, 48, 64, 72, 96, 128, 192, 256, 512}; + // otherwise, test resolution against "known good" values, and reject invalid ones + const auto knownResolutions = {8, 16, 20, 22, 24, 32, 48, 64, 72, 96, 128, 192, 256, 512}; - // assume invalid - bool invalidXRes = true, invalidYRes = true; + // assume invalid + bool invalidXRes = true, invalidYRes = true; - for (const auto res : knownResolutions) { - if (xRes == res) - invalidXRes = false; - if (yRes == res) - invalidYRes = false; - } + for (const auto res : knownResolutions) { + if (xRes == res) + invalidXRes = false; + if (yRes == res) + invalidYRes = false; + } - if (invalidXRes) { - ldLog() << LD_ERROR << "Icon" << path << "has invalid x resolution:" << xRes; - return false; - } + if (invalidXRes) { + ldLog() << LD_ERROR << "Icon" << path << "has invalid x resolution:" << xRes; + return false; + } - if (invalidYRes) { - ldLog() << LD_ERROR << "Icon" << path << "has invalid x resolution:" << xRes; + if (invalidYRes) { + ldLog() << LD_ERROR << "Icon" << path << "has invalid x resolution:" << xRes; + return false; + } + } catch (const CImgException& e) { + ldLog() << LD_ERROR << "CImg error: " << e.what() << std::endl; return false; } }