Skip to content

Commit 77a59ee

Browse files
Merge pull request #1395 from SixLabors/js/fix-935
Auto repair Png options to use Bit8.
2 parents 60eba39 + d4fb78b commit 77a59ee

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public static void AdjustOptions<TPixel>(
3535
options.ColorType ??= pngMetadata.ColorType ?? SuggestColorType<TPixel>();
3636
options.BitDepth ??= pngMetadata.BitDepth ?? SuggestBitDepth<TPixel>();
3737

38+
// Ensure bit depth and color type are a supported combination.
39+
// Bit8 is the only bit depth supported by all color types.
40+
byte bits = (byte)options.BitDepth;
41+
byte[] validBitDepths = PngConstants.ColorTypes[options.ColorType.Value];
42+
if (Array.IndexOf(validBitDepths, bits) == -1)
43+
{
44+
options.BitDepth = PngBitDepth.Bit8;
45+
}
46+
3847
options.InterlaceMethod ??= pngMetadata.InterlaceMethod;
3948

4049
use16Bit = options.BitDepth == PngBitDepth.Bit16;
@@ -44,12 +53,6 @@ public static void AdjustOptions<TPixel>(
4453
{
4554
options.ChunkFilter = PngChunkFilter.ExcludeAll;
4655
}
47-
48-
// Ensure we are not allowing impossible combinations.
49-
if (!PngConstants.ColorTypes.ContainsKey(options.ColorType.Value))
50-
{
51-
throw new NotSupportedException("Color type is not supported or not valid.");
52-
}
5356
}
5457

5558
/// <summary>
@@ -68,15 +71,10 @@ public static IndexedImageFrame<TPixel> CreateQuantizedFrame<TPixel>(
6871
return null;
6972
}
7073

71-
byte bits = (byte)options.BitDepth;
72-
if (Array.IndexOf(PngConstants.ColorTypes[options.ColorType.Value], bits) == -1)
73-
{
74-
throw new NotSupportedException("Bit depth is not supported or not valid.");
75-
}
76-
7774
// Use the metadata to determine what quantization depth to use if no quantizer has been set.
7875
if (options.Quantizer is null)
7976
{
77+
byte bits = (byte)options.BitDepth;
8078
var maxColors = ImageMaths.GetColorCountForBitDepth(bits);
8179
options.Quantizer = new WuQuantizer(new QuantizerOptions { MaxColors = maxColors });
8280
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,17 @@ static void RunTest(string serialized)
556556
provider);
557557
}
558558

559+
[Fact]
560+
public void EncodeFixesInvalidOptions()
561+
{
562+
// https://github.com/SixLabors/ImageSharp/issues/935
563+
using var ms = new MemoryStream();
564+
var testFile = TestFile.Create(TestImages.Png.Issue935);
565+
using Image<Rgba32> image = testFile.CreateRgba32Image(new PngDecoder());
566+
567+
image.Save(ms, new PngEncoder { ColorType = PngColorType.RgbWithAlpha });
568+
}
569+
559570
private static void TestPngEncoderCore<TPixel>(
560571
TestImageProvider<TPixel> provider,
561572
PngColorType pngColorType,

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public static class Png
107107
public const string Issue1177_1 = "Png/issues/Issue_1177_1.png";
108108
public const string Issue1177_2 = "Png/issues/Issue_1177_2.png";
109109

110+
// Issue 935: https://github.com/SixLabors/ImageSharp/issues/935
111+
public const string Issue935 = "Png/issues/Issue_935.png";
112+
110113
public static class Bad
111114
{
112115
public const string MissingDataChunk = "Png/xdtn0g01.png";
870 Bytes
Loading

0 commit comments

Comments
 (0)