/*
 * File: assets_preview.h
 * PNG output and rendered previews for Tiled and campaign maps.
 * Implementation include; compiled only through assets.c.
 */

#ifndef ASSETS_PREVIEW_H
#define ASSETS_PREVIEW_H

/* PNG previews use stored DEFLATE blocks, so no external image library is needed. */
static void be32(uint8_t *out, uint32_t value)
{
    int i;
    for (i = 3; i >= 0; --i) { out[i] = (uint8_t)value; value >>= 8; }
}

static void png_chunk(FILE *f, const char *type, const uint8_t *data, size_t size)
{
    uint8_t word[4];
    uint32_t crc = UINT32_MAX;
    size_t i;
    be32(word, (uint32_t)size); fwrite(word, 1, 4, f); fwrite(type, 1, 4, f);
    if (size) fwrite(data, 1, size, f);
    for (i = 0; i < size + 4; ++i) {
        uint8_t b = i < 4 ? (uint8_t)type[i] : data[i - 4];
        int bit;
        crc ^= b;
        for (bit = 0; bit < 8; ++bit) crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320u : 0);
    }
    be32(word, ~crc); fwrite(word, 1, 4, f);
}

static void write_png(const char *name, const uint8_t *pixels_rgb, int width, int height, int channels)
{
    size_t stride = (size_t)width * (size_t)channels, size = (stride + 1) * (size_t)height;
    uint8_t *raw = allocate(size), *z = allocate(size + (size / 65535 + 1) * 5 + 6), header[13];
    size_t pos = 0, used = 2, i;
    uint32_t a = 1, b = 0;
    FILE *f;
    for (i = 0; i < (size_t)height; ++i)
        memcpy(raw + i * (stride + 1) + 1, pixels_rgb + i * stride, stride);
    z[0] = 0x78; z[1] = 0x01;
    while (pos < size) {
        size_t length = size - pos > 65535 ? 65535 : size - pos;
        uint16_t complement = (uint16_t)~(uint16_t)length;
        z[used++] = (uint8_t)(pos + length == size);
        z[used++] = (uint8_t)length; z[used++] = (uint8_t)(length >> 8);
        z[used++] = (uint8_t)complement; z[used++] = (uint8_t)(complement >> 8);
        memcpy(z + used, raw + pos, length); used += length; pos += length;
    }
    for (i = 0; i < size; ++i) { a = (a + raw[i]) % 65521; b = (b + a) % 65521; }
    be32(z + used, b * 65536 + a); used += 4;
    be32(header, (uint32_t)width); be32(header + 4, (uint32_t)height);
    header[8] = 8; header[9] = (uint8_t)(channels == 4 ? 6 : 2);
    header[10] = header[11] = header[12] = 0;
    f = output(name, "wb");
    fwrite("\x89PNG\r\n\x1a\n", 1, 8, f);
    png_chunk(f, "IHDR", header, sizeof(header));
    png_chunk(f, "IDAT", z, used); png_chunk(f, "IEND", NULL, 0);
    close_file(f);
    free(z); free(raw);
}

static void preview_map(Level *level, uint8_t *image, int image_width, int left, int top)
{
    int units[CELLS], highlighted[CELLS] = {0}, slots[10] = {0}, i, y, x;
    for (i = 0; i < CELLS; ++i) units[i] = -1;
    for (i = 0; i < level->count; ++i) {
        int row = level->units[1][i], pos = row * 16 + level->units[0][i];
        units[pos] = unit_art[level->units[3][i]][level->units[2][i]];
        if (slots[row]++ < 4) highlighted[pos] = 1;
    }
    for (y = 0; y < 160; ++y)
        for (x = 0; x < 128; ++x) {
            int pos = (y / 16) * 16 + x / 8, color = graphics[level->display[pos]][y % 16][x % 8];
            int terrain = level->terrain[pos], owner = terrain == 7 || terrain == 8 ? terrain - 5 : -1;
            const uint8_t *rgb_color;
            int u = units[pos], team, kind = art_kind(u, &team), copy;
            if (owner >= 0 && color >= 2) color = owner;
            if (u >= 0 && opacity[u][y % 16][x % 8]) color = pixels[u][y % 16][x % 8];
            if (owner >= 0 && y % 16 < 3 && x % 8 >= 5) color = owner;
            rgb_color = raster_rgb[(y % 16) / 4][color];
            if (highlighted[pos] && kind >= 0 && (lights[kind][y % 16] & (128 >> (x % 8))))
                rgb_color = light_rgb;
            for (copy = 0; copy < 2; ++copy)
                memcpy(image + ((size_t)(top + y) * (size_t)image_width + (size_t)(left + x * 2 + copy)) * 3,
                       rgb_color, 3);
        }
}

static void write_previews(void)
{
    uint8_t sheet[128 * 48 * 4] = {0}, preview[256 * 160 * 3];
    uint8_t *campaign = allocate(1024 * 480 * 3);
    int i, y, x, copy;
    for (i = 0; i < 24; ++i)
        for (y = 0; y < 16; ++y)
            for (x = 0; x < 8; ++x) {
                int team, kind = art_kind(i, &team), color = i < ART ? pixels[i][y][x] : 0;
                const uint8_t *rgb_color = raster_rgb[y / 4][color];
                if (kind >= 0 && (lights[kind][y] & (128 >> x))) rgb_color = light_rgb;
                for (copy = 0; copy < 2; ++copy) {
                    size_t pos = ((size_t)(i / 8 * 16 + y) * 128 + (size_t)(i % 8 * 16 + x * 2 + copy)) * 4;
                    memcpy(sheet + pos, rgb_color, 3);
                    sheet[pos + 3] = (uint8_t)(i < ART && opacity[i][y][x] ? 255 : 0);
                }
            }
    write_png("assets/tiles.png", sheet, 128, 48, 4);
    preview_map(&test_level, preview, 256, 0, 0);
    write_png("maps/preview.png", preview, 256, 160, 3);
    for (i = 0; i < MAPS; ++i)
        preview_map(&levels[i], campaign, 1024, i % 4 * 256, i / 4 * 160);
    write_png("maps/campaign-preview.png", campaign, 1024, 480, 3);
    free(campaign);
}

#endif
