Skip to content

Commit b9994b6

Browse files
Implement QoiMetadata
1 parent 2127b46 commit b9994b6

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,7 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
4949
this.ProcessHeader(stream);
5050

5151
// Create Image object
52-
ImageMetadata metadata = new()
53-
{
54-
DecodedImageFormat = QoiFormat.Instance,
55-
HorizontalResolution = this.header.Width,
56-
VerticalResolution = this.header.Height,
57-
ResolutionUnits = PixelResolutionUnit.AspectRatio
58-
};
52+
ImageMetadata metadata = new();
5953
QoiMetadata qoiMetadata = metadata.GetQoiMetadata();
6054
qoiMetadata.Channels = this.header.Channels;
6155
qoiMetadata.ColorSpace = this.header.ColorSpace;

src/ImageSharp/Formats/Qoi/QoiMetadata.cs

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

4+
using SixLabors.ImageSharp.PixelFormats;
5+
46
namespace SixLabors.ImageSharp.Formats.Qoi;
57

68
/// <summary>
79
/// Provides Qoi specific metadata information for the image.
810
/// </summary>
9-
public class QoiMetadata : IDeepCloneable
11+
public class QoiMetadata : IFormatMetadata<QoiMetadata>
1012
{
1113
/// <summary>
1214
/// Initializes a new instance of the <see cref="QoiMetadata"/> class.
@@ -36,5 +38,55 @@ private QoiMetadata(QoiMetadata other)
3638
public QoiColorSpace ColorSpace { get; set; }
3739

3840
/// <inheritdoc/>
39-
public IDeepCloneable DeepClone() => new QoiMetadata(this);
41+
public static QoiMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
42+
{
43+
PixelColorType color = metadata.PixelTypeInfo.ColorType ?? PixelColorType.RGB;
44+
45+
if (color.HasFlag(PixelColorType.Alpha))
46+
{
47+
return new QoiMetadata { Channels = QoiChannels.Rgba };
48+
}
49+
50+
return new QoiMetadata { Channels = QoiChannels.Rgb };
51+
}
52+
53+
/// <inheritdoc/>
54+
public FormatConnectingMetadata ToFormatConnectingMetadata()
55+
{
56+
int bpp;
57+
PixelColorType colorType;
58+
PixelAlphaRepresentation alpha = PixelAlphaRepresentation.None;
59+
PixelComponentInfo info;
60+
61+
switch (this.Channels)
62+
{
63+
case QoiChannels.Rgb:
64+
bpp = 24;
65+
colorType = PixelColorType.RGB;
66+
info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);
67+
break;
68+
default:
69+
bpp = 32;
70+
colorType = PixelColorType.RGB | PixelColorType.Alpha;
71+
info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8);
72+
alpha = PixelAlphaRepresentation.Unassociated;
73+
break;
74+
}
75+
76+
return new()
77+
{
78+
PixelTypeInfo = new PixelTypeInfo(bpp)
79+
{
80+
AlphaRepresentation = alpha,
81+
ColorType = colorType,
82+
ComponentInfo = info,
83+
}
84+
};
85+
}
86+
87+
/// <inheritdoc/>
88+
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
89+
90+
/// <inheritdoc/>
91+
public QoiMetadata DeepClone() => new(this);
4092
}

0 commit comments

Comments
 (0)