Просмотр исходного кода

* Updates to sortchars.c so that it can output a proper LUT.

tags/v0.99.beta14
Sam Hocevar sam 17 лет назад
Родитель
Сommit
7e355ca89c
1 измененных файлов: 62 добавлений и 10 удалений
  1. +62
    -10
      tools/sortchars.c

+ 62
- 10
tools/sortchars.c Просмотреть файл

@@ -28,15 +28,21 @@
#define FONT 1 /* 0 or 1 */
#define DX 2
#define DY 3
#define RANGE 4
#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
};

static int distance(uint16_t, uint16_t);

int main(int argc, char *argv[])
{
int count[DX][DY];
@@ -98,7 +104,7 @@ int main(int argc, char *argv[])
+= img[(w * (y / DY) + (x / DX)) * 4 + 1];
}

/* Compute max value */
/* Compute max pixel value */
max = 0;
for(i = 0x20; i < GLYPHS; i++)
for(y = 0; y < DY; y++)
@@ -109,34 +115,80 @@ int main(int argc, char *argv[])
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;

ret = cucul_utf32_to_utf8(utf8, i);
utf8[ret] = '\0';
}

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

printf("%d ", t);
bits *= RANGE;
bits |= t;
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;
}
}

printf(" 0x%04d (%s)\n", bits, utf8);
bestchar[i] = best;
}

/* Print results */
printf("/* Generated by sortchars.c */\n");
printf("static char const lookup_ascii[%i] =\n{\n ", FULLRANGE);
for(i = 0; i < FULLRANGE; i++)
{
ret = cucul_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");

cucul_free_canvas(cv);

return 0;
}

static int distance(uint16_t a, uint16_t b)
{
int i, d = 0;

for(i = 0; i < DX * DY; i++)
{
int x = (int)(a & (RANGE - 1)) - (int)(b & (RANGE - 1));
d += x * x;
a /= RANGE;
b /= RANGE;
}

return d;
}


Загрузка…
Отмена
Сохранить