-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiInkMapping.hpp
More file actions
413 lines (328 loc) · 10.2 KB
/
MultiInkMapping.hpp
File metadata and controls
413 lines (328 loc) · 10.2 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//
// MultiInkMapping.hpp
// MultiInkMapping
// MIT License, Copyright (C) Chris Cox 2026
//
// Created by Chris Cox on March 18, 2026.
//
#ifndef MultiInkMapping_h
#define MultiInkMapping_h
#include <cstdio>
#include <cstdint>
#include <cassert>
#include <cstring>
#include <cstdlib>
#define _USE_MATH_DEFINES
#include <cmath>
#include <string>
#include <vector>
#include <memory>
#include <map>
#include <algorithm>
#include "MiniICC.hpp"
/******************************************************************************/
const char kVersionString[] = "0.9b";
/******************************************************************************/
struct labColor {
float L; // 0...100
float A; // +-128.0
float B;
public:
labColor() {};
labColor( float l, float a, float b) : L(l), A(a), B(b) {}
};
/******************************************************************************/
struct namedColor {
std::string name;
labColor color;
public:
namedColor( const std::string &n, float l, float a, float b) : name(n), color(l,a,b) {}
namedColor() {} // type must be default constructable for JSON
};
/******************************************************************************/
struct xyzColor {
float X; // 0..100
float Y;
float Z;
public:
xyzColor() {};
xyzColor( float x, float y, float z) : X(x), Y(y), Z(z) {}
};
/******************************************************************************/
struct Point {
float a; // from Lab for a particular L plane
float b;
public:
Point() {};
Point( float A, float B ) : a(A), b(B) {}
bool operator==(const Point& other) const = default;
};
/******************************************************************************/
struct inkMixPair {
// some compilers don't get the default initialization correct
inkMixPair( size_t dx1, size_t dx2, float f1, float f2 ) : inkIndex1(dx1), inkIndex2(dx2),
ink1Fraction(f1), ink2Fraction(f2) {}
size_t inkIndex1;
size_t inkIndex2;
float ink1Fraction;
float ink2Fraction;
bool operator==(const inkMixPair& other) const = default;
};
/******************************************************************************/
struct overPrintSwatch {
labColor color; // from JSON
std::vector< std::string > inkNames; // from JSON
public:
uint32_t inkBitmap; // filled in by matching names
xyzColor colorXYZ; // filled in after full set read
};
/******************************************************************************/
typedef std::vector< Point > PointList;
typedef std::vector< labColor > color_list;
typedef std::vector< namedColor > named_color_list;
typedef std::vector< color_list > spline_list;
typedef std::vector< inkMixPair > spline_mix_data;
/******************************************************************************/
const xyzColor identityXYZ( 100.0, 100.0, 100.0 );
/******************************************************************************/
inline
float constexpr LERP( const float t, const float x1, const float x2 )
{
return x1 + t*(x2-x1);
}
/********************************************************************************/
inline
xyzColor operator*( const float &scale, const xyzColor &b)
{
xyzColor result;
result.X = scale * b.X;
result.Y = scale * b.Y;
result.Z = scale * b.Z;
return result;
}
/********************************************************************************/
inline
xyzColor& operator*=( xyzColor &a, float s)
{
a.X *= s;
a.Y *= s;
a.Z *= s;
return a;
}
/********************************************************************************/
inline
xyzColor operator*( const xyzColor &a, const xyzColor &b)
{
xyzColor result;
#if 1
result.X = a.X * b.X * (1.0f / 100.0f);
result.Y = a.Y * b.Y * (1.0f / 100.0f);
result.Z = a.Z * b.Z * (1.0f / 100.0f);
#else
result.X = a.X * b.X / 100.0f;
result.Y = a.Y * b.Y / 100.0f;
result.Z = a.Z * b.Z / 100.0f;
#endif
return result;
}
/********************************************************************************/
inline
xyzColor& operator*=( xyzColor &a, const xyzColor &b)
{
#if 1
a.X = a.X * b.X * (1.0f / 100.0f);
a.Y = a.Y * b.Y * (1.0f / 100.0f);
a.Z = a.Z * b.Z * (1.0f / 100.0f);
#else
a.X = a.X * b.X / 100.0f;
a.Y = a.Y * b.Y / 100.0f;
a.Z = a.Z * b.Z / 100.0f;
#endif
return a;
}
/********************************************************************************/
inline
xyzColor operator/( const xyzColor &a, const xyzColor &b)
{
xyzColor result;
result.X = 100.0f * a.X / b.X;
result.Y = 100.0f * a.Y / b.Y;
result.Z = 100.0f * a.Z / b.Z;
return result;
}
/********************************************************************************/
inline
xyzColor& operator/=( xyzColor &a, const xyzColor &b)
{
a.X = 100.0f * a.X / b.X;
a.Y = 100.0f * a.Y / b.Y;
a.Z = 100.0f * a.Z / b.Z;
return a;
}
/********************************************************************************/
inline
xyzColor& operator/=( xyzColor &a, const float s)
{
a.X /= s;
a.Y /= s;
a.Z /= s;
return a;
}
/********************************************************************************/
inline
xyzColor operator+( const xyzColor &a, const xyzColor &b)
{
xyzColor result;
result.X = a.X + b.X;
result.Y = a.Y + b.Y;
result.Z = a.Z + b.Z;
return result;
}
/********************************************************************************/
inline
xyzColor& operator+=( xyzColor &a, const xyzColor &b)
{
a.X += b.X;
a.Y += b.Y;
a.Z += b.Z;
return a;
}
/********************************************************************************/
inline
xyzColor operator-( const xyzColor &a, const xyzColor &b)
{
xyzColor result;
result.X = a.X - b.X;
result.Y = a.Y - b.Y;
result.Z = a.Z - b.Z;
return result;
}
/********************************************************************************/
inline
xyzColor& operator-=( xyzColor &a, const xyzColor &b)
{
a.X -= b.X;
a.Y -= b.Y;
a.Z -= b.Z;
return a;
}
/********************************************************************************/
// interpolate between 0 and 100.0
inline
float constexpr grid_to_L( size_t grid_value, size_t gridPoints )
{
return (100.0f * (float)grid_value) / (float)(gridPoints - 1);
}
inline
float constexpr grid_to_AB( size_t grid_value, size_t gridPoints )
{
//float middle = 0.5f * (gridPoints-1);
//return (127.0f * ((float)grid_value - middle)) / middle; // gives zero, but doesn't reach -128
return (255.0f * grid_value / (gridPoints-1)) - 128.0f;
}
/********************************************************************************/
// convert 0..100 representation to file representation
inline
int constexpr floatL_to_fileL8( float L )
{
if (L <= 0.0f) return 0;
if (L >= 100.0f) return 255;
return (int)( (255.0f / 100.0f) * L + 0.5f );
}
inline
int constexpr floatAB_to_fileAB8( float A )
{
if (A > 127.0f) return 255;
if (A < -128.0f) return 0;
return (int)( A + 128.0f );
}
inline
uint8_t constexpr float_to_file255( float A )
{
if (A > 1.0f) return 255;
if (A < 0.0f) return 0;
return (uint8_t)( A * 255.0f );
}
inline
uint16_t constexpr float_to_file65535( float A )
{
if (A > 1.0f) return 65535;
if (A < 0.0f) return 0;
return (uint16_t)( A * 65535.0f );
}
/********************************************************************************/
// convert 0..100 representation to file representation
// ICC version 2/4 profile encoding for LAB 16 bit --- not usable in TIFF
inline
int constexpr floatL_to_fileL16( float L )
{
if (L <= 0.0f) return 0;
if (L >= 100.0f) return 65280;
return (int)( (65280.0f / 100.0f) * L + 0.5f );
}
inline
int constexpr floatAB_to_fileAB16( float A )
{
if (A > 127.0f) return 65280;
if (A < -128.0f) return 0;
return (int)( A*256.0f + 32768.0f );
}
/********************************************************************************/
const std::vector<color_space> profileSpaceLookup =
{
kSpace1CLR, // index zero
kSpace1CLR,
kSpace2CLR,
kSpace3CLR,
kSpace4CLR,
kSpace5CLR,
kSpace6CLR,
kSpace7CLR,
kSpace8CLR,
kSpace9CLR,
kSpaceACLR,
kSpaceBCLR,
kSpaceCCLR,
kSpaceDCLR,
kSpaceECLR,
kSpaceFCLR,
};
/******************************************************************************/
struct inkColorSet {
std::string name; // what filename to use
std::string description; // how to describe this combination
std::string copyright; // copyright string for this set
labColor paperColor; // lightest possible color
labColor darkColor; // darkest possible color from combination of inks, calculated if L <= 0
labColor filterColor; // optional color filter to apply, simulating different paper colors
named_color_list primaries; // saturated hues
std::vector< overPrintSwatch > overprints; // overprints of primaries, may be empty
public:
spline_list splines; // built from basic ink data
spline_mix_data mixData; // built from basic ink data
std::map< std::string, int > name_map; // built from inks
std::map< uint32_t, int > overprint_bitmask_map; // built from inks and overprints
};
/******************************************************************************/
// global variables, just because it was quicker to write it this way
struct settings_spec {
int gDataDepth;
int gDataGridPoints;
size_t gTableSizeLimit;
bool gDebugMode;
bool gCreateOutput;
bool gCreateAbstract;
bool gTIFFTables;
bool gSmoothTables;
std::string gDefaultCopyright;
uint32_t gProfileTypes;
std::vector<inkColorSet> colorSets;
};
extern settings_spec globalSettings;
/******************************************************************************/
extern void processInkSetList(void);
/******************************************************************************/
// ICC profile v4 limit
const int kMaxChannels = 15;
/******************************************************************************/
#endif /* MultiInkMapping_h */