Browse Source

Meh

af/merge-core
James Jackson-South 8 years ago
parent
commit
fcc6d531f2
  1. 24
      src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer257.cs
  2. 120
      src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs
  3. 9
      src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs
  4. 2
      src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs

24
src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/FixedByteBuffer257.cs

@ -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 FixedByteBuffer257
{
public fixed byte Data[257];
public byte this[int idx]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
ref byte self = ref Unsafe.As<FixedByteBuffer257, byte>(ref this);
return Unsafe.Add(ref self, idx);
}
}
}
}

120
src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsHuffmanTable.cs

@ -50,69 +50,69 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
const int Length = 257;
using (IBuffer<short> huffcode = memoryManager.Allocate<short>(Length))
{
// Span<short> codes = huffcode.Span;
Span<short> codes = huffcode.Span;
ref short huffcodeRef = ref MemoryMarshal.GetReference(huffcode.Span);
this.GenerateSizeTable(count);
//int k = 0;
//fixed (short* sizesRef = this.Sizes.Data)
//fixed (short* deltaRef = this.ValOffset.Data)
//fixed (long* maxcodeRef = this.MaxCode.Data)
//{
// uint code = 0;
// int j;
// for (j = 1; j <= 16; j++)
// {
// // Compute delta to add to code to compute symbol id.
// deltaRef[j] = (short)(k - code);
// if (sizesRef[k] == j)
// {
// while (sizesRef[k] == j)
// {
// codes[k++] = (short)code++;
// // Unsafe.Add(ref huffcodeRef, k++) = (short)code++;
// // TODO: Throw if invalid?
// }
// }
// // Compute largest code + 1 for this size. preshifted as neeed later.
// maxcodeRef[j] = code << (16 - j);
// code <<= 1;
// }
// maxcodeRef[j] = 0xFFFFFFFF;
//}
//fixed (short* lookaheadRef = this.Lookahead.Data)
//{
// const int FastBits = ScanDecoder.FastBits;
// var fast = new Span<short>(lookaheadRef, 1 << FastBits);
// fast.Fill(255); // Flag for non-accelerated
// fixed (short* sizesRef = this.Sizes.Data)
// {
// for (int i = 0; i < k; i++)
// {
// int s = sizesRef[i];
// if (s <= ScanDecoder.FastBits)
// {
// int c = codes[i] << (FastBits - s);
// int m = 1 << (FastBits - s);
// for (int j = 0; j < m; j++)
// {
// fast[c + j] = (byte)i;
// }
// }
// }
// }
//}
this.GenerateCodeTable(ref huffcodeRef, Length, out int k);
this.GenerateDecoderTables(count, ref huffcodeRef);
this.GenerateLookaheadTables(count, values, ref huffcodeRef, k);
int k = 0;
fixed (short* size = this.Sizes.Data)
fixed (short* delta = this.ValOffset.Data)
fixed (long* maxcode = this.MaxCode.Data)
{
uint code = 0;
int j;
for (j = 1; j <= 16; j++)
{
// Compute delta to add to code to compute symbol id.
delta[j] = (short)(k - code);
if (size[k] == j)
{
while (size[k] == j)
{
codes[k++] = (short)code++;
// Unsafe.Add(ref huffcodeRef, k++) = (short)code++;
// TODO: Throw if invalid?
}
}
// Compute largest code + 1 for this size. preshifted as neeed later.
maxcode[j] = code << (16 - j);
code <<= 1;
}
maxcode[j] = 0xFFFFFFFF;
}
fixed (byte* lookaheadRef = this.Lookahead.Data)
{
const int FastBits = ScanDecoder.FastBits;
var fast = new Span<short>(lookaheadRef, 1 << FastBits);
fast.Fill(255); // Flag for non-accelerated
fixed (short* sizesRef = this.Sizes.Data)
{
for (int i = 0; i < k; i++)
{
int s = sizesRef[i];
if (s <= ScanDecoder.FastBits)
{
int c = codes[i] << (FastBits - s);
int m = 1 << (FastBits - s);
for (int j = 0; j < m; j++)
{
fast[c + j] = (byte)i;
}
}
}
}
}
// this.GenerateCodeTable(ref huffcodeRef, Length, out int k);
// this.GenerateDecoderTables(count, ref huffcodeRef);
// this.GenerateLookaheadTables(count, values, ref huffcodeRef, k);
}
fixed (byte* huffValRef = this.Values.Data)
@ -224,7 +224,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
const int FastBits = ScanDecoder.FastBits;
var lookaheadSpan = new Span<short>(lookaheadRef, 1 << ScanDecoder.FastBits);
lookaheadSpan.Fill(255); // Flag for non-accelerated
lookaheadSpan.Fill(byte.MaxValue); // Flag for non-accelerated
fixed (short* sizesRef = this.Sizes.Data)
{
for (int i = 0; i < k; ++i)

9
src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs

@ -860,14 +860,5 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
}
}
}
private void Reset()
{
// Reset
// TODO: I do not understand why these values are reset? We should surely be tracking the bits across mcu's?
this.bitsCount = 0;
this.bitsData = 0;
this.unexpectedMarkerReached = false;
}
}
}

2
src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/ScanDecoder.cs

@ -607,7 +607,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
// wants to be compared against something shifted to have 16;
// that way we don't need to shift inside the loop.
uint temp = this.codeBuffer >> 16;
for (k = FastBits + 1; ; ++k)
for (k = FastBits + 1; ; k++)
{
if (temp < table.MaxCode[k])
{

Loading…
Cancel
Save