|
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.Webp; |
5 | 7 |
|
6 | 8 | /// <summary> |
7 | 9 | /// Provides Webp specific metadata information for the image. |
8 | 10 | /// </summary> |
9 | | -public class WebpMetadata : IFormatFrameMetadata<WebpMetadata> |
| 11 | +public class WebpMetadata : IFormatMetadata<WebpMetadata> |
10 | 12 | { |
11 | 13 | /// <summary> |
12 | 14 | /// Initializes a new instance of the <see cref="WebpMetadata"/> class. |
@@ -65,12 +67,90 @@ internal static WebpMetadata FromAnimatedMetadata(AnimatedImageMetadata metadata |
65 | 67 | }; |
66 | 68 |
|
67 | 69 | /// <inheritdoc/> |
68 | | - public static WebpMetadata FromFormatConnectingFrameMetadata(FormatConnectingFrameMetadata metadata) |
69 | | - => throw new NotImplementedException(); |
| 70 | + public static WebpMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata) |
| 71 | + { |
| 72 | + WebpBitsPerPixel bitsPerPixel; |
| 73 | + WebpColorType color; |
| 74 | + PixelColorType colorType = metadata.PixelTypeInfo.ColorType ?? PixelColorType.RGB | PixelColorType.Alpha; |
| 75 | + switch (colorType) |
| 76 | + { |
| 77 | + case PixelColorType.RGB: |
| 78 | + case PixelColorType.BGR: |
| 79 | + color = WebpColorType.Rgb; |
| 80 | + bitsPerPixel = WebpBitsPerPixel.Bit24; |
| 81 | + break; |
| 82 | + case PixelColorType.YCbCr: |
| 83 | + color = WebpColorType.Yuv; |
| 84 | + bitsPerPixel = WebpBitsPerPixel.Bit24; |
| 85 | + break; |
| 86 | + default: |
| 87 | + if (colorType.HasFlag(PixelColorType.Alpha)) |
| 88 | + { |
| 89 | + color = WebpColorType.Rgba; |
| 90 | + bitsPerPixel = WebpBitsPerPixel.Bit32; |
| 91 | + break; |
| 92 | + } |
| 93 | + |
| 94 | + color = WebpColorType.Rgb; |
| 95 | + bitsPerPixel = WebpBitsPerPixel.Bit24; |
| 96 | + break; |
| 97 | + } |
| 98 | + |
| 99 | + return new() |
| 100 | + { |
| 101 | + BitsPerPixel = bitsPerPixel, |
| 102 | + ColorType = color, |
| 103 | + FileFormat = WebpFileFormatType.Lossless, |
| 104 | + BackgroundColor = metadata.BackgroundColor, |
| 105 | + RepeatCount = metadata.RepeatCount |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + /// <inheritdoc/> |
| 110 | + public PixelTypeInfo GetPixelTypeInfo() |
| 111 | + { |
| 112 | + int bpp; |
| 113 | + PixelColorType colorType; |
| 114 | + PixelAlphaRepresentation alpha = PixelAlphaRepresentation.None; |
| 115 | + PixelComponentInfo info; |
| 116 | + switch (this.ColorType) |
| 117 | + { |
| 118 | + case WebpColorType.Yuv: |
| 119 | + bpp = 24; |
| 120 | + colorType = PixelColorType.YCbCr; |
| 121 | + info = PixelComponentInfo.Create(3, bpp, 8, 8, 8); |
| 122 | + break; |
| 123 | + case WebpColorType.Rgb: |
| 124 | + bpp = 24; |
| 125 | + colorType = PixelColorType.RGB; |
| 126 | + info = PixelComponentInfo.Create(3, bpp, 8, 8, 8); |
| 127 | + break; |
| 128 | + case WebpColorType.Rgba: |
| 129 | + default: |
| 130 | + bpp = 32; |
| 131 | + colorType = PixelColorType.RGB | PixelColorType.Alpha; |
| 132 | + info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8); |
| 133 | + alpha = PixelAlphaRepresentation.Unassociated; |
| 134 | + break; |
| 135 | + } |
| 136 | + |
| 137 | + return new PixelTypeInfo(bpp) |
| 138 | + { |
| 139 | + AlphaRepresentation = alpha, |
| 140 | + ColorType = colorType, |
| 141 | + ComponentInfo = info, |
| 142 | + }; |
| 143 | + } |
70 | 144 |
|
71 | 145 | /// <inheritdoc/> |
72 | | - public FormatConnectingFrameMetadata ToFormatConnectingFrameMetadata() |
73 | | - => throw new NotImplementedException(); |
| 146 | + public FormatConnectingMetadata ToFormatConnectingMetadata() |
| 147 | + => new() |
| 148 | + { |
| 149 | + PixelTypeInfo = this.GetPixelTypeInfo(), |
| 150 | + ColorTableMode = FrameColorTableMode.Global, |
| 151 | + RepeatCount = this.RepeatCount, |
| 152 | + BackgroundColor = this.BackgroundColor |
| 153 | + }; |
74 | 154 |
|
75 | 155 | /// <inheritdoc/> |
76 | 156 | IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone(); |
|
0 commit comments