python-lcms2 0.0.20__cp312-cp312-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
__init__.py ADDED
File without changes
_lcms2.pyd ADDED
Binary file
lcms2/__init__.py ADDED
@@ -0,0 +1,124 @@
1
+ import os
2
+ import re
3
+ from . import _lcms2
4
+ import numpy as np
5
+ from .implementation import WHITE_POINTS, CMSError, create_profile, open_profile, profile_from_memory
6
+
7
+ DATA_TYPES = _lcms2.DATA_TYPES
8
+
9
+ INTENT = {
10
+ "PERCEPTUAL": 0,
11
+ "RELATIVE_COLORIMETRIC": 1,
12
+ "SATURATION": 2,
13
+ "ABSOLUTE_COLORIMETRIC": 3
14
+ }
15
+
16
+ NON_ICC_INTENT = {
17
+ "PRESERVE_K_ONLY_PERCEPTUAL": 10,
18
+ "PRESERVE_K_ONLY_RELATIVE_COLORIMETRIC": 11,
19
+ "PRESERVE_K_ONLY_SATURATION": 12,
20
+ "PRESERVE_K_PLANE_PERCEPTUAL": 13,
21
+ "PRESERVE_K_PLANE_RELATIVE_COLORIMETRIC": 14,
22
+ "PRESERVE_K_PLANE_SATURATION": 15
23
+ }
24
+
25
+ FLAG = {
26
+ "NONE": 0x0,
27
+ "NOTPRECALC": 0x0100,
28
+ "GAMUTCHECK": 0x1000,
29
+ "SOFTPROOFING": 0x4000,
30
+ "BLACKPOINTCOMPENSATION": 0x2000,
31
+ "PRESERVEBLACK": 0x8000,
32
+ "NULLTRANSFORM": 0x0200,
33
+ "HIGHRESPRECALC": 0x0400,
34
+ "LOWRESPRECALC": 0x0800
35
+ }
36
+
37
+
38
+ def get_version():
39
+ ver = str(_lcms2.get_version())
40
+ return f"{ver[0]}.{ver[1:]}"
41
+
42
+
43
+ class Profile:
44
+ def __init__(self, builtin=None, filename=None, buffer=None,
45
+ white_point=None, degrees=2, temperature=None):
46
+ if builtin is None and filename is None and buffer is None:
47
+ self._assign(profile=_lcms2.create_default_profile())
48
+ return
49
+ if builtin is not None and filename is None and buffer is None:
50
+ self._assign(profile=create_profile(builtin, white_point=white_point, degrees=degrees, temperature=temperature))
51
+ return
52
+ if filename is not None and builtin is None and buffer is None:
53
+ self._assign(profile=open_profile(filename))
54
+ return
55
+ if buffer is not None and builtin is None and filename is None:
56
+ self._assign(profile=profile_from_memory(buffer))
57
+ return
58
+ raise CMSError(
59
+ "Only one source of profile data can be specified: a built-in profile name, filename or bytes object")
60
+
61
+ def _assign(self, profile):
62
+ self.handle = profile.handle
63
+ self.name = profile.name
64
+ self.info = profile.info
65
+ self.copyright = profile.copyright
66
+
67
+ def to_bytes(self):
68
+ return _lcms2.profile_to_bytes(self)
69
+
70
+ def save(self, filename):
71
+ with open(filename, "wb") as f:
72
+ f.write(self.to_bytes())
73
+
74
+
75
+ class Transform:
76
+ def __init__(self, src_profile, src_format, dst_profile, dst_format, intent="PERCEPTUAL", flags="NONE",
77
+ proofing_profile=None, proofing_intent=None):
78
+ if not isinstance(src_profile, (Profile, _lcms2.Profile)):
79
+ raise CMSError(f"Wrong type of src_profile. Expected Profile but got '{type(src_profile)}'")
80
+ if src_format not in DATA_TYPES.keys():
81
+ raise CMSError(f"Invalid source data format: '{src_format}'")
82
+ if not isinstance(dst_profile, (Profile, _lcms2.Profile)):
83
+ raise CMSError(f"Wrong type of dst_profile. Expected Profile but got '{type(src_profile)}'")
84
+ if dst_format not in DATA_TYPES.keys():
85
+ raise CMSError(f"Invalid destination data format: '{dst_format}'")
86
+ if intent not in INTENT.keys():
87
+ raise CMSError(f"Invalid rendering intent: '{intent}'")
88
+ flag_list = re.split("[ ,;|]", flags)
89
+ transform_flags = FLAG["NONE"]
90
+ for f in flag_list:
91
+ if f not in FLAG.keys():
92
+ raise CMSError(f"Invalid flag: '{f}'")
93
+ transform_flags = transform_flags | FLAG[f]
94
+
95
+ if proofing_profile is not None:
96
+ if not isinstance(proofing_profile, (Profile, _lcms2.Profile)):
97
+ raise CMSError(f"Wrong type of proofing_profile. Expected Profile but got '{type(src_profile)}'")
98
+ if proofing_intent not in NON_ICC_INTENT.keys():
99
+ raise CMSError(f"Invalid proofing intent: '{proofing_intent}'")
100
+
101
+ self.src_format = src_format
102
+ self.dst_format = dst_format
103
+ self.transform = _lcms2.Transform(src_profile, DATA_TYPES[src_format][0],
104
+ dst_profile, DATA_TYPES[dst_format][0],
105
+ INTENT[intent], transform_flags,
106
+ proofing_profile,
107
+ NON_ICC_INTENT.get(proofing_intent, 0))
108
+
109
+ def apply(self, src):
110
+ _, src_numpy_type, src_channels = DATA_TYPES[self.src_format]
111
+ _, dst_numpy_type, dst_channels = DATA_TYPES[self.dst_format]
112
+ x = src
113
+ if not isinstance(src, np.ndarray):
114
+ x = np.array(src, dtype=src_numpy_type)
115
+ if x.dtype != src_numpy_type:
116
+ raise CMSError(f"Source data type ({x.dtype}) does not match transform input type ({src_numpy_type}")
117
+ if x.shape[-1] != src_channels:
118
+ raise CMSError(f"Wrong number of input channels ({x.shape[-1]}); expected {src_channels}")
119
+ dst_shape = np.empty_like(x.shape)
120
+ dst_shape[:-1] = x.shape[:-1]
121
+ dst_shape[-1] = dst_channels
122
+ dst = np.empty(shape=dst_shape, dtype=dst_numpy_type)
123
+ self.transform.apply(x, dst, x.size // src_channels)
124
+ return dst
lcms2/_lcms2.c ADDED
@@ -0,0 +1,89 @@
1
+ #define PY_SSIZE_T_CLEAN
2
+ #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
3
+ #include <Python.h>
4
+
5
+ #if PY_VERSION_HEX < 0x030c0000
6
+ #define Py_T_INT T_INT
7
+ #define Py_T_OBJECT_EX T_OBJECT_EX
8
+ #endif
9
+
10
+ #include <numpy/arrayobject.h>
11
+ #include <lcms2.h>
12
+ #include <lcms2_internal.h>
13
+
14
+
15
+ #include "profile.h"
16
+ #include "transform.h"
17
+ #include "definitions.h"
18
+ #include "white_point.h"
19
+
20
+ static PyObject *
21
+ get_version (PyObject *self, PyObject *args) {
22
+ return Py_BuildValue("i", LCMS_VERSION);
23
+ }
24
+
25
+ static
26
+ PyMethodDef pycms_methods[] = {
27
+ {"create_profile", create_profile, METH_VARARGS},
28
+ {"create_default_profile", create_default_profile, METH_NOARGS},
29
+ {"create_rgb_profile", create_rgb_profile, METH_VARARGS},
30
+ {"open_profile", open_profile, METH_VARARGS},
31
+ {"profile_from_memory", profile_from_memory, METH_VARARGS},
32
+ {"profile_to_bytes", profile_to_bytes, METH_VARARGS},
33
+ {"white_point_from_temperature", white_point_from_temperature, METH_VARARGS},
34
+ {"get_version", get_version, METH_NOARGS},
35
+ {NULL, NULL}
36
+ };
37
+
38
+
39
+ static struct PyModuleDef lcms2_def =
40
+ {
41
+ PyModuleDef_HEAD_INIT,
42
+ "_lcms2", /* name of module */
43
+ "", /* module documentation, may be NULL */
44
+ -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
45
+ pycms_methods
46
+ };
47
+
48
+ PyMODINIT_FUNC PyInit__lcms2(void)
49
+ {
50
+ PyObject *m;
51
+ m = PyModule_Create(&lcms2_def);
52
+ if (m == NULL)
53
+ return NULL;
54
+
55
+ if (PyType_Ready(&profile_type) < 0) {
56
+ Py_DECREF(m);
57
+ return NULL;
58
+ }
59
+
60
+ if (PyModule_AddObjectRef(m, "Profile", (PyObject *) &profile_type) < 0) {
61
+ Py_DECREF(m);
62
+ return NULL;
63
+ }
64
+
65
+ if (PyType_Ready(&transform_type) < 0) {
66
+ Py_DECREF(m);
67
+ return NULL;
68
+ }
69
+
70
+ if (PyModule_AddObjectRef(m, "Transform", (PyObject *) &transform_type) < 0) {
71
+ Py_DECREF(m);
72
+ return NULL;
73
+ }
74
+
75
+ PyObject* types_dict = create_data_types_dict();
76
+ if (types_dict == NULL)
77
+ {
78
+ Py_DECREF(m);
79
+ return NULL;
80
+ }
81
+ if (PyModule_AddObjectRef(m, "DATA_TYPES", (PyObject *) types_dict) < 0) {
82
+ Py_DECREF(m);
83
+ return NULL;
84
+ }
85
+
86
+ import_array(); // initialize NumPy
87
+
88
+ return m;
89
+ }
lcms2/_lcms2.pyd ADDED
Binary file
@@ -0,0 +1,221 @@
1
+ from . import _lcms2
2
+ import numpy as np
3
+ import os
4
+
5
+
6
+ class CMSError(Exception):
7
+ def __init__(self, message="LibCMS2 error"):
8
+ super().__init__(message)
9
+
10
+
11
+ def create_profile(profile, white_point=None, degrees=2, temperature=None):
12
+ if profile in ("sRGB", "Lab", "XYZ"):
13
+ coordinates = None
14
+ if profile == "Lab":
15
+ if isinstance(white_point, str):
16
+ coordinates = np.array(WHITE_POINTS[degrees][white_point]["xyY"])
17
+ else:
18
+ if isinstance(temperature, (int, float)):
19
+ wp_list = _lcms2.white_point_from_temperature(temperature)
20
+ coordinates = np.array(wp_list)
21
+ else:
22
+ if white_point is not None:
23
+ coordinates = np.array(white_point)
24
+ if coordinates.size != 3:
25
+ raise CMSError(
26
+ "white_point should be an array-like object of size 3 containing xyY coordinates of the white point")
27
+ p = _lcms2.create_profile(profile, coordinates)
28
+ return p
29
+
30
+ if profile in RGB_COLORSPACES.keys():
31
+ space = RGB_COLORSPACES[profile]
32
+ profile_white_point = space["white point"]
33
+ if profile_white_point not in WHITE_POINTS[degrees].keys():
34
+ raise CMSError(f"Unknown white point `{profile_white_point}`")
35
+ wp = np.array(WHITE_POINTS[degrees][profile_white_point]["xyY"])
36
+ red = np.array(space["red"])
37
+ green = np.array(space["green"])
38
+ blue = np.array(space["blue"])
39
+ tc = space["tone curve"]
40
+ curve_type = int(tc["type"])
41
+ parameters = np.array(tc["parameters"])
42
+ p = _lcms2.create_rgb_profile(wp, red, green, blue, curve_type, parameters)
43
+ return p
44
+
45
+ raise CMSError(
46
+ f"Invalid profile '{profile}'. It must be one of built in profiles ('sRGB', 'Lab' or 'XYZ') or supported RGB profiles.")
47
+
48
+
49
+ def open_profile(filename):
50
+ if not isinstance(filename, str):
51
+ raise CMSError("filename must be a string containing a path to a profile")
52
+ if not os.path.isfile(filename):
53
+ raise CMSError(f"Unable to find '{filename}'.")
54
+
55
+ p = _lcms2.open_profile(filename)
56
+ return p
57
+
58
+
59
+ def profile_from_memory(buffer):
60
+ if not isinstance(buffer, bytes):
61
+ raise CMSError("filename must be a bytes object")
62
+
63
+ p = _lcms2.profile_from_memory(buffer)
64
+ return p
65
+
66
+
67
+ """
68
+ White points of standard illuminants for 2 degrees and 10 degrees field of view
69
+ xyY are the coordinates of the white point,
70
+ CCT is correlated color temperature in Kelvins
71
+ """
72
+ WHITE_POINTS = {2: {'A': {'xyY': [0.44758, 0.40745, 1.0], 'CCT': 2856.0, 'description': 'incandescent / tungsten'},
73
+ 'B': {'xyY': [0.34842, 0.35161, 1.0], 'CCT': 4874.0,
74
+ 'description': 'obsolete, direct sunlight at noon'},
75
+ 'C': {'xyY': [0.31006, 0.31616, 1.0], 'CCT': 6774.0,
76
+ 'description': 'obsolete, average / North sky daylight NTSC 1953, PAL-M'},
77
+ 'D50': {'xyY': [0.34567, 0.3585, 1.0], 'CCT': 5003.0,
78
+ 'description': 'horizon light, ICC profile PCS'},
79
+ 'D55': {'xyY': [0.33242, 0.34743, 1.0], 'CCT': 5503.0,
80
+ 'description': 'mid-morning / mid-afternoon daylight'},
81
+ 'D65': {'xyY': [0.31272, 0.32903, 1.0], 'CCT': 6504.0,
82
+ 'description': 'noon daylight: television, sRGB color space'},
83
+ 'D75': {'xyY': [0.29902, 0.31485, 1.0], 'CCT': 7504.0, 'description': 'North sky daylight'},
84
+ 'D93': {'xyY': [0.28315, 0.29711, 1.0], 'CCT': 9305.0,
85
+ 'description': 'high-efficiency blue phosphor monitors, BT.2035, NTSC-J'},
86
+ 'E': {'xyY': [0.33333, 0.33333, 1.0], 'CCT': 5454.0, 'description': 'equal energy'},
87
+ 'F1': {'xyY': [0.3131, 0.33727, 1.0], 'CCT': 6430.0, 'description': 'daylight fluorescent'},
88
+ 'F2': {'xyY': [0.37208, 0.37529, 1.0], 'CCT': 4230.0, 'description': 'cool white fluorescent'},
89
+ 'F3': {'xyY': [0.4091, 0.3943, 1.0], 'CCT': 3450.0, 'description': 'white fluorescent'},
90
+ 'F4': {'xyY': [0.44018, 0.40329, 1.0], 'CCT': 2940.0, 'description': 'warm white fluorescent'},
91
+ 'F5': {'xyY': [0.31379, 0.34531, 1.0], 'CCT': 6350.0, 'description': 'daylight fluorescent'},
92
+ 'F6': {'xyY': [0.3779, 0.38835, 1.0], 'CCT': 4150.0, 'description': 'light white fluorescent'},
93
+ 'F7': {'xyY': [0.31292, 0.32933, 1.0], 'CCT': 6500.0,
94
+ 'description': 'D65 simulator, daylight simulator'},
95
+ 'F8': {'xyY': [0.34588, 0.35875, 1.0], 'CCT': 5000.0,
96
+ 'description': 'D50 simulator, Sylvania F40 Design 50'},
97
+ 'F9': {'xyY': [0.37417, 0.37281, 1.0], 'CCT': 4150.0,
98
+ 'description': 'cool white deluxe fluorescent'},
99
+ 'F10': {'xyY': [0.34609, 0.35986, 1.0], 'CCT': 5000.0, 'description': 'Philips TL85, Ultralume 50'},
100
+ 'F11': {'xyY': [0.38052, 0.37713, 1.0], 'CCT': 4000.0, 'description': 'Philips TL84, Ultralume 40'},
101
+ 'F12': {'xyY': [0.43695, 0.40441, 1.0], 'CCT': 3000.0, 'description': 'Philips TL83, Ultralume 30'},
102
+ 'FL3.1': {'xyY': [0.4407, 0.4033, 1.0], 'CCT': 2932.0, 'description': 'standard halophosphate'},
103
+ 'FL3.2': {'xyY': [0.3808, 0.3734, 1.0], 'CCT': 3965.0, 'description': 'standard halophosphate'},
104
+ 'FL3.3': {'xyY': [0.3153, 0.3439, 1.0], 'CCT': 6280.0, 'description': 'standard halophosphate'},
105
+ 'FL3.4': {'xyY': [0.4429, 0.4043, 1.0], 'CCT': 2904.0, 'description': 'DeLuxe type'},
106
+ 'FL3.5': {'xyY': [0.3749, 0.3672, 1.0], 'CCT': 4086.0, 'description': 'DeLuxe type'},
107
+ 'FL3.6': {'xyY': [0.3488, 0.36, 1.0], 'CCT': 4894.0, 'description': 'DeLuxe type'},
108
+ 'FL3.7': {'xyY': [0.4384, 0.4045, 1.0], 'CCT': 2979.0, 'description': 'Three-band'},
109
+ 'FL3.8': {'xyY': [0.382, 0.3832, 1.0], 'CCT': 4006.0, 'description': 'Three-band'},
110
+ 'FL3.9': {'xyY': [0.3499, 0.3591, 1.0], 'CCT': 4853.0, 'description': 'Three-band'},
111
+ 'FL3.10': {'xyY': [0.3455, 0.356, 1.0], 'CCT': 5000.0, 'description': 'Three-band'},
112
+ 'FL3.11': {'xyY': [0.3245, 0.3434, 1.0], 'CCT': 5854.0, 'description': 'Three-band'},
113
+ 'FL3.12': {'xyY': [0.4377, 0.4037, 1.0], 'CCT': 2984.0, 'description': 'Multi-band'},
114
+ 'FL3.13': {'xyY': [0.383, 0.3724, 1.0], 'CCT': 3896.0, 'description': 'Multi-band'},
115
+ 'FL3.14': {'xyY': [0.3447, 0.3609, 1.0], 'CCT': 5045.0, 'description': 'Multi-band'},
116
+ 'FL3.15': {'xyY': [0.3127, 0.3288, 1.0], 'CCT': 6509.0, 'description': 'D65 Simulator'},
117
+ 'HP1': {'xyY': [0.533, 0.415, 1.0], 'CCT': 1959.0,
118
+ 'description': 'Standard high pressure sodium lamp'},
119
+ 'HP2': {'xyY': [0.4778, 0.4158, 1.0], 'CCT': 2506.0,
120
+ 'description': 'Color-enhanced high-pressure sodium lamp'},
121
+ 'HP3': {'xyY': [0.4302, 0.4075, 1.0], 'CCT': 3144.0,
122
+ 'description': 'High pressure metal halide lamp'},
123
+ 'HP4': {'xyY': [0.3812, 0.3797, 1.0], 'CCT': 4002.0,
124
+ 'description': 'High pressure metal halide lamp'},
125
+ 'HP5': {'xyY': [0.3776, 0.3713, 1.0], 'CCT': 4039.0,
126
+ 'description': 'High pressure metal halide lamp'},
127
+ 'LED-B1': {'xyY': [0.456, 0.4078, 1.0], 'CCT': 2733.0, 'description': 'phosphor-converted blue'},
128
+ 'LED-B2': {'xyY': [0.4357, 0.4012, 1.0], 'CCT': 2998.0, 'description': 'phosphor-converted blue'},
129
+ 'LED-B3': {'xyY': [0.3756, 0.3723, 1.0], 'CCT': 4103.0, 'description': 'phosphor-converted blue'},
130
+ 'LED-B4': {'xyY': [0.3422, 0.3502, 1.0], 'CCT': 5109.0, 'description': 'phosphor-converted blue'},
131
+ 'LED-B5': {'xyY': [0.3118, 0.3236, 1.0], 'CCT': 6598.0, 'description': 'phosphor-converted blue'},
132
+ 'LED-BH1': {'xyY': [0.4474, 0.4066, 1.0], 'CCT': 2851.0,
133
+ 'description': 'mixing of phosphor-converted blue LED and red LED (blue-hybrid)'},
134
+ 'LED-RGB1': {'xyY': [0.4557, 0.4211, 1.0], 'CCT': 2840.0,
135
+ 'description': 'mixing of red, green, and blue LEDs'},
136
+ 'LED-V1': {'xyY': [0.4548, 0.4044, 1.0], 'CCT': 2724.0, 'description': 'phosphor-converted violet'},
137
+ 'LED-V2': {'xyY': [0.3781, 0.3775, 1.0], 'CCT': 4070.0, 'description': 'phosphor-converted violet'},
138
+ 'ID50': {'xyY': [0.3432, 0.3602, 1.0], 'CCT': 5098.0, 'description': 'natural indoor daylight'},
139
+ 'ID65': {'xyY': [0.3107, 0.3307, 1.0], 'CCT': 6603.0, 'description': 'natural indoor daylight'}},
140
+ 10: {'A': {'xyY': [0.45117, 0.40594, 1.0], 'CCT': 2856.0, 'description': 'incandescent / tungsten'},
141
+ 'B': {'xyY': [0.3498, 0.3527, 1.0], 'CCT': 4874.0,
142
+ 'description': 'obsolete, direct sunlight at noon'},
143
+ 'C': {'xyY': [0.31039, 0.31905, 1.0], 'CCT': 6774.0,
144
+ 'description': 'obsolete, average / North sky daylight NTSC 1953, PAL-M'},
145
+ 'D50': {'xyY': [0.34773, 0.35952, 1.0], 'CCT': 5003.0,
146
+ 'description': 'horizon light, ICC profile PCS'},
147
+ 'D55': {'xyY': [0.33411, 0.34877, 1.0], 'CCT': 5503.0,
148
+ 'description': 'mid-morning / mid-afternoon daylight'},
149
+ 'D65': {'xyY': [0.31382, 0.331, 1.0], 'CCT': 6504.0,
150
+ 'description': 'noon daylight: television, sRGB color space'},
151
+ 'D75': {'xyY': [0.29968, 0.3174, 1.0], 'CCT': 7504.0, 'description': 'North sky daylight'},
152
+ 'D93': {'xyY': [0.28327, 0.30043, 1.0], 'CCT': 9305.0,
153
+ 'description': 'high-efficiency blue phosphor monitors, BT.2035, NTSC-J'},
154
+ 'E': {'xyY': [0.33333, 0.33333, 1.0], 'CCT': 5454.0, 'description': 'equal energy'},
155
+ 'F1': {'xyY': [0.31811, 0.33559, 1.0], 'CCT': 6430.0, 'description': 'daylight fluorescent'},
156
+ 'F2': {'xyY': [0.37925, 0.36733, 1.0], 'CCT': 4230.0, 'description': 'cool white fluorescent'},
157
+ 'F3': {'xyY': [0.41761, 0.38324, 1.0], 'CCT': 3450.0, 'description': 'white fluorescent'},
158
+ 'F4': {'xyY': [0.4492, 0.39074, 1.0], 'CCT': 2940.0, 'description': 'warm white fluorescent'},
159
+ 'F5': {'xyY': [0.31975, 0.34246, 1.0], 'CCT': 6350.0, 'description': 'daylight fluorescent'},
160
+ 'F6': {'xyY': [0.3866, 0.37847, 1.0], 'CCT': 4150.0, 'description': 'light white fluorescent'},
161
+ 'F7': {'xyY': [0.31569, 0.3296, 1.0], 'CCT': 6500.0,
162
+ 'description': 'D65 simulator, daylight simulator'},
163
+ 'F8': {'xyY': [0.34902, 0.35939, 1.0], 'CCT': 5000.0,
164
+ 'description': 'D50 simulator, Sylvania F40 Design 50'},
165
+ 'F9': {'xyY': [0.37829, 0.37045, 1.0], 'CCT': 4150.0,
166
+ 'description': 'cool white deluxe fluorescent'},
167
+ 'F10': {'xyY': [0.3509, 0.35444, 1.0], 'CCT': 5000.0, 'description': 'Philips TL85, Ultralume 50'},
168
+ 'F11': {'xyY': [0.38541, 0.37123, 1.0], 'CCT': 4000.0,
169
+ 'description': 'Philips TL84, Ultralume 40'},
170
+ 'F12': {'xyY': [0.44256, 0.39717, 1.0], 'CCT': 3000.0,
171
+ 'description': 'Philips TL83, Ultralume 30'}}}
172
+
173
+ RGB_COLORSPACES = {'Adobe RGB (1998)': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'D65',
174
+ 'red': [0.64, 0.33, 0.297361], 'green': [0.21, 0.71, 0.627355],
175
+ 'blue': [0.15, 0.06, 0.075285]},
176
+ 'Apple RGB': {'tone curve': {'type': 1, 'parameters': [1.8]}, 'white point': 'D65',
177
+ 'red': [0.625, 0.34, 0.244634], 'green': [0.28, 0.595, 0.672034],
178
+ 'blue': [0.155, 0.07, 0.083332]},
179
+ 'Best RGB': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'D50',
180
+ 'red': [0.7347, 0.2653, 0.228457], 'green': [0.215, 0.775, 0.737352],
181
+ 'blue': [0.13, 0.035, 0.034191]},
182
+ 'Beta RGB': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'D50',
183
+ 'red': [0.6888, 0.3112, 0.303273], 'green': [0.1986, 0.7551, 0.663786],
184
+ 'blue': [0.1265, 0.0352, 0.032941]},
185
+ 'Bruce RGB': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'D65',
186
+ 'red': [0.64, 0.33, 0.240995], 'green': [0.28, 0.65, 0.683554],
187
+ 'blue': [0.15, 0.06, 0.075452]},
188
+ 'CIE RGB': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'E',
189
+ 'red': [0.735, 0.265, 0.176204], 'green': [0.274, 0.717, 0.812985],
190
+ 'blue': [0.167, 0.009, 0.010811]},
191
+ 'ColorMatch RGB': {'tone curve': {'type': 1, 'parameters': [1.8]}, 'white point': 'D50',
192
+ 'red': [0.63, 0.34, 0.274884], 'green': [0.295, 0.605, 0.658132],
193
+ 'blue': [0.15, 0.075, 0.066985]},
194
+ 'Don RGB 4': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'D50',
195
+ 'red': [0.696, 0.3, 0.27835], 'green': [0.215, 0.765, 0.68797],
196
+ 'blue': [0.13, 0.035, 0.03368]},
197
+ 'ECI RGB v2': {
198
+ 'tone curve': {'type': 4, 'parameters': [3.000000, 0.862076, 0.137924, 0.110703, 0.080002]},
199
+ 'white point': 'D50', 'red': [0.67, 0.33, 0.32025],
200
+ 'green': [0.21, 0.71, 0.602071], 'blue': [0.14, 0.08, 0.077679]},
201
+ 'Ekta Space PS5': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'D50',
202
+ 'red': [0.695, 0.305, 0.260629], 'green': [0.26, 0.7, 0.734946],
203
+ 'blue': [0.11, 0.005, 0.004425]},
204
+ 'NTSC RGB': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'C',
205
+ 'red': [0.67, 0.33, 0.298839], 'green': [0.21, 0.71, 0.586811],
206
+ 'blue': [0.14, 0.08, 0.11435]},
207
+ 'PAL/SECAM RGB': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'D65',
208
+ 'red': [0.64, 0.33, 0.222021], 'green': [0.29, 0.6, 0.706645],
209
+ 'blue': [0.15, 0.06, 0.071334]},
210
+ 'ProPhoto RGB': {'tone curve': {'type': 1, 'parameters': [1.8]}, 'white point': 'D50',
211
+ 'red': [0.7347, 0.2653, 0.28804], 'green': [0.1596, 0.8404, 0.711874],
212
+ 'blue': [0.0366, 0.0001, 8.6e-05]},
213
+ 'SMPTE-C RGB': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'D65',
214
+ 'red': [0.63, 0.34, 0.212395], 'green': [0.31, 0.595, 0.701049],
215
+ 'blue': [0.155, 0.07, 0.086556]},
216
+ '_sRGB': {'tone curve': {'type': 4, 'parameters': [2.399994, 0.947861, 0.052139, 0.077393, 0.040451]},
217
+ 'white point': 'D65', 'red': [0.64, 0.33, 0.212656],
218
+ 'green': [0.3, 0.6, 0.715158], 'blue': [0.15, 0.06, 0.072186]},
219
+ 'Wide Gamut RGB': {'tone curve': {'type': 1, 'parameters': [2.2]}, 'white point': 'D50',
220
+ 'red': [0.735, 0.265, 0.258187], 'green': [0.115, 0.826, 0.724938],
221
+ 'blue': [0.157, 0.018, 0.016875]}}
pylcms2/__init__.py ADDED
@@ -0,0 +1,124 @@
1
+ import os
2
+ import re
3
+ import _lcms2
4
+ import numpy as np
5
+ from .implementation import WHITE_POINTS, CMSError, create_profile, open_profile, profile_from_memory
6
+
7
+ DATA_TYPES = _lcms2.DATA_TYPES
8
+
9
+ INTENT = {
10
+ "PERCEPTUAL": 0,
11
+ "RELATIVE_COLORIMETRIC": 1,
12
+ "SATURATION": 2,
13
+ "ABSOLUTE_COLORIMETRIC": 3
14
+ }
15
+
16
+ NON_ICC_INTENT = {
17
+ "PRESERVE_K_ONLY_PERCEPTUAL": 10,
18
+ "PRESERVE_K_ONLY_RELATIVE_COLORIMETRIC": 11,
19
+ "PRESERVE_K_ONLY_SATURATION": 12,
20
+ "PRESERVE_K_PLANE_PERCEPTUAL": 13,
21
+ "PRESERVE_K_PLANE_RELATIVE_COLORIMETRIC": 14,
22
+ "PRESERVE_K_PLANE_SATURATION": 15
23
+ }
24
+
25
+ FLAG = {
26
+ "NONE": 0x0,
27
+ "NOTPRECALC": 0x0100,
28
+ "GAMUTCHECK": 0x1000,
29
+ "SOFTPROOFING": 0x4000,
30
+ "BLACKPOINTCOMPENSATION": 0x2000,
31
+ "PRESERVEBLACK": 0x8000,
32
+ "NULLTRANSFORM": 0x0200,
33
+ "HIGHRESPRECALC": 0x0400,
34
+ "LOWRESPRECALC": 0x0800
35
+ }
36
+
37
+
38
+ def get_version():
39
+ ver = str(_lcms2.get_version())
40
+ return f"{ver[0]}.{ver[1:]}"
41
+
42
+
43
+ class Profile:
44
+ def __init__(self, builtin=None, filename=None, buffer=None,
45
+ white_point=None, degrees=2, temperature=None):
46
+ if builtin is None and filename is None and buffer is None:
47
+ self._assign(profile=_lcms2.create_default_profile())
48
+ return
49
+ if builtin is not None and filename is None and buffer is None:
50
+ self._assign(profile=create_profile(builtin, white_point=white_point, degrees=degrees, temperature=temperature))
51
+ return
52
+ if filename is not None and builtin is None and buffer is None:
53
+ self._assign(profile=open_profile(filename))
54
+ return
55
+ if buffer is not None and builtin is None and filename is None:
56
+ self._assign(profile=profile_from_memory(buffer))
57
+ return
58
+ raise CMSError(
59
+ "Only one source of profile data can be specified: a built-in profile name, filename or bytes object")
60
+
61
+ def _assign(self, profile):
62
+ self.handle = profile.handle
63
+ self.name = profile.name
64
+ self.info = profile.info
65
+ self.copyright = profile.copyright
66
+
67
+ def to_bytes(self):
68
+ return _lcms2.profile_to_bytes(self)
69
+
70
+ def save(self, filename):
71
+ with open(filename, "wb") as f:
72
+ f.write(self.to_bytes())
73
+
74
+
75
+ class Transform:
76
+ def __init__(self, src_profile, src_format, dst_profile, dst_format, intent="PERCEPTUAL", flags="NONE",
77
+ proofing_profile=None, proofing_intent=None):
78
+ if not isinstance(src_profile, (Profile, _lcms2.Profile)):
79
+ raise CMSError(f"Wrong type of src_profile. Expected Profile but got '{type(src_profile)}'")
80
+ if src_format not in DATA_TYPES.keys():
81
+ raise CMSError(f"Invalid source data format: '{src_format}'")
82
+ if not isinstance(dst_profile, (Profile, _lcms2.Profile)):
83
+ raise CMSError(f"Wrong type of dst_profile. Expected Profile but got '{type(src_profile)}'")
84
+ if dst_format not in DATA_TYPES.keys():
85
+ raise CMSError(f"Invalid destination data format: '{dst_format}'")
86
+ if intent not in INTENT.keys():
87
+ raise CMSError(f"Invalid rendering intent: '{intent}'")
88
+ flag_list = re.split("[ ,;|]", flags)
89
+ transform_flags = FLAG["NONE"]
90
+ for f in flag_list:
91
+ if f not in FLAG.keys():
92
+ raise CMSError(f"Invalid flag: '{f}'")
93
+ transform_flags = transform_flags | FLAG[f]
94
+
95
+ if proofing_profile is not None:
96
+ if not isinstance(proofing_profile, (Profile, _lcms2.Profile)):
97
+ raise CMSError(f"Wrong type of proofing_profile. Expected Profile but got '{type(src_profile)}'")
98
+ if proofing_intent not in NON_ICC_INTENT.keys():
99
+ raise CMSError(f"Invalid proofing intent: '{proofing_intent}'")
100
+
101
+ self.src_format = src_format
102
+ self.dst_format = dst_format
103
+ self.transform = _lcms2.Transform(src_profile, DATA_TYPES[src_format][0],
104
+ dst_profile, DATA_TYPES[dst_format][0],
105
+ INTENT[intent], transform_flags,
106
+ proofing_profile,
107
+ NON_ICC_INTENT.get(proofing_intent, 0))
108
+
109
+ def apply(self, src):
110
+ _, src_numpy_type, src_channels = DATA_TYPES[self.src_format]
111
+ _, dst_numpy_type, dst_channels = DATA_TYPES[self.dst_format]
112
+ x = src
113
+ if not isinstance(src, np.ndarray):
114
+ x = np.array(src, dtype=src_numpy_type)
115
+ if x.dtype != src_numpy_type:
116
+ raise CMSError(f"Source data type ({x.dtype}) does not match transform input type ({src_numpy_type}")
117
+ if x.shape[-1] != src_channels:
118
+ raise CMSError(f"Wrong number of input channels ({x.shape[-1]}); expected {src_channels}")
119
+ dst_shape = np.empty_like(x.shape)
120
+ dst_shape[:-1] = x.shape[:-1]
121
+ dst_shape[-1] = dst_channels
122
+ dst = np.empty(shape=dst_shape, dtype=dst_numpy_type)
123
+ self.transform.apply(x, dst, x.size // src_channels)
124
+ return dst
pylcms2/_lcms2.c ADDED
@@ -0,0 +1,84 @@
1
+
2
+ #define PY_SSIZE_T_CLEAN
3
+ #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
4
+ #include <Python.h>
5
+ #include <numpy/arrayobject.h>
6
+ #include <lcms2.h>
7
+ #include <lcms2_internal.h>
8
+
9
+
10
+ #include "profile.h"
11
+ #include "transform.h"
12
+ #include "definitions.h"
13
+ #include "white_point.h"
14
+
15
+ static PyObject *
16
+ get_version (PyObject *self, PyObject *args) {
17
+ return Py_BuildValue("i", LCMS_VERSION);
18
+ }
19
+
20
+ static
21
+ PyMethodDef pycms_methods[] = {
22
+ {"create_profile", create_profile, METH_VARARGS},
23
+ {"create_default_profile", create_default_profile, METH_NOARGS},
24
+ {"create_rgb_profile", create_rgb_profile, METH_VARARGS},
25
+ {"open_profile", open_profile, METH_VARARGS},
26
+ {"profile_from_memory", profile_from_memory, METH_VARARGS},
27
+ {"profile_to_bytes", profile_to_bytes, METH_VARARGS},
28
+ {"white_point_from_temperature", white_point_from_temperature, METH_VARARGS},
29
+ {"get_version", get_version, METH_NOARGS},
30
+ {NULL, NULL}
31
+ };
32
+
33
+
34
+ static struct PyModuleDef lcms2_def =
35
+ {
36
+ PyModuleDef_HEAD_INIT,
37
+ "_lcms2", /* name of module */
38
+ "", /* module documentation, may be NULL */
39
+ -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
40
+ pycms_methods
41
+ };
42
+
43
+ PyMODINIT_FUNC PyInit__lcms2(void)
44
+ {
45
+ PyObject *m;
46
+ m = PyModule_Create(&lcms2_def);
47
+ if (m == NULL)
48
+ return NULL;
49
+
50
+ if (PyType_Ready(&profile_type) < 0) {
51
+ Py_DECREF(m);
52
+ return NULL;
53
+ }
54
+
55
+ if (PyModule_AddObjectRef(m, "Profile", (PyObject *) &profile_type) < 0) {
56
+ Py_DECREF(m);
57
+ return NULL;
58
+ }
59
+
60
+ if (PyType_Ready(&transform_type) < 0) {
61
+ Py_DECREF(m);
62
+ return NULL;
63
+ }
64
+
65
+ if (PyModule_AddObjectRef(m, "Transform", (PyObject *) &transform_type) < 0) {
66
+ Py_DECREF(m);
67
+ return NULL;
68
+ }
69
+
70
+ PyObject* types_dict = create_data_types_dict();
71
+ if (types_dict == NULL)
72
+ {
73
+ Py_DECREF(m);
74
+ return NULL;
75
+ }
76
+ if (PyModule_AddObjectRef(m, "DATA_TYPES", (PyObject *) types_dict) < 0) {
77
+ Py_DECREF(m);
78
+ return NULL;
79
+ }
80
+
81
+ import_array(); // initialize NumPy
82
+
83
+ return m;
84
+ }