-
-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PngWriteDefines to Magick.NET #1661
base: main
Are you sure you want to change the base?
Conversation
- This enum defines flags for specifying PNG chunks to include or exclude. - The flags are based on the documentation found at https://imagemagick.org/script/command-line-options.php#strip - Includes chunks such as bKGD, cHRM, EXIF, gAMA, iCCP, iTXt, sRGB, tEXt, zCCP, zTXt, and date.
…PNG specification naming conventions.
…arnings - Updated XML comments for PreserveColorMap property to resolve SA1623. - Removed an empty line to resolve SA1507.
- Added copyright notice to the header that was previously missing. - Added `#if NETSTANDARD == false` to hide `[Flags]` attribute for non-.NET Standard frameworks. This resolves build failures on Windows ARM64 processors. - Added justification to SuppressMessage in PngChunkFlags "The lowercase names are consistent with the naming conventions of PNG chunk types as defined in the PNG specification."
- Removed bitwise shift operators and used explicit int values for flag definitions.
- Resolved StyleCop error SA1137: Elements should have the same indentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for helping me with this. I left some review comments.
yield return new MagickDefine(Format, "bit-depth", BitDepth.Value); | ||
|
||
if (ColorType.HasValue) | ||
yield return new MagickDefine(Format, "color-type", ColorType.Value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a numeric value and not the name of the color type:
% png:color-type can be 0, 2, 3, 4, or 6.
%
% When png:color-type is 0 (Grayscale), png:bit-depth can
% be 1, 2, 4, 8, or 16.
%
% When png:color-type is 2 (RGB), png:bit-depth can
% be 8 or 16.
%
% When png:color-type is 3 (Indexed), png:bit-depth can
% be 1, 2, 4, or 8. This refers to the number of bits
% used to store the index. The color samples always have
% bit-depth 8 in indexed PNG files.
%
% When png:color-type is 4 (Gray-Matte) or 6 (RGB-Matte),
% png:bit-depth can be 8 or 16.
This also means we probably need a new enumeration for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have created a new PngColorType enum with values for Grayscale, RGB, Indexed, GrayMatte, and RGBMatte. Additionally, I implemented a private method GetPngColorTypeValue to convert these values appropriately, as well as a private method ValidateBitDepth to ensure the bit depth is valid based on the color type.
Here are the key changes:
Enum Creation: Introduced PngColorType enum for clarity and type safety.
Conversion Method: Added GetPngColorTypeValue to map the PngColorType enum to the correct numeric values.
Validation Method: Implemented ValidateBitDepth to check the bit depth against the color type, throwing an ArgumentException for invalid combinations.
I will commit these changes. If any modifications are needed, please let me know. I'm more than happy to make any necessary corrections.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need the complexity to check for invalid combinations. This might change in ImageMagick in the future. We should leave this up to ImageMagick.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your feedback. I've removed the validation as suggested.
if (CompressionLevel.HasValue) | ||
yield return new MagickDefine(Format, "compression-level", CompressionLevel.Value); | ||
|
||
if (CompressionFilter.HasValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put this in alphabetical order.
yield return new MagickDefine(Format, "compression-filter", CompressionFilter.Value); | ||
|
||
if (CompressionStrategy.HasValue) | ||
yield return new MagickDefine(Format, "compression-strategy", CompressionStrategy.Value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need a numeric value here.
if (IncludeChunks.HasValue) | ||
yield return new MagickDefine(Format, "include-chunks", EnumHelper.ConvertFlags(IncludeChunks.Value)); | ||
|
||
if (IgnoreCrc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This option is only used when reading a PNG file.
} | ||
|
||
[Fact] | ||
public void ShouldSetTheDefineNull() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ShouldNotSetTheDefineWhenNull
And please change this in the other files also.
using var image = new MagickImage(); | ||
image.Settings.SetDefines(new PngWriteDefines | ||
{ | ||
BitDepth = 8u, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use an uppercase U
.
public class ThePreserveColorMapPropety | ||
{ | ||
[Fact] | ||
public void ShouldSetTheDefine() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ShouldSetTheDefineWhenSetToTrue
- Alphabetical ordering for CompressionLevel and CompressionFilter. - Converted CompressionFilter and CompressionStrategy to numeric values.
- This option is only used when reading a PNG. - Removed TheIncludeChunksProperty class from PngWriteDefinesTests
…roperty. - Updated the class name to follow the naming convention for property test classes.
…nNull PngWriteDefinesTests. - TheBitDepthProperty - TheColorTypeProperty - TheCompressionFilterProperty - TheExcludeChunksProperty - TheCompressionStrategyProperty
- Updated tests to use numeric values for properties in - TheColorTypeProperty - TheCompressionFilterProperty - ThreCompressionStrategyProperty
…iteDefinesTests..
…ue in ThePreserveColorMapPropety class test
Description
This pull request introduces the PngWriteDefines class to the Magick.NET project. The PngWriteDefines class includes several enums (PngChunkFlags, PngCompressionFilter, PngCompressionStrategy),
Changes Proposed:
Added the PngWriteDefines class with the following properties:
BitDepth - Specifies the bit depth for the PNG image.
ColorType - Specifies the color type for the PNG image.
CompressionFilter - Specifies the compression filter for the PNG image using the PngCompressionFilter enum.
CompressionLevel - Specifies the compression level for the PNG image.
CompressionStrategy - Specifies the compression strategy for the PNG image using the PngCompressionStrategy enum.
ExcludeChunks - Specifies the chunks to be excluded using the PngChunkFlags enum.
IncludeChunks - Specifies the chunks to be included using the PngChunkFlags enum.
IgnoreCrc - Indicates whether the PNG decoder should ignore the CRC.
PreserveICCP - Indicates whether to preserve the ICC profile.
PreserveColormap - Indicates whether to preserve the colormap.