Skip to content

Commit

Permalink
Merge branch 'master' into bp/upscalesse
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpopow authored Nov 24, 2021
2 parents 65870b9 + e11f318 commit 984a725
Show file tree
Hide file tree
Showing 16 changed files with 612 additions and 566 deletions.
58 changes: 58 additions & 0 deletions src/ImageSharp/Formats/Jpeg/Components/Block8x8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,64 @@ public nint GetLastNonZeroIndex()
}
}

/// <summary>
/// Transpose the block inplace.
/// </summary>
[MethodImpl(InliningOptions.ShortMethod)]
public void TransposeInplace()
{
ref short elemRef = ref Unsafe.As<Block8x8, short>(ref this);

// row #0
Swap(ref Unsafe.Add(ref elemRef, 1), ref Unsafe.Add(ref elemRef, 8));
Swap(ref Unsafe.Add(ref elemRef, 2), ref Unsafe.Add(ref elemRef, 16));
Swap(ref Unsafe.Add(ref elemRef, 3), ref Unsafe.Add(ref elemRef, 24));
Swap(ref Unsafe.Add(ref elemRef, 4), ref Unsafe.Add(ref elemRef, 32));
Swap(ref Unsafe.Add(ref elemRef, 5), ref Unsafe.Add(ref elemRef, 40));
Swap(ref Unsafe.Add(ref elemRef, 6), ref Unsafe.Add(ref elemRef, 48));
Swap(ref Unsafe.Add(ref elemRef, 7), ref Unsafe.Add(ref elemRef, 56));

// row #1
Swap(ref Unsafe.Add(ref elemRef, 10), ref Unsafe.Add(ref elemRef, 17));
Swap(ref Unsafe.Add(ref elemRef, 11), ref Unsafe.Add(ref elemRef, 25));
Swap(ref Unsafe.Add(ref elemRef, 12), ref Unsafe.Add(ref elemRef, 33));
Swap(ref Unsafe.Add(ref elemRef, 13), ref Unsafe.Add(ref elemRef, 41));
Swap(ref Unsafe.Add(ref elemRef, 14), ref Unsafe.Add(ref elemRef, 49));
Swap(ref Unsafe.Add(ref elemRef, 15), ref Unsafe.Add(ref elemRef, 57));

// row #2
Swap(ref Unsafe.Add(ref elemRef, 19), ref Unsafe.Add(ref elemRef, 26));
Swap(ref Unsafe.Add(ref elemRef, 20), ref Unsafe.Add(ref elemRef, 34));
Swap(ref Unsafe.Add(ref elemRef, 21), ref Unsafe.Add(ref elemRef, 42));
Swap(ref Unsafe.Add(ref elemRef, 22), ref Unsafe.Add(ref elemRef, 50));
Swap(ref Unsafe.Add(ref elemRef, 23), ref Unsafe.Add(ref elemRef, 58));

// row #3
Swap(ref Unsafe.Add(ref elemRef, 28), ref Unsafe.Add(ref elemRef, 35));
Swap(ref Unsafe.Add(ref elemRef, 29), ref Unsafe.Add(ref elemRef, 43));
Swap(ref Unsafe.Add(ref elemRef, 30), ref Unsafe.Add(ref elemRef, 51));
Swap(ref Unsafe.Add(ref elemRef, 31), ref Unsafe.Add(ref elemRef, 59));

// row #4
Swap(ref Unsafe.Add(ref elemRef, 37), ref Unsafe.Add(ref elemRef, 44));
Swap(ref Unsafe.Add(ref elemRef, 38), ref Unsafe.Add(ref elemRef, 52));
Swap(ref Unsafe.Add(ref elemRef, 39), ref Unsafe.Add(ref elemRef, 60));

// row #5
Swap(ref Unsafe.Add(ref elemRef, 46), ref Unsafe.Add(ref elemRef, 53));
Swap(ref Unsafe.Add(ref elemRef, 47), ref Unsafe.Add(ref elemRef, 61));

// row #6
Swap(ref Unsafe.Add(ref elemRef, 55), ref Unsafe.Add(ref elemRef, 62));

static void Swap(ref short a, ref short b)
{
short tmp = a;
a = b;
b = tmp;
}
}

/// <summary>
/// Calculate the total sum of absolute differences of elements in 'a' and 'b'.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ private void DecodeBlockBaseline(
{
i += r;
s = buffer.Receive(s);
Unsafe.Add(ref blockDataRef, ZigZag.ZigZagOrder[i++]) = (short)s;
Unsafe.Add(ref blockDataRef, ZigZag.TransposingOrder[i++]) = (short)s;
}
else
{
Expand Down Expand Up @@ -571,7 +571,7 @@ private void DecodeBlockProgressiveAC(ref Block8x8 block, ref HuffmanTable acTab
if (s != 0)
{
s = buffer.Receive(s);
Unsafe.Add(ref blockDataRef, ZigZag.ZigZagOrder[i]) = (short)(s << low);
Unsafe.Add(ref blockDataRef, ZigZag.TransposingOrder[i]) = (short)(s << low);
}
else
{
Expand Down Expand Up @@ -647,7 +647,7 @@ private void DecodeBlockProgressiveACRefined(ref short blockDataRef, ref Huffman

do
{
ref short coef = ref Unsafe.Add(ref blockDataRef, ZigZag.ZigZagOrder[k]);
ref short coef = ref Unsafe.Add(ref blockDataRef, ZigZag.TransposingOrder[k]);
if (coef != 0)
{
buffer.CheckBits();
Expand All @@ -673,7 +673,7 @@ private void DecodeBlockProgressiveACRefined(ref short blockDataRef, ref Huffman

if ((s != 0) && (k < 64))
{
Unsafe.Add(ref blockDataRef, ZigZag.ZigZagOrder[k]) = (short)s;
Unsafe.Add(ref blockDataRef, ZigZag.TransposingOrder[k]) = (short)s;
}
}
}
Expand All @@ -682,7 +682,7 @@ private void DecodeBlockProgressiveACRefined(ref short blockDataRef, ref Huffman
{
for (; k <= end; k++)
{
ref short coef = ref Unsafe.Add(ref blockDataRef, ZigZag.ZigZagOrder[k]);
ref short coef = ref Unsafe.Add(ref blockDataRef, ZigZag.TransposingOrder[k]);

if (coef != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ internal struct JpegBlockPostProcessor
/// </summary>
public Block8x8F SourceBlock;

/// <summary>
/// Temporal block to store intermediate computation results.
/// </summary>
public Block8x8F WorkspaceBlock;

/// <summary>
/// The quantization table as <see cref="Block8x8F"/>.
/// </summary>
Expand All @@ -45,7 +40,6 @@ public JpegBlockPostProcessor(IRawJpegData decoder, IJpegComponent component)
this.subSamplingDivisors = component.SubSamplingDivisors;

this.SourceBlock = default;
this.WorkspaceBlock = default;
}

/// <summary>
Expand All @@ -71,7 +65,7 @@ public void ProcessBlockColorsInto(
// Dequantize:
block.MultiplyInPlace(ref this.DequantiazationTable);

FastFloatingPointDCT.TransformIDCT(ref block, ref this.WorkspaceBlock);
FastFloatingPointDCT.TransformIDCT(ref block);

// To conform better to libjpeg we actually NEED TO loose precision here.
// This is because they store blocks as Int16 between all the operations.
Expand Down
Loading

0 comments on commit 984a725

Please sign in to comment.