|
5 | 5 | """
|
6 | 6 | import png
|
7 | 7 |
|
8 |
| -palette = [(0xff, 0xff, 0xff, i) for i in range(1, 255)] |
| 8 | +palette = [(0xff, 0xff, 0xff, i) for i in range(255, -1, -1)] |
9 | 9 |
|
10 |
| -def ungamma(x: int) -> int: |
11 |
| - """Terrible ungamma, [0, 256)""" |
12 |
| - a = 0.055 |
13 |
| - srgb = x / 255.0 |
14 |
| - if srgb <= 0.04045: |
15 |
| - return int(srgb / 12.92 * 255) |
16 |
| - else: |
17 |
| - return int(((srgb + a) / (1 + a)) ** 2.4 * 255) |
18 |
| - |
19 |
| -def read_pgm2(pgmf): |
20 |
| - """A dirty P2 pgm reader for fontgen only.""" |
| 10 | +def read_pgm2(pgmf) -> list: |
| 11 | + """ |
| 12 | + A dirty P2 pgm reader for fontgen only. |
| 13 | + |
| 14 | + Actually returns List[list], but let's not mess that up for now. |
| 15 | + """ |
21 | 16 | assert pgrm.readline() == 'P3\n' # type
|
22 | 17 | assert pgrm.readline()[0] == '#' # comment signature
|
23 | 18 | (w, h) = map(int, pgrm.readline().split())
|
24 | 19 | depth = int(pgmf.readline())
|
25 | 20 | assert depth <= 255
|
26 | 21 |
|
27 |
| - return (w, h, [list(itertools.islice(pgmf, w)) for i in range(h)]) |
| 22 | + return (w, h, [int(i) for i in pgmf]) |
28 | 23 |
|
29 | 24 | def convert(filename):
|
| 25 | + """ |
| 26 | + Takes a pgm and makes it a pingus png font sprite. |
| 27 | + """ |
30 | 28 | global palette
|
31 | 29 | with open(filename) as pgmf:
|
32 | 30 | w, h, pixels = read_pgm2(pgmf)
|
33 | 31 | writer = png.Writer(size=(w,h), palette=palette, compression=9, bitdepth=8)
|
34 | 32 |
|
| 33 | + # We don't need any gamma here: freetype's greyscale output is a coverage map. |
| 34 | + with open(filename[:-4] + '.png', 'wb'): |
| 35 | + writer.write(pixels) |
| 36 | + |
| 37 | +import sys |
| 38 | +convert(sys.argv[1]) |
0 commit comments