Repository: sz3/libcimbar
File: src/lib/encoder/SimpleEncoder.h (C/C++)

Code before:
#include "cimb_translator/CimbWriter.h"
#include "cimb_translator/Config.h"
#include "compression/zstd_compressor.h"
#include "fountain/fountain_encoder_stream.h"

#include <opencv2/opencv.hpp>
#include <optional>
#include <string>

class SimpleEncoder
{
public:
	SimpleEncoder(unsigned ecc_bytes=40, unsigned bits_per_symbol=0, unsigned bits_per_color=0);

	template <typename STREAM>
	std::optional<cv::Mat> encode_next(STREAM& stream);

	template <typename STREAM>
	fountain_encoder_stream::ptr create_fountain_encoder(STREAM& stream, int compression_level=6);


protected:
	unsigned _eccBytes;
	unsigned _bitsPerSymbol;
	unsigned _bitsPerColor;
};

inline SimpleEncoder::SimpleEncoder(unsigned ecc_bytes, unsigned bits_per_symbol, unsigned bits_per_color)
    : _eccBytes(ecc_bytes)
    , _bitsPerSymbol(bits_per_symbol? bits_per_symbol : cimbar::Config::symbol_bits())
    , _bitsPerColor(bits_per_color? bits_per_color : cimbar::Config::color_bits())
{
}

/* while bits == f.read(buffer, 8192)
 *     encode(bits)
 *
 * char buffer[2000];
 * while f.read(buffer)
 *     bit_buffer bb(buffer)
 *     while bb
 *         bits1 = bb.get(_bitsPerSymbol)
 *         bits2 = bb.get(_bitsPerColor)
 *
 * */

template <typename STREAM>
inline std::optional<cv::Mat> SimpleEncoder::encode_next(STREAM& stream)
{
	if (!stream.good())
		return std::nullopt;

Review comment (suggestion):
Ideally this would auto-increment, but it's not clear who should own the increment. The *decoder* is the part of the system that would like encode_id to be distinct (within its life cycle), but the decoder's life cycle is quite different from the encoder's!

A random encode_id (and increment from there) might be reasonable.

Code after:
#include "cimb_translator/CimbWriter.h"
#include "cimb_translator/Config.h"
#include "compression/zstd_compressor.h"
#include "fountain/fountain_encoder_stream.h"

#include <opencv2/opencv.hpp>
#include <optional>
#include <string>

class SimpleEncoder
{
public:
	SimpleEncoder(unsigned ecc_bytes=40, unsigned bits_per_symbol=0, unsigned bits_per_color=0);
	void set_encode_id(uint8_t encode_id); // [0-127] -- the high bit is ignored.

	template <typename STREAM>
	std::optional<cv::Mat> encode_next(STREAM& stream);

	template <typename STREAM>
	fountain_encoder_stream::ptr create_fountain_encoder(STREAM& stream, int compression_level=6);

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

inline SimpleEncoder::SimpleEncoder(unsigned ecc_bytes, unsigned bits_per_symbol, unsigned bits_per_color)
    : _eccBytes(ecc_bytes)
    , _bitsPerSymbol(bits_per_symbol? bits_per_symbol : cimbar::Config::symbol_bits())
    , _bitsPerColor(bits_per_color? bits_per_color : cimbar::Config::color_bits())
{
}

inline void SimpleEncoder::set_encode_id(uint8_t encode_id)
{
	_encodeId = encode_id;
}

/* while bits == f.read(buffer, 8192)
 *     encode(bits)
 *
 * char buffer[2000];
 * while f.read(buffer)
 *     bit_buffer bb(buffer)
 *     while bb
 *         bits1 = bb.get(_bitsPerSymbol)
 *         bits2 = bb.get(_bitsPerColor)
 *
 * */