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