Skip to content

Commit f53c78d

Browse files
Merge pull request #2157 from SixLabors/backport/2135-2154
V2 Backport: #2133, #2154
2 parents bbe396e + b2857ae commit f53c78d

File tree

7 files changed

+59
-8
lines changed

7 files changed

+59
-8
lines changed

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

+6
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,12 @@ private JpegColorSpace DeduceJpegColorSpace(byte componentCount)
530530
return JpegColorSpace.RGB;
531531
}
532532

533+
// If these values are 1-3 for a 3-channel image, then the image is assumed to be YCbCr.
534+
if (this.Components[2].Id == 3 && this.Components[1].Id == 2 && this.Components[0].Id == 1)
535+
{
536+
return JpegColorSpace.YCbCr;
537+
}
538+
533539
// 3-channel non-subsampled images are assumed to be RGB.
534540
if (this.Components[2].VerticalSamplingFactor == 1 && this.Components[1].VerticalSamplingFactor == 1 && this.Components[0].VerticalSamplingFactor == 1 &&
535541
this.Components[2].HorizontalSamplingFactor == 1 && this.Components[1].HorizontalSamplingFactor == 1 && this.Components[0].HorizontalSamplingFactor == 1)

src/ImageSharp/Formats/Webp/WebpDecoderCore.cs

+16-8
Original file line numberDiff line numberDiff line change
@@ -660,14 +660,22 @@ private uint ReadChunkSize()
660660
/// </summary>
661661
/// <param name="chunkType">The chunk type.</param>
662662
/// <returns>True, if its an optional chunk type.</returns>
663-
private static bool IsOptionalVp8XChunk(WebpChunkType chunkType) => chunkType switch
663+
private static bool IsOptionalVp8XChunk(WebpChunkType chunkType)
664664
{
665-
WebpChunkType.Alpha => true,
666-
WebpChunkType.Animation => true,
667-
WebpChunkType.Exif => true,
668-
WebpChunkType.Iccp => true,
669-
WebpChunkType.Xmp => true,
670-
_ => false
671-
};
665+
switch (chunkType)
666+
{
667+
case WebpChunkType.Alpha:
668+
case WebpChunkType.Iccp:
669+
case WebpChunkType.Exif:
670+
case WebpChunkType.Xmp:
671+
return true;
672+
case WebpChunkType.AnimationParameter:
673+
case WebpChunkType.Animation:
674+
WebpThrowHelper.ThrowNotSupportedException("Animated webp are not yet supported.");
675+
return false;
676+
default:
677+
return false;
678+
}
679+
}
672680
}
673681
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,19 @@ public void Issue2057_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
216216
}
217217
}
218218

219+
// https://github.com/SixLabors/ImageSharp/issues/2133
220+
[Theory]
221+
[WithFile(TestImages.Jpeg.Issues.Issue2133DeduceColorSpace, PixelTypes.Rgba32)]
222+
public void Issue2133_DeduceColorSpace<TPixel>(TestImageProvider<TPixel> provider)
223+
where TPixel : unmanaged, IPixel<TPixel>
224+
{
225+
using (Image<TPixel> image = provider.GetImage(JpegDecoder))
226+
{
227+
image.DebugSave(provider);
228+
image.CompareToOriginal(provider);
229+
}
230+
}
231+
219232
// DEBUG ONLY!
220233
// The PDF.js output should be saved by "tests\ImageSharp.Tests\Formats\Jpg\pdfjs\jpeg-converter.htm"
221234
// into "\tests\Images\ActualOutput\JpegDecoderTests\"

tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using System.IO;
56
using SixLabors.ImageSharp.Formats.Webp;
67
using SixLabors.ImageSharp.PixelFormats;
@@ -369,6 +370,19 @@ public void WebpDecoder_ThrowImageFormatException_OnInvalidImages<TPixel>(TestIm
369370
}
370371
});
371372

373+
// https://github.com/SixLabors/ImageSharp/issues/2154
374+
[Theory]
375+
[WithFile(Lossless.Issue2154, PixelTypes.Rgba32)]
376+
public void WebpDecoder_ThrowsNotSupportedException_Issue2154<TPixel>(TestImageProvider<TPixel> provider)
377+
where TPixel : unmanaged, IPixel<TPixel> =>
378+
Assert.Throws<NotSupportedException>(
379+
() =>
380+
{
381+
using (provider.GetImage(WebpDecoder))
382+
{
383+
}
384+
});
385+
372386
#if SUPPORTS_RUNTIME_INTRINSICS
373387
private static void RunDecodeLossyWithHorizontalFilter()
374388
{

tests/ImageSharp.Tests/TestImages.cs

+4
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public static class Issues
265265
public const string Issue2057App1Parsing = "Jpg/issues/Issue2057-App1Parsing.jpg";
266266
public const string ExifNullArrayTag = "Jpg/issues/issue-2056-exif-null-array.jpg";
267267
public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg";
268+
public const string Issue2133DeduceColorSpace = "Jpg/issues/Issue2133.jpg";
268269

269270
public static class Fuzz
270271
{
@@ -631,6 +632,9 @@ public static class Lossless
631632
public const string LossLessCorruptImage3 = "Webp/lossless_color_transform.webp"; // cross_color, predictor
632633

633634
public const string LossLessCorruptImage4 = "Webp/near_lossless_75.webp"; // predictor, cross_color.
635+
636+
// Issues
637+
public const string Issue2154 = "Webp/issues/Issue2154.webp";
634638
}
635639

636640
public static class Lossy
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:df1160649539a1f73d4c52043358092c59c584763daf18007ff73b72865ddbc1
3+
size 37342

0 commit comments

Comments
 (0)