def test_encode_decode(self): s = b'0123456789' * 12 + b'01234' expected_rs_code = ( b'\x1a\xd0\xc75-\x08\xde\x9a\xdc\x9a\x17_\xa2\xf6\x15\x8b\x0e\xb2\xd4\xbb\x19\xe6:\x88\x1b\xee\x92\xd7N\xab' ) inbuff = BytesIO(s * 2) outbuff = BytesIO() with reed_solomon_stream(inbuff) as reed_read, reed_solomon_stream( outbuff, mode='write') as reed_write: b = reed_read.read(125) self.assertEqual(s + expected_rs_code, b) reed_write.write(b) outbuff.seek(0) self.assertEqual(s, outbuff.read())
def _get_decoder_stream(outfile, ecc, fountain): # set up the outstream: image -> reedsolomon -> fountain -> zstd_decompress -> raw bytes f = open(outfile, 'wb') if fountain: import zstandard as zstd from cimbar.fountain.fountain_decoder_stream import fountain_decoder_stream decompressor = zstd.ZstdDecompressor().stream_writer(f) f = fountain_decoder_stream(decompressor, _fountain_chunk_size(ecc)) on_rss_failure = b'' if fountain else None return reed_solomon_stream(f, ecc, mode='write', on_failure=on_rss_failure) if ecc else f
def setUpClass(cls): cls.inputs_dir = TemporaryDirectory() cls.src_file = path.join(cls.inputs_dir.name, 'infile.txt') random_data = bytearray(random.getrandbits(8) for _ in range(4000)) src_data = random_data * 4 with open(cls.src_file, 'wb') as f: f.write(src_data) cls.encoded_file = path.join(cls.inputs_dir.name, 'encoded.png') encode(cls.src_file, cls.encoded_file, dark=True) cls.decode_clean = path.join(cls.inputs_dir.name, 'decode-no-ecc-clean.txt') with reed_solomon_stream(cls.src_file, 30) as rss, open(cls.decode_clean, 'wb') as f: f.write(rss.read(7500))
def _get_encoder_stream(src, ecc, fountain, compression_level=6): # various checks to set up the instream. # the hierarchy is raw bytes -> zstd -> fountain -> reedsolomon -> image f = open(src, 'rb') if fountain: import zstandard as zstd from cimbar.fountain.fountain_encoder_stream import fountain_encoder_stream reader = zstd.ZstdCompressor(level=compression_level).stream_reader(f) f = fountain_encoder_stream(reader, _fountain_chunk_size(ecc)) estream = reed_solomon_stream(f, ecc) if ecc else f read_size = _fountain_chunk_size(ecc) if fountain else 16384 read_count = (f.len // read_size) * 2 if fountain else 1 params = { 'read_size': read_size, 'read_count': read_count, } return estream, params