Skip to content

Commit

Permalink
Attempt to use 988x988 grid...?
Browse files Browse the repository at this point in the history
Removing various hard coded "155"s, and whatnot
  • Loading branch information
sz3 committed Jun 1, 2023
1 parent c4f8750 commit 7766cfd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/lib/cimb_translator/CimbReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/cimb_translator/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -61,7 +61,7 @@ unsigned Config::cell_offset()

unsigned Config::cells_per_col()
{
return 166;
return 162;
}

unsigned Config::total_cells()
Expand Down
6 changes: 4 additions & 2 deletions src/lib/encoder/Decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Decoder

protected:
unsigned _eccBytes;
unsigned _eccBlockSize;
unsigned _colorBits;
unsigned _bitsPerOp;
unsigned _interleaveBlocks;
Expand All @@ -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)
Expand All @@ -64,7 +66,7 @@ inline unsigned Decoder::do_decode(CimbReader& reader, STREAM& ostream)
{
bitbuffer bb(cimbar::Config::capacity(_bitsPerOp));
std::vector<unsigned> interleaveLookup = Interleave::interleave_reverse(reader.num_reads(), _interleaveBlocks, _interleavePartitions);
std::array<PositionData, 27232> colorPositions; // 27232 = the number of cells == reader.num_reads(). Can we calculate this from config at compile time?
std::array<PositionData, 25920> colorPositions; // 25920 = the number of cells == reader.num_reads(). Can we calculate this from config at compile time?

// read symbols first
while (!reader.done())
Expand All @@ -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);
}

Expand Down
12 changes: 7 additions & 5 deletions src/lib/encoder/SimpleEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ class SimpleEncoder

protected:
unsigned _eccBytes;
unsigned _eccBlockSize;
unsigned _bitsPerSymbol;
unsigned _bitsPerColor;
bool _dark;
uint8_t _encodeId = 0;
};

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())
{
}

Expand Down Expand Up @@ -66,7 +68,7 @@ inline std::optional<cv::Mat> 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())
{
Expand Down
27 changes: 13 additions & 14 deletions src/lib/encoder/reed_solomon_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ template <typename STREAM>
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);
}
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/encoder/test/reed_solomon_streamTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace {
string exampleDecodedBlock()
{
string ex = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
"012345678901234567890123456789012345678901234567890123456789";
"012345678901234567890123456789012345678901234567890123456789";
return ex;
}

Expand All @@ -33,7 +33,7 @@ TEST_CASE( "reed_solomon_streamTest/testEncodeOnce", "[unit]" )
input += "0123456789";

stringstream ins(input);
reed_solomon_stream<stringstream> rss(ins, 15);
reed_solomon_stream<stringstream> rss(ins, 15, 155);

assertEquals( 155, rss.readsome() );
assertEquals( exampleEncodedBlock(), string(rss.buffer(), 155) );
Expand All @@ -42,7 +42,7 @@ TEST_CASE( "reed_solomon_streamTest/testEncodeOnce", "[unit]" )
TEST_CASE( "reed_solomon_streamTest/testDecodeOnce", "[unit]" )
{
stringstream outs;
reed_solomon_stream<stringstream> rss(outs, 15);
reed_solomon_stream<stringstream> rss(outs, 15, 155);

string encoded = exampleEncodedBlock();
rss.write(encoded.data(), encoded.size());
Expand All @@ -55,7 +55,7 @@ TEST_CASE( "reed_solomon_streamTest/testDecodeOnce", "[unit]" )
TEST_CASE( "reed_solomon_streamTest/testDecodeBad", "[unit]" )
{
stringstream outs;
reed_solomon_stream<stringstream> rss(outs, 15);
reed_solomon_stream<stringstream> rss(outs, 15, 155);

string encoded = string(155, 'f');
rss.write(encoded.data(), encoded.size());
Expand Down

0 comments on commit 7766cfd

Please sign in to comment.