#pragma once
#include <cstdint>
#include <vector>

namespace bmp2tile
{

// Ported directly from sverx's gfxcomp_stc4.c ("Simple Tile Compressor
// Four", .stc4compr) -- the exact encoding devkitSMS's
// SMS_loadSTC4compressedTilesatAddr expects. STC4 is a superset of STC0:
// for each 4-byte group it picks the cheapest of three strategies, using
// a `buffer` holding the immediately-preceding group's raw bytes (this
// state persists across tile boundaries, matching the reference, which
// never resets it per-tile -- `input` is one continuous stream):
//
//   1. Exact repeat of `buffer`: counted via a run-length byte (1-31,
//      `MaxGroupRepetitions`) rather than re-emitting anything. A run
//      longer than 31 is split into multiple run-length bytes.
//   2. "Group match": if enough of this group's bytes equal `buffer`'s
//      (or `buffer`'s bitwise complement) to beat the group's own STC0
//      encoding, emit a tag byte (which bytes differ) plus just the
//      differing bytes.
//   3. Plain STC0 encoding (see Stc0.h) of this group, when neither of
//      the above wins.
//
// Byte-value ranges in the output are mutually exclusive by construction,
// which is what lets the decompressor tell them apart with no separate
// framing: 0x00 = end marker, 0x01-0x1F = run length, 0x20-0x2F = group
// match, 0x30-0x3F = inverted group match, 0x40-0xFF = a plain STC0 mask
// byte (whose top 2 bits, encoding byte0, are always one of %01/%10/%11 --
// never %00 -- so a plain mask byte can never fall below 0x40).
//
// `input.size()` should be a multiple of 4, same caveat as CompressStc0.
std::vector<uint8_t> CompressStc4(const std::vector<uint8_t>& input);

}  // namespace bmp2tile
