mirror of https://github.com/SixLabors/ImageSharp
100 changed files with 1916 additions and 2914 deletions
@ -1,40 +1,94 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
// Copyright (c) Six Labors and contributors.
|
||||
// Licensed under the Apache License, Version 2.0.
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Buffers.Binary; |
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Gif |
namespace SixLabors.ImageSharp.Formats.Gif |
||||
{ |
{ |
||||
/// <summary>
|
/// <summary>
|
||||
/// The Graphic Control Extension contains parameters used when
|
/// The Graphic Control Extension contains parameters used when
|
||||
/// processing a graphic rendering block.
|
/// processing a graphic rendering block.
|
||||
/// </summary>
|
/// </summary>
|
||||
internal sealed class GifGraphicsControlExtension |
internal readonly struct GifGraphicsControlExtension |
||||
{ |
{ |
||||
|
public const int Size = 8; |
||||
|
|
||||
|
public GifGraphicsControlExtension( |
||||
|
DisposalMethod disposalMethod, |
||||
|
bool transparencyFlag, |
||||
|
ushort delayTime, |
||||
|
byte transparencyIndex) |
||||
|
{ |
||||
|
this.DisposalMethod = disposalMethod; |
||||
|
this.TransparencyFlag = transparencyFlag; |
||||
|
this.DelayTime = delayTime; |
||||
|
this.TransparencyIndex = transparencyIndex; |
||||
|
} |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets or sets the disposal method which indicates the way in which the
|
/// Gets the disposal method which indicates the way in which the
|
||||
/// graphic is to be treated after being displayed.
|
/// graphic is to be treated after being displayed.
|
||||
/// </summary>
|
/// </summary>
|
||||
public DisposalMethod DisposalMethod { get; set; } |
public DisposalMethod DisposalMethod { get; } |
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets or sets a value indicating whether transparency flag is to be set.
|
/// Gets a value indicating whether transparency flag is to be set.
|
||||
/// This indicates whether a transparency index is given in the Transparent Index field.
|
/// This indicates whether a transparency index is given in the Transparent Index field.
|
||||
/// (This field is the least significant bit of the byte.)
|
/// (This field is the least significant bit of the byte.)
|
||||
/// </summary>
|
/// </summary>
|
||||
public bool TransparencyFlag { get; set; } |
public bool TransparencyFlag { get; } |
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets or sets the transparency index.
|
|
||||
/// The Transparency Index is such that when encountered, the corresponding pixel
|
|
||||
/// of the display device is not modified and processing goes on to the next pixel.
|
|
||||
/// </summary>
|
|
||||
public byte TransparencyIndex { get; set; } |
|
||||
|
|
||||
/// <summary>
|
/// <summary>
|
||||
/// Gets or sets the delay time.
|
/// Gets the delay time.
|
||||
/// If not 0, this field specifies the number of hundredths (1/100) of a second to
|
/// If not 0, this field specifies the number of hundredths (1/100) of a second to
|
||||
/// wait before continuing with the processing of the Data Stream.
|
/// wait before continuing with the processing of the Data Stream.
|
||||
/// The clock starts ticking immediately after the graphic is rendered.
|
/// The clock starts ticking immediately after the graphic is rendered.
|
||||
/// </summary>
|
/// </summary>
|
||||
public int DelayTime { get; set; } |
public ushort DelayTime { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the transparency index.
|
||||
|
/// The Transparency Index is such that when encountered, the corresponding pixel
|
||||
|
/// of the display device is not modified and processing goes on to the next pixel.
|
||||
|
/// </summary>
|
||||
|
public byte TransparencyIndex { get; } |
||||
|
|
||||
|
public byte PackField() |
||||
|
{ |
||||
|
PackedField field = default; |
||||
|
|
||||
|
field.SetBits(3, 3, (int)this.DisposalMethod); // 1-3 : Reserved, 4-6 : Disposal
|
||||
|
|
||||
|
// TODO: Allow this as an option.
|
||||
|
field.SetBit(6, false); // 7 : User input - 0 = none
|
||||
|
field.SetBit(7, this.TransparencyFlag); // 8: Has transparent.
|
||||
|
|
||||
|
return field.Byte; |
||||
|
} |
||||
|
|
||||
|
public void WriteTo(Span<byte> buffer) |
||||
|
{ |
||||
|
buffer[0] = GifConstants.ExtensionIntroducer; |
||||
|
buffer[1] = GifConstants.GraphicControlLabel; |
||||
|
buffer[2] = 4; // Block Size
|
||||
|
buffer[3] = this.PackField(); // Packed Field
|
||||
|
BinaryPrimitives.WriteUInt16LittleEndian(buffer.Slice(4, 2), this.DelayTime); // Delay Time
|
||||
|
buffer[6] = this.TransparencyIndex; |
||||
|
buffer[7] = GifConstants.Terminator; |
||||
|
} |
||||
|
|
||||
|
public static GifGraphicsControlExtension Parse(ReadOnlySpan<byte> buffer) |
||||
|
{ |
||||
|
// We've already read the Extension Introducer introducer & Graphic Control Label
|
||||
|
// Start from the block size (0)
|
||||
|
byte packed = buffer[1]; |
||||
|
|
||||
|
return new GifGraphicsControlExtension( |
||||
|
disposalMethod: (DisposalMethod)((packed & 0x1C) >> 2), |
||||
|
delayTime: BinaryPrimitives.ReadUInt16LittleEndian(buffer.Slice(2, 2)), |
||||
|
transparencyIndex: buffer[4], |
||||
|
transparencyFlag: (packed & 0x01) == 1); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
@ -1,16 +0,0 @@ |
|||||
namespace SixLabors.ImageSharp.Formats.Jpeg.Common.Decoder |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Various utilities for <see cref="IJpegComponent"/>.
|
|
||||
/// </summary>
|
|
||||
internal static class ComponentUtils |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Gets a reference to the <see cref="Block8x8"/> at the given row and column index from <see cref="IJpegComponent.SpectralBlocks"/>
|
|
||||
/// </summary>
|
|
||||
public static ref Block8x8 GetBlockReference(this IJpegComponent component, int bx, int by) |
|
||||
{ |
|
||||
return ref component.SpectralBlocks[bx, by]; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,24 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
|
using System.Runtime.InteropServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components |
||||
|
{ |
||||
|
[StructLayout(LayoutKind.Sequential)] |
||||
|
internal unsafe struct FixedByteBuffer256 |
||||
|
{ |
||||
|
public fixed byte Data[256]; |
||||
|
|
||||
|
public byte this[int idx] |
||||
|
{ |
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
get |
||||
|
{ |
||||
|
ref byte self = ref Unsafe.As<FixedByteBuffer256, byte>(ref this); |
||||
|
return Unsafe.Add(ref self, idx); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
|
using System.Runtime.InteropServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components |
||||
|
{ |
||||
|
[StructLayout(LayoutKind.Sequential)] |
||||
|
internal unsafe struct FixedInt16Buffer18 |
||||
|
{ |
||||
|
public fixed short Data[18]; |
||||
|
|
||||
|
public short this[int idx] |
||||
|
{ |
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
get |
||||
|
{ |
||||
|
ref short self = ref Unsafe.As<FixedInt16Buffer18, short>(ref this); |
||||
|
return Unsafe.Add(ref self, idx); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
|
using System.Runtime.InteropServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components |
||||
|
{ |
||||
|
[StructLayout(LayoutKind.Sequential)] |
||||
|
internal unsafe struct FixedInt16Buffer256 |
||||
|
{ |
||||
|
public fixed short Data[256]; |
||||
|
|
||||
|
public short this[int idx] |
||||
|
{ |
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
get |
||||
|
{ |
||||
|
ref short self = ref Unsafe.As<FixedInt16Buffer256, short>(ref this); |
||||
|
return Unsafe.Add(ref self, idx); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
|
using System.Runtime.InteropServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components |
||||
|
{ |
||||
|
[StructLayout(LayoutKind.Sequential)] |
||||
|
internal unsafe struct FixedInt64Buffer18 |
||||
|
{ |
||||
|
public fixed long Data[18]; |
||||
|
|
||||
|
public long this[int idx] |
||||
|
{ |
||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
||||
|
get |
||||
|
{ |
||||
|
ref long self = ref Unsafe.As<FixedInt64Buffer18, long>(ref this); |
||||
|
return Unsafe.Add(ref self, idx); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,43 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.Numerics; |
|
||||
using SixLabors.ImageSharp.Memory; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Represents a component block
|
|
||||
/// </summary>
|
|
||||
internal class PdfJsComponent : IDisposable |
|
||||
{ |
|
||||
#pragma warning disable SA1401
|
|
||||
/// <summary>
|
|
||||
/// Gets or sets the output
|
|
||||
/// </summary>
|
|
||||
public IBuffer<short> Output; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets or sets the scaling factors
|
|
||||
/// </summary>
|
|
||||
public Vector2 Scale; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets or sets the number of blocks per line
|
|
||||
/// </summary>
|
|
||||
public int BlocksPerLine; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets or sets the number of blocks per column
|
|
||||
/// </summary>
|
|
||||
public int BlocksPerColumn; |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public void Dispose() |
|
||||
{ |
|
||||
this.Output?.Dispose(); |
|
||||
this.Output = null; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,32 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Contains all the decoded component blocks
|
|
||||
/// </summary>
|
|
||||
internal sealed class PdfJsComponentBlocks : IDisposable |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Gets or sets the component blocks
|
|
||||
/// </summary>
|
|
||||
public PdfJsComponent[] Components { get; set; } |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public void Dispose() |
|
||||
{ |
|
||||
if (this.Components != null) |
|
||||
{ |
|
||||
for (int i = 0; i < this.Components.Length; i++) |
|
||||
{ |
|
||||
this.Components[i].Dispose(); |
|
||||
} |
|
||||
|
|
||||
this.Components = null; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,513 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
using SixLabors.ImageSharp.Memory; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Performs the inverse Descrete Cosine Transform on each frame component.
|
|
||||
/// </summary>
|
|
||||
internal static class PdfJsIDCT |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Precomputed values scaled up by 14 bits
|
|
||||
/// </summary>
|
|
||||
public static readonly short[] Aanscales = |
|
||||
{ |
|
||||
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, 22725, 31521, 29692, 26722, 22725, 17855, |
|
||||
12299, 6270, 21407, 29692, 27969, 25172, 21407, 16819, 11585, |
|
||||
5906, 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, |
|
||||
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, 12873, |
|
||||
17855, 16819, 15137, 12873, 10114, 6967, 3552, 8867, 12299, |
|
||||
11585, 10426, 8867, 6967, 4799, 2446, 4520, 6270, 5906, 5315, |
|
||||
4520, 3552, 2446, 1247 |
|
||||
}; |
|
||||
|
|
||||
private const int DctCos1 = 4017; // cos(pi/16)
|
|
||||
private const int DctSin1 = 799; // sin(pi/16)
|
|
||||
private const int DctCos3 = 3406; // cos(3*pi/16)
|
|
||||
private const int DctSin3 = 2276; // sin(3*pi/16)
|
|
||||
private const int DctCos6 = 1567; // cos(6*pi/16)
|
|
||||
private const int DctSin6 = 3784; // sin(6*pi/16)
|
|
||||
private const int DctSqrt2 = 5793; // sqrt(2)
|
|
||||
private const int DctSqrt1D2 = 2896; // sqrt(2) / 2
|
|
||||
|
|
||||
#pragma warning disable SA1310 // Field names must not contain underscore
|
|
||||
private const int FIX_1_082392200 = 277; // FIX(1.082392200)
|
|
||||
private const int FIX_1_414213562 = 362; // FIX(1.414213562)
|
|
||||
private const int FIX_1_847759065 = 473; // FIX(1.847759065)
|
|
||||
private const int FIX_2_613125930 = 669; // FIX(2.613125930)
|
|
||||
#pragma warning restore SA1310 // Field names must not contain underscore
|
|
||||
|
|
||||
private const int ConstBits = 8; |
|
||||
private const int Pass1Bits = 2; // Factional bits in scale factors
|
|
||||
private const int MaxJSample = 255; |
|
||||
private const int CenterJSample = 128; |
|
||||
private const int RangeCenter = (MaxJSample * 2) + 2; |
|
||||
|
|
||||
// First segment of range limit table: limit[x] = 0 for x < 0
|
|
||||
// allow negative subscripts of simple table
|
|
||||
private const int TableOffset = 2 * (MaxJSample + 1); |
|
||||
private const int LimitOffset = TableOffset - (RangeCenter - CenterJSample); |
|
||||
|
|
||||
// Each IDCT routine is responsible for range-limiting its results and
|
|
||||
// converting them to unsigned form (0..MaxJSample). The raw outputs could
|
|
||||
// be quite far out of range if the input data is corrupt, so a bulletproof
|
|
||||
// range-limiting step is required. We use a mask-and-table-lookup method
|
|
||||
// to do the combined operations quickly, assuming that MaxJSample+1
|
|
||||
// is a power of 2.
|
|
||||
private const int RangeMask = (MaxJSample * 4) + 3; // 2 bits wider than legal samples
|
|
||||
|
|
||||
private static readonly byte[] Limit = new byte[5 * (MaxJSample + 1)]; |
|
||||
|
|
||||
static PdfJsIDCT() |
|
||||
{ |
|
||||
// Main part of range limit table: limit[x] = x
|
|
||||
int i; |
|
||||
for (i = 0; i <= MaxJSample; i++) |
|
||||
{ |
|
||||
Limit[TableOffset + i] = (byte)i; |
|
||||
} |
|
||||
|
|
||||
// End of range limit table: Limit[x] = MaxJSample for x > MaxJSample
|
|
||||
for (; i < 3 * (MaxJSample + 1); i++) |
|
||||
{ |
|
||||
Limit[TableOffset + i] = MaxJSample; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// A port of Poppler's IDCT method which in turn is taken from:
|
|
||||
/// Christoph Loeffler, Adriaan Ligtenberg, George S. Moschytz,
|
|
||||
/// 'Practical Fast 1-D DCT Algorithms with 11 Multiplications',
|
|
||||
/// IEEE Intl. Conf. on Acoustics, Speech & Signal Processing, 1989, 988-991.
|
|
||||
/// </summary>
|
|
||||
/// <param name="component">The fram component</param>
|
|
||||
/// <param name="blockBufferOffset">The block buffer offset</param>
|
|
||||
/// <param name="computationBuffer">The computational buffer for holding temp values</param>
|
|
||||
/// <param name="quantizationTable">The quantization table</param>
|
|
||||
public static void QuantizeAndInverse(PdfJsFrameComponent component, int blockBufferOffset, ref Span<short> computationBuffer, ref Span<short> quantizationTable) |
|
||||
{ |
|
||||
Span<short> blockData = component.BlockData.Slice(blockBufferOffset); |
|
||||
int v0, v1, v2, v3, v4, v5, v6, v7; |
|
||||
int p0, p1, p2, p3, p4, p5, p6, p7; |
|
||||
int t; |
|
||||
|
|
||||
// inverse DCT on rows
|
|
||||
for (int row = 0; row < 64; row += 8) |
|
||||
{ |
|
||||
// gather block data
|
|
||||
p0 = blockData[row]; |
|
||||
p1 = blockData[row + 1]; |
|
||||
p2 = blockData[row + 2]; |
|
||||
p3 = blockData[row + 3]; |
|
||||
p4 = blockData[row + 4]; |
|
||||
p5 = blockData[row + 5]; |
|
||||
p6 = blockData[row + 6]; |
|
||||
p7 = blockData[row + 7]; |
|
||||
|
|
||||
// dequant p0
|
|
||||
p0 *= quantizationTable[row]; |
|
||||
|
|
||||
// check for all-zero AC coefficients
|
|
||||
if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) == 0) |
|
||||
{ |
|
||||
t = ((DctSqrt2 * p0) + 512) >> 10; |
|
||||
short st = (short)t; |
|
||||
computationBuffer[row] = st; |
|
||||
computationBuffer[row + 1] = st; |
|
||||
computationBuffer[row + 2] = st; |
|
||||
computationBuffer[row + 3] = st; |
|
||||
computationBuffer[row + 4] = st; |
|
||||
computationBuffer[row + 5] = st; |
|
||||
computationBuffer[row + 6] = st; |
|
||||
computationBuffer[row + 7] = st; |
|
||||
continue; |
|
||||
} |
|
||||
|
|
||||
// dequant p1 ... p7
|
|
||||
p1 *= quantizationTable[row + 1]; |
|
||||
p2 *= quantizationTable[row + 2]; |
|
||||
p3 *= quantizationTable[row + 3]; |
|
||||
p4 *= quantizationTable[row + 4]; |
|
||||
p5 *= quantizationTable[row + 5]; |
|
||||
p6 *= quantizationTable[row + 6]; |
|
||||
p7 *= quantizationTable[row + 7]; |
|
||||
|
|
||||
// stage 4
|
|
||||
v0 = ((DctSqrt2 * p0) + 128) >> 8; |
|
||||
v1 = ((DctSqrt2 * p4) + 128) >> 8; |
|
||||
v2 = p2; |
|
||||
v3 = p6; |
|
||||
v4 = ((DctSqrt1D2 * (p1 - p7)) + 128) >> 8; |
|
||||
v7 = ((DctSqrt1D2 * (p1 + p7)) + 128) >> 8; |
|
||||
v5 = p3 << 4; |
|
||||
v6 = p5 << 4; |
|
||||
|
|
||||
// stage 3
|
|
||||
v0 = (v0 + v1 + 1) >> 1; |
|
||||
v1 = v0 - v1; |
|
||||
t = ((v2 * DctSin6) + (v3 * DctCos6) + 128) >> 8; |
|
||||
v2 = ((v2 * DctCos6) - (v3 * DctSin6) + 128) >> 8; |
|
||||
v3 = t; |
|
||||
v4 = (v4 + v6 + 1) >> 1; |
|
||||
v6 = v4 - v6; |
|
||||
v7 = (v7 + v5 + 1) >> 1; |
|
||||
v5 = v7 - v5; |
|
||||
|
|
||||
// stage 2
|
|
||||
v0 = (v0 + v3 + 1) >> 1; |
|
||||
v3 = v0 - v3; |
|
||||
v1 = (v1 + v2 + 1) >> 1; |
|
||||
v2 = v1 - v2; |
|
||||
t = ((v4 * DctSin3) + (v7 * DctCos3) + 2048) >> 12; |
|
||||
v4 = ((v4 * DctCos3) - (v7 * DctSin3) + 2048) >> 12; |
|
||||
v7 = t; |
|
||||
t = ((v5 * DctSin1) + (v6 * DctCos1) + 2048) >> 12; |
|
||||
v5 = ((v5 * DctCos1) - (v6 * DctSin1) + 2048) >> 12; |
|
||||
v6 = t; |
|
||||
|
|
||||
// stage 1
|
|
||||
computationBuffer[row] = (short)(v0 + v7); |
|
||||
computationBuffer[row + 7] = (short)(v0 - v7); |
|
||||
computationBuffer[row + 1] = (short)(v1 + v6); |
|
||||
computationBuffer[row + 6] = (short)(v1 - v6); |
|
||||
computationBuffer[row + 2] = (short)(v2 + v5); |
|
||||
computationBuffer[row + 5] = (short)(v2 - v5); |
|
||||
computationBuffer[row + 3] = (short)(v3 + v4); |
|
||||
computationBuffer[row + 4] = (short)(v3 - v4); |
|
||||
} |
|
||||
|
|
||||
// inverse DCT on columns
|
|
||||
for (int col = 0; col < 8; ++col) |
|
||||
{ |
|
||||
p0 = computationBuffer[col]; |
|
||||
p1 = computationBuffer[col + 8]; |
|
||||
p2 = computationBuffer[col + 16]; |
|
||||
p3 = computationBuffer[col + 24]; |
|
||||
p4 = computationBuffer[col + 32]; |
|
||||
p5 = computationBuffer[col + 40]; |
|
||||
p6 = computationBuffer[col + 48]; |
|
||||
p7 = computationBuffer[col + 56]; |
|
||||
|
|
||||
// check for all-zero AC coefficients
|
|
||||
if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) == 0) |
|
||||
{ |
|
||||
t = ((DctSqrt2 * p0) + 8192) >> 14; |
|
||||
|
|
||||
// convert to 8 bit
|
|
||||
t = (t < -2040) ? 0 : (t >= 2024) ? MaxJSample : (t + 2056) >> 4; |
|
||||
short st = (short)t; |
|
||||
|
|
||||
blockData[col] = st; |
|
||||
blockData[col + 8] = st; |
|
||||
blockData[col + 16] = st; |
|
||||
blockData[col + 24] = st; |
|
||||
blockData[col + 32] = st; |
|
||||
blockData[col + 40] = st; |
|
||||
blockData[col + 48] = st; |
|
||||
blockData[col + 56] = st; |
|
||||
continue; |
|
||||
} |
|
||||
|
|
||||
// stage 4
|
|
||||
v0 = ((DctSqrt2 * p0) + 2048) >> 12; |
|
||||
v1 = ((DctSqrt2 * p4) + 2048) >> 12; |
|
||||
v2 = p2; |
|
||||
v3 = p6; |
|
||||
v4 = ((DctSqrt1D2 * (p1 - p7)) + 2048) >> 12; |
|
||||
v7 = ((DctSqrt1D2 * (p1 + p7)) + 2048) >> 12; |
|
||||
v5 = p3; |
|
||||
v6 = p5; |
|
||||
|
|
||||
// stage 3
|
|
||||
// Shift v0 by 128.5 << 5 here, so we don't need to shift p0...p7 when
|
|
||||
// converting to UInt8 range later.
|
|
||||
v0 = ((v0 + v1 + 1) >> 1) + 4112; |
|
||||
v1 = v0 - v1; |
|
||||
t = ((v2 * DctSin6) + (v3 * DctCos6) + 2048) >> 12; |
|
||||
v2 = ((v2 * DctCos6) - (v3 * DctSin6) + 2048) >> 12; |
|
||||
v3 = t; |
|
||||
v4 = (v4 + v6 + 1) >> 1; |
|
||||
v6 = v4 - v6; |
|
||||
v7 = (v7 + v5 + 1) >> 1; |
|
||||
v5 = v7 - v5; |
|
||||
|
|
||||
// stage 2
|
|
||||
v0 = (v0 + v3 + 1) >> 1; |
|
||||
v3 = v0 - v3; |
|
||||
v1 = (v1 + v2 + 1) >> 1; |
|
||||
v2 = v1 - v2; |
|
||||
t = ((v4 * DctSin3) + (v7 * DctCos3) + 2048) >> 12; |
|
||||
v4 = ((v4 * DctCos3) - (v7 * DctSin3) + 2048) >> 12; |
|
||||
v7 = t; |
|
||||
t = ((v5 * DctSin1) + (v6 * DctCos1) + 2048) >> 12; |
|
||||
v5 = ((v5 * DctCos1) - (v6 * DctSin1) + 2048) >> 12; |
|
||||
v6 = t; |
|
||||
|
|
||||
// stage 1
|
|
||||
p0 = v0 + v7; |
|
||||
p7 = v0 - v7; |
|
||||
p1 = v1 + v6; |
|
||||
p6 = v1 - v6; |
|
||||
p2 = v2 + v5; |
|
||||
p5 = v2 - v5; |
|
||||
p3 = v3 + v4; |
|
||||
p4 = v3 - v4; |
|
||||
|
|
||||
// convert to 8-bit integers
|
|
||||
p0 = (p0 < 16) ? 0 : (p0 >= 4080) ? MaxJSample : p0 >> 4; |
|
||||
p1 = (p1 < 16) ? 0 : (p1 >= 4080) ? MaxJSample : p1 >> 4; |
|
||||
p2 = (p2 < 16) ? 0 : (p2 >= 4080) ? MaxJSample : p2 >> 4; |
|
||||
p3 = (p3 < 16) ? 0 : (p3 >= 4080) ? MaxJSample : p3 >> 4; |
|
||||
p4 = (p4 < 16) ? 0 : (p4 >= 4080) ? MaxJSample : p4 >> 4; |
|
||||
p5 = (p5 < 16) ? 0 : (p5 >= 4080) ? MaxJSample : p5 >> 4; |
|
||||
p6 = (p6 < 16) ? 0 : (p6 >= 4080) ? MaxJSample : p6 >> 4; |
|
||||
p7 = (p7 < 16) ? 0 : (p7 >= 4080) ? MaxJSample : p7 >> 4; |
|
||||
|
|
||||
// store block data
|
|
||||
blockData[col] = (short)p0; |
|
||||
blockData[col + 8] = (short)p1; |
|
||||
blockData[col + 16] = (short)p2; |
|
||||
blockData[col + 24] = (short)p3; |
|
||||
blockData[col + 32] = (short)p4; |
|
||||
blockData[col + 40] = (short)p5; |
|
||||
blockData[col + 48] = (short)p6; |
|
||||
blockData[col + 56] = (short)p7; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// A port of <see href="https://github.com/libjpeg-turbo/libjpeg-turbo/blob/master/jidctfst.c#L171"/>
|
|
||||
/// A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
|
|
||||
/// on each row(or vice versa, but it's more convenient to emit a row at
|
|
||||
/// a time). Direct algorithms are also available, but they are much more
|
|
||||
/// complex and seem not to be any faster when reduced to code.
|
|
||||
///
|
|
||||
/// This implementation is based on Arai, Agui, and Nakajima's algorithm for
|
|
||||
/// scaled DCT.Their original paper (Trans.IEICE E-71(11):1095) is in
|
|
||||
/// Japanese, but the algorithm is described in the Pennebaker & Mitchell
|
|
||||
/// JPEG textbook(see REFERENCES section in file README.ijg). The following
|
|
||||
/// code is based directly on figure 4-8 in P&M.
|
|
||||
/// While an 8-point DCT cannot be done in less than 11 multiplies, it is
|
|
||||
/// possible to arrange the computation so that many of the multiplies are
|
|
||||
/// simple scalings of the final outputs.These multiplies can then be
|
|
||||
/// folded into the multiplications or divisions by the JPEG quantization
|
|
||||
/// table entries. The AA&N method leaves only 5 multiplies and 29 adds
|
|
||||
/// to be done in the DCT itself.
|
|
||||
/// The primary disadvantage of this method is that with fixed-point math,
|
|
||||
/// accuracy is lost due to imprecise representation of the scaled
|
|
||||
/// quantization values.The smaller the quantization table entry, the less
|
|
||||
/// precise the scaled value, so this implementation does worse with high -
|
|
||||
/// quality - setting files than with low - quality ones.
|
|
||||
/// </summary>
|
|
||||
/// <param name="component">The frame component</param>
|
|
||||
/// <param name="blockBufferOffset">The block buffer offset</param>
|
|
||||
/// <param name="computationBuffer">The computational buffer for holding temp values</param>
|
|
||||
/// <param name="multiplierTable">The multiplier table</param>
|
|
||||
public static void QuantizeAndInverseFast(PdfJsFrameComponent component, int blockBufferOffset, ref Span<short> computationBuffer, ref Span<short> multiplierTable) |
|
||||
{ |
|
||||
Span<short> blockData = component.BlockData.Slice(blockBufferOffset); |
|
||||
int p0, p1, p2, p3, p4, p5, p6, p7; |
|
||||
|
|
||||
for (int col = 0; col < 8; col++) |
|
||||
{ |
|
||||
// Gather block data
|
|
||||
p0 = blockData[col]; |
|
||||
p1 = blockData[col + 8]; |
|
||||
p2 = blockData[col + 16]; |
|
||||
p3 = blockData[col + 24]; |
|
||||
p4 = blockData[col + 32]; |
|
||||
p5 = blockData[col + 40]; |
|
||||
p6 = blockData[col + 48]; |
|
||||
p7 = blockData[col + 56]; |
|
||||
|
|
||||
int tmp0 = p0 * multiplierTable[col]; |
|
||||
|
|
||||
// Due to quantization, we will usually find that many of the input
|
|
||||
// coefficients are zero, especially the AC terms. We can exploit this
|
|
||||
// by short-circuiting the IDCT calculation for any column in which all
|
|
||||
// the AC terms are zero. In that case each output is equal to the
|
|
||||
// DC coefficient (with scale factor as needed).
|
|
||||
// With typical images and quantization tables, half or more of the
|
|
||||
// column DCT calculations can be simplified this way.
|
|
||||
if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) == 0) |
|
||||
{ |
|
||||
short dcval = (short)tmp0; |
|
||||
|
|
||||
computationBuffer[col] = dcval; |
|
||||
computationBuffer[col + 8] = dcval; |
|
||||
computationBuffer[col + 16] = dcval; |
|
||||
computationBuffer[col + 24] = dcval; |
|
||||
computationBuffer[col + 32] = dcval; |
|
||||
computationBuffer[col + 40] = dcval; |
|
||||
computationBuffer[col + 48] = dcval; |
|
||||
computationBuffer[col + 56] = dcval; |
|
||||
|
|
||||
continue; |
|
||||
} |
|
||||
|
|
||||
// Even part
|
|
||||
int tmp1 = p2 * multiplierTable[col + 16]; |
|
||||
int tmp2 = p4 * multiplierTable[col + 32]; |
|
||||
int tmp3 = p6 * multiplierTable[col + 48]; |
|
||||
|
|
||||
int tmp10 = tmp0 + tmp2; // Phase 3
|
|
||||
int tmp11 = tmp0 - tmp2; |
|
||||
|
|
||||
int tmp13 = tmp1 + tmp3; // Phases 5-3
|
|
||||
int tmp12 = Multiply(tmp1 - tmp3, FIX_1_414213562) - tmp13; // 2*c4
|
|
||||
|
|
||||
tmp0 = tmp10 + tmp13; // Phase 2
|
|
||||
tmp3 = tmp10 - tmp13; |
|
||||
tmp1 = tmp11 + tmp12; |
|
||||
tmp2 = tmp11 - tmp12; |
|
||||
|
|
||||
// Odd Part
|
|
||||
int tmp4 = p1 * multiplierTable[col + 8]; |
|
||||
int tmp5 = p3 * multiplierTable[col + 24]; |
|
||||
int tmp6 = p5 * multiplierTable[col + 40]; |
|
||||
int tmp7 = p7 * multiplierTable[col + 56]; |
|
||||
|
|
||||
int z13 = tmp6 + tmp5; // Phase 6
|
|
||||
int z10 = tmp6 - tmp5; |
|
||||
int z11 = tmp4 + tmp7; |
|
||||
int z12 = tmp4 - tmp7; |
|
||||
|
|
||||
tmp7 = z11 + z13; // Phase 5
|
|
||||
tmp11 = Multiply(z11 - z13, FIX_1_414213562); // 2*c4
|
|
||||
|
|
||||
int z5 = Multiply(z10 + z12, FIX_1_847759065); // 2*c2
|
|
||||
tmp10 = z5 - Multiply(z12, FIX_1_082392200); // 2*(c2-c6)
|
|
||||
tmp12 = z5 - Multiply(z10, FIX_2_613125930); // 2*(c2+c6)
|
|
||||
|
|
||||
tmp6 = tmp12 - tmp7; // Phase 2
|
|
||||
tmp5 = tmp11 - tmp6; |
|
||||
tmp4 = tmp10 - tmp5; |
|
||||
|
|
||||
computationBuffer[col] = (short)(tmp0 + tmp7); |
|
||||
computationBuffer[col + 56] = (short)(tmp0 - tmp7); |
|
||||
computationBuffer[col + 8] = (short)(tmp1 + tmp6); |
|
||||
computationBuffer[col + 48] = (short)(tmp1 - tmp6); |
|
||||
computationBuffer[col + 16] = (short)(tmp2 + tmp5); |
|
||||
computationBuffer[col + 40] = (short)(tmp2 - tmp5); |
|
||||
computationBuffer[col + 24] = (short)(tmp3 + tmp4); |
|
||||
computationBuffer[col + 32] = (short)(tmp3 - tmp4); |
|
||||
} |
|
||||
|
|
||||
// Pass 2: process rows from work array, store into output array.
|
|
||||
// Note that we must descale the results by a factor of 8 == 2**3,
|
|
||||
// and also undo the pass 1 bits scaling.
|
|
||||
for (int row = 0; row < 64; row += 8) |
|
||||
{ |
|
||||
p1 = computationBuffer[row + 1]; |
|
||||
p2 = computationBuffer[row + 2]; |
|
||||
p3 = computationBuffer[row + 3]; |
|
||||
p4 = computationBuffer[row + 4]; |
|
||||
p5 = computationBuffer[row + 5]; |
|
||||
p6 = computationBuffer[row + 6]; |
|
||||
p7 = computationBuffer[row + 7]; |
|
||||
|
|
||||
// Add range center and fudge factor for final descale and range-limit.
|
|
||||
int z5 = computationBuffer[row] + (RangeCenter << (Pass1Bits + 3)) + (1 << (Pass1Bits + 2)); |
|
||||
|
|
||||
// Check for all-zero AC coefficients
|
|
||||
if ((p1 | p2 | p3 | p4 | p5 | p6 | p7) == 0) |
|
||||
{ |
|
||||
byte dcval = Limit[LimitOffset + (RightShift(z5, Pass1Bits + 3) & RangeMask)]; |
|
||||
|
|
||||
blockData[row] = dcval; |
|
||||
blockData[row + 1] = dcval; |
|
||||
blockData[row + 2] = dcval; |
|
||||
blockData[row + 3] = dcval; |
|
||||
blockData[row + 4] = dcval; |
|
||||
blockData[row + 5] = dcval; |
|
||||
blockData[row + 6] = dcval; |
|
||||
blockData[row + 7] = dcval; |
|
||||
|
|
||||
continue; |
|
||||
} |
|
||||
|
|
||||
// Even part
|
|
||||
int tmp10 = z5 + p4; |
|
||||
int tmp11 = z5 - p4; |
|
||||
|
|
||||
int tmp13 = p2 + p6; |
|
||||
int tmp12 = Multiply(p2 - p6, FIX_1_414213562) - tmp13; // 2*c4
|
|
||||
|
|
||||
int tmp0 = tmp10 + tmp13; |
|
||||
int tmp3 = tmp10 - tmp13; |
|
||||
int tmp1 = tmp11 + tmp12; |
|
||||
int tmp2 = tmp11 - tmp12; |
|
||||
|
|
||||
// Odd part
|
|
||||
int z13 = p5 + p3; |
|
||||
int z10 = p5 - p3; |
|
||||
int z11 = p1 + p7; |
|
||||
int z12 = p1 - p7; |
|
||||
|
|
||||
int tmp7 = z11 + z13; // Phase 5
|
|
||||
tmp11 = Multiply(z11 - z13, FIX_1_414213562); // 2*c4
|
|
||||
|
|
||||
z5 = Multiply(z10 + z12, FIX_1_847759065); // 2*c2
|
|
||||
tmp10 = z5 - Multiply(z12, FIX_1_082392200); // 2*(c2-c6)
|
|
||||
tmp12 = z5 - Multiply(z10, FIX_2_613125930); // 2*(c2+c6)
|
|
||||
|
|
||||
int tmp6 = tmp12 - tmp7; // Phase 2
|
|
||||
int tmp5 = tmp11 - tmp6; |
|
||||
int tmp4 = tmp10 - tmp5; |
|
||||
|
|
||||
// Final output stage: scale down by a factor of 8, offset, and range-limit
|
|
||||
blockData[row] = Limit[LimitOffset + (RightShift(tmp0 + tmp7, Pass1Bits + 3) & RangeMask)]; |
|
||||
blockData[row + 7] = Limit[LimitOffset + (RightShift(tmp0 - tmp7, Pass1Bits + 3) & RangeMask)]; |
|
||||
blockData[row + 1] = Limit[LimitOffset + (RightShift(tmp1 + tmp6, Pass1Bits + 3) & RangeMask)]; |
|
||||
blockData[row + 6] = Limit[LimitOffset + (RightShift(tmp1 - tmp6, Pass1Bits + 3) & RangeMask)]; |
|
||||
blockData[row + 2] = Limit[LimitOffset + (RightShift(tmp2 + tmp5, Pass1Bits + 3) & RangeMask)]; |
|
||||
blockData[row + 5] = Limit[LimitOffset + (RightShift(tmp2 - tmp5, Pass1Bits + 3) & RangeMask)]; |
|
||||
blockData[row + 3] = Limit[LimitOffset + (RightShift(tmp3 + tmp4, Pass1Bits + 3) & RangeMask)]; |
|
||||
blockData[row + 4] = Limit[LimitOffset + (RightShift(tmp3 - tmp4, Pass1Bits + 3) & RangeMask)]; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Descale and correctly round an int value that's scaled by <paramref name="n"/> bits.
|
|
||||
/// We assume <see cref="RightShift"/> rounds towards minus infinity, so adding
|
|
||||
/// the fudge factor is correct for either sign of <paramref name="value"/>.
|
|
||||
/// </summary>
|
|
||||
/// <param name="value">The value</param>
|
|
||||
/// <param name="n">The number of bits</param>
|
|
||||
/// <returns>The <see cref="int"/></returns>
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static int Descale(int value, int n) |
|
||||
{ |
|
||||
return RightShift(value + (1 << (n - 1)), n); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Multiply a variable by an int constant, and immediately descale.
|
|
||||
/// </summary>
|
|
||||
/// <param name="val">The value</param>
|
|
||||
/// <param name="c">The multiplier</param>
|
|
||||
/// <returns>The <see cref="int"/></returns>
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
private static int Multiply(int val, int c) |
|
||||
{ |
|
||||
return Descale(val * c, ConstBits); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Right-shifts the value by the given amount
|
|
||||
/// </summary>
|
|
||||
/// <param name="value">The value</param>
|
|
||||
/// <param name="shift">The amount to shift by</param>
|
|
||||
/// <returns>The <see cref="int"/></returns>
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
private static int RightShift(int value, int shift) |
|
||||
{ |
|
||||
return value >> shift; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,149 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.Diagnostics; |
|
||||
using System.Numerics; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
using SixLabors.ImageSharp.Memory; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Represents a section of the jpeg component data laid out in pixel order.
|
|
||||
/// </summary>
|
|
||||
internal struct PdfJsJpegPixelArea : IDisposable |
|
||||
{ |
|
||||
private readonly MemoryManager memoryManager; |
|
||||
|
|
||||
private readonly int imageWidth; |
|
||||
|
|
||||
private readonly int imageHeight; |
|
||||
|
|
||||
private IBuffer<byte> componentData; |
|
||||
|
|
||||
private int rowStride; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Initializes a new instance of the <see cref="PdfJsJpegPixelArea"/> struct.
|
|
||||
/// </summary>
|
|
||||
/// <param name="memoryManager">The <see cref="MemoryManager"/> to use for buffer allocations.</param>
|
|
||||
/// <param name="imageWidth">The image width</param>
|
|
||||
/// <param name="imageHeight">The image height</param>
|
|
||||
/// <param name="numberOfComponents">The number of components</param>
|
|
||||
public PdfJsJpegPixelArea(MemoryManager memoryManager, int imageWidth, int imageHeight, int numberOfComponents) |
|
||||
{ |
|
||||
this.memoryManager = memoryManager; |
|
||||
this.imageWidth = imageWidth; |
|
||||
this.imageHeight = imageHeight; |
|
||||
this.Width = 0; |
|
||||
this.Height = 0; |
|
||||
this.NumberOfComponents = numberOfComponents; |
|
||||
this.componentData = null; |
|
||||
this.rowStride = 0; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the number of components
|
|
||||
/// </summary>
|
|
||||
public int NumberOfComponents { get; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the width
|
|
||||
/// </summary>
|
|
||||
public int Width { get; private set; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the height
|
|
||||
/// </summary>
|
|
||||
public int Height { get; private set; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Organsizes the decoded jpeg components into a linear array ordered by component.
|
|
||||
/// This must be called before attempting to retrieve the data.
|
|
||||
/// </summary>
|
|
||||
/// <param name="components">The jpeg component blocks</param>
|
|
||||
/// <param name="width">The pixel area width</param>
|
|
||||
/// <param name="height">The pixel area height</param>
|
|
||||
public void LinearizeBlockData(PdfJsComponentBlocks components, int width, int height) |
|
||||
{ |
|
||||
this.Width = width; |
|
||||
this.Height = height; |
|
||||
int numberOfComponents = this.NumberOfComponents; |
|
||||
this.rowStride = width * numberOfComponents; |
|
||||
var scale = new Vector2(this.imageWidth / (float)width, this.imageHeight / (float)height); |
|
||||
|
|
||||
this.componentData = this.memoryManager.Allocate<byte>(width * height * numberOfComponents); |
|
||||
Span<byte> componentDataSpan = this.componentData.Span; |
|
||||
const uint Mask3Lsb = 0xFFFFFFF8; // Used to clear the 3 LSBs
|
|
||||
|
|
||||
using (IBuffer<int> xScaleBlockOffset = this.memoryManager.Allocate<int>(width)) |
|
||||
{ |
|
||||
Span<int> xScaleBlockOffsetSpan = xScaleBlockOffset.Span; |
|
||||
for (int i = 0; i < numberOfComponents; i++) |
|
||||
{ |
|
||||
ref PdfJsComponent component = ref components.Components[i]; |
|
||||
Vector2 componentScale = component.Scale * scale; |
|
||||
int offset = i; |
|
||||
Span<short> output = component.Output.Span; |
|
||||
int blocksPerScanline = (component.BlocksPerLine + 1) << 3; |
|
||||
|
|
||||
// Precalculate the xScaleBlockOffset
|
|
||||
int j; |
|
||||
for (int x = 0; x < width; x++) |
|
||||
{ |
|
||||
j = (int)(x * componentScale.X); |
|
||||
xScaleBlockOffsetSpan[x] = (int)((j & Mask3Lsb) << 3) | (j & 7); |
|
||||
} |
|
||||
|
|
||||
// Linearize the blocks of the component
|
|
||||
for (int y = 0; y < height; y++) |
|
||||
{ |
|
||||
j = (int)(y * componentScale.Y); |
|
||||
int index = blocksPerScanline * (int)(j & Mask3Lsb) | ((j & 7) << 3); |
|
||||
for (int x = 0; x < width; x++) |
|
||||
{ |
|
||||
componentDataSpan[offset] = (byte)output[index + xScaleBlockOffsetSpan[x]]; |
|
||||
offset += numberOfComponents; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets a <see cref="Span{Byte}"/> representing the row 'y' beginning from the the first byte on that row.
|
|
||||
/// </summary>
|
|
||||
/// <param name="y">The y-coordinate of the pixel row. Must be greater than or equal to zero and less than the height of the pixel area.</param>
|
|
||||
/// <returns>The <see cref="Span{TPixel}"/></returns>
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public Span<byte> GetRowSpan(int y) |
|
||||
{ |
|
||||
this.CheckCoordinates(y); |
|
||||
return this.componentData.Slice(y * this.rowStride, this.rowStride); |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public void Dispose() |
|
||||
{ |
|
||||
this.componentData?.Dispose(); |
|
||||
this.componentData = null; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Checks the coordinates to ensure they are within bounds.
|
|
||||
/// </summary>
|
|
||||
/// <param name="y">The y-coordinate of the row. Must be greater than zero and less than the height of the area.</param>
|
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
|
||||
/// Thrown if the coordinates are not within the bounds of the image.
|
|
||||
/// </exception>
|
|
||||
[Conditional("DEBUG")] |
|
||||
private void CheckCoordinates(int y) |
|
||||
{ |
|
||||
if (y < 0 || y >= this.Height) |
|
||||
{ |
|
||||
throw new ArgumentOutOfRangeException(nameof(y), y, $"{y} is outwith the area bounds."); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,67 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
using SixLabors.ImageSharp.Memory; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Contains the quantization tables.
|
|
||||
/// </summary>
|
|
||||
internal sealed class PdfJsQuantizationTables : IDisposable |
|
||||
{ |
|
||||
public PdfJsQuantizationTables(MemoryManager memoryManager) |
|
||||
{ |
|
||||
this.Tables = memoryManager.Allocate2D<short>(64, 4); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the ZigZag scan table
|
|
||||
/// </summary>
|
|
||||
public static byte[] DctZigZag |
|
||||
{ |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
get; |
|
||||
} |
|
||||
|
|
||||
= |
|
||||
{ |
|
||||
0, |
|
||||
1, 8, |
|
||||
16, 9, 2, |
|
||||
3, 10, 17, 24, |
|
||||
32, 25, 18, 11, 4, |
|
||||
5, 12, 19, 26, 33, 40, |
|
||||
48, 41, 34, 27, 20, 13, 6, |
|
||||
7, 14, 21, 28, 35, 42, 49, 56, |
|
||||
57, 50, 43, 36, 29, 22, 15, |
|
||||
23, 30, 37, 44, 51, 58, |
|
||||
59, 52, 45, 38, 31, |
|
||||
39, 46, 53, 60, |
|
||||
61, 54, 47, |
|
||||
55, 62, |
|
||||
63 |
|
||||
}; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets or sets the quantization tables.
|
|
||||
/// </summary>
|
|
||||
public Buffer2D<short> Tables |
|
||||
{ |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
get; set; |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public void Dispose() |
|
||||
{ |
|
||||
if (this.Tables != null) |
|
||||
{ |
|
||||
this.Tables.Dispose(); |
|
||||
this.Tables = null; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,131 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System.Runtime.CompilerServices; |
|
||||
using SixLabors.ImageSharp.PixelFormats; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Provides 8-bit lookup tables for converting from YCbCr to Rgb colorspace.
|
|
||||
/// Methods to build the tables are based on libjpeg implementation.
|
|
||||
/// </summary>
|
|
||||
internal readonly struct PdfJsYCbCrToRgbTables |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// The red red-chrominance table
|
|
||||
/// </summary>
|
|
||||
public static int[] CrRTable = new int[256]; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// The blue blue-chrominance table
|
|
||||
/// </summary>
|
|
||||
public static int[] CbBTable = new int[256]; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// The green red-chrominance table
|
|
||||
/// </summary>
|
|
||||
public static int[] CrGTable = new int[256]; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// The green blue-chrominance table
|
|
||||
/// </summary>
|
|
||||
public static int[] CbGTable = new int[256]; |
|
||||
|
|
||||
// Speediest right-shift on some machines and gives us enough accuracy at 4 decimal places.
|
|
||||
private const int ScaleBits = 16; |
|
||||
|
|
||||
private const int Half = 1 << (ScaleBits - 1); |
|
||||
|
|
||||
private const int MinSample = 0; |
|
||||
|
|
||||
private const int HalfSample = 128; |
|
||||
|
|
||||
private const int MaxSample = 255; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Initializes the YCbCr tables
|
|
||||
/// </summary>
|
|
||||
public static void Create() |
|
||||
{ |
|
||||
for (int i = 0, x = -128; i <= 255; i++, x++) |
|
||||
{ |
|
||||
// i is the actual input pixel value, in the range 0..255
|
|
||||
// The Cb or Cr value we are thinking of is x = i - 128
|
|
||||
// Cr=>R value is nearest int to 1.402 * x
|
|
||||
CrRTable[i] = RightShift((Fix(1.402F) * x) + Half); |
|
||||
|
|
||||
// Cb=>B value is nearest int to 1.772 * x
|
|
||||
CbBTable[i] = RightShift((Fix(1.772F) * x) + Half); |
|
||||
|
|
||||
// Cr=>G value is scaled-up -0.714136286
|
|
||||
CrGTable[i] = (-Fix(0.714136286F)) * x; |
|
||||
|
|
||||
// Cb => G value is scaled - up - 0.344136286 * x
|
|
||||
// We also add in Half so that need not do it in inner loop
|
|
||||
CbGTable[i] = ((-Fix(0.344136286F)) * x) + Half; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Optimized method to pack bytes to the image from the YCbCr color space.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
||||
/// <param name="packed">The packed pixel.</param>
|
|
||||
/// <param name="y">The y luminance component.</param>
|
|
||||
/// <param name="cb">The cb chroma component.</param>
|
|
||||
/// <param name="cr">The cr chroma component.</param>
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void PackYCbCr<TPixel>(ref TPixel packed, byte y, byte cb, byte cr) |
|
||||
where TPixel : struct, IPixel<TPixel> |
|
||||
{ |
|
||||
byte r = (byte)(y + CrRTable[cr]).Clamp(0, 255); |
|
||||
|
|
||||
// The values for the G calculation are left scaled up, since we must add them together before rounding.
|
|
||||
byte g = (byte)(y + RightShift(CbGTable[cb] + CrGTable[cr])).Clamp(0, 255); |
|
||||
|
|
||||
byte b = (byte)(y + CbBTable[cb]).Clamp(0, 255); |
|
||||
|
|
||||
packed.PackFromRgba32(new Rgba32(r, g, b, 255)); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Optimized method to pack bytes to the image from the YccK color space.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
||||
/// <param name="packed">The packed pixel.</param>
|
|
||||
/// <param name="y">The y luminance component.</param>
|
|
||||
/// <param name="cb">The cb chroma component.</param>
|
|
||||
/// <param name="cr">The cr chroma component.</param>
|
|
||||
/// <param name="k">The keyline component.</param>
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void PackYccK<TPixel>(ref TPixel packed, byte y, byte cb, byte cr, byte k) |
|
||||
where TPixel : struct, IPixel<TPixel> |
|
||||
{ |
|
||||
int c = (MaxSample - (y + CrRTable[cr])).Clamp(0, 255); |
|
||||
|
|
||||
// The values for the G calculation are left scaled up, since we must add them together before rounding.
|
|
||||
int m = (MaxSample - (y + RightShift(CbGTable[cb] + CrGTable[cr]))).Clamp(0, 255); |
|
||||
|
|
||||
int cy = (MaxSample - (y + CbBTable[cb])).Clamp(0, 255); |
|
||||
|
|
||||
byte r = (byte)((c * k) / MaxSample); |
|
||||
byte g = (byte)((m * k) / MaxSample); |
|
||||
byte b = (byte)((cy * k) / MaxSample); |
|
||||
|
|
||||
packed.PackFromRgba32(new Rgba32(r, g, b, MaxSample)); |
|
||||
} |
|
||||
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
private static int Fix(float x) |
|
||||
{ |
|
||||
return (int)((x * (1L << ScaleBits)) + 0.5F); |
|
||||
} |
|
||||
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
private static int RightShift(int x) |
|
||||
{ |
|
||||
return x >> ScaleBits; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,28 @@ |
|||||
|
using System.Runtime.CompilerServices; |
||||
|
using BenchmarkDotNet.Attributes; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Benchmarks.General |
||||
|
{ |
||||
|
public class StructCasting |
||||
|
{ |
||||
|
[Benchmark(Baseline = true)] |
||||
|
public short ExplicitCast() |
||||
|
{ |
||||
|
int x = 5 * 2; |
||||
|
return (short)x; |
||||
|
} |
||||
|
|
||||
|
[Benchmark] |
||||
|
public short UnsafeCast() |
||||
|
{ |
||||
|
int x = 5 * 2; |
||||
|
return Unsafe.As<int, short>(ref x); |
||||
|
} |
||||
|
|
||||
|
[Benchmark] |
||||
|
public short UnsafeCastRef() |
||||
|
{ |
||||
|
return Unsafe.As<int, short>(ref Unsafe.AsRef(5 * 2)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
using System; |
||||
|
using SixLabors.ImageSharp.Formats.Bmp; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.Formats.Bmp |
||||
|
{ |
||||
|
public class BmpFileHeaderTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void TestWrite() |
||||
|
{ |
||||
|
var header = new BmpFileHeader(1, 2, 3, 4); |
||||
|
|
||||
|
byte[] buffer = new byte[14]; |
||||
|
|
||||
|
header.WriteTo(buffer); |
||||
|
|
||||
|
Assert.Equal("AQACAAAAAwAAAAQAAAA=", Convert.ToBase64String(buffer)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
using System; |
||||
|
using System.Buffers.Binary; |
||||
|
using System.Text; |
||||
|
using SixLabors.ImageSharp.Formats.Png; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests.Formats.Png |
||||
|
{ |
||||
|
public class PngChunkTypeTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void ChunkTypeIdsAreCorrect() |
||||
|
{ |
||||
|
Assert.Equal(PngChunkType.Header, GetType("IHDR")); |
||||
|
Assert.Equal(PngChunkType.Palette, GetType("PLTE")); |
||||
|
Assert.Equal(PngChunkType.Data, GetType("IDAT")); |
||||
|
Assert.Equal(PngChunkType.End, GetType("IEND")); |
||||
|
Assert.Equal(PngChunkType.PaletteAlpha, GetType("tRNS")); |
||||
|
Assert.Equal(PngChunkType.Text, GetType("tEXt")); |
||||
|
Assert.Equal(PngChunkType.Gamma, GetType("gAMA")); |
||||
|
Assert.Equal(PngChunkType.Physical, GetType("pHYs")); |
||||
|
} |
||||
|
|
||||
|
private static PngChunkType GetType(string text) |
||||
|
{ |
||||
|
return (PngChunkType)BinaryPrimitives.ReadInt32BigEndian(Encoding.UTF8.GetBytes(text)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1 +1 @@ |
|||||
Subproject commit 5a66c9c6da02bf27345f90adc05d415c0d0450ea |
Subproject commit f1c585d0b931504d33ae2741ede72c0bf5ae5cb7 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:5bef85b47c222ce3f3f5bea302619336290cfeb329537e704c45de939072fd93 |
||||
|
size 2192567 |
||||
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:efdbe5aae2ebc1841dd58f07bb999b776ebf99f629b81a2c537fdb0f62edddc1 |
||||
|
size 3764739 |
||||
Loading…
Reference in new issue