[1840] | 1 | /* |
---|
| 2 | * sortchars analyse ASCII characters |
---|
| 3 | * Copyright (c) 2007 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
| 5 | * |
---|
| 6 | * $Id: sortchars.c 2821 2008-09-27 13:12:46Z sam $ |
---|
| 7 | * |
---|
| 8 | * This program is free software. It comes without any warranty, to |
---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #include "config.h" |
---|
[2299] | 16 | |
---|
[1840] | 17 | #if !defined(__KERNEL__) |
---|
| 18 | # include <stdio.h> |
---|
| 19 | # include <string.h> |
---|
| 20 | # include <stdlib.h> |
---|
| 21 | #endif |
---|
[2299] | 22 | |
---|
[2821] | 23 | #include "caca.h" |
---|
[1840] | 24 | |
---|
| 25 | #define GLYPHS 0x7f |
---|
[1842] | 26 | #define FONT 0 /* 0 or 1 */ |
---|
[1840] | 27 | #define DX 2 |
---|
| 28 | #define DY 3 |
---|
[1841] | 29 | #define RANGEBITS 2 |
---|
| 30 | #define RANGE (1 << RANGEBITS) |
---|
| 31 | #define FULLRANGE (1 << (RANGEBITS * DX * DY)) |
---|
[1840] | 32 | |
---|
| 33 | int total[GLYPHS][DX][DY]; |
---|
[1841] | 34 | int16_t allbits[GLYPHS]; |
---|
| 35 | int bestchar[FULLRANGE]; |
---|
[1840] | 36 | |
---|
| 37 | static int curve[17] = /* 17 instead of 16 */ |
---|
| 38 | { |
---|
| 39 | 0, 4, 6, 8, 9, 10, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15 |
---|
[1871] | 40 | //0, 3, 5, 7, 8, 9, 10, 11, 12, 13, 13, 14, 14, 15, 15, 15, 15 |
---|
[1842] | 41 | //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15 |
---|
[1840] | 42 | }; |
---|
| 43 | |
---|
[1841] | 44 | static int distance(uint16_t, uint16_t); |
---|
[1842] | 45 | static void testcircle(void); |
---|
[1841] | 46 | |
---|
[1840] | 47 | int main(int argc, char *argv[]) |
---|
| 48 | { |
---|
| 49 | int count[DX][DY]; |
---|
| 50 | char utf8[7]; |
---|
[2821] | 51 | caca_canvas_t *cv; |
---|
| 52 | caca_font_t *f; |
---|
[1840] | 53 | char const * const * fonts; |
---|
| 54 | uint8_t *img; |
---|
| 55 | unsigned int w, h, x, y; |
---|
| 56 | int ret, i, max; |
---|
| 57 | |
---|
[2821] | 58 | /* Load a libcaca internal font */ |
---|
| 59 | fonts = caca_get_font_list(); |
---|
[1840] | 60 | if(fonts[FONT] == NULL) |
---|
| 61 | { |
---|
[2821] | 62 | fprintf(stderr, "error: libcaca was compiled without any fonts\n"); |
---|
[1840] | 63 | return -1; |
---|
| 64 | } |
---|
[2821] | 65 | f = caca_load_font(fonts[FONT], 0); |
---|
[1840] | 66 | if(f == NULL) |
---|
| 67 | { |
---|
| 68 | fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]); |
---|
| 69 | return -1; |
---|
| 70 | } |
---|
| 71 | |
---|
[2821] | 72 | cv = caca_create_canvas(1, 1); |
---|
| 73 | caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK); |
---|
[1840] | 74 | |
---|
| 75 | /* Create our bitmap buffer (32-bit ARGB) */ |
---|
[2821] | 76 | w = caca_get_font_width(f); |
---|
| 77 | h = caca_get_font_height(f); |
---|
[1840] | 78 | img = malloc(4 * w * h); |
---|
| 79 | |
---|
| 80 | /* Zero our structures */ |
---|
| 81 | for(y = 0; y < DY; y++) |
---|
| 82 | for(x = 0; x < DX; x++) |
---|
| 83 | count[x][y] = 0; |
---|
| 84 | |
---|
| 85 | for(y = 0; y < h; y++) |
---|
| 86 | for(x = 0; x < w; x++) |
---|
| 87 | count[x * DX / w][y * DY / h]++; |
---|
| 88 | |
---|
| 89 | for(i = 0x20; i < GLYPHS; i++) |
---|
| 90 | for(y = 0; y < DY; y++) |
---|
| 91 | for(x = 0; x < DX; x++) |
---|
[2397] | 92 | total[i][x][y] = 0; |
---|
[1840] | 93 | |
---|
| 94 | /* Draw all glyphs and count their pixels */ |
---|
| 95 | for(i = 0x20; i < GLYPHS; i++) |
---|
| 96 | { |
---|
[2821] | 97 | caca_put_char(cv, 0, 0, i); |
---|
[1840] | 98 | |
---|
| 99 | /* Render the canvas onto our image buffer */ |
---|
[2821] | 100 | caca_render_canvas(cv, f, img, w, h, 4 * w); |
---|
[1840] | 101 | |
---|
| 102 | for(y = 0; y < h * DY; y++) |
---|
| 103 | for(x = 0; x < w * DX; x++) |
---|
| 104 | total[i][x / w][y / h] |
---|
| 105 | += img[(w * (y / DY) + (x / DX)) * 4 + 1]; |
---|
| 106 | } |
---|
| 107 | |
---|
[1841] | 108 | /* Compute max pixel value */ |
---|
[1840] | 109 | max = 0; |
---|
| 110 | for(i = 0x20; i < GLYPHS; i++) |
---|
| 111 | for(y = 0; y < DY; y++) |
---|
| 112 | for(x = 0; x < DX; x++) |
---|
| 113 | { |
---|
| 114 | int val = total[i][x][y] * 256 / count[x][y]; |
---|
| 115 | if(val > max) |
---|
| 116 | max = val; |
---|
| 117 | } |
---|
| 118 | |
---|
[1841] | 119 | /* Compute bits for all glyphs */ |
---|
[1840] | 120 | for(i = 0x20; i < GLYPHS; i++) |
---|
| 121 | { |
---|
| 122 | int bits = 0; |
---|
| 123 | |
---|
| 124 | if(i >= 0x7f && i <= 0x9f) |
---|
[1841] | 125 | { |
---|
| 126 | allbits[i] = 0; |
---|
[1840] | 127 | continue; |
---|
[1841] | 128 | } |
---|
[1840] | 129 | |
---|
| 130 | for(y = 0; y < DY; y++) |
---|
| 131 | { |
---|
| 132 | for(x = 0; x < DX; x++) |
---|
| 133 | { |
---|
| 134 | int t = total[i][x][y] * 16 * 256 / (count[x][y] * max); |
---|
| 135 | bits *= RANGE; |
---|
[1841] | 136 | bits |= curve[t] / (16 / RANGE); |
---|
[1840] | 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
[1841] | 140 | allbits[i] = bits; |
---|
[1840] | 141 | } |
---|
| 142 | |
---|
[1841] | 143 | /* Find a glyph for all combinations */ |
---|
| 144 | for(i = 0; i < FULLRANGE; i++) |
---|
| 145 | { |
---|
| 146 | int j, mindist = 0x1000, best = 0; |
---|
| 147 | |
---|
| 148 | for(j = 0x20; j < GLYPHS; j++) |
---|
| 149 | { |
---|
| 150 | int d = distance(i, allbits[j]); |
---|
| 151 | if(d < mindist) |
---|
| 152 | { |
---|
| 153 | best = j; |
---|
| 154 | mindist = d; |
---|
| 155 | if(d == 0) |
---|
| 156 | break; |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | bestchar[i] = best; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /* Print results */ |
---|
| 164 | printf("/* Generated by sortchars.c */\n"); |
---|
[1871] | 165 | printf("static char const cells_to_ascii[%i] =\n{\n ", FULLRANGE); |
---|
[1841] | 166 | for(i = 0; i < FULLRANGE; i++) |
---|
| 167 | { |
---|
[2821] | 168 | ret = caca_utf32_to_utf8(utf8, bestchar[i]); |
---|
[1841] | 169 | utf8[ret] = '\0'; |
---|
| 170 | printf("%i, ", bestchar[i]); |
---|
| 171 | if((i % 16) == 15 && i != FULLRANGE - 1) |
---|
| 172 | printf("\n "); |
---|
| 173 | } |
---|
[1871] | 174 | printf("\n};\n\n"); |
---|
| 175 | printf("static uint16_t const ascii_to_cells[%i] =\n{\n ", GLYPHS); |
---|
| 176 | for(i = 0; i < GLYPHS; i++) |
---|
| 177 | { |
---|
[2821] | 178 | ret = caca_utf32_to_utf8(utf8, bestchar[i]); |
---|
[1871] | 179 | utf8[ret] = '\0'; |
---|
| 180 | printf("0x%03x, ", allbits[i]); |
---|
| 181 | if((i % 8) == 7 && i != GLYPHS - 1) |
---|
| 182 | printf("\n "); |
---|
| 183 | } |
---|
[1841] | 184 | printf("\n};\n"); |
---|
| 185 | |
---|
[2821] | 186 | caca_free_canvas(cv); |
---|
[1840] | 187 | |
---|
[1842] | 188 | testcircle(); |
---|
| 189 | |
---|
[1840] | 190 | return 0; |
---|
| 191 | } |
---|
| 192 | |
---|
[1842] | 193 | static int distance(uint16_t mychar, uint16_t x) |
---|
[1841] | 194 | { |
---|
| 195 | int i, d = 0; |
---|
| 196 | |
---|
| 197 | for(i = 0; i < DX * DY; i++) |
---|
| 198 | { |
---|
[1842] | 199 | int t = (int)(mychar & (RANGE - 1)) - (int)(x & (RANGE - 1)); |
---|
| 200 | d += t > 0 ? 1 * t : -2 * t; |
---|
| 201 | mychar /= RANGE; |
---|
| 202 | x /= RANGE; |
---|
[1841] | 203 | } |
---|
| 204 | |
---|
| 205 | return d; |
---|
| 206 | } |
---|
| 207 | |
---|
[1842] | 208 | #define WIDTH 40 |
---|
| 209 | #define HEIGHT 18 |
---|
| 210 | |
---|
| 211 | static void testcircle(void) |
---|
| 212 | { |
---|
| 213 | char utf8[7]; |
---|
| 214 | uint8_t *buf = malloc(256 * 256); |
---|
| 215 | uint16_t *dst = malloc(WIDTH * DX * HEIGHT * DY * sizeof(uint16_t)); |
---|
| 216 | int x, y, ret; |
---|
| 217 | |
---|
| 218 | memset(buf, 0, 256 * 256); |
---|
| 219 | memset(dst, 0, WIDTH * DX * HEIGHT * DY); |
---|
| 220 | |
---|
| 221 | /* Fill image */ |
---|
| 222 | for(y = 0; y < 256; y++) |
---|
| 223 | for(x = 0; x < 256; x++) |
---|
| 224 | { |
---|
| 225 | int dist2 = (x - 128) * (x - 128) + (y - 128) * (y - 128); |
---|
| 226 | if(dist2 < 25000 && dist2 > 18000) |
---|
| 227 | buf[y * 256 + x] = 255; |
---|
| 228 | else if(dist2 < 14000 && dist2 > 9000) |
---|
| 229 | buf[y * 256 + x] = 204; |
---|
| 230 | else if(dist2 < 6000 && dist2 > 3000) |
---|
| 231 | buf[y * 256 + x] = 153; |
---|
| 232 | else if(dist2 < 1600 && dist2 > 300) |
---|
| 233 | buf[y * 256 + x] = 102; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /* Parse image */ |
---|
| 237 | for(y = 0; y < HEIGHT * DY; y++) |
---|
| 238 | for(x = 0; x < WIDTH * DX; x++) |
---|
| 239 | dst[y * WIDTH * DX + x] = (int)buf[(y * 256 / (HEIGHT * DY)) * 256 + (x * 256 / (WIDTH * DX))] * RANGE / 256; |
---|
| 240 | |
---|
[1871] | 241 | printf("/* example:\n"); |
---|
[1842] | 242 | for(y = 0; y < HEIGHT; y++) |
---|
| 243 | { |
---|
| 244 | for(x = 0; x < WIDTH; x++) |
---|
| 245 | { |
---|
| 246 | uint16_t bits = 0; |
---|
| 247 | int i, j; |
---|
| 248 | for(j = 0; j < DY; j++) |
---|
| 249 | for(i = 0; i < DX; i++) |
---|
| 250 | { |
---|
| 251 | bits *= RANGE; |
---|
| 252 | bits |= dst[(y * DY + j) * WIDTH * DX + x * DX + i]; |
---|
| 253 | } |
---|
| 254 | |
---|
[2821] | 255 | ret = caca_utf32_to_utf8(utf8, bestchar[bits]); |
---|
[1842] | 256 | utf8[ret] = '\0'; |
---|
| 257 | printf("%s", utf8); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | printf("\n"); |
---|
| 261 | } |
---|
[1871] | 262 | printf("*/\n"); |
---|
[1842] | 263 | } |
---|
| 264 | |
---|