Debian Codegraph @

tools/sortchars.c

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