|
29 | 29 |
|
30 | 30 | from struct import pack |
31 | 31 | from platform import system |
32 | | -import os.path |
| 32 | +from zlib import compress, crc32 |
33 | 33 | import sys |
34 | | -import zlib |
35 | 34 |
|
36 | 35 |
|
37 | 36 | class ScreenshotError(Exception): |
@@ -216,39 +215,37 @@ def save_img(self, data, width, height, output): |
216 | 215 | http://inaps.org/journal/comment-fonctionne-le-png |
217 | 216 | ''' |
218 | 217 |
|
219 | | - self.debug('save_img') |
220 | | - |
221 | | - to_take = (width * 3 + 3) & -4 |
222 | | - padding = 0 if to_take % 8 == 0 else (to_take % 8) // 2 |
| 218 | + zcrc32 = crc32 |
| 219 | + zcompr = compress |
| 220 | + len_sl = width * 3 |
223 | 221 | scanlines = b''.join( |
224 | | - [b'0' + data[(y * to_take):(y * to_take) + to_take - padding] |
| 222 | + [b'0' + data[y * len_sl:y * len_sl + len_sl] |
225 | 223 | for y in range(height)]) |
226 | 224 |
|
227 | 225 | magic = pack(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10) |
228 | 226 |
|
229 | 227 | # Header: size, marker, data, CRC32 |
230 | 228 | ihdr = [b'', b'IHDR', b'', b''] |
231 | 229 | ihdr[2] = pack(b'>2I5B', width, height, 8, 2, 0, 0, 0) |
232 | | - ihdr[3] = pack(b'>I', zlib.crc32(b''.join(ihdr[1:3])) & 0xffffffff) |
| 230 | + ihdr[3] = pack(b'>I', zcrc32(b''.join(ihdr[1:3])) & 0xffffffff) |
233 | 231 | ihdr[0] = pack(b'>I', len(ihdr[2])) |
234 | 232 |
|
235 | 233 | # Data: size, marker, data, CRC32 |
236 | 234 | idat = [b'', b'IDAT', b'', b''] |
237 | | - idat[2] = zlib.compress(scanlines, 9) |
238 | | - idat[3] = pack(b'>I', zlib.crc32(b''.join(idat[1:3])) & 0xffffffff) |
| 235 | + idat[2] = zcompr(scanlines, 9) |
| 236 | + idat[3] = pack(b'>I', zcrc32(b''.join(idat[1:3])) & 0xffffffff) |
239 | 237 | idat[0] = pack(b'>I', len(idat[2])) |
240 | 238 |
|
241 | 239 | # Footer: size, marker, None, CRC32 |
242 | 240 | iend = [b'', b'IEND', b'', b''] |
243 | | - iend[3] = pack(b'>I', zlib.crc32(iend[1]) & 0xffffffff) |
| 241 | + iend[3] = pack(b'>I', zcrc32(iend[1]) & 0xffffffff) |
244 | 242 | iend[0] = pack(b'>I', len(iend[2])) |
245 | 243 |
|
246 | 244 | with open(output, 'wb') as fileh: |
247 | 245 | fileh.write( |
248 | 246 | magic + b''.join(ihdr) + b''.join(idat) + b''.join(iend)) |
249 | | - if not os.path.isfile(output): |
250 | | - msg = 'Impossible to write data to file "{}".'.format(output) |
251 | | - raise ScreenshotError(msg) |
| 247 | + return |
| 248 | + raise ScreenshotError('MSS: error writing data to "{}".'.format(output)) |
252 | 249 |
|
253 | 250 |
|
254 | 251 | class MSSMac(MSS): |
|
0 commit comments