-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.cpp
140 lines (126 loc) · 3.74 KB
/
module.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <lcms2.h>
#include <map>
#include <memory>
namespace py = pybind11;
struct Profile {
Profile(cmsHPROFILE p) : p(p) { }
Profile(Profile &&profile) { std::swap(p, profile.p); }
~Profile() {
if (p)
cmsCloseProfile(p);
}
cmsHPROFILE p = nullptr;
};
struct Transform {
Transform(cmsHTRANSFORM t) : t(t) { }
Transform(Transform &&transform) { std::swap(t, transform.t); }
~Transform() {
if (t)
cmsDeleteTransform(t);
}
cmsHTRANSFORM t = nullptr;
};
const std::map<std::string, std::function<Profile ()>> standardProfiles {
{"srgb", []()->auto { return Profile(cmsCreate_sRGBProfile()); }}
};
Profile getProfile(const std::string &profileId) {
try {
Profile profile(cmsOpenProfileFromFile(profileId.c_str(), "r"));
if (profile.p == nullptr) {
return standardProfiles.at(profileId)();
}
return profile;
}
catch (std::out_of_range &) {
throw py::key_error(profileId + " not found");
}
}
const std::map<py::dtype, cmsUInt32Number> dataTypes {
{py::dtype("uint8"), TYPE_RGB_8},
{py::dtype("uint16"), TYPE_RGB_16},
{py::dtype("float32"), TYPE_RGB_FLT}
};
cmsUInt32Number getDataType(const py::dtype &dtype) {
try {
return dataTypes.at(dtype);
}
catch (std::out_of_range &) {
throw std::domain_error(std::string(py::str(static_cast<const py::object &>(dtype))) + " not implemented");
}
}
void doTransform(Transform &t, const py::array &inputImage, py::array &outputImage) {
#ifdef THREADS
const int lines = static_cast<int>(inputImage.shape(0));
#pragma omp parallel for num_threads(THREADS) schedule(static)
for (int i = 0; i < lines; ++i) {
cmsDoTransform(
t.t,
inputImage.data(i, 0, 0),
outputImage.mutable_data(i, 0, 0),
static_cast<cmsUInt32Number>(inputImage.shape(1))
);
}
#else
cmsDoTransform(
t.t,
inputImage.data(0, 0, 0),
outputImage.mutable_data(0, 0, 0),
static_cast<cmsUInt32Number>(inputImage.shape(0) * inputImage.shape(1))
);
#endif
}
void applyProfile(
const py::array inputImage,
py::array outputImage,
const std::string &inputProfile,
const std::string &outputProfile) {
Profile ip(getProfile(inputProfile));
Profile op(getProfile(outputProfile));
cmsUInt32Number inputDataType = getDataType(inputImage.dtype());
cmsUInt32Number outputDataType = getDataType(outputImage.dtype());
Transform t(cmsCreateTransform(
ip.p,
inputDataType,
op.p,
outputDataType,
INTENT_PERCEPTUAL,
0
));
doTransform(
t,
inputImage,
outputImage
);
}
struct Transformer {
Transformer(const std::string &inputType, const std::string &outputType, const std::string &inputProfile, const std::string &outputProfile) :
ip(getProfile(inputProfile)),
op(getProfile(outputProfile)),
t(cmsCreateTransform(
ip.p,
getDataType(py::dtype(inputType)),
op.p,
getDataType(py::dtype(outputType)),
INTENT_PERCEPTUAL,
0
)) {
}
void apply(const py::array inputImage, py::array outputImage) {
doTransform(
t,
inputImage,
outputImage
);
}
Profile ip;
Profile op;
Transform t;
};
PYBIND11_MODULE(pylcms, m) {
m.def("apply_profile", &applyProfile);
py::class_<Transformer>(m, "Transformer")
.def(py::init<const std::string &, const std::string &, const std::string &, const std::string &>())
.def("apply", &Transformer::apply);
}