Skip to content

Commit 9130e77

Browse files
author
BoboTiG
committed
Add test file for IndexError
1 parent 5cda465 commit 9130e77

4 files changed

Lines changed: 159 additions & 60 deletions

File tree

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
dist/
2-
tests/
3-
*.pyc
42
MANIFEST*
53
*.png
64
*.png.old
7-
*.raw
8-
test/test-linux
5+
*.pyc
96
test/.*
7+
test/test-linux*.raw
8+
tests/
9+
test/test-linux

test/test-raw-windows.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
''' Fichier test pour IndexError::Swap.efficient().
5+
6+
`pixels` est rapatrié depuis test-windows.raw et casté sous
7+
sont bon type afin d'être dans la situation exacte du
8+
module MSS.
9+
10+
save_img() ne peut être modifiée car elle fonctionne tel
11+
quel pour GNU/Linux aussi. Sauf si une optimisation est
12+
possible, je pense notament à la création de `scanlines`.
13+
14+
Le fichier test-windows.raw se trouve à l'adresse :
15+
https://raw.githubusercontent.com/BoboTiG/python-mss/develop/test/test-windows.raw
16+
'''
17+
18+
from __future__ import print_function, unicode_literals
19+
20+
from ctypes import cast, c_char, POINTER
21+
from time import time
22+
from struct import pack
23+
from sys import argv, exit
24+
from zlib import compress, crc32
25+
26+
def save_img(data, width, height, output):
27+
''' Copied from MSS.save_img(). '''
28+
29+
len_sl = (width * 3 + 3) & -4
30+
padding = 0 if len_sl % 8 == 0 else (len_sl % 8) // 2
31+
scanlines = b''.join(
32+
[b'0' + data[y * len_sl:y * len_sl + len_sl - padding]
33+
for y in range(height)])
34+
zcrc32 = crc32
35+
zcompr = compress
36+
b = pack
37+
38+
magic = b(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10)
39+
40+
# Header: size, marker, data, CRC32
41+
ihdr = [b'', b'IHDR', b'', b'']
42+
ihdr[2] = b(b'>2I5B', width, height, 8, 2, 0, 0, 0)
43+
ihdr[3] = b(b'>I', zcrc32(b''.join(ihdr[1:3])) & 0xffffffff)
44+
ihdr[0] = b(b'>I', len(ihdr[2]))
45+
46+
# Data: size, marker, data, CRC32
47+
idat = [b'', b'IDAT', b'', b'']
48+
idat[2] = zcompr(scanlines)
49+
idat[3] = b(b'>I', zcrc32(b''.join(idat[1:3])) & 0xffffffff)
50+
idat[0] = b(b'>I', len(idat[2]))
51+
52+
# Footer: size, marker, None, CRC32
53+
iend = [b'', b'IEND', b'', b'']
54+
iend[3] = b(b'>I', zcrc32(iend[1]) & 0xffffffff)
55+
iend[0] = b(b'>I', len(iend[2]))
56+
57+
with open(output, 'wb') as fileh:
58+
fileh.write(
59+
magic + b''.join(ihdr) + b''.join(idat) + b''.join(iend))
60+
61+
62+
def to_rgb(pixels, buffer_len):
63+
for i in xrange(0, buffer_len - 2, 3):
64+
yield pixels[i + 2]
65+
yield pixels[i + 1]
66+
yield pixels[i]
67+
68+
69+
if len(argv) != 2:
70+
print('Usage: python2 {0} test-windows.raw'.format(argv[0]))
71+
print(' python3 {0} test-windows.raw'.format(argv[0]))
72+
exit(1)
73+
74+
width, height = 1280, 929
75+
output = '{0}.png'.format(argv[1])
76+
with open(argv[1], 'rb') as fileh:
77+
data = fileh.read()
78+
buffer_len = len(data)
79+
pixels = cast(data, POINTER(c_char * buffer_len)).contents
80+
81+
xrange = getattr(__builtins__, 'xrange', range) # xrange pour toute version
82+
83+
# Ici, on inverse le B et le R
84+
start = time()
85+
86+
# Version 1
87+
# Fonctionne sous Python 2 et 3, rapide.
88+
#pixels[2:buffer_len:3], pixels[0:buffer_len:3] = pixels[0:buffer_len:3], pixels[2:buffer_len:3]
89+
90+
# Version 2
91+
# Fonctionne sous Python 2 et 3, lente.
92+
#pixels = b''.join(to_rgb(pixels, buffer_len))
93+
94+
# Version 3
95+
# Fonctionne sous Python 2 seulement, ultra rapide.
96+
#pixels = str(to_rgb(pixels, buffer_len))
97+
98+
print(time() - start)
99+
100+
# Enregistrement de l'image
101+
save_img(pixels, width, height, output)

test/test-raw.py

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,69 @@
11
#!/usr/bin/env python
22
# coding: utf-8
33

4-
# Test raw data generated by test-linux.
4+
''' Test raw data.
5+
Data could be contents of self.image generated by the get_pixels() method.
6+
Data coulb be contents generated by test-linux.
7+
'''
58

69
import mss
710
import sys
811

9-
if mss.__version__ < '0.1.0':
10-
from struct import pack
11-
import os.path
12-
import zlib
12+
if mss.__version__ < '1.0.1':
13+
from struct import pack
14+
from zlib import compress, crc32
1315

