/***
   This file is part of fly-ocr.

   Copyright (c) 1990-1997 Sam Leffler
   Copyright (c) 1991-1997 Silicon Graphics, Inc.
   Copyright (C) 2016-2020 Sandro Mani <manisandro@gmail.com>
   Copyright (C) 2021 LLC "RusBITech-Astra" (https://astralinux.ru)

    Based on gImageReader source code by Sandro Mani under the GNU
    General Public License (GPL) version 3 or later

   Foobar is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Foobar is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with fly-ocr. If not, see <https://www.gnu.org/licenses/>.
***/

#ifndef FAX4ENCODER_H
#define FAX4ENCODER_H

#include <cstdint>

class CCITTFax4Encoder
{
public:
    CCITTFax4Encoder();
    ~CCITTFax4Encoder();
    uint8_t* encode(const uint8_t* buffer, uint32_t width, uint32_t height, uint32_t rowbytes, uint32_t& outputSize);

private:
    struct EncoderState;
    struct TableEntry;
    EncoderState* m_state;

    void encode2DRow(const uint8_t* codeline, const uint8_t* refline, uint32_t linebits);
    inline void putspan(int32_t span, const TableEntry* tab);
    inline void putbits(uint16_t bits, uint16_t length);
    inline void flushbits();

    static const TableEntry whiteCodes[];
    static const TableEntry blackCodes[];
    static const TableEntry horizcode;
    static const TableEntry passcode;
    static const TableEntry vcodes[];
};

#endif // CCITT_H
