File: sortchars.c

package info (click to toggle)
libcaca 0.99.beta19-2.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 7,436 kB
  • sloc: ansic: 22,372; python: 2,316; cs: 1,213; cpp: 1,125; java: 916; objc: 836; makefile: 606; sh: 457; ruby: 193; asm: 65
file content (265 lines) | stat: -rw-r--r-- 6,781 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 *  sortchars     analyse ASCII characters
 *  Copyright (c) 2007-2014 Sam Hocevar <sam@hocevar.net>
 *                All Rights Reserved
 *
 *
 *  This program is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What the Fuck You Want
 *  to Public License, Version 2, as published by Sam Hocevar. See
 *  http://www.wtfpl.net/ for more details.
 */

#include "config.h"

#if !defined(__KERNEL__)
#   include <stdio.h>
#   include <string.h>
#   include <stdlib.h>
#endif

#include "caca.h"

#define GLYPHS 0x7f
#define FONT 0 /* 0 or 1 */
#define DX 2
#define DY 3
#define RANGEBITS 2
#define RANGE (1 << RANGEBITS)
#define FULLRANGE (1 << (RANGEBITS * DX * DY))

int total[GLYPHS][DX][DY];
int16_t allbits[GLYPHS];
int bestchar[FULLRANGE];

static int curve[17] = /* 17 instead of 16 */
{
    0, 4, 6, 8, 9, 10, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15
    //0, 3, 5, 7, 8, 9, 10, 11, 12, 13, 13, 14, 14, 15, 15, 15, 15
    //0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15
};

static int distance(uint16_t, uint16_t);
static void testcircle(void);

int main(int argc, char *argv[])
{
    int count[DX][DY];
    char utf8[7];
    caca_canvas_t *cv;
    caca_font_t *f;
    char const * const * fonts;
    uint8_t *img;
    unsigned int w, h, x, y;
    int ret, i, max;

    /* Load a libcaca internal font */
    fonts = caca_get_font_list();
    if(fonts[FONT] == NULL)
    {
        fprintf(stderr, "error: libcaca was compiled without any fonts\n");
        return -1;
    }
    f = caca_load_font(fonts[FONT], 0);
    if(f == NULL)
    {
        fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
        return -1;
    }

    cv = caca_create_canvas(1, 1);
    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);

    /* Create our bitmap buffer (32-bit ARGB) */
    w = caca_get_font_width(f);
    h = caca_get_font_height(f);
    img = malloc(4 * w * h);

    /* Zero our structures */
    for(y = 0; y < DY; y++)
        for(x = 0; x < DX; x++)
            count[x][y] = 0;

    for(y = 0; y < h; y++)
        for(x = 0; x < w; x++)
            count[x * DX / w][y * DY / h]++;

    for(i = 0x20; i < GLYPHS; i++)
        for(y = 0; y < DY; y++)
            for(x = 0; x < DX; x++)
                total[i][x][y] = 0;

    /* Draw all glyphs and count their pixels */
    for(i = 0x20; i < GLYPHS; i++)
    {
        caca_put_char(cv, 0, 0, i);

        /* Render the canvas onto our image buffer */
        caca_render_canvas(cv, f, img, w, h, 4 * w);

        for(y = 0; y < h * DY; y++)
            for(x = 0; x < w * DX; x++)
                total[i][x / w][y / h]
                  += img[(w * (y / DY) + (x / DX)) * 4 + 1];
    }

    /* Compute max pixel value */
    max = 0;
    for(i = 0x20; i < GLYPHS; i++)
        for(y = 0; y < DY; y++)
            for(x = 0; x < DX; x++)
            {
                int val = total[i][x][y] * 256 / count[x][y];
                if(val > max)
                    max = val;
            }

    /* Compute bits for all glyphs */
    for(i = 0x20; i < GLYPHS; i++)
    {
        int bits = 0;

        if(i >= 0x7f && i <= 0x9f)
        {
            allbits[i] = 0;
            continue;
        }

        for(y = 0; y < DY; y++)
        {
            for(x = 0; x < DX; x++)
            {
                int t = total[i][x][y] * 16 * 256 / (count[x][y] * max);
                bits *= RANGE;
                bits |= curve[t] / (16 / RANGE);
            }
        }

        allbits[i] = bits;
    }

    /* Find a glyph for all combinations */
    for(i = 0; i < FULLRANGE; i++)
    {
        int j, mindist = 0x1000, best = 0;

        for(j = 0x20; j < GLYPHS; j++)
        {
            int d = distance(i, allbits[j]);
            if(d < mindist)
            {
                best = j;
                mindist = d;
                if(d == 0)
                    break;
            }
        }

        bestchar[i] = best;
    }

    /* Print results */
    printf("/* Generated by sortchars.c */\n");
    printf("static char const cells_to_ascii[%i] =\n{\n    ", FULLRANGE);
    for(i = 0; i < FULLRANGE; i++)
    {
        ret = caca_utf32_to_utf8(utf8, bestchar[i]);
        utf8[ret] = '\0';
        printf("%i, ", bestchar[i]);
        if((i % 16) == 15 && i != FULLRANGE - 1)
            printf("\n    ");
    }
    printf("\n};\n\n");
    printf("static uint16_t const ascii_to_cells[%i] =\n{\n    ", GLYPHS);
    for(i = 0; i < GLYPHS; i++)
    {
        ret = caca_utf32_to_utf8(utf8, bestchar[i]);
        utf8[ret] = '\0';
        printf("0x%03x, ", allbits[i]);
        if((i % 8) == 7 && i != GLYPHS - 1)
            printf("\n    ");
    }
    printf("\n};\n");

    caca_free_canvas(cv);

    testcircle();

    return 0;
}

