Skip to content

fix(images): cap Q8 output depth at 8 bits - #1681

Open
mohamedelghazali wants to merge 5 commits into
KnifeLemon:mainfrom
mohamedelghazali:fix/cap-q8-image-depth-1547
Open

mohamedelghazali wants to merge 5 commits into
KnifeLemon:mainfrom
mohamedelghazali:fix/cap-q8-image-depth-1547

Conversation

@mohamedelghazali

Copy link
Copy Markdown
Contributor

Summary

Fixes #1547.

Problem

Filee bundles Magick.NET-Q8 (IMagickImage<byte>), which holds 8 bits per channel in memory. When reading a 16-bit source (e.g., 16-bit PNG/TIFF or Camera RAW), ImageMagick keeps image.Depth == 16. Because ImageEncoder never lowered Depth, coders that honor image depth (TIFF, PSD, PSB, PPM, PAM, MIFF, JXL, JP2, and 12-bit AVIF) wrote 16-bit (or 12-bit) output files filled with duplicated 8-bit samples, unnecessarily inflating file sizes.

Root Cause

ImageEncoder.ApplyOptions, ImageEncoder.Write, and ImageEncoder.WriteImage passed image.Depth unchanged to ImageMagick writers.

Changes

  • src/Filee.Engines/Magick/ImageEncoder.cs: Clamp image.Depth to 8 whenever image.Depth > 8 in ApplyOptions, Write (both single-frame and multi-frame paths), and WriteImage.
    • Preserves < 8-bit sources (such as 1-bit monochrome images).
    • Does not affect FITS → TIFF, which bypasses ImageEncoder via FitsRendering.WriteTiff in MagickImageConverter.ConvertAsync.
  • tests/Filee.Engines.Tests/ImageFormatTests.cs: Add regression tests covering 16-bit source conversion across tiff, psd, psb, ppm, pam, miff, jxl, jp2, and avif, plus a unit test verifying ApplyOptions caps 16-bit depth to 8 while preserving 1-bit depth.

Verification

  • Submitted for GitHub Actions CI (dotnet format --verify-no-changes, Windows x64/ARM64 build & tests, Linux build & tests).

Ensure image depth is set to 8 bits for images with higher depth to prevent issues with file writing.
@github-actions github-actions Bot added the bug Something isn't working label Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 16-bit sources are written as 16-bit files that hold only 8-bit data

1 participant