/// <summary> /// Finds the next file marker within the byte stream. /// </summary> /// <param name="marker">The buffer to read file markers to</param> /// <param name="stream">The input stream</param> /// <returns>The <see cref="JpegFileMarker"/></returns> public static JpegFileMarker FindNextFileMarker(byte[] marker, DoubleBufferedStreamReader stream) { int value = stream.Read(marker, 0, 2); if (value == 0) { return(new JpegFileMarker(JpegConstants.Markers.EOI, stream.Length - 2)); } if (marker[0] == JpegConstants.Markers.XFF) { // According to Section B.1.1.2: // "Any marker may optionally be preceded by any number of fill bytes, which are bytes assigned code 0xFF." int m = marker[1]; while (m == JpegConstants.Markers.XFF) { int suffix = stream.ReadByte(); if (suffix == -1) { return(new JpegFileMarker(JpegConstants.Markers.EOI, stream.Length - 2)); } m = suffix; } return(new JpegFileMarker((byte)m, stream.Position - 2)); } return(new JpegFileMarker(marker[1], stream.Position - 2, true)); }
/// <summary> /// Initializes a new instance of the <see cref="ScanDecoder"/> class. /// </summary> /// <param name="stream">The input stream.</param> /// <param name="frame">The image frame.</param> /// <param name="dcHuffmanTables">The DC Huffman tables.</param> /// <param name="acHuffmanTables">The AC Huffman tables.</param> /// <param name="fastACTables">The fast AC decoding tables.</param> /// <param name="componentsLength">The length of the components. Different to the array length.</param> /// <param name="restartInterval">The reset interval.</param> /// <param name="spectralStart">The spectral selection start.</param> /// <param name="spectralEnd">The spectral selection end.</param> /// <param name="successiveHigh">The successive approximation bit high end.</param> /// <param name="successiveLow">The successive approximation bit low end.</param> public ScanDecoder( DoubleBufferedStreamReader stream, JpegFrame frame, HuffmanTables dcHuffmanTables, HuffmanTables acHuffmanTables, FastACTables fastACTables, int componentsLength, int restartInterval, int spectralStart, int spectralEnd, int successiveHigh, int successiveLow) { this.dctZigZag = ZigZag.CreateUnzigTable(); this.stream = stream; this.frame = frame; this.dcHuffmanTables = dcHuffmanTables; this.acHuffmanTables = acHuffmanTables; this.fastACTables = fastACTables; this.components = frame.Components; this.marker = JpegConstants.Markers.XFF; this.markerPosition = 0; this.componentsLength = componentsLength; this.restartInterval = restartInterval; this.spectralStart = spectralStart; this.spectralEnd = spectralEnd; this.successiveHigh = successiveHigh; this.successiveLow = successiveLow; }
/// <summary> /// Initializes a new instance of the <see cref="HuffmanScanDecoder"/> class. /// </summary> /// <param name="stream">The input stream.</param> /// <param name="frame">The image frame.</param> /// <param name="dcHuffmanTables">The DC Huffman tables.</param> /// <param name="acHuffmanTables">The AC Huffman tables.</param> /// <param name="componentsLength">The length of the components. Different to the array length.</param> /// <param name="restartInterval">The reset interval.</param> /// <param name="spectralStart">The spectral selection start.</param> /// <param name="spectralEnd">The spectral selection end.</param> /// <param name="successiveHigh">The successive approximation bit high end.</param> /// <param name="successiveLow">The successive approximation bit low end.</param> public HuffmanScanDecoder( DoubleBufferedStreamReader stream, JpegFrame frame, HuffmanTable[] dcHuffmanTables, HuffmanTable[] acHuffmanTables, int componentsLength, int restartInterval, int spectralStart, int spectralEnd, int successiveHigh, int successiveLow) { this.dctZigZag = ZigZag.CreateUnzigTable(); this.stream = stream; this.scanBuffer = new HuffmanScanBuffer(stream); this.frame = frame; this.dcHuffmanTables = dcHuffmanTables; this.acHuffmanTables = acHuffmanTables; this.components = frame.Components; this.componentsLength = componentsLength; this.restartInterval = restartInterval; this.todo = restartInterval; this.spectralStart = spectralStart; this.spectralEnd = spectralEnd; this.successiveHigh = successiveHigh; this.successiveLow = successiveLow; }
public void DoubleBufferedStreamReaderCanReadSubsequentSingleByteCorrectly() { using (MemoryStream stream = this.CreateTestStream()) { byte[] expected = stream.ToArray(); var reader = new DoubleBufferedStreamReader(this.allocator, stream); for (int i = 0; i < expected.Length; i++) { Assert.Equal(expected[i], reader.ReadByte()); Assert.Equal(i + 1, reader.Position); if (i < DoubleBufferedStreamReader.ChunkLength) { Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength); } else if (i >= DoubleBufferedStreamReader.ChunkLength && i < DoubleBufferedStreamReader.ChunkLength * 2) { // We should have advanced to the second chunk now. Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 2); } else { // We should have advanced to the third chunk now. Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 3); } } } }
public void DoubleBufferedStreamReaderCanSkip() { using (MemoryStream stream = this.CreateTestStream()) { byte[] expected = stream.ToArray(); var reader = new DoubleBufferedStreamReader(this.allocator, stream); int skip = 50; int plusOne = 1; int skip2 = DoubleBufferedStreamReader.ChunkLength; // Skip reader.Skip(skip); Assert.Equal(skip, reader.Position); Assert.Equal(stream.Position, reader.Position); // Read Assert.Equal(expected[skip], reader.ReadByte()); // Skip Again reader.Skip(skip2); // First Skip + First Read + Second Skip int position = skip + plusOne + skip2; Assert.Equal(position, reader.Position); Assert.Equal(stream.Position, reader.Position); Assert.Equal(expected[position], reader.ReadByte()); } }
public void DoubleBufferedStreamReaderCanReadSubsequentMultipleByteCorrectly() { using (MemoryStream stream = this.CreateTestStream()) { var buffer = new byte[2]; byte[] expected = stream.ToArray(); var reader = new DoubleBufferedStreamReader(this.allocator, stream); for (int i = 0, o = 0; i < expected.Length / 2; i++, o += 2) { Assert.Equal(2, reader.Read(buffer, 0, 2)); Assert.Equal(expected[o], buffer[0]); Assert.Equal(expected[o + 1], buffer[1]); Assert.Equal(o + 2, reader.Position); int offset = i * 2; if (offset < DoubleBufferedStreamReader.ChunkLength) { Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength); } else if (offset >= DoubleBufferedStreamReader.ChunkLength && offset < DoubleBufferedStreamReader.ChunkLength * 2) { // We should have advanced to the second chunk now. Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 2); } else { // We should have advanced to the third chunk now. Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength * 3); } } } }
public void CreateStreams() { this.stream1 = new MemoryStream(this.buffer); this.stream2 = new MemoryStream(this.buffer); this.stream3 = new MemoryStream(this.buffer); this.stream4 = new MemoryStream(this.buffer); this.reader1 = new DoubleBufferedStreamReader(Configuration.Default.MemoryAllocator, this.stream2); this.reader2 = new DoubleBufferedStreamReader(Configuration.Default.MemoryAllocator, this.stream2); }
public HuffmanScanBuffer(DoubleBufferedStreamReader stream) { this.stream = stream; this.data = 0ul; this.remainingBits = 0; this.Marker = JpegConstants.Markers.XFF; this.MarkerPosition = 0; this.badData = false; this.NoData = false; }
public HuffmanScanBuffer(DoubleBufferedStreamReader stream) { this.stream = stream; this.data = 0ul; this.remain = 0; this.Marker = JpegConstants.Markers.XFF; this.MarkerPosition = 0; this.BadMarker = false; this.noMore = false; this.Eof = false; }
public int DoubleBufferedStreamReadByte() { int r = 0; DoubleBufferedStreamReader reader = this.reader1; for (int i = 0; i < reader.Length; i++) { r += reader.ReadByte(); } return(r); }
public int DoubleBufferedStreamRead() { int r = 0; DoubleBufferedStreamReader reader = this.reader2; byte[] b = this.chunk2; for (int i = 0; i < reader.Length / 2; i++) { r += reader.Read(b, 0, 2); } return(r); }
public void DoubleBufferedStreamReaderCanReadSingleByteFromOrigin() { using (MemoryStream stream = this.CreateTestStream()) { byte[] expected = stream.ToArray(); var reader = new DoubleBufferedStreamReader(this.allocator, stream); Assert.Equal(expected[0], reader.ReadByte()); // We've read a whole chunk but increment by 1 in our reader. Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength); Assert.Equal(1, reader.Position); } }
public void DoubleBufferedStreamReaderCanReadMultipleBytesFromOrigin() { using (MemoryStream stream = this.CreateTestStream()) { var buffer = new byte[2]; byte[] expected = stream.ToArray(); var reader = new DoubleBufferedStreamReader(this.allocator, stream); Assert.Equal(2, reader.Read(buffer, 0, 2)); Assert.Equal(expected[0], buffer[0]); Assert.Equal(expected[1], buffer[1]); // We've read a whole chunk but increment by the buffer length in our reader. Assert.Equal(stream.Position, DoubleBufferedStreamReader.ChunkLength); Assert.Equal(buffer.Length, reader.Position); } }