Browse Source

Remove hardcoded subsampling. Fix #817 (#834)

af/merge-core
James Jackson-South 7 years ago
committed by GitHub
parent
commit
19270664b0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 38
      src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs
  2. 1
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs
  3. 3
      tests/ImageSharp.Tests/TestImages.cs
  4. 2
      tests/Images/External
  5. 3
      tests/Images/Input/Jpg/baseline/ycck-subsample-1222.jpg

38
src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs

@ -2,8 +2,6 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
@ -12,9 +10,9 @@ using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
{
/// <summary>
/// Represents a single frame component
/// Represents a single frame component.
/// </summary>
internal class JpegComponent : IDisposable, IJpegComponent
internal sealed class JpegComponent : IDisposable, IJpegComponent
{
private readonly MemoryAllocator memoryAllocator;
@ -31,12 +29,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
}
/// <summary>
/// Gets the component Id
/// Gets the component Id.
/// </summary>
public byte Id { get; }
/// <summary>
/// Gets or sets DC coefficient predictor
/// Gets or sets DC coefficient predictor.
/// </summary>
public int DcPredictor { get; set; }
@ -69,22 +67,22 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
public Size SamplingFactors { get; set; }
/// <summary>
/// Gets the number of blocks per line
/// Gets the number of blocks per line.
/// </summary>
public int WidthInBlocks { get; private set; }
/// <summary>
/// Gets the number of blocks per column
/// Gets the number of blocks per column.
/// </summary>
public int HeightInBlocks { get; private set; }
/// <summary>
/// Gets or sets the index for the DC Huffman table
/// Gets or sets the index for the DC Huffman table.
/// </summary>
public int DCHuffmanTableId { get; set; }
/// <summary>
/// Gets or sets the index for the AC Huffman table
/// Gets or sets the index for the AC Huffman table.
/// </summary>
public int ACHuffmanTableId { get; set; }
@ -109,24 +107,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
int blocksPerColumnForMcu = this.Frame.McusPerColumn * this.VerticalSamplingFactor;
this.SizeInBlocks = new Size(blocksPerLineForMcu, blocksPerColumnForMcu);
// For 4-component images (either CMYK or YCbCrK), we only support two
// hv vectors: [0x11 0x11 0x11 0x11] and [0x22 0x11 0x11 0x22].
// Theoretically, 4-component JPEG images could mix and match hv values
// but in practice, those two combinations are the only ones in use,
// and it simplifies the applyBlack code below if we can assume that:
// - for CMYK, the C and K channels have full samples, and if the M
// and Y channels subsample, they subsample both horizontally and
// vertically.
// - for YCbCrK, the Y and K channels have full samples.
if (this.Index == 0 || this.Index == 3)
{
this.SubSamplingDivisors = new Size(1, 1);
}
else
{
JpegComponent c0 = this.Frame.Components[0];
this.SubSamplingDivisors = c0.SamplingFactors.DivideBy(this.SamplingFactors);
}
JpegComponent c0 = this.Frame.Components[0];
this.SubSamplingDivisors = c0.SamplingFactors.DivideBy(this.SamplingFactors);
int totalNumberOfBlocks = blocksPerColumnForMcu * (blocksPerLineForMcu + 1);
int width = this.WidthInBlocks + 1;

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

@ -21,6 +21,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
TestImages.Jpeg.Baseline.Jpeg444,
TestImages.Jpeg.Baseline.Bad.BadEOF,
TestImages.Jpeg.Baseline.MultiScanBaselineCMYK,
TestImages.Jpeg.Baseline.YcckSubsample1222,
TestImages.Jpeg.Baseline.Bad.BadRST,
TestImages.Jpeg.Issues.MultiHuffmanBaseline394,
TestImages.Jpeg.Issues.ExifDecodeOutOfRange694,

3
tests/ImageSharp.Tests/TestImages.cs

@ -140,13 +140,14 @@ namespace SixLabors.ImageSharp.Tests
public const string MultiScanBaselineCMYK = "Jpg/baseline/MultiScanBaselineCMYK.jpg";
public const string Ratio1x1 = "Jpg/baseline/ratio-1x1.jpg";
public const string Testorig12bit = "Jpg/baseline/testorig12.jpg";
public const string YcckSubsample1222 = "Jpg/baseline/ycck-subsample-1222.jpg";
public static readonly string[] All =
{
Cmyk, Ycck, Exif, Floorplan,
Calliphora, Turtle, GammaDalaiLamaGray,
Hiyamugi, Jpeg400, Jpeg420Exif, Jpeg444,
Ratio1x1, Testorig12bit
Ratio1x1, Testorig12bit, YcckSubsample1222
};
}

2
tests/Images/External

@ -1 +1 @@
Subproject commit 74995302e3c9a5913f1cf09d71b15f888d0ec022
Subproject commit 32dc8aec1109b681f056264e6bde1f3a76f08aba

3
tests/Images/Input/Jpg/baseline/ycck-subsample-1222.jpg

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ca7c16a2984d14ebc483780d9d1e8c131eca01d89f53512cc7add538c93a38c6
size 31527
Loading…
Cancel
Save