-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiInkMapping.cpp
More file actions
2584 lines (2051 loc) · 93.6 KB
/
MultiInkMapping.cpp
File metadata and controls
2584 lines (2051 loc) · 93.6 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Multi Ink Mapping
MIT License, Copyright (C) Chris Cox 2026
Created by Chris Cox around March 2, 2026.
Simulate inks (1-15) painted or printed on paper - for custom printing, profile, and 3DLUT generation.
Is it accurate? Nope.
Accuracy would need a lot more measurements, and math, and might not look as good.
Does it look reasonable? Yes.
And that's all I need from it.
This assumes that the primaries are somewhat saturated, and define a convex hull.
Primaries will be sorted by hue to make sure they are in order to make a convex hull.
This further assumes that the primaries are really transparent, so ink order does not matter. (this is NOT realistic)
BUG - overprints are inset, leading to thin areas around primaries
too linear on the interpolation from overprint to paper?
need some way to get the splines closer to primary saturation.
force chroma >= straight line between primaries?
better ink mix model for splines?
changing splines to lerp doesn't seem to make a difference
should I supersample the range of each lut entry to get the greatest value?
Try adding another point to the overprint spline - halfway between primaries (lighter than overprint)
helps some, still not quite right
See Experimental-Turquoise-Orange-Green_abstract.icc
Experimental-Violet-Magenta-YellowOrange_abstract.icc
TODO - would be nice to add measured overprint colors
SOLIDS DONE
Maybe prebuild a faster lookup system by ink fractions that can handle any fractions?
hash((int)(100*fraction1)) and chain? Still expensive.
sum of fractions (scaled to int) for bucket, then sub lookup if match?
cheaper, might work.
but still suffers from "find closest" versus "exact" problems
build predictable lookup for full inks, then fractional extrapolation for partials?
currently looks up only fulls, then multiply for partials - which fails sometimes (hard edge where they meet)
TODO - allow additional combinations of inks (n+2, n+3, tertiary, etc.) when building splines
take max chroma points for hull?
maybe do all binary combinations, with lookup for any measured, then sort.
or build in more combinations and sort midpoints by hue, before interpolating?
Mix data would need to be updated along with sort! merge into single structure?
tertiary mixtures might be useful in some cases, but not most
quaternary mixtures... aren't likely to be useful (mostly mud)
maybe set limits on chroma and luma to keep mixtures?
for (i=0;i<inks;++i)
for (j=(i+1);j<inks;++j)
overprints.push_back( estimate(inkFractions(i,j)) )
sort( overprints, hue_less );
make midpoints into larger list?
then make splines from list?
TODO - What about tints and shades? need percentages of mixes, plus measurement.
Um, special case names for "paper" and "dark"?
How to use these when building splines?
{ "Ink1Name", 0.25, "Ink2Name", 0.75, measuredOverprint }
{ "Ink1Name", 0.25, "none", 0, measuredTint }
{ "InInk1Namek1", 0.25, "dark", 0.25, measuredShade }
Do I really want to support full IT8 profile data? No.
*/
#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdint>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <map>
#include <algorithm>
#include "MultiInkMapping.hpp"
#include "Options.hpp"
#include "MiniTIFF.hpp"
#include "MiniICC.hpp"
// MSVC headers blow chunks
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif
/********************************************************************************/
static
void VerifyDecreasingL( const color_list &list )
{
#if !NDEBUG
size_t count = list.size();
for (size_t i = 1; i < count; ++i) {
float currentL = list[i].L;
float previousL = list[i-1].L;
assert( currentL <= previousL);
}
#endif
}
/********************************************************************************/
const float XD50 = 96.4212f;
const float YD50 = 100.0f;
const float ZD50 = 82.5188f;
static
float CIECurve( const float input )
{
const float scale = 7.787037f; // powf( 29.0/6.0, 2.0) / 3.0;
const float breakpoint = 0.008856f; // powf( 6.0/29.0, 3.0 );
const float offset = (float)(4.0/29.0);
if (input > breakpoint)
return cbrtf( input );
else
return (input * scale + offset);
}
/********************************************************************************/
static
float CIEReverseCurve( const float input )
{
const float scale = (float)(1.0 / 7.787037); // 0.128418549 // 3.0 * powf( 6.0/29.0, 2.0);
const float breakpoint = (float)(6.0/29.0);
const float offset = (float)(4.0/29.0);
if (input > breakpoint)
return input*input*input; // powf(input,3);
else
return scale*(input - offset);
}
/********************************************************************************/
static
xyzColor LAB2XYZ( const labColor &input )
{
xyzColor result;
float tempY = (input.L + 16.0f)/116.0f;
float Y = YD50 * CIEReverseCurve( tempY );
float X = XD50 * CIEReverseCurve( tempY + input.A / 500.0f );
float Z = ZD50 * CIEReverseCurve( tempY - input.B / 200.0f );
result.X = X;
result.Y = Y;
result.Z = Z;
return result;
}
/********************************************************************************/
static
labColor XYZ2LAB( const xyzColor &input )
{
labColor result;
float tempY = CIECurve( input.Y / YD50 );
float tempX = CIECurve( input.X / XD50 );
float tempZ = CIECurve( input.Z / ZD50 );
float L = 116.0f * tempY - 16.0f;
float a = 500.0f * ( tempX - tempY );
float b = 200.0f * ( tempY - tempZ );
result.L = L;
result.A = a;
result.B = b;
return result;
}
/********************************************************************************/
// linear interpolation
inline
xyzColor interp2inks( const float t, const xyzColor &ink1, const xyzColor &ink2 )
{
xyzColor result;
result = ink1 + t * (ink2 - ink1);
return result;
}
/********************************************************************************/
// linear interpolation in LAB - really only useful for nearby colors or neutrals
inline
labColor interp2inks( const float t, const labColor &ink1, const labColor &ink2 )
{
labColor result;
result.L = LERP( t, ink1.L, ink2.L );
result.A = LERP( t, ink1.A, ink2.A );
result.B = LERP( t, ink1.B, ink2.B );
return result;
}
/********************************************************************************/
static
color_list mix_pure_ink_spline( int steps, const labColor &paperColor, const labColor &inkColor, const labColor &darkColor)
{
int i;
xyzColor mix;
labColor mixLAB;
color_list temp;
xyzColor paperColorXYZ = LAB2XYZ( paperColor );
xyzColor darkColorXYZ = LAB2XYZ( darkColor );
xyzColor inkColorXYZ = LAB2XYZ( inkColor );
// exact paper
temp.push_back( paperColor );
// interp paper->ink
for (i=1; i < (steps/2); ++i) {
float t = (float) i / (float) (steps/2);
mix = interp2inks( t, paperColorXYZ, inkColorXYZ );
mixLAB = XYZ2LAB( mix );
temp.push_back( mixLAB );
}
// exact ink
temp.push_back( inkColor );
// interp ink->dark
for (i=(steps/2)+1; i < (steps-1); ++i) {
float t = (float) (i - (steps/2)) / (float) (steps/2);
mix = interp2inks( t, inkColorXYZ, darkColorXYZ );
mixLAB = XYZ2LAB( mix );
temp.push_back( mixLAB );
}
// exact dark
temp.push_back( darkColor );
// error checking
VerifyDecreasingL(temp);
return temp;
}
/********************************************************************************/
bool labLMore(const labColor &a, const labColor &b)
{
return a.L > b.L;
}
/********************************************************************************/
// try to handle a list of points
// brightest should be paper, darkest should be dark mix
static
color_list mix_overprint_ink_spline2( float Lstep, const std::vector<labColor> &points_in )
{
color_list temp;
size_t pointCount = points_in.size();
assert ( pointCount >= 3 ); // 3 points minimum
// copy list, sort decreasing L*
std::vector<labColor> points( points_in );
std::sort( points.begin(), points.end(), labLMore );
// exact first color, should be paper
temp.push_back( points[0] );
for (size_t i = 1; i < pointCount; ++i) {
labColor prevColor = points[i-1];
labColor nextColor = points[i];
float range = prevColor.L - nextColor.L;
xyzColor prevColorXYZ = LAB2XYZ( prevColor );
xyzColor nextColorXYZ = LAB2XYZ( nextColor );
for (float L = (prevColor.L - Lstep); L > nextColor.L; L -= Lstep) {
float t = (prevColor.L - L) / range;
xyzColor mix = interp2inks( t, prevColorXYZ, nextColorXYZ );
labColor mixLAB = XYZ2LAB( mix );
temp.push_back( mixLAB );
}
// exact sample
temp.push_back( nextColor );
}
// last point should be dark mix
// error checking
VerifyDecreasingL(temp);
return temp;
}
/********************************************************************************/
bool labHueLess(const namedColor &a, const namedColor &b)
{
float angle1 = (float)(M_PI + atan2f(a.color.A,a.color.B));
float angle2 = (float)(M_PI + atan2f(b.color.A,b.color.B));
return angle1 < angle2;
}
/********************************************************************************/
/*
TODO - handle edge cases better
paper->(ink1 + ink2) changes suddenly when they get to 1,1
Need better interpolation of partial inks
interp > half? (would still have jumps)
interp any partial? (singles can be ignored)
interp if more than 2 inks partial? sounds best
get >= 1.0 map
get any > 0.0 map
then interp?
lookupSolidMap
lookupFractionMap
solidsXYZ
fractionsXYZ
solidsABC solidsABCD, interp from ABC to ABCD ?
seems effectively hedral interpolation
need to lookup per fractional channel
*/
// returns bitmap and overprint XYZ value
uint32_t lookup_overprints( const inkColorSet &inkSet,
const std::vector<float> &inkFractionList,
size_t inkCount,
xyzColor &overprint_result )
{
uint32_t opBitmap = 0;
// see if we have an overprint that can be looked up
if (inkSet.overprints.size() > 0) {
uint32_t lookupBitmap = 0;
int bitCount = 0;
for (size_t i = 0; i < inkCount; ++i) {
float thisFraction = inkFractionList[i];
if (thisFraction >= 1.0) {
lookupBitmap |= (1UL << i);
++bitCount;
}
}
if (bitCount > 1) {
// do we have a matching overprint?
auto iter = inkSet.overprint_bitmask_map.find(lookupBitmap);
if ( iter != inkSet.overprint_bitmask_map.end() ) {
size_t opIndex = (size_t) iter->second;
opBitmap = inkSet.overprints[ opIndex ].inkBitmap;
overprint_result = inkSet.overprints[ opIndex ].colorXYZ;
}
// Can we at least come close? Need closest match ignoring some channels, while still having 2 or more channels.
// TODO - Or should I add fake entries to the map for this? preprocess sounds saner.
}
}
return opBitmap;
}
/********************************************************************************/
// trying to estimate appearance of overprints among arbitrary inks
static
xyzColor estimate_fractional_ink_mix( const inkColorSet &inkSet, const std::vector<xyzColor> &inkList,
const std::vector<float> &inkFractionList, const xyzColor &paperColor, size_t inkCount )
{
assert( inkCount >= 1 && inkCount <= kMaxChannels );
xyzColor overprint = identityXYZ;
#if 1
// find any known overprints for these inks
uint32_t opBitmap = lookup_overprints( inkSet, inkFractionList, inkCount, overprint );
// interp and multiply remaining inks that weren't in the overprint data
for (size_t i = 0; i < inkCount; ++i) {
if ((opBitmap & (1UL << i)) != 0)
continue;
float thisFraction = inkFractionList[i];
if (thisFraction > 0.0) {
auto &ink = inkList[i];
xyzColor fractionalInk = interp2inks( thisFraction, identityXYZ, ink );
overprint *= fractionalInk;
}
}
#else
for (int i = 0; i < inkCount; ++i) {
auto &ink = inkList[i];
float thisFraction = inkFractionList[i];
if (thisFraction > 0.0) {
xyzColor fractionalInk = interp2inks( thisFraction, identityXYZ, ink );
overprint *= fractionalInk;
}
}
#endif
overprint *= paperColor;
return overprint;
}
/********************************************************************************/
// here, we want the darkest possible result - which mixes all the inks
// DEFERRED - should prefer a near neutral.
static
xyzColor estimate_darkest_ink_overprint( const std::vector<xyzColor> &inkList, const xyzColor &paperColor )
{
const float Ylimit = 1.3f; // author's preference
xyzColor overprint = identityXYZ;
for ( const auto &ink : inkList ) {
overprint *= ink;
}
overprint *= paperColor;
// Bring down luminance if needed to give a reasonable visual result
// Scaling all channels to reduce chroma of the overprint
float maxVal = std::max( overprint.X, std::max( overprint.Y, overprint.Z ));
if (maxVal > Ylimit) {
float scale = Ylimit / maxVal;
overprint *= scale;
}
return overprint;
}
/********************************************************************************/
// convenience converter
static
xyzColor estimate_darkest_ink_overprint( const std::vector<namedColor> &inkList, const xyzColor &paperColor )
{
std::vector<xyzColor> tempListXYZ( inkList.size() );
for (size_t i = 0; i < inkList.size(); ++i)
tempListXYZ[i] = LAB2XYZ( inkList[i].color ) / paperColor;
return estimate_darkest_ink_overprint( tempListXYZ, paperColor );
}
/********************************************************************************/
static
void subdivide_ink_splines( inkColorSet &inkSet, const int divisions, const int steps,
const size_t ink1Index, const size_t ink2Index, const xyzColor &paperColor,
const std::vector<xyzColor> &inkListXYZ )
{
color_list temp; // reuse to reduce allocations/deletions
labColor ink1 = inkSet.primaries[ink1Index].color;
labColor ink2 = inkSet.primaries[ink2Index].color;
xyzColor ink1Color = LAB2XYZ( ink1 );
xyzColor ink2Color = LAB2XYZ( ink2 );
std::vector<float> mixFractions = { 0.25, 0.5, 0.75, 1.0 };
std::vector<labColor> samplesLAB( 2 + mixFractions.size() );
std::vector<xyzColor> samplesXYZ( mixFractions.size() );
std::vector<xyzColor> mixesXYZ( mixFractions.size() );
std::vector<float> inkFractions(kMaxChannels,0.0f);
for (size_t i = 0; i < mixFractions.size(); ++i) {
auto thisFraction = mixFractions[i];
inkFractions[ink1Index] = thisFraction;
inkFractions[ink2Index] = thisFraction;
xyzColor halfwayMix = estimate_fractional_ink_mix( inkSet, inkListXYZ, inkFractions, paperColor, kMaxChannels );
samplesXYZ[i] = halfwayMix;
}
// d == 0 is the last pure ink spline
// d == division is this pure ink spline (handled elsewhere)
for (int d = 1; d < divisions; ++d) {
float t = (float)d / (float)divisions;
float t1 = 1.0f;
float t2 = 1.0f;
if (t <= 0.5f) {
for (size_t i = 0; i < mixFractions.size(); ++i) {
xyzColor mix1 = interp2inks( t*2.0f, ink1Color, samplesXYZ[i] );
mixesXYZ[i] = mix1;
}
t2 = t*2.0f; // going 0 -> 1
}
else {
for (size_t i = 0; i < mixFractions.size(); ++i) {
xyzColor mix2 = interp2inks( (t-0.5f)*2.0f, samplesXYZ[i], ink2Color );
mixesXYZ[i] = mix2;
}
t1 = (1.0f - t) * 2.0f; // going 1 -> 0
}
samplesLAB[0] = inkSet.paperColor;
for (size_t i = 0; i < mixFractions.size(); ++i)
samplesLAB[1+i] = XYZ2LAB( mixesXYZ[i] );
samplesLAB[1 + mixFractions.size()] = inkSet.darkColor;
temp = mix_overprint_ink_spline2( 2.0, samplesLAB );
inkSet.splines.push_back( temp );
inkSet.mixData.push_back( inkMixPair( ink1Index, ink2Index, t1, t2 ) );
}
}
/********************************************************************************/
static
void prepare_ink_dark( inkColorSet &inkSet )
{
// If the combined color has <= 0 L, estimate it from primaries
// so we can get something sort of realistic for the mix
if (inkSet.darkColor.L <= 0) {
xyzColor paperColor = LAB2XYZ( inkSet.paperColor );
xyzColor mix = estimate_darkest_ink_overprint( inkSet.primaries, paperColor );
labColor mixLAB = XYZ2LAB( mix );
if (globalSettings.gDebugMode)
printf("Estimated overprint for %s is (%f, %f, %f)\n",
inkSet.name.c_str(),
mixLAB.L, mixLAB.A, mixLAB.B );
inkSet.darkColor = mixLAB;
}
}
/********************************************************************************/
// create splines from mixes of inks and paper colors
static
void mix_ink_splines( inkColorSet &inkSet )
{
const int steps = 51; // odd so we have a midpoint
const int divisions = 16; // even so we have a midpoint (17 splines per surface)
size_t inkCount = inkSet.primaries.size();
assert(inkCount > 0 && inkCount <= kMaxChannels);
xyzColor paperColor = LAB2XYZ( inkSet.paperColor );
std::vector<xyzColor> inkListXYZ(kMaxChannels);
for (size_t i = 0; i < inkCount; ++i)
inkListXYZ[i] = LAB2XYZ(inkSet.primaries[i].color) / paperColor;
// first ink spline, always calculated
color_list temp = mix_pure_ink_spline( steps, inkSet.paperColor, inkSet.primaries[0].color, inkSet.darkColor );
inkSet.splines.push_back( temp );
inkSet.mixData.push_back( inkMixPair( 0, 0, 1.0, 0.0 ) );
// iterate any additional inks, keeping splines in order
for (size_t k = 1; k < inkCount; ++k) {
subdivide_ink_splines( inkSet, divisions, steps,
k-1, k,
paperColor, inkListXYZ );
// pure ink spline paper->ink2->dark
temp = mix_pure_ink_spline( steps, inkSet.paperColor, inkSet.primaries[k].color, inkSet.darkColor );
inkSet.splines.push_back( temp );
inkSet.mixData.push_back( inkMixPair( k, k, 1.0, 0.0 ) );
}
// if we can make a solid, then wrap around from last ink to the first!
if (inkCount > 2) {
subdivide_ink_splines( inkSet, divisions, steps,
inkCount-1, 0,
paperColor, inkListXYZ );
}
assert( inkSet.splines.size() == inkSet.mixData.size() );
}
/********************************************************************************/
// t ranges 0..1.0
// we are interpolating between B and C
static
float SplineInterp( float t, float A, float B, float C, float D )
{
#if 0
// debug
return LERP( t, B, C );
#else
// catmull rom - cardinal spline with tension = 0.5
// needs scaling by 0.5 at end
const float M11 = -1.0, M12 = 3.0, M13 = -3.0, M14 = 1.0;
const float M21 = 2.0, M22 = -5.0, M23 = 4.0, M24 = -1.0;
const float M31 = -1.0, M32 = 0.0, M33 = 1.0, M34 = 0.0;
const float M41 = 0.0, M42 = 2.0, M43 = 0.0, M44 = 0.0;
// cubic interp
float value;
value = A * (M41 + t * (M31 + t * (M21 + t*M11) ) );
value += B * (M42 + t * (M32 + t * (M22 + t*M12) ) );
value += C * (M43 + t * (M33 + t * (M23 + t*M13) ) );
value += D * (M44 + t * (M34 + t * (M24 + t*M14) ) );
return 0.5f * value;
#endif
}
/********************************************************************************/
// need function to search spline for correct point and return interpolated values
// given L*, binary search the spline and return the A and B values that go with it
static
void SearchSpline( const color_list &spline, float Ltarget, float &A, float &B )
{
// find points in list that bracket L
// list is greatest to least (white to black)
// ccox - start with a simple linear search
size_t index = 1;
for ( ; index < spline.size(); ++index) {
if (spline[index].L <= Ltarget)
break;
}
assert( index < spline.size() );
// find t that gives the correct L to within tolerance (between index-1 and index)
int sample0 = (int)index - 2;
int sample1 = (int)index - 1;
int sample2 = (int)index;
int sample3 = (int)index + 1;
// clip to end points
if (sample0 < 0) sample0 = 0;
if (sample1 < 0) sample1 = 0;
if (sample3 > (int)(spline.size())-1) sample3 = (int)(spline.size())-1;
// quick and dirty binary search
float t = 0.5f;
const float Ltolerance = 0.1f; // this seems to be good enough
float Ttop = 0.0f;
float Tbottom = 1.0f;
float Ltest = SplineInterp( t, spline[(size_t)sample0].L, spline[(size_t)sample1].L, spline[(size_t)sample2].L, spline[(size_t)sample3].L );
// quick and dirty binary search
while ( fabsf( Ltest - Ltarget ) > Ltolerance) {
if (Ltest < Ltarget) {
// between top and current
Tbottom = t;
} else {
// between current and bottom
Ttop = t;
}
t = (Ttop + Tbottom) * 0.5f;
Ltest = SplineInterp( t, spline[(size_t)sample0].L, spline[(size_t)sample1].L, spline[(size_t)sample2].L, spline[(size_t)sample3].L );
}
// interpolate colors and return result
A = SplineInterp( t, spline[(size_t)sample0].A, spline[(size_t)sample1].A, spline[(size_t)sample2].A, spline[(size_t)sample3].A );
B = SplineInterp( t, spline[(size_t)sample0].B, spline[(size_t)sample1].B, spline[(size_t)sample2].B, spline[(size_t)sample3].B );
}
/********************************************************************************/
// are we less than our darkest, or greater than our brightest point?
static
bool ClippedL( float Linput, labColor &output, const inkColorSet &inkSet )
{
output.L = output.A = output.B = 0.0f;
if (Linput < inkSet.darkColor.L) {
output = inkSet.darkColor;
return true;
}
if (Linput > inkSet.paperColor.L) {
output = inkSet.paperColor;
return true;
}
return false;
}
/********************************************************************************/
static
void SplineInterpList( const size_t subdivisions, const PointList &input, PointList &result,
bool wrapAround )
{
const int pointCount = (int)input.size();
result.reserve( subdivisions+1 );
// iterate through list
for (size_t i = 0; i <= subdivisions; ++i) {
/// which input points are we between?
float floatIndex = ((float)pointCount * i) / (float)subdivisions;
int pointIndex = int( floatIndex );
int p0 = pointIndex - 1;
int p1 = pointIndex;
int p2 = pointIndex + 1;
int p3 = pointIndex + 2;
if (wrapAround) {
// wrap around if inks > 2, so we get a solid shape
if (p0 < 0)
p0 = p0 + pointCount;
if (p1 < 0)
p1 = p1 + pointCount;
if (p1 >= pointCount)
p1 = p1 - pointCount;
if (p2 >= pointCount)
p2 = p2 - pointCount;
if (p3 >= pointCount)
p3 = p3 - pointCount;
} else {
// clamp
if (p0 < 0) p0 = 0;
if (p1 < 0) p1 = 0;
if (p1 >= pointCount) p1 = pointCount-1;
if (p2 >= pointCount) p2 = pointCount-1;
if (p3 >= pointCount) p3 = pointCount-1;
}
float t = floatIndex - pointIndex;
Point newPoint;
newPoint.a = SplineInterp( t, input[(size_t)p0].a, input[(size_t)p1].a, input[(size_t)p2].a, input[(size_t)p3].a );
newPoint.b = SplineInterp( t, input[(size_t)p0].b, input[(size_t)p1].b, input[(size_t)p2].b, input[(size_t)p3].b );
result.emplace_back( newPoint );
} // end for subdivisions
}
/********************************************************************************/
static
void LinearInterpList( const size_t subdivisions, const PointList &input, PointList &result,
bool wrapAround )
{
const int pointCount = (int)input.size();
result.reserve( subdivisions+1 );
for (size_t i = 0; i <= subdivisions; ++i) {
/// which input points are we between?
float floatIndex = ((float)pointCount * i) / (float)subdivisions;
int pointIndex = int( floatIndex );
int p1 = pointIndex;
int p2 = pointIndex + 1;
if (wrapAround) {
// wrap around if inks > 2, so we get a solid shape
if (p1 < 0)
p1 = p1 + pointCount;
if (p1 >= pointCount)
p1 = p1 - pointCount;
if (p2 >= pointCount)
p2 = p2 - pointCount;
} else {
// clamp
if (p1 < 0) p1 = 0;
if (p1 >= pointCount) p1 = pointCount-1;
if (p2 >= pointCount) p2 = pointCount-1;
}
float t = floatIndex - pointIndex;
Point newPoint;
newPoint.a = LERP( t, input[(size_t)p1].a, input[(size_t)p2].a );
newPoint.b = LERP( t, input[(size_t)p1].b, input[(size_t)p2].b );
result.emplace_back( newPoint );
} // end for subdivisions
}
/********************************************************************************/
static
void PointListFromSplines( const size_t subdivisions, const PointList &input, PointList &result,
bool wrapAround)
{
// evaluate the spline at a fixed number of points, put those into a list of points
const int pointCount = (int)input.size();
if (pointCount == 1) {
// nothing to interpolate
result = input;
return;
}
result.clear();
if (pointCount < 4)
LinearInterpList( subdivisions, input, result, wrapAround );
else
SplineInterpList( subdivisions, input, result, wrapAround );
}
/********************************************************************************/
static
void InterpMixList( const size_t subdivisions, const spline_mix_data &input, spline_mix_data &result,
bool wrapAround )
{
const int pointCount = (int)input.size();
result.reserve( subdivisions+1 );
for (size_t i = 0; i <= subdivisions; ++i) {
/// which input points are we between?
float floatIndex = ((float)pointCount * i) / (float)subdivisions;
int pointIndex = int( floatIndex );
int p1 = pointIndex;
int p2 = pointIndex + 1;
if (wrapAround) {
// wrap around if inks > 2, so we get a solid shape
if (p1 < 0)
p1 = p1 + pointCount;
if (p1 >= pointCount)
p1 = p1 - pointCount;
if (p2 >= pointCount)
p2 = p2 - pointCount;
} else {
// clamp
if (p1 < 0) p1 = 0;
if (p1 >= pointCount) p1 = pointCount-1;
if (p2 >= pointCount) p2 = pointCount-1;
}
float t = floatIndex - pointIndex;
// I need the ink numbers, not spline numbers!
// could have 2 or 3 different inks specified (on boundaries)
// need to figure out which pair we're interpolating
size_t index1 = input[(size_t)p1].inkIndex1;
size_t index2 = input[(size_t)p1].inkIndex2;
if (input[(size_t)p2].ink1Fraction > input[(size_t)p1].ink1Fraction)
index1 = input[(size_t)p2].inkIndex1;
if (input[(size_t)p2].ink2Fraction > input[(size_t)p1].ink2Fraction)
index2 = input[(size_t)p2].inkIndex2;
float fraction1 = LERP( t, input[(size_t)p1].ink1Fraction, input[(size_t)p2].ink1Fraction );
float fraction2 = LERP( t, input[(size_t)p1].ink2Fraction, input[(size_t)p2].ink2Fraction );
// fractions will not add up to unity, because we have overprints!
result.emplace_back( inkMixPair(index1,index2,fraction1,fraction2 ) );
} // end for subdivisions
}
/********************************************************************************/
static
void MixPointsFromSplines( const size_t subdivisions, const spline_mix_data &input, spline_mix_data &result,
bool wrapAround)
{
// evaluate the spline at a fixed number of points, put those into a list of points
const int pointCount = (int)input.size();
if (pointCount == 1) {
// nothing to interpolate
result = input;
return;
}
result.clear();
InterpMixList( subdivisions, input, result, wrapAround );
}
/********************************************************************************/
static
size_t FindClosestPointInList( const PointList &list, Point &input )
{
// ccox - start with brute force linear search
/*
DEFERRED - find a way to accelerate the search
list is always more or less circular
needs to work with points inside and outside, and FAR outside
But we only execute this for one slice at a time -- so 21x21 points
then change data
Could use radial projection for angles, with precomputed angles - we don't have the center here
Could use grid to narrow search - with aux data structure
Could limit the points by removing those closer than 0.1 deltaE
Count is between 1 and 300
*/
size_t count = list.size();
assert( count > 0 );
if (count == 1)
return 0;
float closest_dist = 1e20f; // much greater than our maximum possible distance
size_t closest_index = size_t(-1); // really largest positive value because it is unsigned
for (size_t i = 0; i < count; ++i) {
float distA = input.a - list[i].a;
float distB = input.b - list[i].b;
float dist = distA*distA + distB*distB; // leave it squared
if (dist < closest_dist) {
closest_dist = dist;
closest_index = i;
}
}
assert( closest_index >= 0);
return closest_index;
}
/********************************************************************************/
// really simple tent, with a twist to reduce scum dots
inline
float Smooth3( float a, float b, float c)
{
// scum dot reduction
if (b == 1.0f || b == 0.0f)
return b;
return (a + 4*b + c) / 6.0f;
}
/********************************************************************************/
// prev, current, next
inline
void constexpr Smooth3( std::vector<float> &a, const std::vector<float> &b, const std::vector<float> &c, size_t channels)
{
for (size_t i = 0; i < channels; ++i)
a[i] = Smooth3(a[i],b[i],c[i]);
}
// prev, current, next
inline
void constexpr Smooth3( float *a, float *b, float *c, size_t channels)
{
for (size_t i = 0; i < channels; ++i)
a[i] = Smooth3(a[i],b[i],c[i]);
}
/********************************************************************************/
// filter in place, in one dimension, for 3 channels
static
void SmoothOneDirection3( float *data, size_t gridPoints, size_t planeStep, size_t rowStep, size_t colStep )
{
size_t i, j, k;
for (i = 0; i < gridPoints; ++i) {
for (j = 0; j < gridPoints; ++j) {
k = 0;
// special case first value
float last0 = data[ i * planeStep + j * rowStep + k*colStep + 0 ];
float last1 = data[ i * planeStep + j * rowStep + k*colStep + 1 ];
float last2 = data[ i * planeStep + j * rowStep + k*colStep + 2 ];