Skip to content

Commit c01bded

Browse files
Update WebpMetadata.cs
1 parent 0e54081 commit c01bded

1 file changed

Lines changed: 85 additions & 5 deletions

File tree

src/ImageSharp/Formats/Webp/WebpMetadata.cs

Lines changed: 85 additions & 5 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.Webp;
57

68
/// <summary>
79
/// Provides Webp specific metadata information for the image.
810
/// </summary>
9-
public class WebpMetadata : IFormatFrameMetadata<WebpMetadata>
11+
public class WebpMetadata : IFormatMetadata<WebpMetadata>
1012
{
1113
/// <summary>
1214
/// Initializes a new instance of the <see cref="WebpMetadata"/> class.
@@ -65,12 +67,90 @@ internal static WebpMetadata FromAnimatedMetadata(AnimatedImageMetadata metadata
6567
};
6668

6769
/// <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+
}
70144

71145
/// <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+
};
74154

75155
/// <inheritdoc/>
76156
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();

0 commit comments

Comments
 (0)