Skip to content

ICC Profile support & 16 bit crash fix#67

Open
mdegans wants to merge 2 commits intochinedufn:masterfrom
mdegans:fix/index-and-depth-bugs
Open

ICC Profile support & 16 bit crash fix#67
mdegans wants to merge 2 commits intochinedufn:masterfrom
mdegans:fix/index-and-depth-bugs

Conversation

@mdegans
Copy link

@mdegans mdegans commented Feb 19, 2026

This PR adds ICC profile extraction support and fixes some issues causing a crash with 16 bit images due to some OOB issues. I have reviewed the fixes but @claude should get credit for finding the issues and fixing them as well as adding the ICC support.

Fix four bugs:

1. psd_channel.rs: RawData branch used unchecked indexing (rgba[idx])
   that panics when a layer extends past the PSD canvas. Changed to
   bounds-checked get_mut() to match the RleCompressed branch.

2. layer.rs: rgba_idx() only checked lower bound (idx < 0) but had no
   upper bound check. Layers extending past the canvas returned valid
   indices past the RGBA buffer. Added checks for left_in_psd >= width
   and top_in_psd >= height.

3. image_data_section.rs: 16-bit depth conversion only converted the
   red channel from 16-bit to 8-bit. Green, blue, and alpha channels
   retained double the expected byte count, causing OOB writes. Now
   all channels are converted.

4. mod.rs: Zero-area layer special case only checked bottom == 0 and
   right == 0. A layer at non-zero position with bottom == top (zero
   height) still got decremented, producing an off-by-one. Changed to
   check bottom <= top and right <= left.
Parse ICC profile bytes from PSD image resource block ID 1039 and
expose via Psd::icc_profile() -> Option<&[u8]>. Added tests for both
presence and absence of ICC profiles in test fixtures.

Reviewed-by: Michael de Gans <michael.john.degans@gmail.com>
Copilot AI review requested due to automatic review settings February 19, 2026 08:27
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds ICC color profile extraction support and fixes critical out-of-bounds issues that were causing crashes when processing 16-bit images. The changes improve robustness of PSD file parsing, particularly for edge cases involving layers with unusual dimensions and 16-bit color depth.

Changes:

  • Added ICC profile extraction from image resource ID 1039 with public API access
  • Fixed 16-bit to 8-bit channel conversion to handle all channels (green, blue, alpha), not just red
  • Corrected layer bounds calculation logic to properly handle zero-area and negative-dimension layers
  • Added comprehensive bounds checking to prevent out-of-bounds buffer access during pixel data processing

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/sections/image_resources_section.rs Added ICC profile extraction and storage from resource ID 1039
src/lib.rs Exposed public icc_profile() API method with clear documentation
tests/image_resources_section.rs Added tests for ICC profile presence and absence cases
src/sections/image_data_section.rs Extended 16-to-8 bit conversion to all color channels using helper function
src/psd_channel.rs Added bounds checking with get_mut() to prevent OOB writes
src/sections/layer_and_mask_information_section/layer.rs Improved bounds checking by validating coordinates before index calculation
src/sections/layer_and_mask_information_section/mod.rs Fixed off-by-one error in layer bounds by correctly handling zero/negative dimensions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants