Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 10 additions & 28 deletions src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,9 +10,9 @@
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;

Expand All @@ -31,12 +29,12 @@ public JpegComponent(MemoryAllocator memoryAllocator, JpegFrame frame, byte id,
}

/// <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; }

Expand Down Expand Up @@ -69,22 +67,22 @@ public JpegComponent(MemoryAllocator memoryAllocator, JpegFrame frame, byte id,
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; }

Expand All @@ -109,24 +107,8 @@ public void Init()
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public partial class JpegDecoderTests
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,
Expand Down
3 changes: 2 additions & 1 deletion tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ public static class Bad
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
};
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.