Enable native libpng support to fix truncated PNG output for large images#28
Open
changhee-somite wants to merge 1 commit intoseqscope:mainfrom
Open
Conversation
…ages 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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
draw-xycommandcimg_use_pngcompile definitionProblem
When
cimg_use_pngis not defined, CImg falls back to ImageMagick'sconvertcommand for PNG encoding. ImageMagick has policy-enforced resource limits (typically 256MP area, 1GiB memory in/etc/ImageMagick-6/policy.xml) that cause silent failures when processing large images.For example, an 11460×22360 pixel image (256.3 megapixels) exceeds the default 256MP limit, resulting in a truncated PNG file containing only header chunks (IHDR, cHRM, bKGD) but no image data (IDAT) or end marker (IEND). This causes downstream tools like GDAL to fail with
ERROR 1: libpng: Read Error.Root Cause Analysis
Traced via
strace:The CImg → ImageMagick pipeline:
convert -quality 100 /tmp/xxx.ppm output.pngSolution
Enable native libpng support so CImg writes PNG directly without ImageMagick:
find_package(PNG REQUIRED)- locate system libpngadd_compile_definitions(cimg_use_png)- enable CImg's native PNG code pathPNG::PNGto the executableTest Results
Tested with a 675M line Xenium dataset producing an 11460×22360 PNG:
libpng: Read ErrorTest plan
-DUSE_LIBDEFLATE=ONspatula draw-xyon large dataset (>256 megapixels)gdal_translate)🤖 Generated with Claude Code