Skip to content

Improvements to Apple CgBI PNG handling#3137

Draft
Erik-White wants to merge 9 commits into
SixLabors:mainfrom
Erik-White:png-cgbi-improvements
Draft

Improvements to Apple CgBI PNG handling#3137
Erik-White wants to merge 9 commits into
SixLabors:mainfrom
Erik-White:png-cgbi-improvements

Conversation

@Erik-White

@Erik-White Erik-White commented May 27, 2026

Copy link
Copy Markdown
Contributor

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

Following on from #3136 , this attempts to streamline and improve the related code as well as improve test coverage.

  • CgBI images that are not supported are now explicitly rejected (i.e. bit depth != 8 or truecolor)
  • Broke out ChunkedReadStream from ZlibInflateStream to separate the concerns instead of mixing modes in ZlibInflateStream
  • Broke out CgBI transform from PngDecoderCore into new PngCgbiProcessor static class
  • Added tests for PngCgbiProcessor
  • Small improvements to the CgBI shuffle methods (e.g. construct the shuffle patterns only once, remove redundant floor(), replace magic numbers where possible)

@Erik-White

Copy link
Copy Markdown
Contributor Author

Curious to hear your thoughts. The changes should all be relatively self-contained per commit in case there are sections that you don't think should be included.

@JimBobSquarePants

Copy link
Copy Markdown
Member

This looks like a lot of changes. Will take time to review

@Erik-White

Copy link
Copy Markdown
Contributor Author

Understood. As I mentioned in the description, each commit is fairly self contained but I could also break up the PR into a couple of smaller units if that would be helpful?

[InlineData(32)]
[InlineData(33)]
[InlineData(64)]
public void ApplyTransformVector512_MatchesScalar(int pixelCount) =>

@JimBobSquarePants JimBobSquarePants Jun 11, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We have FeatureTestRunner that we use for this kind of stuff, so we are not running tests unnecessarily

};

// CgBI premultiplied BGRA: c' = c * a / 255
byte r = (byte)((p * 13) & 0xFF);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is screaming AI to me. random numbers with no explanation.

[MemberNotNullWhen(true, nameof(CompressedStream))]
private bool InitializeInflateStream(bool isCriticalChunk)
{
// Apple CgBI IDATs omit the zlib CMF/FLG header and the Adler-32 trailer,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we no longer return early?

/// Adler-32 trailer is not validated.
/// </summary>
internal sealed class ZlibInflateStream : Stream
internal sealed class ZlibInflateStream : IDisposable

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure I'm comfortable with a type suffixed Stream that is no longer inherits Stream. This feels like we're violating runtime design guidelines.

@JimBobSquarePants

Copy link
Copy Markdown
Member

replace magic numbers where possible
What magic numbers?

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.

2 participants