Skip to content

Commit cf42862

Browse files
author
BoboTiG
committed
MSSImage: few optimizations into png()
1 parent 169ca07 commit cf42862

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

mss.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- remove 'ext' argument, using only PNG
2727
- do not overwrite existing image files
2828
- few optimizations into MSSLinux::get_pixels()
29+
- few optimizations into MSSImage::png()
2930
3031
You can always get the latest version of this module at:
3132
@@ -749,27 +750,22 @@ def png(self):
749750
http://inaps.org/journal/comment-fonctionne-le-png
750751
'''
751752

752-
width, height, data = self.width, self.height, self.data
753-
to_take = (width * 3 + 3) & -4
753+
to_take = (self.width * 3 + 3) & -4
754754
padding = 0 if to_take % 8 == 0 else (to_take % 8) // 2
755-
offset = 0
756-
scanlines = b''
757-
758-
for y in range(height):
759-
scanlines += b'0' + data[offset:offset+to_take-padding]
760-
offset += to_take
755+
height, data = self.height, self.data
756+
scanlines = [b''.join([b'0', data[to_take*y:to_take*y+to_take-padding]]) for y in range(height)]
761757

762758
magic = pack(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10)
763759

764760
# Header: size, marker, data, CRC32
765761
ihdr = [b'', b'IHDR', b'', b'']
766-
ihdr[2] = pack(b'>2I5B', width, height, 8, 2, 0, 0, 0)
762+
ihdr[2] = pack(b'>2I5B', self.width, self.height, 8, 2, 0, 0, 0)
767763
ihdr[3] = pack(b'>I', zlib.crc32(b''.join(ihdr[1:3])) & 0xffffffff)
768764
ihdr[0] = pack(b'>I', len(ihdr[2]))
769765

770766
# Data: size, marker, data, CRC32
771767
idat = [b'', b'IDAT', b'', b'']
772-
idat[2] = zlib.compress(scanlines, 9)
768+
idat[2] = zlib.compress(b''.join(scanlines), 9)
773769
idat[3] = pack(b'>I', zlib.crc32(b''.join(idat[1:3])) & 0xffffffff)
774770
idat[0] = pack(b'>I', len(idat[2]))
775771

0 commit comments

Comments
 (0)