Skip to content

Commit 57335b8

Browse files
committed
Add detect color type tests
1 parent 310fefd commit 57335b8

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,13 @@ public void MetadataIsParsedCorrectly(
6767
string imagePath,
6868
int expectedPixelSize,
6969
bool exifProfilePresent,
70-
bool iccProfilePresent)
71-
{
72-
TestMetadataImpl(
70+
bool iccProfilePresent) => TestMetadataImpl(
7371
useIdentify,
7472
JpegDecoder,
7573
imagePath,
7674
expectedPixelSize,
7775
exifProfilePresent,
7876
iccProfilePresent);
79-
}
8077

8178
[Theory]
8279
[MemberData(nameof(RatioFiles))]
@@ -133,15 +130,45 @@ public void Decode_VerifyQuality(string imagePath, int quality)
133130
var testFile = TestFile.Create(imagePath);
134131
using (var stream = new MemoryStream(testFile.Bytes, false))
135132
{
136-
var decoder = new JpegDecoder();
137-
using (Image<Rgba32> image = decoder.Decode<Rgba32>(Configuration.Default, stream))
133+
using (Image<Rgba32> image = JpegDecoder.Decode<Rgba32>(Configuration.Default, stream))
138134
{
139135
JpegMetadata meta = image.Metadata.GetJpegMetadata();
140136
Assert.Equal(quality, meta.Quality);
141137
}
142138
}
143139
}
144140

141+
[Theory]
142+
[InlineData(TestImages.Jpeg.Baseline.Floorplan, JpegColorType.Luminance)]
143+
[InlineData(TestImages.Jpeg.Baseline.Jpeg420Small, JpegColorType.YCbCrRatio420)]
144+
[InlineData(TestImages.Jpeg.Baseline.Jpeg444, JpegColorType.YCbCrRatio444)]
145+
[InlineData(TestImages.Jpeg.Baseline.JpegRgb, JpegColorType.Rgb)]
146+
public void Identify_DetectsCorrectColorType(string imagePath, JpegColorType expectedColorType)
147+
{
148+
var testFile = TestFile.Create(imagePath);
149+
using (var stream = new MemoryStream(testFile.Bytes, false))
150+
{
151+
IImageInfo image = JpegDecoder.Identify(Configuration.Default, stream);
152+
JpegMetadata meta = image.Metadata.GetJpegMetadata();
153+
Assert.Equal(expectedColorType, meta.ColorType);
154+
}
155+
}
156+
157+
[Theory]
158+
[WithFile(TestImages.Jpeg.Baseline.Floorplan, PixelTypes.Rgba32, JpegColorType.Luminance)]
159+
[WithFile(TestImages.Jpeg.Baseline.Jpeg420Small, PixelTypes.Rgba32, JpegColorType.YCbCrRatio420)]
160+
[WithFile(TestImages.Jpeg.Baseline.Jpeg444, PixelTypes.Rgba32, JpegColorType.YCbCrRatio444)]
161+
[WithFile(TestImages.Jpeg.Baseline.JpegRgb, PixelTypes.Rgba32, JpegColorType.Rgb)]
162+
public void Decode_DetectsCorrectColorType<TPixel>(TestImageProvider<TPixel> provider, JpegColorType expectedColorType)
163+
where TPixel : unmanaged, IPixel<TPixel>
164+
{
165+
using (Image<TPixel> image = provider.GetImage(JpegDecoder))
166+
{
167+
JpegMetadata meta = image.Metadata.GetJpegMetadata();
168+
Assert.Equal(expectedColorType, meta.ColorType);
169+
}
170+
}
171+
145172
private static void TestImageInfo(string imagePath, IImageDecoder decoder, bool useIdentify, Action<IImageInfo> test)
146173
{
147174
var testFile = TestFile.Create(imagePath);
@@ -161,9 +188,7 @@ private static void TestMetadataImpl(
161188
string imagePath,
162189
int expectedPixelSize,
163190
bool exifProfilePresent,
164-
bool iccProfilePresent)
165-
{
166-
TestImageInfo(
191+
bool iccProfilePresent) => TestImageInfo(
167192
imagePath,
168193
decoder,
169194
useIdentify,
@@ -207,7 +232,6 @@ private static void TestMetadataImpl(
207232
Assert.Null(iccProfile);
208233
}
209234
});
210-
}
211235

212236
[Theory]
213237
[InlineData(false)]
@@ -237,9 +261,7 @@ public void IgnoreMetadata_ControlsWhetherMetadataIsParsed(bool ignoreMetadata)
237261
[Theory]
238262
[InlineData(false)]
239263
[InlineData(true)]
240-
public void Decoder_Reads_Correct_Resolution_From_Jfif(bool useIdentify)
241-
{
242-
TestImageInfo(
264+
public void Decoder_Reads_Correct_Resolution_From_Jfif(bool useIdentify) => TestImageInfo(
243265
TestImages.Jpeg.Baseline.Floorplan,
244266
JpegDecoder,
245267
useIdentify,
@@ -248,14 +270,11 @@ public void Decoder_Reads_Correct_Resolution_From_Jfif(bool useIdentify)
248270
Assert.Equal(300, imageInfo.Metadata.HorizontalResolution);
249271
Assert.Equal(300, imageInfo.Metadata.VerticalResolution);
250272
});
251-
}
252273

253274
[Theory]
254275
[InlineData(false)]
255276
[InlineData(true)]
256-
public void Decoder_Reads_Correct_Resolution_From_Exif(bool useIdentify)
257-
{
258-
TestImageInfo(
277+
public void Decoder_Reads_Correct_Resolution_From_Exif(bool useIdentify) => TestImageInfo(
259278
TestImages.Jpeg.Baseline.Jpeg420Exif,
260279
JpegDecoder,
261280
useIdentify,
@@ -264,6 +283,5 @@ public void Decoder_Reads_Correct_Resolution_From_Exif(bool useIdentify)
264283
Assert.Equal(72, imageInfo.Metadata.HorizontalResolution);
265284
Assert.Equal(72, imageInfo.Metadata.VerticalResolution);
266285
});
267-
}
268286
}
269287
}

tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public async Task Identify_IsCancellable()
180180
public void Issue1732_DecodesWithRgbColorSpace<TPixel>(TestImageProvider<TPixel> provider)
181181
where TPixel : unmanaged, IPixel<TPixel>
182182
{
183-
using (Image<TPixel> image = provider.GetImage(new JpegDecoder()))
183+
using (Image<TPixel> image = provider.GetImage(JpegDecoder))
184184
{
185185
image.DebugSave(provider);
186186
image.CompareToOriginal(provider);

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public static class Bad
190190
public const string Jpeg420Exif = "Jpg/baseline/jpeg420exif.jpg";
191191
public const string Jpeg444 = "Jpg/baseline/jpeg444.jpg";
192192
public const string Jpeg420Small = "Jpg/baseline/jpeg420small.jpg";
193+
public const string JpegRgb = "Jpg/baseline/jpeg-rgb.jpg";
193194
public const string Testorig420 = "Jpg/baseline/testorig.jpg";
194195
public const string MultiScanBaselineCMYK = "Jpg/baseline/MultiScanBaselineCMYK.jpg";
195196
public const string Ratio1x1 = "Jpg/baseline/ratio-1x1.jpg";
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)