Skip to content

Commit fabfd5b

Browse files
Added a couple of tests for CicpProfile
1 parent b12bada commit fabfd5b

4 files changed

Lines changed: 82 additions & 0 deletions

File tree

src/ImageSharp/Formats/Png/PngEncoderCore.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,12 @@ private void WriteCicpChunk(Stream stream, ImageMetadata metaData)
786786
return;
787787
}
788788

789+
// by spec, the matrix coefficients must be set to Identity
790+
if (metaData.CicpProfile.MatrixCoefficients != Metadata.Profiles.CICP.CicpMatrixCoefficients.Identity)
791+
{
792+
throw new NotSupportedException("CICP matrix coefficients other than Identity are not supported in PNG");
793+
}
794+
789795
Span<byte> outputBytes = this.chunkDataBuffer.Span[..4];
790796
outputBytes[0] = (byte)metaData.CicpProfile.ColorPrimaries;
791797
outputBytes[1] = (byte)metaData.CicpProfile.TransferCharacteristics;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using SixLabors.ImageSharp.Formats;
5+
using SixLabors.ImageSharp.Formats.Png;
6+
using SixLabors.ImageSharp.Metadata.Profiles.CICP;
7+
using SixLabors.ImageSharp.PixelFormats;
8+
9+
namespace SixLabors.ImageSharp.Tests.Metadata.Profiles.Cicp;
10+
11+
public class CicpProfileTests
12+
{
13+
[Theory]
14+
[WithFile(TestImages.Png.AdamHeadsHlg, PixelTypes.Rgba64)]
15+
public async Task ReadCicpMetadata_FromPng_Works<TPixel>(TestImageProvider<TPixel> provider)
16+
where TPixel : unmanaged, IPixel<TPixel>
17+
{
18+
using (Image<TPixel> image = await provider.GetImageAsync(PngDecoder.Instance))
19+
{
20+
CicpProfile actual = image.Metadata.CicpProfile ?? image.Frames.RootFrame.Metadata.CicpProfile;
21+
CicpProfileContainsExpectedValues(actual);
22+
}
23+
}
24+
25+
[Fact]
26+
public void WritingPng_PreservesCicpProfile()
27+
{
28+
// arrange
29+
using var image = new Image<Rgba32>(1, 1);
30+
var original = new CicpProfile()
31+
{
32+
ColorPrimaries = CicpColorPrimaries.ItuRBt2020_2,
33+
TransferCharacteristics = CicpTransferCharacteristics.SmpteSt2084,
34+
MatrixCoefficients = CicpMatrixCoefficients.Identity,
35+
FullRange = true,
36+
};
37+
image.Metadata.CicpProfile = original;
38+
var encoder = new PngEncoder();
39+
40+
// act
41+
using Image<Rgba32> reloadedImage = WriteAndRead(image, encoder);
42+
43+
// assert
44+
CicpProfile actual = reloadedImage.Metadata.CicpProfile ?? reloadedImage.Frames.RootFrame.Metadata.CicpProfile;
45+
Assert.NotNull(actual);
46+
Assert.Equal(actual.ColorPrimaries, original.ColorPrimaries);
47+
Assert.Equal(actual.TransferCharacteristics, original.TransferCharacteristics);
48+
Assert.Equal(actual.MatrixCoefficients, original.MatrixCoefficients);
49+
Assert.Equal(actual.FullRange, original.FullRange);
50+
}
51+
52+
private static void CicpProfileContainsExpectedValues(CicpProfile cicp)
53+
{
54+
Assert.NotNull(cicp);
55+
Assert.Equal(CicpColorPrimaries.ItuRBt2020_2, cicp.ColorPrimaries);
56+
Assert.Equal(CicpTransferCharacteristics.AribStdB67, cicp.TransferCharacteristics);
57+
Assert.Equal(CicpMatrixCoefficients.Identity, cicp.MatrixCoefficients);
58+
Assert.True(cicp.FullRange);
59+
}
60+
61+
private static Image<Rgba32> WriteAndRead(Image<Rgba32> image, IImageEncoder encoder)
62+
{
63+
using (var memStream = new MemoryStream())
64+
{
65+
image.Save(memStream, encoder);
66+
image.Dispose();
67+
68+
memStream.Position = 0;
69+
return Image.Load<Rgba32>(memStream);
70+
}
71+
}
72+
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public static class Png
6161
public const string TestPattern31x31 = "Png/testpattern31x31.png";
6262
public const string TestPattern31x31HalfTransparent = "Png/testpattern31x31-halftransparent.png";
6363
public const string XmpColorPalette = "Png/xmp-colorpalette.png";
64+
public const string AdamHeadsHlg = "Png/adamHeadsHLG.png";
6465

6566
// Animated
6667
// https://philip.html5.org/tests/apng/tests.html
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)