Browse Source

Implement QoiMetadata

pull/2751/head
James Jackson-South 2 years ago
parent
commit
b9994b60db
  1. 8
      src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs
  2. 56
      src/ImageSharp/Formats/Qoi/QoiMetadata.cs

8
src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs

@ -49,13 +49,7 @@ internal class QoiDecoderCore : IImageDecoderInternals
this.ProcessHeader(stream);
// Create Image object
ImageMetadata metadata = new()
{
DecodedImageFormat = QoiFormat.Instance,
HorizontalResolution = this.header.Width,
VerticalResolution = this.header.Height,
ResolutionUnits = PixelResolutionUnit.AspectRatio
};
ImageMetadata metadata = new();
QoiMetadata qoiMetadata = metadata.GetQoiMetadata();
qoiMetadata.Channels = this.header.Channels;
qoiMetadata.ColorSpace = this.header.ColorSpace;

56
src/ImageSharp/Formats/Qoi/QoiMetadata.cs

@ -1,12 +1,14 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Qoi;
/// <summary>
/// Provides Qoi specific metadata information for the image.
/// </summary>
public class QoiMetadata : IDeepCloneable
public class QoiMetadata : IFormatMetadata<QoiMetadata>
{
/// <summary>
/// Initializes a new instance of the <see cref="QoiMetadata"/> class.
@ -36,5 +38,55 @@ public class QoiMetadata : IDeepCloneable
public QoiColorSpace ColorSpace { get; set; }
/// <inheritdoc/>
public IDeepCloneable DeepClone() => new QoiMetadata(this);
public static QoiMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
{
PixelColorType color = metadata.PixelTypeInfo.ColorType ?? PixelColorType.RGB;
if (color.HasFlag(PixelColorType.Alpha))
{
return new QoiMetadata { Channels = QoiChannels.Rgba };
}
return new QoiMetadata { Channels = QoiChannels.Rgb };
}
/// <inheritdoc/>
public FormatConnectingMetadata ToFormatConnectingMetadata()
{
int bpp;
PixelColorType colorType;
PixelAlphaRepresentation alpha = PixelAlphaRepresentation.None;
PixelComponentInfo info;
switch (this.Channels)
{
case QoiChannels.Rgb:
bpp = 24;
colorType = PixelColorType.RGB;
info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);
break;
default:
bpp = 32;
colorType = PixelColorType.RGB | PixelColorType.Alpha;
info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8);
alpha = PixelAlphaRepresentation.Unassociated;
break;
}
return new()
{
PixelTypeInfo = new PixelTypeInfo(bpp)
{
AlphaRepresentation = alpha,
ColorType = colorType,
ComponentInfo = info,
}
};
}
/// <inheritdoc/>
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
/// <inheritdoc/>
public QoiMetadata DeepClone() => new(this);
}

Loading…
Cancel
Save