Browse Source

Changing InputStream and Bits to private fields.

pull/49/head
Olivia 10 years ago
parent
commit
f510331bd7
  1. 101
      src/ImageSharp/Formats/Jpg/JpegDecoderCore.cs

101
src/ImageSharp/Formats/Jpg/JpegDecoderCore.cs

@ -31,16 +31,6 @@ namespace ImageSharp.Formats
/// </summary> /// </summary>
internal const int LutSize = 8; internal const int LutSize = 8;
/// <summary>
/// The input stream.
/// </summary>
internal Stream InputStream;
/// <summary>
/// Holds the unprocessed bits that have been taken from the byte-stream.
/// </summary>
internal Bits Bits;
/// <summary> /// <summary>
/// The maximum number of color components /// The maximum number of color components
/// </summary> /// </summary>
@ -116,6 +106,16 @@ namespace ImageSharp.Formats
/// </summary> /// </summary>
private Bytes bytes; private Bytes bytes;
/// <summary>
/// The byte buffer.
/// </summary>
private Stream inputStream;
/// <summary>
/// Holds the unprocessed bits that have been taken from the byte-stream.
/// </summary>
private Bits bits;
/// <summary> /// <summary>
/// The image width /// The image width
/// </summary> /// </summary>
@ -205,7 +205,7 @@ namespace ImageSharp.Formats
this.temp = new byte[2 * BlockF.BlockSize]; this.temp = new byte[2 * BlockF.BlockSize];
this.componentArray = new Component[MaxComponents]; this.componentArray = new Component[MaxComponents];
this.progCoeffs = new Block8x8F[MaxComponents][]; this.progCoeffs = new Block8x8F[MaxComponents][];
this.Bits = default(Bits); this.bits = default(Bits);
this.bytes = Bytes.Create(); this.bytes = Bytes.Create();
// TODO: This looks like it could be static. // TODO: This looks like it could be static.
@ -246,6 +246,17 @@ namespace ImageSharp.Formats
} }
} }
/// <summary>
/// Gets the input stream.
/// </summary>
public Stream InputStream
{
get
{
return this.inputStream;
}
}
/// <summary> /// <summary>
/// Decodes the image from the specified this._stream and sets /// Decodes the image from the specified this._stream and sets
/// the data to image. /// the data to image.
@ -259,7 +270,7 @@ namespace ImageSharp.Formats
where TColor : struct, IPackedPixel<TPacked> where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct, IEquatable<TPacked> where TPacked : struct, IEquatable<TPacked>
{ {
this.InputStream = stream; this.inputStream = stream;
// Check for the Start Of Image marker. // Check for the Start Of Image marker.
this.ReadFull(this.temp, 0, 2); this.ReadFull(this.temp, 0, 2);
@ -487,7 +498,7 @@ namespace ImageSharp.Formats
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal byte ReadByte() internal byte ReadByte()
{ {
return this.bytes.ReadByte(this.InputStream); return this.bytes.ReadByte(this.inputStream);
} }
/// <summary> /// <summary>
@ -648,19 +659,19 @@ namespace ImageSharp.Formats
throw new ImageFormatException("Uninitialized Huffman table"); throw new ImageFormatException("Uninitialized Huffman table");
} }
if (this.Bits.UnreadBits < 8) if (this.bits.UnreadBits < 8)
{ {
var errorCode = this.Bits.EnsureNBits(8, this); var errorCode = this.bits.EnsureNBits(8, this);
if (errorCode == ErrorCodes.NoError) if (errorCode == ErrorCodes.NoError)
{ {
ushort v = huffman.Lut[(this.Bits.Accumulator >> (this.Bits.UnreadBits - LutSize)) & 0xff]; ushort v = huffman.Lut[(this.bits.Accumulator >> (this.bits.UnreadBits - LutSize)) & 0xff];
if (v != 0) if (v != 0)
{ {
byte n = (byte)((v & 0xff) - 1); byte n = (byte)((v & 0xff) - 1);
this.Bits.UnreadBits -= n; this.bits.UnreadBits -= n;
this.Bits.Mask >>= n; this.bits.Mask >>= n;
return (byte)(v >> 8); return (byte)(v >> 8);
} }
} }
@ -673,22 +684,22 @@ namespace ImageSharp.Formats
int code = 0; int code = 0;
for (int i = 0; i < MaxCodeLength; i++) for (int i = 0; i < MaxCodeLength; i++)
{ {
if (this.Bits.UnreadBits == 0) if (this.bits.UnreadBits == 0)
{ {
var errorCode = this.Bits.EnsureNBits(1, this); var errorCode = this.bits.EnsureNBits(1, this);
if (errorCode != ErrorCodes.NoError) if (errorCode != ErrorCodes.NoError)
{ {
throw new MissingFF00Exception(); throw new MissingFF00Exception();
} }
} }
if ((this.Bits.Accumulator & this.Bits.Mask) != 0) if ((this.bits.Accumulator & this.bits.Mask) != 0)
{ {
code |= 1; code |= 1;
} }
this.Bits.UnreadBits--; this.bits.UnreadBits--;
this.Bits.Mask >>= 1; this.bits.Mask >>= 1;
if (code <= huffman.MaxCodes[i]) if (code <= huffman.MaxCodes[i])
{ {
@ -707,18 +718,18 @@ namespace ImageSharp.Formats
/// <returns>The <see cref="bool"/></returns> /// <returns>The <see cref="bool"/></returns>
private bool DecodeBit() private bool DecodeBit()
{ {
if (this.Bits.UnreadBits == 0) if (this.bits.UnreadBits == 0)
{ {
var errorCode = this.Bits.EnsureNBits(1, this); var errorCode = this.bits.EnsureNBits(1, this);
if (errorCode != ErrorCodes.NoError) if (errorCode != ErrorCodes.NoError)
{ {
throw new MissingFF00Exception(); throw new MissingFF00Exception();
} }
} }
bool ret = (this.Bits.Accumulator & this.Bits.Mask) != 0; bool ret = (this.bits.Accumulator & this.bits.Mask) != 0;
this.Bits.UnreadBits--; this.bits.UnreadBits--;
this.Bits.Mask >>= 1; this.bits.Mask >>= 1;
return ret; return ret;
} }
@ -729,19 +740,19 @@ namespace ImageSharp.Formats
/// <returns>The <see cref="uint"/></returns> /// <returns>The <see cref="uint"/></returns>
private uint DecodeBits(int count) private uint DecodeBits(int count)
{ {
if (this.Bits.UnreadBits < count) if (this.bits.UnreadBits < count)
{ {
var errorCode = this.Bits.EnsureNBits(count, this); var errorCode = this.bits.EnsureNBits(count, this);
if (errorCode != ErrorCodes.NoError) if (errorCode != ErrorCodes.NoError)
{ {
throw new MissingFF00Exception(); throw new MissingFF00Exception();
} }
} }
uint ret = this.Bits.Accumulator >> (this.Bits.UnreadBits - count); uint ret = this.bits.Accumulator >> (this.bits.UnreadBits - count);
ret = (uint)(ret & ((1 << count) - 1)); ret = (uint)(ret & ((1 << count) - 1));
this.Bits.UnreadBits -= count; this.bits.UnreadBits -= count;
this.Bits.Mask >>= count; this.bits.Mask >>= count;
return ret; return ret;
} }
@ -756,11 +767,11 @@ namespace ImageSharp.Formats
{ {
this.bytes.I -= this.bytes.UnreadableBytes; this.bytes.I -= this.bytes.UnreadableBytes;
this.bytes.UnreadableBytes = 0; this.bytes.UnreadableBytes = 0;
if (this.Bits.UnreadBits >= 8) if (this.bits.UnreadBits >= 8)
{ {
this.Bits.Accumulator >>= 8; this.bits.Accumulator >>= 8;
this.Bits.UnreadBits -= 8; this.bits.UnreadBits -= 8;
this.Bits.Mask >>= 8; this.bits.Mask >>= 8;
} }
} }
@ -775,7 +786,7 @@ namespace ImageSharp.Formats
// Unread the overshot bytes, if any. // Unread the overshot bytes, if any.
if (this.bytes.UnreadableBytes != 0) if (this.bytes.UnreadableBytes != 0)
{ {
if (this.Bits.UnreadBits >= 8) if (this.bits.UnreadBits >= 8)
{ {
this.UnreadByteStuffedByte(); this.UnreadByteStuffedByte();
} }
@ -798,7 +809,7 @@ namespace ImageSharp.Formats
length -= this.bytes.J - this.bytes.I; length -= this.bytes.J - this.bytes.I;
this.bytes.I += this.bytes.J - this.bytes.I; this.bytes.I += this.bytes.J - this.bytes.I;
this.bytes.Fill(this.InputStream); this.bytes.Fill(this.inputStream);
} }
} }
} }
@ -812,7 +823,7 @@ namespace ImageSharp.Formats
// Unread the overshot bytes, if any. // Unread the overshot bytes, if any.
if (this.bytes.UnreadableBytes != 0) if (this.bytes.UnreadableBytes != 0)
{ {
if (this.Bits.UnreadBits >= 8) if (this.bits.UnreadBits >= 8)
{ {
this.UnreadByteStuffedByte(); this.UnreadByteStuffedByte();
} }
@ -835,7 +846,7 @@ namespace ImageSharp.Formats
break; break;
} }
this.bytes.Fill(this.InputStream); this.bytes.Fill(this.inputStream);
} }
} }
@ -1541,7 +1552,7 @@ namespace ImageSharp.Formats
} }
} }
this.Bits = default(Bits); this.bits = default(Bits);
int mcu = 0; int mcu = 0;
byte expectedRst = JpegConstants.Markers.RST0; byte expectedRst = JpegConstants.Markers.RST0;
@ -1694,7 +1705,7 @@ namespace ImageSharp.Formats
} }
// Reset the Huffman decoder. // Reset the Huffman decoder.
this.Bits = default(Bits); this.bits = default(Bits);
// Reset the DC components, as per section F.2.1.3.1. // Reset the DC components, as per section F.2.1.3.1.
dc = new int[MaxComponents]; dc = new int[MaxComponents];
@ -1749,7 +1760,7 @@ namespace ImageSharp.Formats
throw new ImageFormatException("Excessive DC component"); throw new ImageFormatException("Excessive DC component");
} }
int deltaDC = this.Bits.ReceiveExtend(value, this); int deltaDC = this.bits.ReceiveExtend(value, this);
dc[compIndex] += deltaDC; dc[compIndex] += deltaDC;
// b[0] = dc[compIndex] << al; // b[0] = dc[compIndex] << al;
@ -1777,7 +1788,7 @@ namespace ImageSharp.Formats
break; break;
} }
int ac = this.Bits.ReceiveExtend(val1, this); int ac = this.bits.ReceiveExtend(val1, this);
// b[Unzig[zig]] = ac << al; // b[Unzig[zig]] = ac << al;
Block8x8F.SetScalarAt(b, unzigPtr[zig], ac << al); Block8x8F.SetScalarAt(b, unzigPtr[zig], ac << al);

Loading…
Cancel
Save