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

namespace bmp2tile
{

// Ported directly from sverx's gfxcomp_stc0.c ("Simple Tile Compressor
// Zero", .stc0compr) -- this is the exact per-4-byte-group encoding
// devkitSMS's SMS_loadSTC0compressedTilesatAddr expects.
//
// Format: `input` is processed 4 bytes at a time (matching one row of
// devkitSMS's 32-byte-per-tile separated-bitplane format -- PackTilePlanar's
// output concatenated across tiles), with no state carried between groups.
// Each group emits one mask byte -- 2 bits per source byte, MSB-first
// (byte0 in bits 7-6 ... byte3 in bits 1-0) -- meaning:
//   00 = repeat the most recent raw value already seen earlier in this
//        group (no byte follows)
//   01 = a new raw value (the byte follows in the stream)
//   10 = the byte is 0x00 (no byte follows)
//   11 = the byte is 0xFF (no byte follows)
// followed by however many raw bytes code 01 required (0-4). Because
// byte0 can never use code 00 (there's nothing earlier in the group to
// repeat), a real mask byte is always >= 0x40, which is what makes a
// trailing 0x00 an unambiguous end-of-stream marker.
//
// `input.size()` should be a multiple of 4; any trailing 1-3 bytes are
// silently ignored (the reference has no partial-group handling either).
std::vector<uint8_t> CompressStc0(const std::vector<uint8_t>& input);

// Encodes one 4-byte group into a mask byte + 0-4 raw values, per the
// scheme described above. `values`/`valuesCount` are out-parameters
// (matching the reference's `values[]`/`valuescount`). Exposed (not just
// an implementation detail of CompressStc0) because STC4 needs this exact
// same per-group encoding as one of its candidate strategies -- shared
// directly rather than duplicated, so the two can't silently drift apart.
uint8_t EncodeStc0Group(const uint8_t* group, uint8_t values[4], int& valuesCount);

}  // namespace bmp2tile
