From 5cc4256a931b98ea167291397421d0db60c5d40c Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 2 Dec 2018 00:22:56 +0100 Subject: implement lzss block --- src/blocks/lzssblock.nim | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src/blocks') diff --git a/src/blocks/lzssblock.nim b/src/blocks/lzssblock.nim index f68f665..b23cee2 100644 --- a/src/blocks/lzssblock.nim +++ b/src/blocks/lzssblock.nim @@ -14,19 +14,38 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import ../bitio/bitreader, ../bitio/bitwriter +import lists +import ../bitio/integers, ../bitio/bitreader, ../bitio/bitwriter +import ../lzss/lzsschain, ../lzss/lzssencoder +import ../huffman/huffmantree, ../huffman/huffmantreebuilder, ../huffman/huffmanencoder, ../huffman/huffmandecoder +import ../lzsshuffman/lzsshuffmanstats, ../lzsshuffman/lzsshuffmandecoder, ../lzsshuffman/lzsshuffmanencoder + +const maxDataByteLength = 32_000 type LzssBlock* = object - discard + lzssChain: LzssChain proc readSerialised*(bitReader: BitReader): LzssBlock = - discard + let symbolHuffmanTree = huffmantree.deserialise(bitReader, uint16) + let positionHuffmanTree = huffmantree.deserialise(bitReader, uint16) + let symbolDecoder = symbolHuffmanTree.decoder() + let positionDecoder = positionHuffmanTree.decoder() + LzssBlock(lzssChain: readChain(bitReader, symbolDecoder, positionDecoder, maxDataByteLength)) proc writeSerialisedTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) = - discard + let (symbolStats, positionStats) = aggregateStats(lzssBlock.lzssChain) + let symbolHuffmanTree = buildHuffmanTree(symbolStats) + let positionHuffmanTree = buildHuffmanTree(positionStats) + let symbolEncoder = symbolHuffmanTree.encoder(uint16) + let positionEncoder = positionHuffmanTree.encoder(uint16) + symbolHuffmanTree.serialise(bitWriter) + positionHuffmanTree.serialise(bitWriter) + lzssBlock.lzssChain.writeChain(symbolEncoder, positionEncoder, bitWriter) proc readRaw*(bitReader: BitReader): LzssBlock = - discard + let byteBuf = bitReader.readSeq(maxDataByteLength, uint8) + LzssBlock(lzssChain: lzssEncode(byteBuf.data)) proc writeRawTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) = - discard + let byteSeq = lzssBlock.lzssChain.decode() + bitWriter.writeSeq(byteSeq.len * wordBitLength, byteSeq) -- cgit v1.2.3