|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # coding: utf-8 |
3 | 3 |
|
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 | +''' |
5 | 8 |
|
6 | 9 | import mss |
7 | 10 | import sys |
8 | 11 |
|
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 |
13 | 15 |
|
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(). ''' |
22 | 18 |
|
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 |
28 | 27 |
|
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) |
30 | 29 |
|
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])) |
36 | 35 |
|
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])) |
42 | 41 |
|
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])) |
47 | 46 |
|
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)) |
54 | 50 |
|
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])) |
0 commit comments