This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathicon.py
158 lines (132 loc) · 4.77 KB
/
icon.py
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
"""
*******************************************************************************
* Ledger - Non secure firmware
* (c) 2016 Ledger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************
"""
from PIL import Image
import sys
import os
import math
if len(sys.argv) == 2:
if not os.path.exists(sys.argv[1]):
print(sys.argv[0] + ": [<Wmax>] [<Hmax>] <imagefile> [hexbitmaponly]")
print("Error: " + sys.argv[1] + " does not exists !")
sys.exit(-1)
elif len(sys.argv) < 4:
print(sys.argv[0] + ": [<Wmax>] [<Hmax>] <imagefile> [hexbitmaponly]")
sys.exit(-1)
hexbitmaponly = False
if len(sys.argv) == 5:
if sys.argv[4] == "hexbitmaponly":
hexbitmaponly = True
if len(sys.argv) != 2:
widthmax = int(sys.argv[1])
heightmax = int(sys.argv[2])
filename = sys.argv[3]
else:
widthmax = 4096
heightmax = 4096
filename = sys.argv[1]
im = Image.open(filename)
im.load()
width, height = im.size
colors = {}
palette = im.getpalette()
bname = os.path.splitext(os.path.basename(filename))[0]
if width > widthmax:
width = widthmax
if height > heightmax:
height = heightmax
def hexbyte(b):
return hex(0x100 + b)[3:]
maxcolor = 0
for row in range(height):
for col in range(width):
# return an index in the indexed colors list for indexed address spaces
color_index = im.getpixel((col, row))
if color_index > maxcolor:
maxcolor = color_index
if (maxcolor + 1 > 16):
raise Exception("toomuch colors %i" % maxcolor)
# not a pow 2 max color count
if math.pow(2, math.log(maxcolor + 1, 2)) != maxcolor + 1:
maxcolor = int(math.pow(2, math.log(maxcolor + 1, 2) + 1))
# enforce if lower
# maxcolor=15
bits_per_pixel = int(math.log(maxcolor + 1, 2))
# display color array
if not hexbitmaponly:
sys.stdout.write("""#define GLYPH_""" + bname + """_WIDTH """ + str(width) + """
""")
sys.stdout.write("""#define GLYPH_""" + bname + """_HEIGHT """ + str(height) + """
""")
sys.stdout.write("""#define GLYPH_""" + bname + """_BPP """ + str(bits_per_pixel) + """
""")
sys.stdout.write("""unsigned int const C_""" + bname + """_colors[] = {
""");
# color index encoding
for i in range(maxcolor + 1):
sys.stdout.write(
" 0x00" + hexbyte(palette[i * 3]) + hexbyte(palette[i * 3 + 1]) + hexbyte(palette[i * 3 + 2]) + ", \n")
sys.stdout.write("""};
unsigned char const C_""" + bname + """_bitmap[] = {\n """)
else:
# write BPP
sys.stdout.write(hexbyte(int(math.log(maxcolor + 1, 2))))
# write clor table U4BE for each
for i in range(maxcolor + 1):
sys.stdout.write("00" + hexbyte(palette[i * 3]) + hexbyte(palette[i * 3 + 1]) + hexbyte(palette[i * 3 + 2]))
current_byte = 0
current_bit = 0
byte_count = 0
# packed, row preferred
for row in range(height):
# row first
for col in range(width):
# return an index in the indexed colors list for indexed address spaces
# left to right
# perform implicit rotation here (0,0 is left top in bagl, and generally left bottom for various canvas)
color_index = im.getpixel((col, row))
# le encoded
current_byte += (color_index << current_bit)
current_bit += bits_per_pixel
if current_bit >= 8:
if not hexbitmaponly:
sys.stdout.write("0x" + hexbyte(current_byte) + ", ")
byte_count += 1
if byte_count >= 16:
byte_count = 0
sys.stdout.write("\n ")
else:
sys.stdout.write(hexbyte(current_byte))
current_bit = 0
current_byte = 0
# print last byte if any
if current_bit > 0:
if not hexbitmaponly:
sys.stdout.write("0x" + hexbyte(current_byte) + ", ")
byte_count += 1
if byte_count >= 16:
byte_count = 0
sys.stdout.write("\n ")
else:
sys.stdout.write(hexbyte(current_byte))
if not hexbitmaponly:
# origin 0,0 is left top for blue, instead of left bottom for all image encodings
sys.stdout.write("""};
{ """ + str(width) + """, """ + str(height) + """, """ + str(
int(math.log(maxcolor + 1, 2))) + """, C_""" + bname + """_colors, C_""" + bname + """_bitmap},
""")