ICC Profile support & 16 bit crash fix#67
Open
mdegans wants to merge 2 commits intochinedufn:masterfrom
Open
Conversation
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>
There was a problem hiding this comment.
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.
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.
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.