|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | | -# Test raw data. |
| 4 | +# Test raw data generated by test-linux. |
5 | 5 |
|
6 | 6 | import mss |
7 | | -from struct import pack |
8 | | -import os.path |
9 | | -import zlib |
10 | 7 | import sys |
11 | 8 |
|
| 9 | +if mss.__version__ < '0.1.0': |
| 10 | + from struct import pack |
| 11 | + import os.path |
| 12 | + import zlib |
12 | 13 |
|
13 | | -def save_img(data, width, height, output): |
14 | | - ''' Dump data to the image file. |
15 | | - Pure python PNG implementation. |
16 | | - Image represented as RGB tuples, no interlacing. |
17 | | - http://inaps.org/journal/comment-fonctionne-le-png |
18 | | - ''' |
| 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 | + ''' |
19 | 22 |
|
20 | | - to_take = (width * 3 + 3) & -4 |
21 | | - padding = 0 if to_take % 8 == 0 else (to_take % 8) // 2 |
22 | | - scanlines = b''.join( |
23 | | - [b'0' + data[(y * to_take):(y * to_take) + to_take - padding] |
24 | | - for y in range(height)]) |
| 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)]) |
25 | 28 |
|
26 | | - magic = pack(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10) |
| 29 | + magic = pack(b'>8B', 137, 80, 78, 71, 13, 10, 26, 10) |
27 | 30 |
|
28 | | - # Header: size, marker, data, CRC32 |
29 | | - ihdr = [b'', b'IHDR', b'', b''] |
30 | | - ihdr[2] = pack(b'>2I5B', width, height, 8, 2, 0, 0, 0) |
31 | | - ihdr[3] = pack(b'>I', zlib.crc32(b''.join(ihdr[1:3])) & 0xffffffff) |
32 | | - ihdr[0] = pack(b'>I', len(ihdr[2])) |
| 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])) |
33 | 36 |
|
34 | | - # Data: size, marker, data, CRC32 |
35 | | - idat = [b'', b'IDAT', b'', b''] |
36 | | - idat[2] = zlib.compress(scanlines, 9) |
37 | | - idat[3] = pack(b'>I', zlib.crc32(b''.join(idat[1:3])) & 0xffffffff) |
38 | | - idat[0] = pack(b'>I', len(idat[2])) |
| 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])) |
39 | 42 |
|
40 | | - # Footer: size, marker, None, CRC32 |
41 | | - iend = [b'', b'IEND', b'', b''] |
42 | | - iend[3] = pack(b'>I', zlib.crc32(iend[1]) & 0xffffffff) |
43 | | - iend[0] = pack(b'>I', len(iend[2])) |
| 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])) |
44 | 47 |
|
45 | | - with open(output, 'wb') as fileh: |
46 | | - fileh.write( |
47 | | - magic + b''.join(ihdr) + b''.join(idat) + b''.join(iend)) |
48 | | - if not os.path.isfile(output): |
49 | | - msg = 'Impossible to write data to file "{}".'.format(output) |
50 | | - raise ScreenshotError(msg) |
| 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 "{}".'.format(output) |
| 53 | + raise ScreenshotError(msg) |
51 | 54 |
|
52 | 55 | if len(sys.argv) < 4: |
53 | | - print('python {0} data.raw width height'.format(sys.argv[0])) |
| 56 | + print('python {} data.raw width height'.format(sys.argv[0])) |
54 | 57 | else: |
55 | | - with open(sys.argv[1], 'rb') as f: |
56 | | - data = f.read() |
| 58 | + with open(sys.argv[1], 'rb') as fileh: |
| 59 | + data = fileh.read() |
57 | 60 | width = int(sys.argv[2]) |
58 | 61 | height = int(sys.argv[3]) |
| 62 | + output = '{}.png'.format(sys.argv[1]) |
59 | 63 | if mss.__version__ < '0.1.0': |
60 | 64 | print('Outdated version of MSS, please `pip install --upgrade mss`') |
61 | | - save_img(data, width, height, sys.argv[1] + '.png') |
| 65 | + save_img(data, width, height, output) |
62 | 66 | else: |
63 | 67 | mss = mss.MSS() |
64 | | - mss.save_img(data, width, height, sys.argv[1] + '.png') |
65 | | - print('File {}.png created.'.format(sys.argv[1])) |
| 68 | + mss.save_img(data, width, height, output) |
| 69 | + print('File {} created.'.format(output)) |
| 70 | + sys.exit(0) |
| 71 | + print('Impossible to get contents of "{}".'.format(sys.argv[1])) |
0 commit comments