forked from hkc/cc-stuff
1
0
Fork 0

Compare commits

..

No commits in common. "30b6534fc13740e40de92405e41bc1436286b15c" and "bedc6d9166056932e71bbb56ecd2732685e9afe6" have entirely different histories.

1 changed files with 23 additions and 82 deletions

105
img2cpi.c
View File

@ -14,9 +14,6 @@
#define MAX_COLOR_DIFFERENCE 768
#define K_MEANS_ITERATIONS 4
#ifndef HAS_POPCNT
# define HAS_POPCNT 1
#endif
struct rgba { uint8_t r, g, b, a; };
union color {
@ -40,11 +37,11 @@ const extern struct palette DEFAULT_PALETTE, DEFAULT_GRAY_PALETTE;
struct arguments {
int width, height;
enum conversion_mode {
CONVERSION_BLOCK,
CONVERSION_CHAR_PRECISE,
CONVERSION_CHAR_FAST,
} conversion_mode;
enum character_mode {
CHARACTER_BLOCK,
CHARACTER_CHAR_PRECISE,
CHARACTER_CHAR_FAST,
} character_mode;
enum cpi_version {
CPI_VERSION_AUTO,
CPI_VERSION_RAW,
@ -73,7 +70,7 @@ struct arguments {
} args = {
.width = 4 * 8 - 1, // 4x3 blocks screen
.height = 3 * 6 - 2,
.conversion_mode = CONVERSION_CHAR_PRECISE,
.character_mode = CHARACTER_CHAR_PRECISE,
.cpi_version = CPI_VERSION_AUTO,
.placement = PLACEMENT_FULL,
.input_path = NULL,
@ -121,7 +118,7 @@ void image_unload(struct image *img);
void get_size_keep_aspect(int w, int h, int dw, int dh, int *ow, int *oh);
void convert_2x3(const struct image_pal *img, struct cc_char *characters);
void convert_8x11(const struct image_pal *img, struct cc_char *characters, bool precise);
void convert_8x11(const struct image_pal *img, struct cc_char *characters);
// Only one global custom palette is maintained
struct palette *custom_palette_resize(uint8_t size);
@ -132,13 +129,7 @@ bool k_means_iteration(struct k_means_state *state);
void k_means_end(struct k_means_state *state);
struct palette *palette_k_means(const struct image *image, const struct palette *prototype);
inline static int popcnt32(uint32_t mask);
inline static int glyph_hamming_distance(const GlyphBitmap *lhs, const GlyphBitmap *rhs);
inline static float weighted_glyph_hamming_distance(const GlyphBitmap *lhs, const GlyphBitmap *rhs, const typeof(float[11][8]) *weights);
uint8_t closest_glyph_symbol_fast(const GlyphBitmap *target);
uint8_t closest_glyph_symbol_precise(const GlyphBitmap *target, const typeof(float[11][8]) *weights);
void construct_chunk_color_glyph(GlyphBitmap *result, typeof(float[11][8]) *weights, const typeof(float[8][11][0x10]) *chunk_palette_diffs, uint8_t color_pair);
inline static uint8_t closest_chunk_color_symbol(const typeof(float[8][11][0x10]) *chunk_palette_diffs, uint8_t color_pair, bool precise);
inline static uint8_t closest_chunk_color_symbol(const typeof(float[8][11][0x10]) *chunk_palette_diffs, uint8_t color_pair);
const char *known_file_extensions[] = {
".png", ".jpg", ".jpeg", ".jfif", ".jpg", ".gif",
@ -204,7 +195,7 @@ int main(int argc, char **argv) {
}
struct image *canvas;
if (args.conversion_mode == CONVERSION_BLOCK) {
if (args.character_mode == CHARACTER_BLOCK) {
canvas = image_new(args.width * 2, args.height * 3);
} else {
canvas = image_new(args.width * 8, args.height * 11);
@ -257,19 +248,10 @@ int main(int argc, char **argv) {
return EXIT_FAILURE;
}
switch (args.conversion_mode) {
case CONVERSION_BLOCK:
if (args.character_mode == CHARACTER_BLOCK) {
convert_2x3(quantized_image, characters);
break;
case CONVERSION_CHAR_PRECISE:
convert_8x11(quantized_image, characters, true);
break;
case CONVERSION_CHAR_FAST:
convert_8x11(quantized_image, characters, false);
break;
default:
fprintf(stderr, "BUG: invalid args.conversion_mode\n");
return EXIT_FAILURE;
} else {
convert_8x11(quantized_image, characters);
}
// TODO: implement something other than CPIv0
@ -320,7 +302,7 @@ bool parse_cmdline(int argc, char **argv) {
exit(EXIT_SUCCESS);
break;
case 'f': // --fast
args.conversion_mode = CONVERSION_BLOCK;
args.character_mode = CHARACTER_BLOCK;
fprintf(stderr, "Warning: `--fast` is deprecated, use `--mode block` instead\n");
if (args.cpi_version != CPI_VERSION_AUTO) {
fprintf(stderr, "Warning: text mode ignores version\n");
@ -329,11 +311,11 @@ bool parse_cmdline(int argc, char **argv) {
case 'm': // --mode
{
if (0 == strcmp(optarg, "block") || 0 == strcmp(optarg, "fast") || 0 == strcmp(optarg, "2x3")) {
args.conversion_mode = CONVERSION_BLOCK;
args.character_mode = CHARACTER_BLOCK;
} else if (0 == strcmp(optarg, "char") || 0 == strcmp(optarg, "char-precise") || 0 == strcmp(optarg, "8x11") || 0 == strcmp(optarg, "6x9")) {
args.conversion_mode = CONVERSION_CHAR_PRECISE;
args.character_mode = CHARACTER_CHAR_PRECISE;
} else if (0 == strcmp(optarg, "char-fast") || 0 == strcmp(optarg, "8x11-fast") || 0 == strcmp(optarg, "6x9-fast")) {
args.conversion_mode = CONVERSION_CHAR_FAST;
args.character_mode = CHARACTER_CHAR_FAST;
}
}
break;
@ -650,7 +632,7 @@ void convert_2x3(const struct image_pal *img, struct cc_char *characters) {
}
}
void convert_8x11(const struct image_pal *img, struct cc_char *characters, bool precise) {
void convert_8x11(const struct image_pal *img, struct cc_char *characters) {
int w = img->w / 8, h = img->h / 11;
float palette_self_diffs[0x100][0x10] = {{(float) 0xffffff}};
for (int input_color = 0x0; input_color < 0x100 && input_color < img->palette->count; input_color++) {
@ -677,7 +659,7 @@ void convert_8x11(const struct image_pal *img, struct cc_char *characters, bool
char closest_sym = 0x00, closest_color = 0xae;
for (int color = 0x00; color <= 0xff; color++) {
{
const int sym = closest_chunk_color_symbol(&chunk_palette_diffs, color, precise);
const int sym = closest_chunk_color_symbol(&chunk_palette_diffs, color);
float difference = 0;
for (int oy = 0; oy < 11; oy++) {
unsigned char sym_line = font_atlas[sym][oy];
@ -835,28 +817,7 @@ struct palette *palette_k_means(const struct image *image, const struct palette
return palette;
}
int popcnt32(uint32_t mask) {
#if HAS_POPCNT
return __builtin_popcount(mask);
#else
int res = 0;
for (; mask; mask >>= 1) {
res += mask & 1;
}
return res;
#endif
}
int glyph_hamming_distance(const GlyphBitmap *lhs, const GlyphBitmap *rhs) {
int dist = 0;
dist += popcnt32(*((uint32_t*)&((*lhs)[0])) ^ *((uint32_t*)&((*rhs)[0])));
dist += popcnt32(*((uint32_t*)&((*lhs)[4])) ^ *((uint32_t*)&((*rhs)[4])));
dist += popcnt32(*((uint16_t*)&((*lhs)[8])) ^ *((uint16_t*)&((*rhs)[8])));
dist += popcnt32( ( ((*lhs)[10])) ^ ( ((*rhs)[10])));
return dist;
}
float weighted_glyph_hamming_distance(const GlyphBitmap *lhs, const GlyphBitmap *rhs, const 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;
for (int oy = 0; oy < 11; oy++) {
uint8_t sym_line = (*lhs)[oy] ^ (*rhs)[oy];
@ -870,23 +831,7 @@ float weighted_glyph_hamming_distance(const GlyphBitmap *lhs, const GlyphBitmap
return dist;
}
uint8_t closest_glyph_symbol_fast(const GlyphBitmap *target) {
uint8_t best = 0x01;
int best_dist = glyph_hamming_distance(target, &font_atlas[best]);
for (int sym = 0x02; sym <= 0xFF; sym++) {
if (sym == '\t' || sym == '\n' || sym == '\r' || sym == '\x0e') {
continue;
}
int dist = glyph_hamming_distance(target, &font_atlas[sym]);
if (dist <= best_dist) {
best_dist = dist;
best = sym;
}
}
return best;
}
uint8_t closest_glyph_symbol_precise(const GlyphBitmap *target, const typeof(float[11][8]) *weights) {
uint8_t closest_glyph_symbol(const GlyphBitmap *target, const typeof(float[11][8]) *weights) {
uint8_t best = 0x01;
float best_dist = weighted_glyph_hamming_distance(target, &font_atlas[best], weights);
for (int sym = 0x02; sym <= 0xFF; sym++) {
@ -920,15 +865,11 @@ void construct_chunk_color_glyph(GlyphBitmap *result, typeof(float[11][8]) *weig
}
}
uint8_t closest_chunk_color_symbol(const typeof(float[8][11][0x10]) *chunk_palette_diffs, uint8_t color_pair, bool precise) {
uint8_t closest_chunk_color_symbol(const typeof(float[8][11][0x10]) *chunk_palette_diffs, uint8_t color_pair) {
GlyphBitmap glyph;
float weights[11][8];
construct_chunk_color_glyph(&glyph, precise ? &weights : NULL, chunk_palette_diffs, color_pair);
if (precise) {
return closest_glyph_symbol_precise(&glyph, &weights);
} else {
return closest_glyph_symbol_fast(&glyph);
}
construct_chunk_color_glyph(&glyph, &weights, chunk_palette_diffs, color_pair);
return closest_glyph_symbol(&glyph, &weights);
}
const struct palette DEFAULT_PALETTE = PALETTE(