public override long Seek(long offset, SeekOrigin origin) { if (m_Compressed) { long absPos = offset; if (origin == SeekOrigin.Current) { absPos += m_Uncomp.Position; } if (absPos < 0) { throw new Exception("Cannot seek past the begining of the stream."); } long pos = m_Uncomp.Position; m_Uncomp.Seek(0, SeekOrigin.End); while ((origin == SeekOrigin.End || absPos >= m_Uncomp.Length) && RawStream.Position < RawStream.Length) { int block = Raw.ReadInt32(); int ucLen = Raw.ReadInt32(); if (m_ReadBuff == null || m_ReadBuff.Length < block) { m_ReadBuff = new byte[block]; } if (m_CompBuff == null || m_CompBuff.Length < ucLen) { m_CompBuff = new byte[ucLen]; } else { ucLen = m_CompBuff.Length; } Raw.Read(m_ReadBuff, 0, block); ZLibError error = ZLib.uncompress(m_CompBuff, ref ucLen, m_ReadBuff, block); if (error != ZLibError.Z_OK) { throw new Exception("ZLib error uncompressing: " + error.ToString()); } m_Uncomp.Write(m_CompBuff, 0, ucLen); } m_Uncomp.Position = pos; return(m_Uncomp.Seek(offset, origin)); } else { return(RawStream.Seek(offset, origin)); } }
public override long Seek(long offset, SeekOrigin origin) { if (m_IsCompressed) { return(m_Buffer.Seek(offset, origin)); } else { return(RawStream.Seek(offset, origin)); } }
internal NodeHeader ReadTree(ulong logical, byte level) { var physical = MapToPhysical(logical); RawStream.Seek((long)physical, SeekOrigin.Begin); var dataSize = level > 0 ? SuperBlock.NodeSize : SuperBlock.LeafSize; var buffer = new byte[dataSize]; RawStream.Read(buffer, 0, buffer.Length); var result = NodeHeader.Create(buffer, 0); VerifyChecksum(result.Checksum, buffer, 0x20, (int)dataSize - 0x20); return(result); }
public IEnumerable <short[, ]> Run() { if (Format.Channels <= 0) { yield break; } if (ProgressNotifier != null) { ProgressNotifier.Reset(); } RawStream.Seek(0, SeekMode.Origin); uint decodedSize = 0; while (decodedSize < RawStream.Length) { // Calculate the size of the block int blockLen = 0x2000; if (decodedSize + blockLen > RawStream.Length) { blockLen = (int)(RawStream.Length - decodedSize); } // Decode yield return(DecodeBlock(blockLen)); // Increase decoded size decodedSize += (uint)blockLen; // Show progress if (ProgressNotifier != null) { ProgressNotifier.Update((int)decodedSize, RawStream.Length); } } if (ProgressNotifier != null) { ProgressNotifier.End(); } }
short[] DecodeChunk(int channel) { var reader = new DataReader(RawStream); // Get header RawStream.Seek(0xF, SeekMode.Current); sbyte header = reader.ReadSByte(); header = (sbyte)(header ^ 0x80); RawStream.Seek(-0x10, SeekMode.Current); // ... get scale byte scale = (byte)(header & 0xF); // ... get coefficients int coefIdx = header >> 4; double coef1 = CoefficientTable[coefIdx, 0]; double coef2 = CoefficientTable[coefIdx, 1]; // Get all the samples in the chunk uint chunkData = 0; var samples = new short[Format.SamplesPerChunk]; for (int i = 0; i < Format.SamplesPerChunk; i++, chunkData >>= 4) { if (i % 8 == 0) { chunkData = reader.ReadUInt32() ^ 0x80808080; } byte value = (byte)(chunkData & 0xF); samples[i] = DecodeSample(value, scale, coef1, coef2, channel); } return(samples); }
public override void LoadRaw() { ushort[,] vpred = new ushort[2, 2]; ushort[] hpred = new ushort[2]; ushort csize; int step = 0; int huff = 0; int split = 0; int row; RawStream ifp = state.ifp; ifp.Seek(state.meta_offset, SeekOrigin.Begin); ushort ver0 = (ushort)ifp.ReadByte(); ushort ver1 = (ushort)ifp.ReadByte(); if (ver0 == 0x49 || ver1 == 0x58) { ifp.Seek(2110, SeekOrigin.Current); } if (ver0 == 0x46) { huff = 2; } if (state.tiff_bps == 14) { huff += 3; } vpred[0, 0] = ifp.get2(); vpred[0, 1] = ifp.get2(); vpred[1, 0] = ifp.get2(); vpred[1, 1] = ifp.get2(); int max = 1 << state.tiff_bps & 0x7fff; if ((csize = ifp.get2()) > 1) { step = max / (csize - 1); } if (ver0 == 0x44 && ver1 == 0x20 && step > 0) { int i; for (i = 0; i < csize; i++) { state.curve[i * step] = ifp.get2(); } for (i = 0; i < max; i++) { state.curve[i] = (ushort)((state.curve[i - i % step] * (step - i % step) + state.curve[i - i % step + step] * (i % step)) / step); } ifp.Seek(state.meta_offset + 562, SeekOrigin.Begin); split = ifp.get2(); } else if (ver0 != 0x46 && csize <= 0x4001) { max = csize; ifp.ReadShorts(state.curve, max); } int tempIdx = 0; HuffmanTree htree = new HuffmanTree(nikon_tree[huff], ref tempIdx); ifp.Seek(state.data_offset, SeekOrigin.Begin); ifp.ResetBits(); for (row = 0; row < state.height; row++) { if (split != 0 && row == split) { tempIdx = 0; htree = new HuffmanTree(nikon_tree[huff], ref tempIdx); } for (int col = 0; col < state.raw_width; col++) { int leaf = htree.ReadNextSymbolLength(ifp); int len = leaf & 15; int shl = leaf >> 4; int diff = (((int)ifp.GetBits(len - shl) << 1) + 1) << shl >> 1; if ((diff & (1 << (len - 1))) == 0) { diff -= (1 << len) - (shl == 0 ? 1 : 0); } if (col < 2) { vpred[row & 1, col] = (ushort)(vpred[row & 1, col] + diff); hpred[col] = vpred[row & 1, col]; } else { hpred[col & 1] = (ushort)(hpred[col & 1] + diff); } if (hpred[col & 1] >= max) { throw new Exception("derror()"); } if ((uint)(col - state.left_margin) < state.width) { state.BAYER_set(row, col - state.left_margin, state.curve[hpred[col & 1] & 0x3fff]); } } } }