From 7766cfd8819d1984e8172691409f14adf6e605ca Mon Sep 17 00:00:00 2001 From: sz Date: Sat, 13 Aug 2022 01:19:19 -0500 Subject: [PATCH] Attempt to use 988x988 grid...? Removing various hard coded "155"s, and whatnot --- src/lib/cimb_translator/CimbReader.cpp | 2 +- src/lib/cimb_translator/Config.cpp | 8 +++--- src/lib/encoder/Decoder.h | 6 +++-- src/lib/encoder/SimpleEncoder.h | 12 +++++---- src/lib/encoder/reed_solomon_stream.h | 27 +++++++++---------- .../encoder/test/reed_solomon_streamTest.cpp | 8 +++--- 6 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/lib/cimb_translator/CimbReader.cpp b/src/lib/cimb_translator/CimbReader.cpp index 621ccd51..e1695954 100644 --- a/src/lib/cimb_translator/CimbReader.cpp +++ b/src/lib/cimb_translator/CimbReader.cpp @@ -40,7 +40,7 @@ namespace { } cv::adaptiveThreshold(symbols, symbols, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, blockSize, 0); - bitbuffer bb(1024*128); + bitbuffer bb(std::pow(Config::image_size(), 2) / 8); bitmatrix::mat_to_bitbuffer(symbols, bb.get_writer()); return bb; } diff --git a/src/lib/cimb_translator/Config.cpp b/src/lib/cimb_translator/Config.cpp index 905dc97c..40d8894f 100644 --- a/src/lib/cimb_translator/Config.cpp +++ b/src/lib/cimb_translator/Config.cpp @@ -26,17 +26,17 @@ unsigned Config::bits_per_cell() unsigned Config::ecc_bytes() { - return 28; + return 40; } unsigned Config::ecc_block_size() { - return 148; + return 216; } int Config::image_size() { - return 1012; + return 988; } unsigned Config::anchor_size() @@ -61,7 +61,7 @@ unsigned Config::cell_offset() unsigned Config::cells_per_col() { - return 166; + return 162; } unsigned Config::total_cells() diff --git a/src/lib/encoder/Decoder.h b/src/lib/encoder/Decoder.h index d4fe284a..b877b34c 100644 --- a/src/lib/encoder/Decoder.h +++ b/src/lib/encoder/Decoder.h @@ -30,6 +30,7 @@ class Decoder protected: unsigned _eccBytes; + unsigned _eccBlockSize; unsigned _colorBits; unsigned _bitsPerOp; unsigned _interleaveBlocks; @@ -39,6 +40,7 @@ class Decoder inline Decoder::Decoder(int ecc_bytes, int color_bits, bool interleave) : _eccBytes(ecc_bytes >= 0? ecc_bytes : cimbar::Config::ecc_bytes()) + , _eccBlockSize(cimbar::Config::ecc_block_size()) , _colorBits(color_bits >= 0? color_bits : cimbar::Config::color_bits()) , _bitsPerOp(cimbar::Config::symbol_bits() + _colorBits) , _interleaveBlocks(interleave? cimbar::Config::interleave_blocks() : 0) @@ -64,7 +66,7 @@ inline unsigned Decoder::do_decode(CimbReader& reader, STREAM& ostream) { bitbuffer bb(cimbar::Config::capacity(_bitsPerOp)); std::vector interleaveLookup = Interleave::interleave_reverse(reader.num_reads(), _interleaveBlocks, _interleavePartitions); - std::array colorPositions; // 27232 = the number of cells == reader.num_reads(). Can we calculate this from config at compile time? + std::array colorPositions; // 25920 = the number of cells == reader.num_reads(). Can we calculate this from config at compile time? // read symbols first while (!reader.done()) @@ -88,7 +90,7 @@ inline unsigned Decoder::do_decode(CimbReader& reader, STREAM& ostream) bb.write(bits, p.i, _colorBits); } - reed_solomon_stream rss(ostream, _eccBytes); + reed_solomon_stream rss(ostream, _eccBytes, _eccBlockSize); return bb.flush(rss); } diff --git a/src/lib/encoder/SimpleEncoder.h b/src/lib/encoder/SimpleEncoder.h index 2139c8c8..b21c4cca 100644 --- a/src/lib/encoder/SimpleEncoder.h +++ b/src/lib/encoder/SimpleEncoder.h @@ -26,6 +26,7 @@ class SimpleEncoder protected: unsigned _eccBytes; + unsigned _eccBlockSize; unsigned _bitsPerSymbol; unsigned _bitsPerColor; bool _dark; @@ -33,10 +34,11 @@ class SimpleEncoder }; inline SimpleEncoder::SimpleEncoder(int ecc_bytes, unsigned bits_per_symbol, int bits_per_color) - : _eccBytes(ecc_bytes >= 0? ecc_bytes : cimbar::Config::ecc_bytes()) - , _bitsPerSymbol(bits_per_symbol? bits_per_symbol : cimbar::Config::symbol_bits()) - , _bitsPerColor(bits_per_color >= 0? bits_per_color : cimbar::Config::color_bits()) - , _dark(cimbar::Config::dark()) + : _eccBytes(ecc_bytes >= 0? ecc_bytes : cimbar::Config::ecc_bytes()) + , _eccBlockSize(cimbar::Config::ecc_block_size()) + , _bitsPerSymbol(bits_per_symbol? bits_per_symbol : cimbar::Config::symbol_bits()) + , _bitsPerColor(bits_per_color >= 0? bits_per_color : cimbar::Config::color_bits()) + , _dark(cimbar::Config::dark()) { } @@ -66,7 +68,7 @@ inline std::optional SimpleEncoder::encode_next(STREAM& stream, int can unsigned bits_per_op = _bitsPerColor + _bitsPerSymbol; CimbWriter writer(_bitsPerSymbol, _bitsPerColor, _dark, canvas_size); - reed_solomon_stream rss(stream, _eccBytes); + reed_solomon_stream rss(stream, _eccBytes, _eccBlockSize); bitreader br; while (rss.good()) { diff --git a/src/lib/encoder/reed_solomon_stream.h b/src/lib/encoder/reed_solomon_stream.h index ff3646aa..2f883f0f 100644 --- a/src/lib/encoder/reed_solomon_stream.h +++ b/src/lib/encoder/reed_solomon_stream.h @@ -11,13 +11,10 @@ template class reed_solomon_stream { public: - static const unsigned buffer_size = 155; - -public: - reed_solomon_stream(STREAM& stream, unsigned ecc) - : _stream(stream) - , _rs(ecc) - , _good(stream.good()) + reed_solomon_stream(STREAM& stream, unsigned ecc, unsigned buffer_size) + : _stream(stream) + , _rs(ecc) + , _good(stream.good()) { _buffer.resize(buffer_size, 0); } @@ -32,10 +29,12 @@ class reed_solomon_stream return _stream.tellp(); } - std::streamsize readsome(char* data=NULL, unsigned length=buffer_size) + std::streamsize readsome(char* data=NULL, unsigned length=0) { if (!data) data = _buffer.data(); + if (!length) + length = _buffer.size(); _stream.read(data, length - _rs.parity()); std::streamsize bytes = _stream.gcount(); @@ -49,7 +48,7 @@ class reed_solomon_stream // else _rs.encode(data, bytes, data); - return buffer_size; + return _buffer.size(); } reed_solomon_stream& write(const char* data, unsigned length) @@ -63,16 +62,16 @@ class reed_solomon_stream } // else - while (length >= buffer_size) + while (length >= _buffer.size()) { - ssize_t bytes = _rs.decode(data, buffer_size, _buffer.data()); + ssize_t bytes = _rs.decode(data, _buffer.size(), _buffer.data()); if (bytes <= 0) - _stream << ReedSolomon::BadChunk(buffer_size - _rs.parity()); + _stream << ReedSolomon::BadChunk(_buffer.size() - _rs.parity()); else _stream.write(_buffer.data(), bytes); - length -= buffer_size; - data += buffer_size; + length -= _buffer.size(); + data += _buffer.size(); } return *this; } diff --git a/src/lib/encoder/test/reed_solomon_streamTest.cpp b/src/lib/encoder/test/reed_solomon_streamTest.cpp index 3a19d7bb..e80eb597 100644 --- a/src/lib/encoder/test/reed_solomon_streamTest.cpp +++ b/src/lib/encoder/test/reed_solomon_streamTest.cpp @@ -14,7 +14,7 @@ namespace { string exampleDecodedBlock() { string ex = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" - "012345678901234567890123456789012345678901234567890123456789"; + "012345678901234567890123456789012345678901234567890123456789"; return ex; } @@ -33,7 +33,7 @@ TEST_CASE( "reed_solomon_streamTest/testEncodeOnce", "[unit]" ) input += "0123456789"; stringstream ins(input); - reed_solomon_stream rss(ins, 15); + reed_solomon_stream rss(ins, 15, 155); assertEquals( 155, rss.readsome() ); assertEquals( exampleEncodedBlock(), string(rss.buffer(), 155) ); @@ -42,7 +42,7 @@ TEST_CASE( "reed_solomon_streamTest/testEncodeOnce", "[unit]" ) TEST_CASE( "reed_solomon_streamTest/testDecodeOnce", "[unit]" ) { stringstream outs; - reed_solomon_stream rss(outs, 15); + reed_solomon_stream rss(outs, 15, 155); string encoded = exampleEncodedBlock(); rss.write(encoded.data(), encoded.size()); @@ -55,7 +55,7 @@ TEST_CASE( "reed_solomon_streamTest/testDecodeOnce", "[unit]" ) TEST_CASE( "reed_solomon_streamTest/testDecodeBad", "[unit]" ) { stringstream outs; - reed_solomon_stream rss(outs, 15); + reed_solomon_stream rss(outs, 15, 155); string encoded = string(155, 'f'); rss.write(encoded.data(), encoded.size());