-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiOS-PNG-Normalizer.py
More file actions
183 lines (145 loc) · 4.94 KB
/
Copy pathiOS-PNG-Normalizer.py
File metadata and controls
183 lines (145 loc) · 4.94 KB
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
'''
iOS-PNG-Normalizer v2.0
By CNDY
https://github.com/CNDY1390/iOS-PNG-Normalizer
This program is free software.
License
GNU3.0
General Public License v3.0
'''
from struct import unpack, pack
from zlib import decompress, compress, crc32
import stat
import sys
import os
def getNormalizedPNG(filename):
pngheader = b"\x89PNG\r\n\x1a\n"
with open(filename, "rb") as file:
oldPNG = file.read()
if oldPNG[:8] != pngheader:
return None
newPNG = oldPNG[:8]
chunkPos = len(newPNG)
idatAcc = b""
breakLoop = False
# For each chunk in the PNG file
while chunkPos < len(oldPNG):
skip = False
# Reading chunk
chunkLength = oldPNG[chunkPos:chunkPos+4]
chunkLength = unpack(">L", chunkLength)[0]
chunkType = oldPNG[chunkPos+4:chunkPos+8]
chunkData = oldPNG[chunkPos+8:chunkPos+8+chunkLength]
chunkCRC = oldPNG[chunkPos+chunkLength+8:chunkPos+chunkLength+12]
chunkCRC = unpack(">L", chunkCRC)[0]
chunkPos += chunkLength + 12
# Parsing the header chunk
if chunkType == b"IHDR":
width = unpack(">L", chunkData[0:4])[0]
height = unpack(">L", chunkData[4:8])[0]
# Parsing the image chunk
if chunkType == b"IDAT":
# Store the chunk data for later decompression
idatAcc += chunkData
skip = True
# Removing CgBI chunk
if chunkType == b"CgBI":
skip = True
# Add all accumulated IDATA chunks
if chunkType == b"IEND":
try:
# Uncompressing the image chunk
bufSize = width * height * 4 + height
chunkData = decompress(idatAcc, -15, bufSize)
except Exception as e:
# The PNG image is normalized
print(e)
return None
chunkType = b"IDAT"
# Swapping red & blue bytes for each pixel
newdata = []
for y in range(height):
i = len(newdata)
newdata.append(chunkData[i:i+1])
for x in range(width):
i = len(newdata)
newdata.append(chunkData[i+2:i+3])
newdata.append(chunkData[i+1:i+2])
newdata.append(chunkData[i:i+1])
newdata.append(chunkData[i+3:i+4])
# Compressing the image chunk
chunkData = b''.join(newdata)
chunkData = compress(chunkData)
chunkLength = len(chunkData)
chunkCRC = crc32(chunkType)
chunkCRC = crc32(chunkData, chunkCRC)
chunkCRC = (chunkCRC + 0x100000000) % 0x100000000
breakLoop = True
if not skip:
newPNG += pack(">L", chunkLength)
newPNG += chunkType
if chunkLength > 0:
newPNG += chunkData
newPNG += pack(">L", chunkCRC)
if breakLoop:
break
return newPNG
def updatePNG(filename):
data = getNormalizedPNG(filename)
if data is not None:
with open(filename, "wb") as file:
file.write(data)
return True
return data
def getFiles(base):
global _dirs
global _pngs
if base == ".":
_dirs = []
_pngs = []
if base in _dirs:
return
files = os.listdir(base)
for file in files:
filepath = os.path.join(base, file)
try:
st = os.lstat(filepath)
except os.error:
continue
if stat.S_ISDIR(st.st_mode):
if filepath not in _dirs:
getFiles(filepath)
_dirs.append(filepath)
elif file[-4:].lower() == ".png":
if filepath not in _pngs:
_pngs.append(filepath)
if base == ".":
return _dirs, _pngs
print("-------------------------")
print(" iOS PNG Normalizer v2.0 ")
print("-------------------------")
print(" ")
print("[+] Searching PNG files...", end=' ')
dirs, pngs = getFiles(".")
print("ok")
if len(pngs) == 0:
print(" ")
print("[!] Alert: There are no PNG files found. Move this python file to the folder that contains the PNG files to normalize.")
sys.exit()
print(" ")
print(" - %d PNG files were found at this folder (and subfolders)." % len(pngs))
print(" ")
while True:
normalize = input(
"[?] Do you want to normalize all images (Y/N)? ").lower()
if len(normalize) > 0 and (normalize[0] == "y" or normalize[0] == "n"):
break
normalized = 0
if normalize[0] == "y":
for ipng in range(len(pngs)):
perc = (float(ipng) / len(pngs)) * 100.0
print("%.2f%% %s" % (perc, pngs[ipng]))
if updatePNG(pngs[ipng]):
normalized += 1
print(" ")
print("[+] %d PNG files were normalized." % normalized)