- runtime/hd_sprites.c : choisit les sprites à dessiner en HD à partir de ce que le jeu a dessiné (dernier sprite encore visible par pixel), charge les images HD sur un fil à part, cache LRU. - Couleurs : correction par indice (palette du jeu - palette de l'export), interpolée dans le shader ; les planches d'objets partagées entre pièces et les fondus ont les bonnes couleurs. - Objets dessinés avec leur propre fond : la partie du fond de la couleur du décor revient au décor HD. - runtime/dxt.c : compression DXT5 et mipmaps sur le fil de chargement (4 fois moins de mémoire, sans à-coup) ; mémoire privée 243 Mo avec décors et sprites HD (objectif < 256 Mo). - Menu F12 : « Pack HD (mods) » avec deux sous-réglages, « Décors HD » (hd_decors) et « Personnages et objets HD » (hd_sprites). - tools/hd_export.py : --planches, --sprites, --sans-decors (planches RGBA, couleurs prolongées dans la transparence). - png.c sans état global (décodage sur le fil de chargement). - docs : MODS.md (sprites HD), SETTINGS.md (nouvelles clés), ROADMAP cochée. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
157 lines
6.6 KiB
C
157 lines
6.6 KiB
C
/* Compression DXT5 : voir dxt.h.
|
||
*
|
||
* Couleur : axe principal des couleurs du bloc (covariance, quelques itérations de la puissance), extrémités
|
||
* = projections extrêmes resserrées d'un seizième, quantifiées en 5:6:5 ; chaque pixel prend la plus proche
|
||
* des 4 couleurs. Alpha : min et max du bloc, 8 valeurs interpolées. */
|
||
#include <math.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include "dxt.h"
|
||
|
||
static int clamp255(int v) { return v < 0 ? 0 : v > 255 ? 255 : v; }
|
||
|
||
static uint16_t to565(const int c[3]) {
|
||
return (uint16_t)((clamp255(c[0]) * 31 + 127) / 255 << 11 | (clamp255(c[1]) * 63 + 127) / 255 << 5 |
|
||
(clamp255(c[2]) * 31 + 127) / 255);
|
||
}
|
||
|
||
static void from565(uint16_t v, int c[3]) {
|
||
int r = v >> 11 & 31, g = v >> 5 & 63, b = v & 31;
|
||
c[0] = r << 3 | r >> 2; c[1] = g << 2 | g >> 4; c[2] = b << 3 | b >> 2;
|
||
}
|
||
|
||
/* px : 16 pixels R, G, B, A ; out : 16 octets */
|
||
static void block(const uint8_t px[16][4], uint8_t *out) {
|
||
int i, c, amin = 255, amax = 0, mean[3] = {0}, lo[3], hi[3], pal[4][3];
|
||
double cov[6] = {0}, ax[3] = {1, 1, 1}, pmin = 1e30, pmax = -1e30;
|
||
uint16_t c0, c1;
|
||
uint32_t cidx = 0;
|
||
uint64_t aidx = 0;
|
||
int it;
|
||
/* alpha */
|
||
for (i = 0; i < 16; i++) { if (px[i][3] < amin) amin = px[i][3]; if (px[i][3] > amax) amax = px[i][3]; }
|
||
out[0] = (uint8_t)amax; out[1] = (uint8_t)amin;
|
||
if (amax > amin) {
|
||
int av[8];
|
||
av[0] = amax; av[1] = amin;
|
||
for (i = 1; i <= 6; i++) av[i + 1] = ((7 - i) * amax + i * amin + 3) / 7;
|
||
for (i = 0; i < 16; i++) {
|
||
int k, best = 0, bd = 1 << 30;
|
||
for (k = 0; k < 8; k++) { int d = abs(av[k] - px[i][3]); if (d < bd) { bd = d; best = k; } }
|
||
aidx |= (uint64_t)best << (3 * i);
|
||
}
|
||
}
|
||
for (i = 0; i < 6; i++) out[2 + i] = (uint8_t)(aidx >> (8 * i));
|
||
/* couleur */
|
||
for (i = 0; i < 16; i++) for (c = 0; c < 3; c++) mean[c] += px[i][c];
|
||
for (c = 0; c < 3; c++) mean[c] = (mean[c] + 8) / 16;
|
||
for (i = 0; i < 16; i++) {
|
||
double r = px[i][0] - mean[0], g = px[i][1] - mean[1], b = px[i][2] - mean[2];
|
||
cov[0] += r * r; cov[1] += r * g; cov[2] += r * b; cov[3] += g * g; cov[4] += g * b; cov[5] += b * b;
|
||
}
|
||
for (it = 0; it < 4; it++) { /* axe principal */
|
||
double x = cov[0] * ax[0] + cov[1] * ax[1] + cov[2] * ax[2];
|
||
double y = cov[1] * ax[0] + cov[3] * ax[1] + cov[4] * ax[2];
|
||
double z = cov[2] * ax[0] + cov[4] * ax[1] + cov[5] * ax[2];
|
||
double m = x * x + y * y + z * z;
|
||
if (m < 1e-12) break;
|
||
m = 1.0 / sqrt(m);
|
||
ax[0] = x * m; ax[1] = y * m; ax[2] = z * m;
|
||
}
|
||
for (i = 0; i < 16; i++) {
|
||
double p = (px[i][0] - mean[0]) * ax[0] + (px[i][1] - mean[1]) * ax[1] + (px[i][2] - mean[2]) * ax[2];
|
||
if (p < pmin) { pmin = p; for (c = 0; c < 3; c++) lo[c] = px[i][c]; }
|
||
if (p > pmax) { pmax = p; for (c = 0; c < 3; c++) hi[c] = px[i][c]; }
|
||
}
|
||
for (c = 0; c < 3; c++) { /* resserrer d'un seizième */
|
||
int d = (hi[c] - lo[c]) / 16;
|
||
hi[c] -= d; lo[c] += d;
|
||
}
|
||
c0 = to565(hi); c1 = to565(lo);
|
||
if (c0 < c1) { uint16_t t = c0; c0 = c1; c1 = t; }
|
||
if (c0 != c1) {
|
||
from565(c0, pal[0]); from565(c1, pal[1]);
|
||
for (c = 0; c < 3; c++) {
|
||
pal[2][c] = (2 * pal[0][c] + pal[1][c] + 1) / 3;
|
||
pal[3][c] = (pal[0][c] + 2 * pal[1][c] + 1) / 3;
|
||
}
|
||
for (i = 0; i < 16; i++) {
|
||
int k, best = 0, bd = 1 << 30;
|
||
for (k = 0; k < 4; k++) {
|
||
int dr = pal[k][0] - px[i][0], dg = pal[k][1] - px[i][1], db = pal[k][2] - px[i][2];
|
||
int d = dr * dr + dg * dg + db * db;
|
||
if (d < bd) { bd = d; best = k; }
|
||
}
|
||
cidx |= (uint32_t)best << (2 * i);
|
||
}
|
||
}
|
||
out[8] = (uint8_t)c0; out[9] = (uint8_t)(c0 >> 8); out[10] = (uint8_t)c1; out[11] = (uint8_t)(c1 >> 8);
|
||
for (i = 0; i < 4; i++) out[12 + i] = (uint8_t)(cidx >> (8 * i));
|
||
}
|
||
|
||
static size_t level_size(int w, int h) { return (size_t)((w + 3) / 4) * (size_t)((h + 3) / 4) * 16; }
|
||
|
||
static void compress(const uint32_t *argb, int w, int h, uint8_t *out) {
|
||
int bx, by, x, y;
|
||
for (by = 0; by < h; by += 4)
|
||
for (bx = 0; bx < w; bx += 4) {
|
||
uint8_t px[16][4];
|
||
for (y = 0; y < 4; y++)
|
||
for (x = 0; x < 4; x++) {
|
||
int sx = bx + x < w ? bx + x : w - 1, sy = by + y < h ? by + y : h - 1;
|
||
uint32_t v = argb[(size_t)sy * w + sx];
|
||
px[y * 4 + x][0] = (uint8_t)(v >> 16); px[y * 4 + x][1] = (uint8_t)(v >> 8);
|
||
px[y * 4 + x][2] = (uint8_t)v; px[y * 4 + x][3] = (uint8_t)(v >> 24);
|
||
}
|
||
block((const uint8_t (*)[4])px, out);
|
||
out += 16;
|
||
}
|
||
}
|
||
|
||
/* niveau suivant : moyenne 2 × 2 (les couleurs des pixels transparents sont déjà celles des voisins) */
|
||
static uint32_t *half(const uint32_t *src, int w, int h, int *nw, int *nh) {
|
||
int x, y, c, W = w > 1 ? w / 2 : 1, H = h > 1 ? h / 2 : 1;
|
||
uint32_t *d = (uint32_t *)malloc((size_t)W * H * 4);
|
||
if (!d) return NULL;
|
||
for (y = 0; y < H; y++)
|
||
for (x = 0; x < W; x++) {
|
||
int x0 = x * 2 < w ? x * 2 : w - 1, x1 = x * 2 + 1 < w ? x * 2 + 1 : w - 1;
|
||
int y0 = y * 2 < h ? y * 2 : h - 1, y1 = y * 2 + 1 < h ? y * 2 + 1 : h - 1;
|
||
uint32_t p[4], v = 0;
|
||
p[0] = src[(size_t)y0 * w + x0]; p[1] = src[(size_t)y0 * w + x1];
|
||
p[2] = src[(size_t)y1 * w + x0]; p[3] = src[(size_t)y1 * w + x1];
|
||
for (c = 0; c < 32; c += 8)
|
||
v |= (uint32_t)(((p[0] >> c & 255) + (p[1] >> c & 255) + (p[2] >> c & 255) + (p[3] >> c & 255) + 2) / 4) << c;
|
||
d[(size_t)y * W + x] = v;
|
||
}
|
||
*nw = W; *nh = H;
|
||
return d;
|
||
}
|
||
|
||
uint8_t *dxt5_mips(const uint32_t *argb, int w, int h, int *levels, size_t *size) {
|
||
size_t total = 0, off = 0;
|
||
int l = w, m = h, n = 0;
|
||
uint8_t *out;
|
||
const uint32_t *cur = argb;
|
||
uint32_t *tmp = NULL;
|
||
for (;;) { total += level_size(l, m); n++; if (l == 1 && m == 1) break; l = l > 1 ? l / 2 : 1; m = m > 1 ? m / 2 : 1; }
|
||
out = (uint8_t *)malloc(total);
|
||
if (!out) return NULL;
|
||
l = w; m = h;
|
||
for (;;) {
|
||
uint32_t *next;
|
||
int nl, nm;
|
||
compress(cur, l, m, out + off);
|
||
off += level_size(l, m);
|
||
if (l == 1 && m == 1) break;
|
||
next = half(cur, l, m, &nl, &nm);
|
||
free(tmp);
|
||
if (!next) { free(out); return NULL; }
|
||
cur = tmp = next; l = nl; m = nm;
|
||
}
|
||
free(tmp);
|
||
*levels = n;
|
||
*size = total;
|
||
return out;
|
||
}
|