summaryrefslogtreecommitdiffstats
path: root/src/blueshift-demomode-image.py
blob: ea0b33413423eab0912f97bd7325497629953515 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
copyright='''
blueshift-demomode — Blueshift-effect demonstration tools
Copyright Ⓒ 2014, 2015  Mattias Andrée (m@maandree.se)

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this library.  If not, see <http://www.gnu.org/licenses/>.
'''


import os, sys

from argparser import *


PROGRAM_NAME = 'blueshift-demomode'
PROGRAM_VERSION = '1'
IMAGE_COMMAND = 'bin/blueshift-demomode-image'


parser = ArgParser('Blueshift-effect demonstration tools',
                   sys.argv[0] + ' (options | [--] images-file)',
                   None, None, True, ArgParser.standard_abbreviations())

parser.add_argumentless(['-h', '-?', '--help'], 0, 'Print this help information')
parser.add_argumentless(['-C', '--copying', '--copyright'], 0, 'Print copyright information')
parser.add_argumentless(['-W', '--warranty'], 0, 'Print non-warranty information')
parser.add_argumentless(['-v', '--version'], 0, 'Print program name and version')
    
parser.parse()
parser.support_alternatives()

if parser.opts['--help'] is not None:
    parser.help()
    sys.exit(0)
elif parser.opts['--copyright'] is not None:
    print(copyright[1 : -1])
    sys.exit(0)
elif parser.opts['--warranty'] is not None:
    print(copyright.split('\n\n')[2])
    sys.exit(0)
elif parser.opts['--version'] is not None:
    print('%s %s' % (PROGRAM_NAME, PROGRAM_VERSION))
    sys.exit(0)


if not len(parser.files) == 1:
    print('%s: you must select exactly one image file' % sys.argv[0], file = sys.stderr);


monitor = None
data = sys.stdin.buffer.read().decode('utf-8', 'strict').split('\n')

if monitor is not None:
    buf = []
    stage = 0
    for d in data:
        if stage == 0:
            if d.startswith('monitors: '):
                d = data[len('monitors: '):].split(' ')
                if monitor in d:
                    stage = 1
        elif stage == 1:
            if d.startswith('monitors: '):
                break
            buf.append(d)
    data = buf

data_max   = [d[len('max: '  ):] for d in data if d.startswith('max: '  )]
data_red   = [d[len('red: '  ):] for d in data if d.startswith('red: '  )]
data_green = [d[len('green: '):] for d in data if d.startswith('green: ')]
data_blue  = [d[len('blue: ' ):] for d in data if d.startswith('blue: ' )]

if any(map(lambda x : len(x) == 0, [data_max, data_red, data_green, data_blue])):
    print('%s: could not find any gamma ramp data' % sys.argv[0], file = sys.stderr);
    sys.exit(1)

data_max   = data_max[0]
data_red   = data_red[0]
data_green = data_green[0]
data_blue  = data_blue[0]


command = [IMAGE_COMMAND, data_max, data_red, data_green, data_blue]


os.close(0)
(read_end, write_end) = os.pipe()
pid = os.fork()

if pid == 0:
    os.close(read_end)
    if not write_end == 1:
        os.dup2(write_end, 1)
        os.close(write_end)
    fd = os.open(parser.files[0], os.O_RDONLY)
    if not fd == 0:
        os.dup2(fd, 0)
        os.close(fd)
    os.execvp('convert', ['convert', '-', 'pam:-'])
else:
    os.close(write_end)
    if not read_end == 0:
        os.dup2(read_end, 0)
        os.close(read_end)
    os.execvp(command[0], command)