Skip to content

Commit d707089

Browse files
author
BoboTiG
committed
MSS: fix save_img() on Windows 8.1
1 parent 698aef0 commit d707089

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

mss.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
If that URL should fail, try contacting the author.
1212
'''
1313

14-
from __future__ import unicode_literals, print_function, division
14+
from __future__ import division, print_function, unicode_literals
1515

1616
__version__ = '1.0.0'
1717
__author__ = "Mickaël 'Tiger-222' Schoentgen"
@@ -190,31 +190,33 @@ def save_img(self, data, width, height, output):
190190
http://inaps.org/journal/comment-fonctionne-le-png
191191
'''
192192

193-
zcrc32 = crc32
194-
zcompr = compress
195-
len_sl = width * 3
193+
len_sl = (width * 3 + 3) & -4
194+
padding = 0 if len_sl % 8 == 0 else (len_sl % 8) // 2
196195
scanlines = b''.join(
197-
[b'0' + data[y * len_sl:y * len_sl + len_sl]
196+
[b'0' + data[y * len_sl:y * len_sl + len_sl - padding]
198197
for y in range(height)])
198+
zcrc32 = crc32
199+
zcompr = compress
200+
b = pack
199201

200-
magic = pack(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10)
202+
magic = b(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10)
201203

202204
# Header: size, marker, data, CRC32
203205
ihdr = [b'', b'IHDR', b'', b'']
204-
ihdr[2] = pack(b'>2I5B', width, height, 8, 2, 0, 0, 0)
205-
ihdr[3] = pack(b'>I', zcrc32(b''.join(ihdr[1:3])) & 0xffffffff)
206-
ihdr[0] = pack(b'>I', len(ihdr[2]))
206+
ihdr[2] = b(b'>2I5B', width, height, 8, 2, 0, 0, 0)
207+
ihdr[3] = b(b'>I', zcrc32(b''.join(ihdr[1:3])) & 0xffffffff)
208+
ihdr[0] = b(b'>I', len(ihdr[2]))
207209

208210
# Data: size, marker, data, CRC32
209211
idat = [b'', b'IDAT', b'', b'']
210212
idat[2] = zcompr(scanlines, 9)
211-
idat[3] = pack(b'>I', zcrc32(b''.join(idat[1:3])) & 0xffffffff)
212-
idat[0] = pack(b'>I', len(idat[2]))
213+
idat[3] = b(b'>I', zcrc32(b''.join(idat[1:3])) & 0xffffffff)
214+
idat[0] = b(b'>I', len(idat[2]))
213215

214216
# Footer: size, marker, None, CRC32
215217
iend = [b'', b'IEND', b'', b'']
216-
iend[3] = pack(b'>I', zcrc32(iend[1]) & 0xffffffff)
217-
iend[0] = pack(b'>I', len(iend[2]))
218+
iend[3] = b(b'>I', zcrc32(iend[1]) & 0xffffffff)
219+
iend[0] = b(b'>I', len(iend[2]))
218220

219221
with open(output, 'wb') as fileh:
220222
fileh.write(

0 commit comments

Comments
 (0)