diff options
author | pacien | 2018-12-02 00:22:56 +0100 |
---|---|---|
committer | pacien | 2018-12-02 00:22:56 +0100 |
commit | 5cc4256a931b98ea167291397421d0db60c5d40c (patch) | |
tree | 54c7ba39e69fb31322db431a82dbf31aedbb53b9 /src/blocks/lzssblock.nim | |
parent | 1850acb5b77aabbf4e9ba24ae6d5314c3d4d896a (diff) | |
download | gziplike-5cc4256a931b98ea167291397421d0db60c5d40c.tar.gz |
implement lzss block
Diffstat (limited to 'src/blocks/lzssblock.nim')
-rw-r--r-- | src/blocks/lzssblock.nim | 31 |
1 files changed, 25 insertions, 6 deletions
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 @@ | |||
14 | # You should have received a copy of the GNU Affero General Public License | 14 | # You should have received a copy of the GNU Affero General Public License |
15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | 15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
16 | 16 | ||
17 | import ../bitio/bitreader, ../bitio/bitwriter | 17 | import lists |
18 | import ../bitio/integers, ../bitio/bitreader, ../bitio/bitwriter | ||
19 | import ../lzss/lzsschain, ../lzss/lzssencoder | ||
20 | import ../huffman/huffmantree, ../huffman/huffmantreebuilder, ../huffman/huffmanencoder, ../huffman/huffmandecoder | ||
21 | import ../lzsshuffman/lzsshuffmanstats, ../lzsshuffman/lzsshuffmandecoder, ../lzsshuffman/lzsshuffmanencoder | ||
22 | |||
23 | const maxDataByteLength = 32_000 | ||
18 | 24 | ||
19 | type LzssBlock* = object | 25 | type LzssBlock* = object |
20 | discard | 26 | lzssChain: LzssChain |
21 | 27 | ||
22 | proc readSerialised*(bitReader: BitReader): LzssBlock = | 28 | proc readSerialised*(bitReader: BitReader): LzssBlock = |
23 | discard | 29 | let symbolHuffmanTree = huffmantree.deserialise(bitReader, uint16) |
30 | let positionHuffmanTree = huffmantree.deserialise(bitReader, uint16) | ||
31 | let symbolDecoder = symbolHuffmanTree.decoder() | ||
32 | let positionDecoder = positionHuffmanTree.decoder() | ||
33 | LzssBlock(lzssChain: readChain(bitReader, symbolDecoder, positionDecoder, maxDataByteLength)) | ||
24 | 34 | ||
25 | proc writeSerialisedTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) = | 35 | proc writeSerialisedTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) = |
26 | discard | 36 | let (symbolStats, positionStats) = aggregateStats(lzssBlock.lzssChain) |
37 | let symbolHuffmanTree = buildHuffmanTree(symbolStats) | ||
38 | let positionHuffmanTree = buildHuffmanTree(positionStats) | ||
39 | let symbolEncoder = symbolHuffmanTree.encoder(uint16) | ||
40 | let positionEncoder = positionHuffmanTree.encoder(uint16) | ||
41 | symbolHuffmanTree.serialise(bitWriter) | ||
42 | positionHuffmanTree.serialise(bitWriter) | ||
43 | lzssBlock.lzssChain.writeChain(symbolEncoder, positionEncoder, bitWriter) | ||
27 | 44 | ||
28 | proc readRaw*(bitReader: BitReader): LzssBlock = | 45 | proc readRaw*(bitReader: BitReader): LzssBlock = |
29 | discard | 46 | let byteBuf = bitReader.readSeq(maxDataByteLength, uint8) |
47 | LzssBlock(lzssChain: lzssEncode(byteBuf.data)) | ||
30 | 48 | ||
31 | proc writeRawTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) = | 49 | proc writeRawTo*(lzssBlock: LzssBlock, bitWriter: BitWriter) = |
32 | discard | 50 | let byteSeq = lzssBlock.lzssChain.decode() |
51 | bitWriter.writeSeq(byteSeq.len * wordBitLength, byteSeq) | ||