Skip to content

Commit

Permalink
Merge pull request #2157 from SixLabors/backport/2135-2154
Browse files Browse the repository at this point in the history
V2 Backport: #2133, #2154
  • Loading branch information
JimBobSquarePants committed Jun 18, 2022
2 parents bbe396e + b2857ae commit f53c78d
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,12 @@ private JpegColorSpace DeduceJpegColorSpace(byte componentCount)
return JpegColorSpace.RGB;
}

// If these values are 1-3 for a 3-channel image, then the image is assumed to be YCbCr.
if (this.Components[2].Id == 3 && this.Components[1].Id == 2 && this.Components[0].Id == 1)
{
return JpegColorSpace.YCbCr;
}

// 3-channel non-subsampled images are assumed to be RGB.
if (this.Components[2].VerticalSamplingFactor == 1 && this.Components[1].VerticalSamplingFactor == 1 && this.Components[0].VerticalSamplingFactor == 1 &&
this.Components[2].HorizontalSamplingFactor == 1 && this.Components[1].HorizontalSamplingFactor == 1 && this.Components[0].HorizontalSamplingFactor == 1)
Expand Down
24 changes: 16 additions & 8 deletions src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,14 +660,22 @@ private uint ReadChunkSize()
/// </summary>
/// <param name="chunkType">The chunk type.</param>
/// <returns>True, if its an optional chunk type.</returns>
private static bool IsOptionalVp8XChunk(WebpChunkType chunkType) => chunkType switch
private static bool IsOptionalVp8XChunk(WebpChunkType chunkType)
{
WebpChunkType.Alpha => true,
WebpChunkType.Animation => true,
WebpChunkType.Exif => true,
WebpChunkType.Iccp => true,
WebpChunkType.Xmp => true,
_ => false
};
switch (chunkType)
{
case WebpChunkType.Alpha:
case WebpChunkType.Iccp:
case WebpChunkType.Exif:
case WebpChunkType.Xmp:
return true;
case WebpChunkType.AnimationParameter:
case WebpChunkType.Animation:
WebpThrowHelper.ThrowNotSupportedException("Animated webp are not yet supported.");
return false;
default:
return false;
}
}
}
}
13 changes: 13 additions & 0 deletions tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ public void Issue2057_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
}
}

// https://github.com/SixLabors/ImageSharp/issues/2133
[Theory]
[WithFile(TestImages.Jpeg.Issues.Issue2133DeduceColorSpace, PixelTypes.Rgba32)]
public void Issue2133_DeduceColorSpace<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(JpegDecoder))
{
image.DebugSave(provider);
image.CompareToOriginal(provider);
}
}

// DEBUG ONLY!
// The PDF.js output should be saved by "tests\ImageSharp.Tests\Formats\Jpg\pdfjs\jpeg-converter.htm"
// into "\tests\Images\ActualOutput\JpegDecoderTests\"
Expand Down
14 changes: 14 additions & 0 deletions tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.IO;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.PixelFormats;
Expand Down Expand Up @@ -369,6 +370,19 @@ public void WebpDecoder_ThrowImageFormatException_OnInvalidImages<TPixel>(TestIm
}
});

// https://github.com/SixLabors/ImageSharp/issues/2154
[Theory]
[WithFile(Lossless.Issue2154, PixelTypes.Rgba32)]
public void WebpDecoder_ThrowsNotSupportedException_Issue2154<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> =>
Assert.Throws<NotSupportedException>(
() =>
{
using (provider.GetImage(WebpDecoder))
{
}
});

#if SUPPORTS_RUNTIME_INTRINSICS
private static void RunDecodeLossyWithHorizontalFilter()
{
Expand Down
4 changes: 4 additions & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public static class Issues
public const string Issue2057App1Parsing = "Jpg/issues/Issue2057-App1Parsing.jpg";
public const string ExifNullArrayTag = "Jpg/issues/issue-2056-exif-null-array.jpg";
public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg";
public const string Issue2133DeduceColorSpace = "Jpg/issues/Issue2133.jpg";

public static class Fuzz
{
Expand Down Expand Up @@ -631,6 +632,9 @@ public static class Lossless
public const string LossLessCorruptImage3 = "Webp/lossless_color_transform.webp"; // cross_color, predictor

public const string LossLessCorruptImage4 = "Webp/near_lossless_75.webp"; // predictor, cross_color.

// Issues
public const string Issue2154 = "Webp/issues/Issue2154.webp";
}

public static class Lossy
Expand Down
3 changes: 3 additions & 0 deletions tests/Images/Input/Jpg/issues/Issue2133.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/Images/Input/Webp/issues/Issue2154.webp
Git LFS file not shown

0 comments on commit f53c78d

Please sign in to comment.