File: ccmx.py

package info (click to toggle)
displaycal-py3 3.9.16-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,120 kB
  • sloc: python: 115,777; javascript: 11,540; xml: 598; sh: 257; makefile: 173
file content (110 lines) | stat: -rw-r--r-- 3,585 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-

# Python 2.5
import codecs
import json
import os
import sys
import time


CCMX_TEMPLATE = """CCMX

DESCRIPTOR "%(Name)s"
KEYWORD "INSTRUMENT"
INSTRUMENT "%(Device)s"
KEYWORD "DISPLAY"
DISPLAY "%(Display)s"
KEYWORD "DISPLAY_TYPE_BASE_ID"
DISPLAY_TYPE_BASE_ID "1"
KEYWORD "REFERENCE"
REFERENCE "%(ReferenceDevice)s"
ORIGINATOR "%(Originator)s"
CREATED "%(DateTime)s"
KEYWORD "COLOR_REP"
COLOR_REP "XYZ"

NUMBER_OF_FIELDS 3
BEGIN_DATA_FORMAT
XYZ_X XYZ_Y XYZ_Z
END_DATA_FORMAT

NUMBER_OF_SETS 3
BEGIN_DATA
%(MatrixXYZ)s
END_DATA
"""


def convert_devicecorrections_to_ccmx(path, target_dir):
    """Convert iColorDisplay DeviceCorrections.txt to individual Argyll CCMX files"""
    with codecs.open(path, "r", "utf8") as devcorrections_file:
        lines = devcorrections_file.read().strip().splitlines()
    # Convert to JSON
    # The DeviceCorrections.txt format is as follows, so a conversion is pretty
    # straightforward:
    # "Description here, e.g. Instrument X for Monitor Y" =
    # {
    #   Name = "Description here, e.g. Instrument X for Monitor Y"
    #   Device = "Instrument X"
    #   Display = "Monitor Y"
    #   ReferenceDevice = "eye-one Pro Rev.D"
    #   MatrixXYZ = "3 3 1482250784 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 "
    # }
    # "Description here, e.g. Instrument X for Monitor Y" =
    # {
    #   Name = "Description here, e.g. Instrument X for Monitor Y"
    #   Device = "Instrument X"
    #   Display = "Monitor Y"
    #   ReferenceDevice = "eye-one Pro Rev.D"
    #   MatrixXYZ = "3 3 1482250784 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 "
    # }
    # ...etc.
    # NOTE: The first three numbers in MatrixXYZ are irrelevant for our purposes.
    for i, line in enumerate(lines):
        parts = line.strip().split("=")
        if len(parts) == 2:
            for j, part in enumerate(parts):
                part = part.strip()
                if part and not part.startswith('"') and not part.endswith('"'):
                    parts[j] = '"%s"' % part
        if parts[-1].strip() not in ("", "{") and i < len(lines) - 1:
            parts[-1] += ","
        lines[i] = ":".join(parts)
    devcorrections_data = "{%s}" % "".join(lines).replace(",}", "}")
    # Parse JSON
    devcorrections = json.loads(devcorrections_data)
    # Convert to ccmx
    imported = 0
    skipped = 0
    for name in devcorrections:
        devcorrection = devcorrections[name]
        values = {
            "DateTime": time.strftime("%a %b %d %H:%M:%S %Y"),
            "Originator": "Quato iColorDisplay",
            "Name": "%s & %s"
            % (devcorrection.get("Device"), devcorrection.get("Display")),
        }
        for key in ("Device", "Display", "ReferenceDevice", "MatrixXYZ"):
            value = devcorrection.get(key)
            if value is None:
                break
            if key == "MatrixXYZ":
                # The first three numbers in the matrix are irrelevant for our
                # purposes (see format example above).
                matrix = value.split()[3:]
                value = "\n".join(
                    [" ".join(part) for part in (matrix[0:3], matrix[3:6], matrix[6:9])]
                )
            values[key] = value
        if value is None:
            skipped += 1
            continue
        imported += 1
        with codecs.open(os.path.join(target_dir, name + ".ccmx"), "w", "utf8") as ccmx:
            ccmx.write(CCMX_TEMPLATE % values)
    return imported, skipped


if __name__ == "__main__":
    convert_devicecorrections_to_ccmx(sys.argv[1], os.path.dirname(sys.argv[1]))