-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicctransform.cpp
261 lines (227 loc) · 7.65 KB
/
icctransform.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2022-present Contributors to the vectorscope project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/colorman
#include "icctransform.h"
#include <QApplication>
#include <QColorSpace>
#include <QMap>
#include <QPointer>
#include <QMutex>
QScopedPointer<ICCTransform, ICCTransform::Deleter> ICCTransform::pi;
class ICCTransformPrivate : public QObject
{
Q_OBJECT
public:
ICCTransformPrivate();
~ICCTransformPrivate();
cmsUInt32Number mapFormat(QImage::Format format);
cmsHTRANSFORM mapTransform(const QString& profile, const QString& outProfile, QImage::Format format);
cmsHTRANSFORM mapTransform(const QColorSpace& colorSpace, const QString& outProfile, QImage::Format format);
QImage mapImage(QImage image, cmsHTRANSFORM transform);
QRgb map(QRgb color, const QString& profile, const QString& outProfile);
QImage map(QImage image, const QString& profile, const QString& outProfile);
QImage map(QImage image, const QColorSpace& colorSpace, const QString& outProfile);
public:
QString inputProfile;
QString outputProfile;
QMap<QString, QMap<QImage::Format, QMap<QString, cmsHTRANSFORM> > > cache;
QPointer<ICCTransform> transform;
};
ICCTransformPrivate::ICCTransformPrivate()
{
}
ICCTransformPrivate::~ICCTransformPrivate()
{
for(QString profile : cache.keys()) {
for (QImage::Format format : cache[profile].keys()) {
for (QString outputProfile : cache[profile][format].keys()) {
cmsDeleteTransform(cache[profile][format][outputProfile]);
}
}
}
cache.clear();
}
cmsUInt32Number
ICCTransformPrivate::mapFormat(QImage::Format format)
{
switch (format) {
case QImage::Format_ARGB32:
case QImage::Format_ARGB32_Premultiplied:
case QImage::Format_RGB32:
return TYPE_BGRA_8;
case QImage::Format_RGB888:
return TYPE_RGB_8;
case QImage::Format_RGBX8888:
case QImage::Format_RGBA8888:
return TYPE_RGBA_8;
case QImage::Format_Grayscale8:
return TYPE_GRAY_8;
case QImage::Format_Grayscale16:
return TYPE_GRAY_16;
case QImage::Format_RGBA64:
case QImage::Format_RGBX64:
return TYPE_RGBA_16;
case QImage::Format_BGR888:
return TYPE_BGR_8;
default:
return 0;
}
}
cmsHTRANSFORM
ICCTransformPrivate::mapTransform(const QString& profile, const QString& outProfile, QImage::Format format)
{
if (!cache.contains(profile)) {
cache.insert(profile, QMap<QImage::Format, QMap<QString, cmsHTRANSFORM> >());
}
if (!cache[profile].contains(format)) {
cache[profile].insert(format, QMap<QString, cmsHTRANSFORM>());
}
if (!cache[profile][format].contains(outProfile)) {
cmsHPROFILE cmsProfile = cmsOpenProfileFromFile(profile.toLocal8Bit().constData(), "r");
cmsHPROFILE cmsDisplayProfile = cmsOpenProfileFromFile(outProfile.toLocal8Bit().constData(), "r");
int flags = (format == QImage::Format_ARGB32_Premultiplied ? cmsFLAGS_COPY_ALPHA : 0);
cache[profile][format].insert(
outProfile,
cmsCreateTransform(cmsProfile, mapFormat(format), cmsDisplayProfile, mapFormat(format), INTENT_PERCEPTUAL, flags)
);
cmsCloseProfile(cmsProfile);
cmsCloseProfile(cmsDisplayProfile);
}
return cache[profile][format][outProfile];
}
cmsHTRANSFORM
ICCTransformPrivate::mapTransform(const QColorSpace& colorSpace, const QString& outProfile, QImage::Format format)
{
QString profile = colorSpace.description();
QByteArray data = colorSpace.iccProfile();
if (!cache.contains(profile)) {
cache.insert(profile, QMap<QImage::Format, QMap<QString, cmsHTRANSFORM> >());
}
if (!cache[profile].contains(format)) {
cache[profile].insert(format, QMap<QString, cmsHTRANSFORM>());
}
if (!cache[profile][format].contains(outProfile)) {
cmsHPROFILE cmsProfile = cmsOpenProfileFromMem(data.constData(), static_cast<cmsUInt32Number>(data.size()));
cmsHPROFILE cmsDisplayProfile = cmsOpenProfileFromFile(outProfile.toLocal8Bit().constData(), "r");
int flags = (format == QImage::Format_ARGB32_Premultiplied ? cmsFLAGS_COPY_ALPHA : 0);
cache[profile][format].insert(
outProfile,
cmsCreateTransform(cmsProfile, mapFormat(format), cmsDisplayProfile, mapFormat(format), INTENT_PERCEPTUAL, flags)
);
cmsCloseProfile(cmsProfile);
cmsCloseProfile(cmsDisplayProfile);
}
return cache[profile][format][outProfile];
}
QImage
ICCTransformPrivate::mapImage(QImage image, cmsHTRANSFORM transform)
{
QImage mapped(image.width(), image.height(), image.format());
cmsDoTransformLineStride(
transform,
image.constBits(),
mapped.bits(),
image.width(),
image.height(),
static_cast<cmsUInt32Number>(image.bytesPerLine()),
static_cast<cmsUInt32Number>(mapped.bytesPerLine()),
0,
0);
mapped.setDevicePixelRatio(image.devicePixelRatio());
return mapped;
}
QRgb
ICCTransformPrivate::map(QRgb color, const QString& profile, const QString& outProfile)
{
cmsHTRANSFORM transform = mapTransform(
profile, outProfile, QImage::Format_RGB32
);
QRgb transformColor;
cmsDoTransform(transform, &color, &transformColor, 1);
return transformColor;
}
QImage
ICCTransformPrivate::map(QImage image, const QString& profile, const QString& outProfile)
{
cmsHTRANSFORM transform = mapTransform(profile, outProfile, image.format());
return mapImage(image, transform);
}
QImage
ICCTransformPrivate::map(QImage image, const QColorSpace& colorSpace, const QString& outProfile)
{
cmsHTRANSFORM transform = mapTransform(colorSpace, outProfile, image.format());
return mapImage(image, transform);
}
#include "icctransform.moc"
ICCTransform::ICCTransform()
: p(new ICCTransformPrivate())
{
}
ICCTransform::~ICCTransform()
{
p->cache.clear();
}
ICCTransform*
ICCTransform::instance()
{
static QMutex mutex;
QMutexLocker locker(&mutex);
if (!pi) {
pi.reset(new ICCTransform());
}
return pi.data();
}
QString
ICCTransform::ICCTransform::inputProfile() const
{
Q_ASSERT(!p->inputProfile.isEmpty());
return p->inputProfile;
}
void
ICCTransform::ICCTransform::setInputProfile(const QString& inputProfile)
{
p->inputProfile = inputProfile;
inputProfileChanged(inputProfile);
}
QString
ICCTransform::ICCTransform::outputProfile() const
{
Q_ASSERT(!p->outputProfile.isEmpty());
return p->outputProfile;
}
void
ICCTransform::ICCTransform::setOutputProfile(const QString& outputProfile)
{
p->outputProfile = outputProfile;
outputProfileChanged(outputProfile);
}
QRgb
ICCTransform::ICCTransform::map(QRgb color)
{
return p->map(color, p->inputProfile, p->outputProfile);
}
QImage
ICCTransform::ICCTransform::map(const QImage& image)
{
return p->map(image, p->inputProfile, p->outputProfile);
}
QRgb
ICCTransform::ICCTransform::map(QRgb color, const QString& inputProfile, const QString& outputProfile)
{
return p->map(color, inputProfile, outputProfile);
}
QImage
ICCTransform::map(const QImage& image, const QString& inputProfile, const QString& outputProfile)
{
return p->map(image, inputProfile, outputProfile);
}
QRgb
ICCTransform::map(QRgb color, const QColorSpace& colorSpace, const QString& outputProfile)
{
return QRgb();
}
QImage
ICCTransform::map(const QImage& image, const QColorSpace& colorSpace, const QString& outputProfile)
{
return p->map(image, colorSpace, outputProfile);
}