Skip to content

Commit 3229351

Browse files
Merge pull request #3034 from Socolin/fix-3033
Fix PixelColorType.YCbCr wrongly interpreted as grayscale in PngEncoder
2 parents bca2fbc + 4208deb commit 3229351

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/ImageSharp/Formats/Png/PngMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static PngMetadata FromFormatConnectingMetadata(FormatConnectingMetadata
111111
color = PngColorType.Rgb;
112112
break;
113113
default:
114-
if (colorType.HasFlag(PixelColorType.Luminance))
114+
if (colorType.HasFlag(PixelColorType.Luminance | PixelColorType.Alpha))
115115
{
116116
color = PngColorType.GrayscaleWithAlpha;
117117
break;

tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,29 @@ public void Identify_ReadsLegacyExifData(string imagePath)
336336

337337
Assert.Equal(42, (int)exif.GetValue(ExifTag.ImageNumber).Value);
338338
}
339+
340+
341+
[Theory]
342+
[InlineData(PixelColorType.Binary, PngColorType.Palette)]
343+
[InlineData(PixelColorType.Indexed, PngColorType.Palette)]
344+
[InlineData(PixelColorType.Luminance, PngColorType.Grayscale)]
345+
[InlineData(PixelColorType.RGB, PngColorType.Rgb)]
346+
[InlineData(PixelColorType.BGR, PngColorType.Rgb)]
347+
[InlineData(PixelColorType.YCbCr, PngColorType.RgbWithAlpha)]
348+
[InlineData(PixelColorType.CMYK, PngColorType.RgbWithAlpha)]
349+
[InlineData(PixelColorType.YCCK, PngColorType.RgbWithAlpha)]
350+
public void FromFormatConnectingMetadata_ConvertColorTypeAsExpected(PixelColorType pixelColorType, PngColorType expectedPngColorType)
351+
{
352+
FormatConnectingMetadata formatConnectingMetadata = new()
353+
{
354+
PixelTypeInfo = new PixelTypeInfo(24)
355+
{
356+
ColorType = pixelColorType,
357+
},
358+
};
359+
360+
PngMetadata actual = PngMetadata.FromFormatConnectingMetadata(formatConnectingMetadata);
361+
362+
Assert.Equal(expectedPngColorType, actual.ColorType);
363+
}
339364
}

0 commit comments

Comments
 (0)