-
Notifications
You must be signed in to change notification settings - Fork 0
/
icons.py
195 lines (166 loc) · 6.71 KB
/
icons.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""win32 icons
"""
import itertools
# John Hornick, "Icons in Win32", September 29, 1995
# http://msdn.microsoft.com/en-us/library/ms997538.aspx
# The Old New Thing, "The format of icon resources"
# http://blogs.msdn.com/b/oldnewthing/archive/2012/07/20/10331787.aspx
from . import _wapi
# dwBytesInRes is the size of the ICONIMAGE blob, dwImageOffset is the
# offset in the file or resource where the ICONIMAGE blob starts.
class ICONDIRENTRY(_wapi.Structure):
_pack_ = 2
_fields_ = [("bWidth", _wapi.BYTE),
("bHeight", _wapi.BYTE),
("bColorCount", _wapi.BYTE),
("bReserved", _wapi.BYTE),
("wPlanes", _wapi.WORD),
("wBitCount", _wapi.WORD),
("dwBytesInRes", _wapi.DWORD),
("dwImageOffset", _wapi.DWORD)]
# An .ico file starts with an ICONDIRHEADER which contains idCount
# ICONDIRENTRY structures that describe the one or more icon images in
# the file.
class ICONDIRHEADER(_wapi.Structure):
_pack_ = 2
_fields_ = [("idReserved", _wapi.WORD), # Must be 0
("idType", _wapi.WORD), # Should check that this is 1 for icons
("idCount", _wapi.WORD), # Number os ICONDIRENTRYs to follow
## ("idEntries", ICONDIRENTRY * 0)
]
@classmethod
def readfrom(cls, fileobj):
header = cls()
fileobj.readinto(header)
header.idEntries = []
for i in range(header.idCount):
entry = ICONDIRENTRY()
fileobj.readinto(entry)
header.idEntries.append(entry)
header.iconimages = []
for entry in header.idEntries:
fileobj.seek(entry.dwImageOffset)
image = fileobj.read(entry.dwBytesInRes)
header.iconimages.append(image)
return header
################################################################
# The RT_GROUP_ICON resource in the exe/dll file is a GRPICONDIR
# structure (see below). The GRPICONDIR structure contains a
# GRPICONDIRENTRY for each icon, the nID members are the resource ids
# of the corresponding RT_ICON resources which are ICONIMAGE
# structures.
class GRPICONDIRENTRY(_wapi.Structure):
_pack_ = 2
_fields_ = [("bWidth", _wapi.BYTE),
("bHeight", _wapi.BYTE),
("bColorCount", _wapi.BYTE),
("bReserved", _wapi.BYTE),
("wPlanes", _wapi.WORD),
("wBitCount", _wapi.WORD),
("dwBytesInRes", _wapi.DWORD),
# nID is the resource id of the RT_ICON resource
("nID", _wapi.WORD)]
def CreateGrpIconDirHeader(iconheader, id_generator):
"""Create a GRPICONDIRHEADER from an ICONDIRHEADER.
"""
# ctypes doesn't support variable type structures; so we
# create one of the size we want NOW.
class GRPICONDIRHEADER(_wapi.Structure):
_pack_ = 2
_fields_ = [("idReserved", _wapi.WORD),
("idType", _wapi.WORD),
("idCount", _wapi.WORD),
("idEntries", GRPICONDIRENTRY * iconheader.idCount)]
def tobytes(self):
return memoryview(self).tobytes()
if iconheader.idCount > 10:
raise ValueError("too many images for this icon: %d" % iconheader.idCount)
grpheader = GRPICONDIRHEADER(
idReserved = iconheader.idReserved,
idType = iconheader.idType,
idCount = iconheader.idCount)
for i in range(iconheader.idCount):
dst = grpheader.idEntries[i]
src = iconheader.idEntries[i]
dst.bWidth = src.bWidth
dst.bHeight = src.bHeight
dst.bColorCount = src.bColorCount
dst.bReserved = src.bReserved
dst.wPlanes = src.wPlanes
dst.wBitCount = src.wBitCount
dst.dwBytesInRes = src.dwBytesInRes
dst.nID = next(id_generator)
if dst.nID > 32767:
raise ValueError("Invalid resource id %s" % dst.nID)
return grpheader
################################################################
def BuildIcons(icon_resources):
"""Create RT_ICON and RT_GROUP_ICON resources from a list of
(icon id, icon file) tuples.
"""
result = []
id_generator = itertools.count(10)
for resource_id, iconpath in icon_resources:
with open(iconpath, "rb") as iconfile:
header = ICONDIRHEADER.readfrom(iconfile)
grp_header = CreateGrpIconDirHeader(header, id_generator)
for i, entry in enumerate(grp_header.idEntries):
# type, name, data
result.append((_wapi.RT_ICON, entry.nID, header.iconimages[i]))
result.append((_wapi.RT_GROUP_ICON, resource_id, grp_header.tobytes()))
return result
################################################################
if __name__ == "__main__":
with open("128.ico", "rb") as ifi:
hdr = ICONDIRHEADER.readfrom(ifi)
print(hdr.iconimages)
## CreateGrpIconDirHeader(hdr, 42)
print(hdr.idType, hdr.idCount)
for entry in hdr.idEntries:
print("ENTRY", entry.bWidth, entry.bHeight, entry.bColorCount,
entry.wPlanes, entry.wBitCount,
entry.dwBytesInRes,
entry.dwImageOffset)
ifi.seek(entry.dwImageOffset)
iconimage = ifi.read(entry.dwBytesInRes)
assert len(iconimage) == entry.dwBytesInRes
print(ifi.tell())
"""
/* Each RT_ICON resource in an image file (containing the icon for one
specific resolution and number of colors) must have a unique id, and
the id must be in the GRPICONDIRHEADER's nID member.
So, we use a *static* variable rt_icon_id which is incremented for each
RT_ICON resource and written into the GRPICONDIRHEADER's nID member.
XXX Do we need a way to reset the rt_icon_id variable to zero? If we
are building a lot of images in one setup script?
*/
for (i = 0; i < pidh->idCount; ++i) {
pgidh->idEntries[i].nID = rt_icon_id++;
}
"""
"""
if (!pfn_UpdateResource(hUpdate,
(Py_UNICODE *)MAKEINTRESOURCE(RT_GROUP_ICON),
(Py_UNICODE *)MAKEINTRESOURCE(icoid),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
pgidh,
gidh_size)) {
SystemError(GetLastError(), "UpdateResource");
goto failed;
}
for (i = 0; i < pidh->idCount; ++i) {
char *cp = &icodata[pidh->idEntries[i].dwImageOffset];
int cBytes = pidh->idEntries[i].dwBytesInRes;
if (!pfn_UpdateResource(hUpdate,
(Py_UNICODE *)MAKEINTRESOURCE(RT_ICON),
(Py_UNICODE *)MAKEINTRESOURCE(pgidh->idEntries[i].nID),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
cp,
cBytes)) {
SystemError(GetLastError(), "UpdateResource");
goto failed;
}
}
"""