Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

254 Zeilen
6.3 KiB

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