Skip to content

Commit 5b47e79

Browse files
Implement JpegMetadata
1 parent 8166213 commit 5b47e79

1 file changed

Lines changed: 93 additions & 3 deletions

File tree

src/ImageSharp/Formats/Jpeg/JpegMetadata.cs

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
// Licensed under the Six Labors Split License.
33

44
using SixLabors.ImageSharp.Formats.Jpeg.Components;
5+
using SixLabors.ImageSharp.PixelFormats;
56

67
namespace SixLabors.ImageSharp.Formats.Jpeg;
78

89
/// <summary>
910
/// Provides Jpeg specific metadata information for the image.
1011
/// </summary>
11-
public class JpegMetadata : IDeepCloneable
12+
public class JpegMetadata : IFormatMetadata<JpegMetadata>
1213
{
1314
/// <summary>
1415
/// Initializes a new instance of the <see cref="JpegMetadata"/> class.
1516
/// </summary>
16-
public JpegMetadata() => this.Comments = new List<JpegComData>();
17+
public JpegMetadata() => this.Comments = [];
1718

1819
/// <summary>
1920
/// Initializes a new instance of the <see cref="JpegMetadata"/> class.
@@ -99,5 +100,94 @@ public int Quality
99100
public IList<JpegComData> Comments { get; }
100101

101102
/// <inheritdoc/>
102-
public IDeepCloneable DeepClone() => new JpegMetadata(this);
103+
public static JpegMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
104+
{
105+
JpegEncodingColor color;
106+
PixelColorType colorType = metadata.PixelTypeInfo.ColorType ?? PixelColorType.YCbCr;
107+
switch (colorType)
108+
{
109+
case PixelColorType.Luminance:
110+
color = JpegEncodingColor.Luminance;
111+
break;
112+
case PixelColorType.CMYK:
113+
color = JpegEncodingColor.Cmyk;
114+
break;
115+
case PixelColorType.YCCK:
116+
color = JpegEncodingColor.Ycck;
117+
break;
118+
default:
119+
if (colorType.HasFlag(PixelColorType.RGB) || colorType.HasFlag(PixelColorType.BGR))
120+
{
121+
color = JpegEncodingColor.Rgb;
122+
}
123+
else
124+
{
125+
color = metadata.Quality <= Quantization.DefaultQualityFactor
126+
? JpegEncodingColor.YCbCrRatio420
127+
: JpegEncodingColor.YCbCrRatio444;
128+
}
129+
130+
break;
131+
}
132+
133+
return new JpegMetadata
134+
{
135+
ColorType = color,
136+
ChrominanceQuality = metadata.Quality,
137+
LuminanceQuality = metadata.Quality,
138+
};
139+
}
140+
141+
/// <inheritdoc/>
142+
public FormatConnectingMetadata ToFormatConnectingMetadata()
143+
{
144+
int bpp;
145+
PixelColorType colorType;
146+
PixelComponentInfo info;
147+
switch (this.ColorType)
148+
{
149+
case JpegEncodingColor.Luminance:
150+
bpp = 8;
151+
colorType = PixelColorType.Luminance;
152+
info = PixelComponentInfo.Create(1, bpp, 8);
153+
break;
154+
case JpegEncodingColor.Cmyk:
155+
bpp = 32;
156+
colorType = PixelColorType.CMYK;
157+
info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8);
158+
break;
159+
case JpegEncodingColor.Ycck:
160+
bpp = 32;
161+
colorType = PixelColorType.YCCK;
162+
info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8);
163+
break;
164+
case JpegEncodingColor.Rgb:
165+
bpp = 24;
166+
colorType = PixelColorType.RGB;
167+
info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);
168+
break;
169+
default:
170+
bpp = 24;
171+
colorType = PixelColorType.YCbCr;
172+
info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);
173+
break;
174+
}
175+
176+
return new FormatConnectingMetadata
177+
{
178+
PixelTypeInfo = new PixelTypeInfo(bpp)
179+
{
180+
AlphaRepresentation = PixelAlphaRepresentation.None,
181+
ColorType = colorType,
182+
ComponentInfo = info,
183+
},
184+
Quality = this.Quality,
185+
};
186+
}
187+
188+
/// <inheritdoc/>
189+
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
190+
191+
/// <inheritdoc/>
192+
public JpegMetadata DeepClone() => new(this);
103193
}

0 commit comments

Comments
 (0)