static int distance(uint16_t mychar, uint16_t x)
{
    int i, d = 0;

    for(i = 0; i < DX * DY; i++)
    {
        int t = (int)(mychar & (RANGE - 1)) - (int)(x & (RANGE - 1));
        d += t > 0 ? 1 * t : -2 * t;
        mychar /= RANGE;
        x /= RANGE;
    }

    return d;
}

#define WIDTH 40
#define HEIGHT 18

static void testcircle(void)
{
    char utf8[7];
    uint8_t *buf = malloc(256 * 256);
    uint16_t *dst = malloc(WIDTH * DX * HEIGHT * DY * sizeof(uint16_t));
    int x, y, ret;

    memset(buf, 0, 256 * 256);
    memset(dst, 0, WIDTH * DX * HEIGHT * DY);

    /* Fill image */
    for(y = 0; y < 256; y++)
        for(x = 0; x < 256; x++)
        {
            int dist2 = (x - 128) * (x - 128) + (y - 128) * (y - 128);
            if(dist2 < 25000 && dist2 > 18000)
                buf[y * 256 + x] = 255;
            else if(dist2 < 14000 && dist2 > 9000)
                buf[y * 256 + x] = 204;
            else if(dist2 < 6000 && dist2 > 3000)
                buf[y * 256 + x] = 153;
            else if(dist2 < 1600 && dist2 > 300)
                buf[y * 256 + x] = 102;
        }

    /* Parse image */
    for(y = 0; y < HEIGHT * DY; y++)
        for(x = 0; x < WIDTH * DX; x++)
            dst[y * WIDTH * DX + x] = (int)buf[(y * 256 / (HEIGHT * DY)) * 256 + (x * 256 / (WIDTH * DX))] * RANGE / 256;

    printf("/* example:\n");
    for(y = 0; y < HEIGHT; y++)
    {
        for(x = 0; x < WIDTH; x++)
        {
            uint16_t bits = 0;
            int i, j;
            for(j = 0; j < DY; j++)
                for(i = 0; i < DX; i++)
                {
                    bits *= RANGE;
                    bits |= dst[(y * DY + j) * WIDTH * DX + x * DX + i];
                }

            ret = caca_utf32_to_utf8(utf8, bestchar[bits]);
            utf8[ret] = '\0';
            printf("%s", utf8);
        }

        printf("\n");
    }
    printf("*/\n");
    free(buf);
    free(dst);
}