-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathspyi.py
More file actions
225 lines (183 loc) · 5.9 KB
/
spyi.py
File metadata and controls
225 lines (183 loc) · 5.9 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import binascii
import zlib
PNG_SIGNATURE = 0x89504e470d0a1a0a
PNG_COLOR_TYPE_RGB = 2
def createImage(refImage, fileName):
encodePGN(refImage, fileName+'.png')
def encodePGN(refImage, fileName):
print "Encoding PNG"
image = open(fileName, 'wb')
width = len(refImage)
height = len(refImage[0])
# Write the PNG Signature
image.write(binascii.unhexlify("%X" % PNG_SIGNATURE))
#Writes the IHDR Chunk
chunkData = ''
chunkData += "%08X" % width #width
chunkData += "%08X" % height #height
chunkData += "%02X" % 8 #bitDepth
chunkData += "%02X" % PNG_COLOR_TYPE_RGB #colorType
chunkData += "%02X" % 0 #compressionMethod
chunkData += "%02X" % 0 #filterMethod
chunkData += "%02X" % 0 #interlaceMethod
chunkData = binascii.unhexlify(chunkData)
writePNGChunk(image, "IHDR", chunkData)
# Write the IDAT Chunk
chunkSize = 0
chunkData = ''
pixData = ''
lastPix = width * height
# Build pixel buffer
for i in xrange(lastPix):
pix = refImage[i%width][i/width]
pixData += '%02X%02X%02X' % pix
# Filter and compress Pixel data
chunkData = filterPixelData(pixData, width)
chunkData = binascii.unhexlify(chunkData)
chunkData = zlib.compress(chunkData)
writePNGChunk(image, "IDAT", chunkData)
# End the PNG
writePNGChunk(image, "IEND", '')
image.close()
print 'Done'
def writePNGChunk(f, chunkType, data):
size = len(data)
f.write(binascii.unhexlify("%08X" % size)) # Chunk Size
chunk = ''
chunk += chunkType
if size > 0 :
chunk += data # Chunk Data
f.write(chunk)
crc = binascii.crc32(chunk) & 0xFFFFFFFF
f.write(binascii.unhexlify("%08X" % crc))
def filterPixelData(data, width):
filterMode = 1
scanlineSize = width*3 # assumes bitDepth of 8
scanIndex = 0
prevStream = '00' * 3 # assumes bitDepth of 8
dataLen = len(data)
fileredData = '01'
for i in xrange(0, dataLen, 2):
byte = int(data[i:i+2], 16)
if scanIndex == scanlineSize:
scanIndex = 0
filterMode = 2
prevScan = data[i-scanlineSize*2:i]
fileredData += '02'
if filterMode == 1:
prevByte = int(prevStream[0:2], 16)
byte -= prevByte
elif filterMode == 2:
byteStart = scanIndex*2
byte -= int(prevScan[byteStart:byteStart+2], 16)
byte %= 256
fileredData += "%02X" % byte
prevStream = prevStream[2:] + data[i:i+2]
scanIndex += 1
return fileredData
# Decoding PNG CODE
# Decoding PNG CODE
# Decoding PNG CODE
def openImage(fileName):
return decodePNG(fileName)
def decodePNG(fileName):
print 'Decoding PNG'
image = open(fileName, 'rb')
sigHex = binascii.hexlify(image.read(8))
if int(sigHex, 16) != PNG_SIGNATURE:
print('ERROR: Only PNG Format is supported. Try a PNG formated image.')
return
chunkType = "Game On"
refImage = []
idata = ''
while chunkType != 'IEND':
chunkSize = int(binascii.hexlify(image.read(4)), 16)
chunkType = image.read(4)
chunk = image.read(chunkSize)
chunkCRC = image.read(4)
if chunkType == "IHDR":
refImage, colorType, bitDepth = createRefImage(chunk)
if colorType != PNG_COLOR_TYPE_RGB:
print('ERROR: Unsuported PNG Color Type: ' + str(colorType) + ' Try another PNG image.')
return
elif chunkType == "IDAT":
idata += chunk
fillRefImage(refImage, idata, bitDepth)
image.close()
print 'Done'
return refImage
def createRefImage(chunk):
# Processes the IHDR chunk and creates a ref image for the PNG
hexData = binascii.hexlify(chunk)
width = int(hexData[0:8], 16)
height = int(hexData[8:16], 16)
bitDepth = int(hexData[16:18], 16)
colorType = int(hexData[18:20], 16)
compressionMethod = int(hexData[20:22], 16)
filterMethod = int(hexData[22:24], 16)
interlaceMethod = int(hexData[24:], 16)
refImage = [x[:] for x in [[(0,0,0)] * height] * width]
print 'Demensions: ' + str((width, height)) + '\tColor Type: ' + str(colorType) + '\tBit Depth: ' + str(bitDepth)
print 'CompressionMethod: ' + str(compressionMethod) + '\tFilterMethod: ' + str(filterMethod) + '\tInterlaceMethod: ' + str(interlaceMethod)
return (refImage, colorType, bitDepth)
def fillRefImage(refImage, chunk, bitDepth):
# Processes the IDAT chunk and creates a ref image for the PNG
scanline = -1
chunk = zlib.decompress(chunk)
hexData = binascii.hexlify(chunk)
width = len(refImage)
hexUnfiltered = ''
filterMode = 0
scanlineSize = width*3 # assumes bitDepth of 8
scanIndex = scanlineSize
prevStream = '00' * 3 # assumes bitDepth of 8
nextScan = [0] * scanlineSize
for i in xrange(0, len(hexData), 2):
byte = int(hexData[i:i+2], 16)
if scanIndex == scanlineSize:
scanIndex = 0
filterMode = byte
prevStream = '00' * 3
prevScan = nextScan
nextScan = []
else:
if filterMode == 1:
prevByte = int(prevStream[0:2], 16)
byte += prevByte
elif filterMode == 2:
byte += prevScan[scanIndex]
elif filterMode == 3:
prevByte = int(prevStream[0:2], 16)
byte += (prevScan[scanIndex] + prevByte)/2
elif filterMode == 4:
a = int(prevStream[0:2], 16)
b = prevScan[scanIndex]
c = 0 if (scanIndex <= 2) else prevScan[scanIndex-3]
byte += paethFilter(a, b, c)
byte %= 256
nextScan.append(byte)
hexUnfiltered += "%0.2X" % byte
prevStream = prevStream[2:] + ("%0.2X" % byte)
scanIndex += 1
colorLen = (bitDepth/8) * 2
for i in xrange(0, len(hexUnfiltered), colorLen * 3):
pixIndex = i/(colorLen * 3)
if pixIndex % width == 0:
scanline += 1
ri = i
gi = ri+colorLen
bi = gi+colorLen
r = int(hexUnfiltered[ri:ri+colorLen], 16)
g = int(hexUnfiltered[gi:gi+colorLen], 16)
b = int(hexUnfiltered[bi:bi+colorLen], 16)
refImage[pixIndex%width][scanline] = (r,g,b)
def paethFilter(a,b,c):
p = a + b - c
pa = abs(p - a)
pb = abs(p - b)
pc = abs(p - c)
if pb >= pa <= pc:
return a
elif pb <= pc:
return b
return c