// Smoke test: verify the exact libsixel call sequence used by peak's
// SixelRenderer emits a valid sixel DCS stream from a BGRA8888 buffer.
#include <cstdio>
#include <cstring>
#include <sixel.h>

static int wcb(char *data, int size, void *priv) {
  fwrite(data, 1, (size_t)size, stdout);
  return 0;
}

int main() {
  const int w = 64, h = 48;
  unsigned char px[w * h * 4];
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++) {
      // gradient: B,G,R
      size_t i = ((size_t)y * w + x) * 4;
      px[i + 0] = (unsigned char)(x * 4);
      px[i + 1] = 128;
      px[i + 2] = (unsigned char)(y * 4);
      px[i + 3] = 255;
    }

  sixel_output_t *out = nullptr;
  if (sixel_output_new(&out, wcb, nullptr, nullptr) != SIXEL_OK || !out) {
    fprintf(stderr, "output_new failed\n");
    return 1;
  }
  sixel_dither_t *d = nullptr;
  if (sixel_dither_new(&d, 256, nullptr) != SIXEL_OK || !d) {
    fprintf(stderr, "dither_new failed\n");
    return 1;
  }
  int st = sixel_dither_initialize(d, px, w, h, SIXEL_PIXELFORMAT_BGRA8888,
                                   SIXEL_LARGE_AUTO, SIXEL_REP_AUTO,
                                   SIXEL_QUALITY_HIGH);
  fprintf(stderr, "initialize status=%d\n", st);
  st = sixel_encode(px, w, h, 4, d, out);
  fprintf(stderr, "encode status=%d\n", st);
  sixel_dither_unref(d);
  sixel_output_unref(out);
  fprintf(stderr, "done\n");
  return st == SIXEL_OK ? 0 : 1;
}