|
1 | 1 | // Copyright (c) Six Labors. |
2 | 2 | // Licensed under the Six Labors Split License. |
3 | 3 |
|
| 4 | +using SixLabors.ImageSharp.PixelFormats; |
| 5 | + |
4 | 6 | namespace SixLabors.ImageSharp.Formats.Qoi; |
5 | 7 |
|
6 | 8 | /// <summary> |
7 | 9 | /// Provides Qoi specific metadata information for the image. |
8 | 10 | /// </summary> |
9 | | -public class QoiMetadata : IDeepCloneable |
| 11 | +public class QoiMetadata : IFormatMetadata<QoiMetadata> |
10 | 12 | { |
11 | 13 | /// <summary> |
12 | 14 | /// Initializes a new instance of the <see cref="QoiMetadata"/> class. |
@@ -36,5 +38,55 @@ private QoiMetadata(QoiMetadata other) |
36 | 38 | public QoiColorSpace ColorSpace { get; set; } |
37 | 39 |
|
38 | 40 | /// <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); |
40 | 92 | } |
0 commit comments