forked from hkc/cc-stuff
1
0
Fork 0

Fix factored out font_atlas linear search

This commit is contained in:
Vftdan 2024-10-03 21:19:56 +02:00
parent 6ebe98e94c
commit 135c3642b0
1 changed files with 5 additions and 4 deletions

View File

@ -793,7 +793,7 @@ struct palette *palette_k_means(const struct image *image, const struct palette
return palette; return palette;
} }
inline static float weighted_glyph_hamming_distance(const GlyphBitmap *lhs, const GlyphBitmap *rhs, typeof(float[11][8]) *weights) { inline static float weighted_glyph_hamming_distance(const GlyphBitmap *lhs, const GlyphBitmap *rhs, const typeof(float[11][8]) *weights) {
float dist = 0; float dist = 0;
for (int oy = 0; oy < 11; oy++) { for (int oy = 0; oy < 11; oy++) {
uint8_t sym_line = (*lhs)[oy] ^ (*rhs)[oy]; uint8_t sym_line = (*lhs)[oy] ^ (*rhs)[oy];
@ -807,14 +807,14 @@ inline static float weighted_glyph_hamming_distance(const GlyphBitmap *lhs, cons
return dist; return dist;
} }
uint8_t closest_glyph_symbol(const GlyphBitmap *target, typeof(float[11][8]) *weights) { uint8_t closest_glyph_symbol(const GlyphBitmap *target, const typeof(float[11][8]) *weights) {
uint8_t best = 0x01; uint8_t best = 0x01;
float best_dist = weighted_glyph_hamming_distance(target, &font_atlas[best], weights); float best_dist = weighted_glyph_hamming_distance(target, &font_atlas[best], weights);
for (int sym = 0x02; sym <= 0xFF; sym++) { for (int sym = 0x02; sym <= 0xFF; sym++) {
if (sym == '\t' || sym == '\n' || sym == '\r' || sym == '\x0e') { if (sym == '\t' || sym == '\n' || sym == '\r' || sym == '\x0e') {
continue; continue;
} }
float dist = weighted_glyph_hamming_distance(target, &font_atlas[best], weights); float dist = weighted_glyph_hamming_distance(target, &font_atlas[sym], weights);
if (dist <= best_dist) { if (dist <= best_dist) {
best_dist = dist; best_dist = dist;
best = sym; best = sym;
@ -829,7 +829,8 @@ void construct_chunk_color_glyph(GlyphBitmap *result, typeof(float[11][8]) *weig
for (int oy = 0; oy < 11; oy++) { for (int oy = 0; oy < 11; oy++) {
uint8_t sym_line = 0; uint8_t sym_line = 0;
for (int ox = 0; ox < 8; ox++) { for (int ox = 0; ox < 8; ox++) {
float dist_diff = (*chunk_palette_diffs)[ox][oy][fg] - (*chunk_palette_diffs)[ox][oy][bg]; // We want lit to minimize distance, so lit should be trueish when background color is further from the pixel than foreground color
float dist_diff = (*chunk_palette_diffs)[ox][oy][bg] - (*chunk_palette_diffs)[ox][oy][fg];
uint8_t lit = dist_diff > 0; uint8_t lit = dist_diff > 0;
sym_line |= lit << (7 - ox); sym_line |= lit << (7 - ox);
if (weights) { if (weights) {