Skip to content

Commit dd395e1

Browse files
Enhance metadata conversion
1 parent f96d287 commit dd395e1

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

src/ImageSharp/Formats/Exr/ExrMetadata.cs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,52 @@ public PixelTypeInfo GetPixelTypeInfo()
9595
}
9696

9797
/// <inheritdoc/>
98-
public FormatConnectingMetadata ToFormatConnectingMetadata() => new()
98+
public FormatConnectingMetadata ToFormatConnectingMetadata()
9999
{
100-
EncodingType = this.Compression is ExrCompression.B44 or ExrCompression.B44A or ExrCompression.Pxr24 ? EncodingType.Lossy : EncodingType.Lossless,
101-
PixelTypeInfo = this.GetPixelTypeInfo()
102-
};
100+
EncodingType type = this.Compression is ExrCompression.B44 or ExrCompression.B44A or ExrCompression.Pxr24
101+
? EncodingType.Lossy
102+
: EncodingType.Lossless;
103+
104+
return new()
105+
{
106+
EncodingType = type,
107+
PixelTypeInfo = this.GetPixelTypeInfo()
108+
};
109+
}
103110

104111
/// <inheritdoc/>
105-
public static ExrMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata) => new() { PixelType = ExrPixelType.Half };
112+
public static ExrMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
113+
{
114+
PixelTypeInfo pixelTypeInfo = metadata.PixelTypeInfo;
115+
PixelComponentInfo? info = pixelTypeInfo.ComponentInfo;
116+
PixelColorType colorType = pixelTypeInfo.ColorType;
117+
118+
int bitsPerComponent = info?.GetMaximumComponentPrecision()
119+
?? (pixelTypeInfo.BitsPerPixel <= 16 ? 16 : 32);
120+
121+
int componentCount = info?.ComponentCount ?? 0;
122+
ExrImageDataType imageDataType = colorType switch
123+
{
124+
PixelColorType.Luminance => ExrImageDataType.Gray,
125+
PixelColorType.RGB or PixelColorType.BGR => ExrImageDataType.Rgb,
126+
PixelColorType.RGB | PixelColorType.Alpha
127+
or PixelColorType.BGR | PixelColorType.Alpha
128+
or PixelColorType.Luminance | PixelColorType.Alpha => ExrImageDataType.Rgba,
129+
_ => componentCount switch
130+
{
131+
>= 4 => ExrImageDataType.Rgba,
132+
>= 3 => ExrImageDataType.Rgb,
133+
1 => ExrImageDataType.Gray,
134+
_ => ExrImageDataType.Unknown,
135+
}
136+
};
137+
138+
return new()
139+
{
140+
PixelType = bitsPerComponent <= 16 ? ExrPixelType.Half : ExrPixelType.Float,
141+
ImageDataType = imageDataType,
142+
};
143+
}
106144

107145
/// <inheritdoc/>
108146
ExrMetadata IDeepCloneable<ExrMetadata>.DeepClone() => new(this);

tests/ImageSharp.Tests/Formats/Exr/ExrMetadataTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using SixLabors.ImageSharp.Formats;
45
using SixLabors.ImageSharp.Formats.Exr;
56
using SixLabors.ImageSharp.Formats.Exr.Constants;
7+
using SixLabors.ImageSharp.PixelFormats;
68

79
namespace SixLabors.ImageSharp.Tests.Formats.Exr;
810

@@ -85,4 +87,32 @@ public void Identify_DetectsCorrectCompression(string imagePath, ExrCompression
8587
Assert.NotNull(metadata);
8688
Assert.Equal(expectedCompression, metadata.Compression);
8789
}
90+
91+
[Theory]
92+
[InlineData(PixelColorType.Binary, 1, ExrImageDataType.Unknown, ExrPixelType.Half)]
93+
[InlineData(PixelColorType.Indexed, 8, ExrImageDataType.Unknown, ExrPixelType.Half)]
94+
[InlineData(PixelColorType.Luminance, 16, ExrImageDataType.Gray, ExrPixelType.Half)]
95+
[InlineData(PixelColorType.RGB, 48, ExrImageDataType.Rgb, ExrPixelType.Float)]
96+
[InlineData(PixelColorType.BGR, 48, ExrImageDataType.Rgb, ExrPixelType.Float)]
97+
[InlineData(PixelColorType.RGB | PixelColorType.Alpha, 64, ExrImageDataType.Rgba, ExrPixelType.Float)]
98+
[InlineData(PixelColorType.BGR | PixelColorType.Alpha, 64, ExrImageDataType.Rgba, ExrPixelType.Float)]
99+
[InlineData(PixelColorType.Luminance | PixelColorType.Alpha, 32, ExrImageDataType.Rgba, ExrPixelType.Float)]
100+
[InlineData(PixelColorType.YCbCr, 48, ExrImageDataType.Unknown, ExrPixelType.Float)]
101+
[InlineData(PixelColorType.CMYK, 64, ExrImageDataType.Unknown, ExrPixelType.Float)]
102+
[InlineData(PixelColorType.YCCK, 64, ExrImageDataType.Unknown, ExrPixelType.Float)]
103+
public void FromFormatConnectingMetadata_ConvertColorTypeAsExpected(PixelColorType pixelColorType, int bitsPerPixel, ExrImageDataType expectedImageDataType, ExrPixelType expectedPixelType)
104+
{
105+
FormatConnectingMetadata formatConnectingMetadata = new()
106+
{
107+
PixelTypeInfo = new PixelTypeInfo(bitsPerPixel)
108+
{
109+
ColorType = pixelColorType,
110+
},
111+
};
112+
113+
ExrMetadata actual = ExrMetadata.FromFormatConnectingMetadata(formatConnectingMetadata);
114+
115+
Assert.Equal(expectedImageDataType, actual.ImageDataType);
116+
Assert.Equal(expectedPixelType, actual.PixelType);
117+
}
88118
}

0 commit comments

Comments
 (0)