|
| 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 | +} |
0 commit comments