From 29423a7973f8e87bc3992eec94458f7fd774023e Mon Sep 17 00:00:00 2001 From: ChangHee Lee Date: Mon, 2 Feb 2026 14:44:36 +0000 Subject: [PATCH] Enable native libpng support to fix truncated PNG output for large images When cimg_use_png is not defined, CImg falls back to ImageMagick's convert command for PNG encoding. ImageMagick has policy-enforced resource limits (typically 256MP area, 1GiB memory) that cause silent failures when processing large images. For example, an 11460x22360 pixel image (256.3 megapixels) exceeds the default 256MP limit, resulting in a truncated PNG file with only the header chunks (IHDR, cHRM, bKGD) but no image data (IDAT) or end marker (IEND). This fix: - Enables find_package(PNG) to locate the system libpng - Adds cimg_use_png compile definition so CImg uses native libpng - Links PNG::PNG to the executable With this change, CImg writes PNG files directly using libpng, bypassing ImageMagick entirely and eliminating the resource limit issue. Tested with a 675M line dataset producing an 11460x22360 PNG: - Before: 91 bytes (truncated, GDAL fails with "libpng: Read Error") - After: 98MB valid PNG (GDAL processes successfully) Co-Authored-By: Claude Opus 4.5 --- CMakeLists.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 014ce0a..0a9d85b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,7 +64,8 @@ if(NOT QGEN_LIBRARIES) message(FATAL_ERROR "libqgen QGEN_LIBRARIES not found") endif() -# find_package(PNG REQUIRED) +# Enable libpng for CImg to avoid ImageMagick resource limits on large images +find_package(PNG REQUIRED) # find_package(ZLIB REQUIRED) message(STATUS ${QGEN_LIBRARIES}) @@ -191,16 +192,15 @@ if(NOT CRYPTOLIB) message(FATAL_ERROR "libcrypto library not found") endif() -# # add PNG library -# find_library(PNGLIB png HINTS /usr/lib/x86_64-linux-gnu/ /usr/lib/ /usr/lib64/) -# if(NOT PNGLIB) -# message(FATAL_ERROR "libpng library not found") -# endif() +# PNG library is found via find_package(PNG) above +# Add cimg_use_png define to enable native PNG support in CImg +# This avoids ImageMagick resource limits that cause truncated PNGs for large images +add_compile_definitions(cimg_use_png) #TARGET_LINK_LIBRARIES(${APP_EXE} PRIVATE ${QGEN_LIBRARIES} ${HTS_LIBRARIES} ${ZLIB} ${LZMA} ${BZIP2} ${CURLLIB} ${DEFLATELIB} ${CRYPTOLIB} "${OpenMP_CXX_FLAGS}") #target_compile_options(${APP_EXE} PRIVATE "${OpenMP_CXX_FLAGS}") #TARGET_LINK_LIBRARIES(${APP_EXE} PRIVATE ${QGEN_LIBRARIES} ${HTS_LIBRARIES} ${ZLIB} ${LZMA} ${BZIP2} ${CURLLIB} ${CRYPTOLIB}) -TARGET_LINK_LIBRARIES(${APP_EXE} PRIVATE ${QGEN_LIBRARIES} ${HTS_LIBRARIES} ${ZLIB} ${LZMA} ${BZIP2} ${CURLLIB} ${CRYPTOLIB} ${DEFLATELIB}) +TARGET_LINK_LIBRARIES(${APP_EXE} PRIVATE ${QGEN_LIBRARIES} ${HTS_LIBRARIES} ${ZLIB} ${LZMA} ${BZIP2} ${CURLLIB} ${CRYPTOLIB} ${DEFLATELIB} PNG::PNG) target_compile_options(${APP_EXE} PRIVATE) install(TARGETS ${APP_EXE} RUNTIME DESTINATION bin)