|
2 | 2 | // Licensed under the Six Labors Split License. |
3 | 3 |
|
4 | 4 | using SixLabors.ImageSharp.Formats.Jpeg.Components; |
| 5 | +using SixLabors.ImageSharp.PixelFormats; |
5 | 6 |
|
6 | 7 | namespace SixLabors.ImageSharp.Formats.Jpeg; |
7 | 8 |
|
8 | 9 | /// <summary> |
9 | 10 | /// Provides Jpeg specific metadata information for the image. |
10 | 11 | /// </summary> |
11 | | -public class JpegMetadata : IDeepCloneable |
| 12 | +public class JpegMetadata : IFormatMetadata<JpegMetadata> |
12 | 13 | { |
13 | 14 | /// <summary> |
14 | 15 | /// Initializes a new instance of the <see cref="JpegMetadata"/> class. |
15 | 16 | /// </summary> |
16 | | - public JpegMetadata() => this.Comments = new List<JpegComData>(); |
| 17 | + public JpegMetadata() => this.Comments = []; |
17 | 18 |
|
18 | 19 | /// <summary> |
19 | 20 | /// Initializes a new instance of the <see cref="JpegMetadata"/> class. |
@@ -99,5 +100,94 @@ public int Quality |
99 | 100 | public IList<JpegComData> Comments { get; } |
100 | 101 |
|
101 | 102 | /// <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); |
103 | 193 | } |
0 commit comments