14-
# Copied from MSS.save_img()
15-
# @deprecated
16-
def save_img(data, width, height, output):
17-
''' Dump data to the image file.
18-
Pure python PNG implementation.
19-
Image represented as RGB tuples, no interlacing.
20-
http://inaps.org/journal/comment-fonctionne-le-png
21-
'''
16+
def save_img(data, width, height, output):
17+
''' Copied from MSS.save_img(). '''
2218

23-
to_take = (width * 3 + 3) & -4
24-
padding = 0 if to_take % 8 == 0 else (to_take % 8) // 2
25-
scanlines = b''.join(
26-
[b'0' + data[(y * to_take):(y * to_take) + to_take - padding]
27-
for y in range(height)])
19+
len_sl = (width * 3 + 3) & -4
20+
padding = 0 if len_sl % 8 == 0 else (len_sl % 8) // 2
21+
scanlines = b''.join(
22+
[b'0' + data[y * len_sl:y * len_sl + len_sl - padding]
23+
for y in range(height)])
24+
zcrc32 = crc32
25+
zcompr = compress
26+
b = pack
2827

29-
magic = pack(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10)
28+
magic = b(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10)
3029

31-
# Header: size, marker, data, CRC32
32-
ihdr = [b'', b'IHDR', b'', b'']
33-
ihdr[2] = pack(b'>2I5B', width, height, 8, 2, 0, 0, 0)
34-
ihdr[3] = pack(b'>I', zlib.crc32(b''.join(ihdr[1:3])) & 0xffffffff)
35-
ihdr[0] = pack(b'>I', len(ihdr[2]))
30+
# Header: size, marker, data, CRC32
31+
ihdr = [b'', b'IHDR', b'', b'']
32+
ihdr[2] = b(b'>2I5B', width, height, 8, 2, 0, 0, 0)
33+
ihdr[3] = b(b'>I', zcrc32(b''.join(ihdr[1:3])) & 0xffffffff)
34+
ihdr[0] = b(b'>I', len(ihdr[2]))
3635

37-
# Data: size, marker, data, CRC32
38-
idat = [b'', b'IDAT', b'', b'']
39-
idat[2] = zlib.compress(scanlines, 9)
40-
idat[3] = pack(b'>I', zlib.crc32(b''.join(idat[1:3])) & 0xffffffff)
41-
idat[0] = pack(b'>I', len(idat[2]))
36+
# Data: size, marker, data, CRC32
37+
idat = [b'', b'IDAT', b'', b'']
38+
idat[2] = zcompr(scanlines)
39+
idat[3] = b(b'>I', zcrc32(b''.join(idat[1:3])) & 0xffffffff)
40+
idat[0] = b(b'>I', len(idat[2]))
4241

43-
# Footer: size, marker, None, CRC32
44-
iend = [b'', b'IEND', b'', b'']
45-
iend[3] = pack(b'>I', zlib.crc32(iend[1]) & 0xffffffff)
46-
iend[0] = pack(b'>I', len(iend[2]))
42+
# Footer: size, marker, None, CRC32
43+
iend = [b'', b'IEND', b'', b'']
44+
iend[3] = b(b'>I', zcrc32(iend[1]) & 0xffffffff)
45+
iend[0] = b(b'>I', len(iend[2]))
4746

48-
with open(output, 'wb') as fileh:
49-
fileh.write(
50-
magic + b''.join(ihdr) + b''.join(idat) + b''.join(iend))
51-
if not os.path.isfile(output):
52-
msg = 'Impossible to write data to file "{0}".'.format(output)
53-
raise ScreenshotError(msg)
47+
with open(output, 'wb') as fileh:
48+
fileh.write(
49+
magic + b''.join(ihdr) + b''.join(idat) + b''.join(iend))
5450

55-
if len(sys.argv) < 4:
56-
print('python {0} data.raw width height'.format(sys.argv[0]))
57-
else:
58-
with open(sys.argv[1], 'rb') as fileh:
59-
data = fileh.read()
60-
width = int(sys.argv[2])
61-
height = int(sys.argv[3])
62-
output = '{0}.png'.format(sys.argv[1])
63-
if mss.__version__ < '0.1.0':
64-
print('Outdated version of MSS, please `pip install --upgrade mss`')
65-
save_img(data, width, height, output)
66-
else:
67-
mss = mss.MSS()
68-
mss.save_img(data, width, height, output)
69-
print('File {0} created.'.format(output))
70-
sys.exit(0)
71-
print('Impossible to get contents of "{0}".'.format(sys.argv[1]))
51+
52+
if len(sys.argv) != 4:
53+
print('Usage: python {0} data.raw width height'.format(sys.argv[0]))
54+
sys.exit(1)
55+
56+
with open(sys.argv[1], 'rb') as fileh:
57+
data = fileh.read()
58+
width = int(sys.argv[2])
59+
height = int(sys.argv[3])
60+
output = '{0}.png'.format(sys.argv[1])
61+
if mss.__version__ < '1.0.1':
62+
print('Outdated version of MSS, please `pip install --upgrade mss`')
63+
save_img(data, width, height, output)
64+
else:
65+
mss = mss.MSS()
66+
mss.save_img(data, width, height, output)
67+
print('File {0} created.'.format(output))
68+
sys.exit(0)
69+
print('Impossible to get contents of "{0}".'.format(sys.argv[1]))

test/test-windows.raw

3.4 MB
Binary file not shown.

0 commit comments

Comments
 (0)