source: libcaca/trunk/tools/sortchars.c @ 1840

Last change on this file since 1840 was 1840, checked in by Sam Hocevar, 13 years ago
  • Small tool to analyse character glyphs depending on their shape. Will be used for improved dithering.
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1/*
2 *  sortchars     analyse ASCII characters
3 *  Copyright (c) 2007 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: sortchars.c 1840 2007-10-22 08:47:05Z 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 1 /* 0 or 1 */
29#define DX 2
30#define DY 3
31#define RANGE 4
32
33int total[GLYPHS][DX][DY];
34
35static int curve[17] = /* 17 instead of 16 */
36{
37    0, 4, 6, 8, 9, 10, 11, 12, 12, 13, 13, 14, 14, 15, 15, 15, 15
38};
39
40int main(int argc, char *argv[])
41{
42    int count[DX][DY];
43    char utf8[7];
44    cucul_canvas_t *cv;
45    cucul_font_t *f;
46    char const * const * fonts;
47    uint8_t *img;
48    unsigned int w, h, x, y;
49    int ret, i, max;
50
51    /* Load a libcucul internal font */
52    fonts = cucul_get_font_list();
53    if(fonts[FONT] == NULL)
54    {
55        fprintf(stderr, "error: libcucul was compiled without any fonts\n");
56        return -1;
57    }
58    f = cucul_load_font(fonts[FONT], 0);
59    if(f == NULL)
60    {
61        fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
62        return -1;
63    }
64
65    cv = cucul_create_canvas(1, 1);
66    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
67
68    /* Create our bitmap buffer (32-bit ARGB) */
69    w = cucul_get_font_width(f);
70    h = cucul_get_font_height(f);
71    img = malloc(4 * w * h);
72
73    /* Zero our structures */
74    for(y = 0; y < DY; y++)
75        for(x = 0; x < DX; x++)
76            count[x][y] = 0;
77
78    for(y = 0; y < h; y++)
79        for(x = 0; x < w; x++)
80            count[x * DX / w][y * DY / h]++;
81
82    for(i = 0x20; i < GLYPHS; i++)
83        for(y = 0; y < DY; y++)
84            for(x = 0; x < DX; x++)
85                total[GLYPHS][x][y] = 0;
86
87    /* Draw all glyphs and count their pixels */
88    for(i = 0x20; i < GLYPHS; i++)
89    {
90        cucul_put_char(cv, 0, 0, i);
91
92        /* Render the canvas onto our image buffer */
93        cucul_render_canvas(cv, f, img, w, h, 4 * w);
94
95        for(y = 0; y < h * DY; y++)
96            for(x = 0; x < w * DX; x++)
97                total[i][x / w][y / h]
98                  += img[(w * (y / DY) + (x / DX)) * 4 + 1];
99    }
100
101    /* Compute max value */
102    max = 0;
103    for(i = 0x20; i < GLYPHS; i++)
104        for(y = 0; y < DY; y++)
105            for(x = 0; x < DX; x++)
106            {
107                int val = total[i][x][y] * 256 / count[x][y];
108                if(val > max)
109                    max = val;
110            }
111
112    for(i = 0x20; i < GLYPHS; i++)
113    {
114        int bits = 0;
115
116        if(i >= 0x7f && i <= 0x9f)
117            continue;
118
119        ret = cucul_utf32_to_utf8(utf8, i);
120        utf8[ret] = '\0';
121
122        for(y = 0; y < DY; y++)
123        {
124            for(x = 0; x < DX; x++)
125            {
126                int t = total[i][x][y] * 16 * 256 / (count[x][y] * max);
127                t = curve[t] / (16 / RANGE);
128
129                printf("%d ", t);
130                bits *= RANGE;
131                bits |= t;
132            }
133        }
134
135        printf(" 0x%04d (%s)\n", bits, utf8);
136    }
137
138    cucul_free_canvas(cv);
139
140    return 0;
141}
142
Note: See TracBrowser for help on using the repository browser.