-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function.cc
1707 lines (1575 loc) · 48.5 KB
/
Function.cc
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
//========================================================================
//
// Function.cc
//
// Copyright 2001-2003 Glyph & Cog, LLC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2006, 2008-2010, 2013-2015, 2017-2020, 2022, 2023 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2006 Jeff Muizelaar <jeff@infidigm.net>
// Copyright (C) 2010 Christian Feuersänger <cfeuersaenger@googlemail.com>
// Copyright (C) 2011 Andrea Canciani <ranma42@gmail.com>
// Copyright (C) 2012 Thomas Freitag <Thomas.Freitag@alfa.de>
// Copyright (C) 2012 Adam Reichold <adamreichold@myopera.com>
// Copyright (C) 2013 Fabio D'Urso <fabiodurso@hotmail.it>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#include <config.h>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include "goo/gmem.h"
#include "goo/gstrtod.h"
#include "Object.h"
#include "Dict.h"
#include "Stream.h"
#include "Error.h"
#include "Function.h"
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
//------------------------------------------------------------------------
// Function
//------------------------------------------------------------------------
Function::Function() : domain {} { }
Function::~Function() { }
Function *Function::parse(Object *funcObj)
{
std::set<int> usedParents;
return parse(funcObj, &usedParents);
}
Function *Function::parse(Object *funcObj, std::set<int> *usedParents)
{
Function *func;
Dict *dict;
int funcType;
if (funcObj->isStream()) {
dict = funcObj->streamGetDict();
} else if (funcObj->isDict()) {
dict = funcObj->getDict();
} else if (funcObj->isName("Identity")) {
return new IdentityFunction();
} else {
error(errSyntaxError, -1, "Expected function dictionary or stream");
return nullptr;
}
Object obj1 = dict->lookup("FunctionType");
if (!obj1.isInt()) {
error(errSyntaxError, -1, "Function type is missing or wrong type");
return nullptr;
}
funcType = obj1.getInt();
if (funcType == 0) {
func = new SampledFunction(funcObj, dict);
} else if (funcType == 2) {
func = new ExponentialFunction(funcObj, dict);
} else if (funcType == 3) {
func = new StitchingFunction(funcObj, dict, usedParents);
} else if (funcType == 4) {
func = new PostScriptFunction(funcObj, dict);
} else {
error(errSyntaxError, -1, "Unimplemented function type ({0:d})", funcType);
return nullptr;
}
if (!func->isOk()) {
delete func;
return nullptr;
}
return func;
}
Function::Function(const Function *func)
{
m = func->m;
n = func->n;
memcpy(domain, func->domain, funcMaxInputs * 2 * sizeof(double));
memcpy(range, func->range, funcMaxOutputs * 2 * sizeof(double));
hasRange = func->hasRange;
}
bool Function::init(Dict *dict)
{
Object obj1;
int i;
//----- Domain
obj1 = dict->lookup("Domain");
if (!obj1.isArray()) {
error(errSyntaxError, -1, "Function is missing domain");
return false;
}
m = obj1.arrayGetLength() / 2;
if (m > funcMaxInputs) {
error(errSyntaxError, -1, "Functions with more than {0:d} inputs are unsupported", funcMaxInputs);
return false;
}
for (i = 0; i < m; ++i) {
Object obj2 = obj1.arrayGet(2 * i);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Illegal value in function domain array");
return false;
}
domain[i][0] = obj2.getNum();
obj2 = obj1.arrayGet(2 * i + 1);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Illegal value in function domain array");
return false;
}
domain[i][1] = obj2.getNum();
}
//----- Range
hasRange = false;
n = 0;
obj1 = dict->lookup("Range");
if (obj1.isArray()) {
hasRange = true;
n = obj1.arrayGetLength() / 2;
if (n > funcMaxOutputs) {
error(errSyntaxError, -1, "Functions with more than {0:d} outputs are unsupported", funcMaxOutputs);
return false;
}
for (i = 0; i < n; ++i) {
Object obj2 = obj1.arrayGet(2 * i);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Illegal value in function range array");
return false;
}
range[i][0] = obj2.getNum();
obj2 = obj1.arrayGet(2 * i + 1);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Illegal value in function range array");
return false;
}
range[i][1] = obj2.getNum();
}
}
return true;
}
//------------------------------------------------------------------------
// IdentityFunction
//------------------------------------------------------------------------
IdentityFunction::IdentityFunction()
{
int i;
// fill these in with arbitrary values just in case they get used
// somewhere
m = funcMaxInputs;
n = funcMaxOutputs;
for (i = 0; i < funcMaxInputs; ++i) {
domain[i][0] = 0;
domain[i][1] = 1;
}
hasRange = false;
}
IdentityFunction::~IdentityFunction() { }
void IdentityFunction::transform(const double *in, double *out) const
{
int i;
for (i = 0; i < funcMaxOutputs; ++i) {
out[i] = in[i];
}
}
//------------------------------------------------------------------------
// SampledFunction
//------------------------------------------------------------------------
SampledFunction::SampledFunction(Object *funcObj, Dict *dict) : cacheOut {}
{
Stream *str;
int sampleBits;
double sampleMul;
Object obj1;
unsigned int buf, bitMask;
int bits;
unsigned int s;
double in[funcMaxInputs];
int i, j, t, bit, idx;
idxOffset = nullptr;
samples = nullptr;
sBuf = nullptr;
ok = false;
//----- initialize the generic stuff
if (!init(dict)) {
return;
}
if (!hasRange) {
error(errSyntaxError, -1, "Type 0 function is missing range");
return;
}
if (m > sampledFuncMaxInputs) {
error(errSyntaxError, -1, "Sampled functions with more than {0:d} inputs are unsupported", sampledFuncMaxInputs);
return;
}
//----- buffer
sBuf = (double *)gmallocn(1 << m, sizeof(double));
//----- get the stream
if (!funcObj->isStream()) {
error(errSyntaxError, -1, "Type 0 function isn't a stream");
return;
}
str = funcObj->getStream();
//----- Size
obj1 = dict->lookup("Size");
if (!obj1.isArray() || obj1.arrayGetLength() != m) {
error(errSyntaxError, -1, "Function has missing or invalid size array");
return;
}
for (i = 0; i < m; ++i) {
Object obj2 = obj1.arrayGet(i);
if (!obj2.isInt()) {
error(errSyntaxError, -1, "Illegal value in function size array");
return;
}
sampleSize[i] = obj2.getInt();
if (sampleSize[i] <= 0) {
error(errSyntaxError, -1, "Illegal non-positive value in function size array");
return;
}
}
idxOffset = (int *)gmallocn(1 << m, sizeof(int));
for (i = 0; i < (1 << m); ++i) {
idx = 0;
for (j = m - 1, t = i; j >= 1; --j, t <<= 1) {
if (sampleSize[j] == 1) {
bit = 0;
} else {
bit = (t >> (m - 1)) & 1;
}
idx = (idx + bit) * sampleSize[j - 1];
}
if (m > 0 && sampleSize[0] == 1) {
bit = 0;
} else {
bit = (t >> (m - 1)) & 1;
}
idxOffset[i] = (idx + bit) * n;
}
//----- BitsPerSample
obj1 = dict->lookup("BitsPerSample");
if (!obj1.isInt()) {
error(errSyntaxError, -1, "Function has missing or invalid BitsPerSample");
return;
}
sampleBits = obj1.getInt();
if (unlikely(sampleBits < 1 || sampleBits > 32)) {
error(errSyntaxError, -1, "Function invalid BitsPerSample");
return;
}
sampleMul = 1.0 / (pow(2.0, (double)sampleBits) - 1);
//----- Encode
obj1 = dict->lookup("Encode");
if (obj1.isArray() && obj1.arrayGetLength() == 2 * m) {
for (i = 0; i < m; ++i) {
Object obj2 = obj1.arrayGet(2 * i);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Illegal value in function encode array");
return;
}
encode[i][0] = obj2.getNum();
obj2 = obj1.arrayGet(2 * i + 1);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Illegal value in function encode array");
return;
}
encode[i][1] = obj2.getNum();
}
} else {
for (i = 0; i < m; ++i) {
encode[i][0] = 0;
encode[i][1] = sampleSize[i] - 1;
}
}
for (i = 0; i < m; ++i) {
if (unlikely((domain[i][1] - domain[i][0]) == 0)) {
error(errSyntaxError, -1, "Illegal value in function domain array");
return;
}
inputMul[i] = (encode[i][1] - encode[i][0]) / (domain[i][1] - domain[i][0]);
}
//----- Decode
obj1 = dict->lookup("Decode");
if (obj1.isArray() && obj1.arrayGetLength() == 2 * n) {
for (i = 0; i < n; ++i) {
Object obj2 = obj1.arrayGet(2 * i);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Illegal value in function decode array");
return;
}
decode[i][0] = obj2.getNum();
obj2 = obj1.arrayGet(2 * i + 1);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Illegal value in function decode array");
return;
}
decode[i][1] = obj2.getNum();
}
} else {
for (i = 0; i < n; ++i) {
decode[i][0] = range[i][0];
decode[i][1] = range[i][1];
}
}
//----- samples
nSamples = n;
for (i = 0; i < m; ++i) {
nSamples *= sampleSize[i];
}
samples = (double *)gmallocn_checkoverflow(nSamples, sizeof(double));
if (!samples) {
error(errSyntaxError, -1, "Function has invalid number of samples");
return;
}
buf = 0;
bits = 0;
bitMask = (1 << sampleBits) - 1;
str->reset();
for (i = 0; i < nSamples; ++i) {
if (sampleBits == 8) {
s = str->getChar();
} else if (sampleBits == 16) {
s = str->getChar();
s = (s << 8) + str->getChar();
} else if (sampleBits == 32) {
s = str->getChar();
s = (s << 8) + str->getChar();
s = (s << 8) + str->getChar();
s = (s << 8) + str->getChar();
} else {
while (bits < sampleBits) {
buf = (buf << 8) | (str->getChar() & 0xff);
bits += 8;
}
s = (buf >> (bits - sampleBits)) & bitMask;
bits -= sampleBits;
}
samples[i] = (double)s * sampleMul;
}
str->close();
// set up the cache
for (i = 0; i < m; ++i) {
in[i] = domain[i][0];
cacheIn[i] = in[i] - 1;
}
transform(in, cacheOut);
ok = true;
}
SampledFunction::~SampledFunction()
{
if (idxOffset) {
gfree(idxOffset);
}
if (samples) {
gfree(samples);
}
if (sBuf) {
gfree(sBuf);
}
}
SampledFunction::SampledFunction(const SampledFunction *func) : Function(func)
{
memcpy(sampleSize, func->sampleSize, funcMaxInputs * sizeof(int));
memcpy(encode, func->encode, funcMaxInputs * 2 * sizeof(double));
memcpy(decode, func->decode, funcMaxOutputs * 2 * sizeof(double));
memcpy(inputMul, func->inputMul, funcMaxInputs * sizeof(double));
nSamples = func->nSamples;
idxOffset = (int *)gmallocn(1 << m, sizeof(int));
memcpy(idxOffset, func->idxOffset, (1 << m) * (int)sizeof(int));
samples = (double *)gmallocn(nSamples, sizeof(double));
memcpy(samples, func->samples, nSamples * sizeof(double));
sBuf = (double *)gmallocn(1 << m, sizeof(double));
memcpy(cacheIn, func->cacheIn, funcMaxInputs * sizeof(double));
memcpy(cacheOut, func->cacheOut, funcMaxOutputs * sizeof(double));
ok = func->ok;
}
void SampledFunction::transform(const double *in, double *out) const
{
double x;
int e[funcMaxInputs];
double efrac0[funcMaxInputs];
double efrac1[funcMaxInputs];
// check the cache
bool inCache = true;
for (int i = 0; i < m; ++i) {
if (in[i] != cacheIn[i]) {
inCache = false;
break;
}
}
if (inCache) {
for (int i = 0; i < n; ++i) {
out[i] = cacheOut[i];
}
return;
}
// map input values into sample array
for (int i = 0; i < m; ++i) {
x = (in[i] - domain[i][0]) * inputMul[i] + encode[i][0];
if (x < 0 || std::isnan(x)) {
x = 0;
} else if (x > sampleSize[i] - 1) {
x = sampleSize[i] - 1;
}
e[i] = (int)x;
if (e[i] == sampleSize[i] - 1 && sampleSize[i] > 1) {
// this happens if in[i] = domain[i][1]
e[i] = sampleSize[i] - 2;
}
efrac1[i] = x - e[i];
efrac0[i] = 1 - efrac1[i];
}
// compute index for the first sample to be used
int idx0 = 0;
for (int k = m - 1; k >= 1; --k) {
idx0 = (idx0 + e[k]) * sampleSize[k - 1];
}
idx0 = (idx0 + e[0]) * n;
// for each output, do m-linear interpolation
for (int i = 0; i < n; ++i) {
// pull 2^m values out of the sample array
for (int j = 0; j < (1 << m); ++j) {
int idx = idx0 + idxOffset[j] + i;
if (likely(idx >= 0 && idx < nSamples)) {
sBuf[j] = samples[idx];
} else {
sBuf[j] = 0; // TODO Investigate if this is what Adobe does
}
}
// do m sets of interpolations
for (int j = 0, t = (1 << m); j < m; ++j, t >>= 1) {
for (int k = 0; k < t; k += 2) {
sBuf[k >> 1] = efrac0[j] * sBuf[k] + efrac1[j] * sBuf[k + 1];
}
}
// map output value to range
out[i] = sBuf[0] * (decode[i][1] - decode[i][0]) + decode[i][0];
if (out[i] < range[i][0]) {
out[i] = range[i][0];
} else if (out[i] > range[i][1]) {
out[i] = range[i][1];
}
}
// save current result in the cache
for (int i = 0; i < m; ++i) {
cacheIn[i] = in[i];
}
for (int i = 0; i < n; ++i) {
cacheOut[i] = out[i];
}
}
bool SampledFunction::hasDifferentResultSet(const Function *func) const
{
if (func->getType() == 0) {
SampledFunction *compTo = (SampledFunction *)func;
if (compTo->getSampleNumber() != nSamples) {
return true;
}
const double *compSamples = compTo->getSamples();
for (int i = 0; i < nSamples; i++) {
if (samples[i] != compSamples[i]) {
return true;
}
}
}
return false;
}
//------------------------------------------------------------------------
// ExponentialFunction
//------------------------------------------------------------------------
ExponentialFunction::ExponentialFunction(Object *funcObj, Dict *dict)
{
Object obj1;
ok = false;
//----- initialize the generic stuff
if (!init(dict)) {
return;
}
if (m != 1) {
error(errSyntaxError, -1, "Exponential function with more than one input");
return;
}
//----- C0
obj1 = dict->lookup("C0");
if (obj1.isArray()) {
if (hasRange && obj1.arrayGetLength() != n) {
error(errSyntaxError, -1, "Function's C0 array is wrong length");
return;
}
n = obj1.arrayGetLength();
if (unlikely(n > funcMaxOutputs)) {
error(errSyntaxError, -1, "Function's C0 array is wrong length");
n = funcMaxOutputs;
}
for (int i = 0; i < n; ++i) {
Object obj2 = obj1.arrayGet(i);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Illegal value in function C0 array");
return;
}
c0[i] = obj2.getNum();
}
} else {
if (hasRange && n != 1) {
error(errSyntaxError, -1, "Function's C0 array is wrong length");
return;
}
n = 1;
c0[0] = 0;
}
//----- C1
obj1 = dict->lookup("C1");
if (obj1.isArray()) {
if (obj1.arrayGetLength() != n) {
error(errSyntaxError, -1, "Function's C1 array is wrong length");
return;
}
for (int i = 0; i < n; ++i) {
Object obj2 = obj1.arrayGet(i);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Illegal value in function C1 array");
return;
}
c1[i] = obj2.getNum();
}
} else {
if (n != 1) {
error(errSyntaxError, -1, "Function's C1 array is wrong length");
return;
}
c1[0] = 1;
}
//----- N (exponent)
obj1 = dict->lookup("N");
if (!obj1.isNum()) {
error(errSyntaxError, -1, "Function has missing or invalid N");
return;
}
e = obj1.getNum();
isLinear = fabs(e - 1.) < 1e-10;
ok = true;
}
ExponentialFunction::~ExponentialFunction() { }
ExponentialFunction::ExponentialFunction(const ExponentialFunction *func) : Function(func)
{
memcpy(c0, func->c0, funcMaxOutputs * sizeof(double));
memcpy(c1, func->c1, funcMaxOutputs * sizeof(double));
e = func->e;
isLinear = func->isLinear;
ok = func->ok;
}
void ExponentialFunction::transform(const double *in, double *out) const
{
double x;
int i;
if (in[0] < domain[0][0]) {
x = domain[0][0];
} else if (in[0] > domain[0][1]) {
x = domain[0][1];
} else {
x = in[0];
}
for (i = 0; i < n; ++i) {
out[i] = c0[i] + (isLinear ? x : pow(x, e)) * (c1[i] - c0[i]);
if (hasRange) {
if (out[i] < range[i][0]) {
out[i] = range[i][0];
} else if (out[i] > range[i][1]) {
out[i] = range[i][1];
}
}
}
return;
}
//------------------------------------------------------------------------
// StitchingFunction
//------------------------------------------------------------------------
StitchingFunction::StitchingFunction(Object *funcObj, Dict *dict, std::set<int> *usedParents)
{
Object obj1;
int i;
ok = false;
funcs = nullptr;
bounds = nullptr;
encode = nullptr;
scale = nullptr;
//----- initialize the generic stuff
if (!init(dict)) {
return;
}
if (m != 1) {
error(errSyntaxError, -1, "Stitching function with more than one input");
return;
}
//----- Functions
obj1 = dict->lookup("Functions");
if (!obj1.isArray()) {
error(errSyntaxError, -1, "Missing 'Functions' entry in stitching function");
return;
}
k = obj1.arrayGetLength();
funcs = (Function **)gmallocn(k, sizeof(Function *));
bounds = (double *)gmallocn(k + 1, sizeof(double));
encode = (double *)gmallocn(2 * k, sizeof(double));
scale = (double *)gmallocn(k, sizeof(double));
for (i = 0; i < k; ++i) {
funcs[i] = nullptr;
}
for (i = 0; i < k; ++i) {
std::set<int> usedParentsAux = *usedParents;
Ref ref;
Object obj2 = obj1.getArray()->get(i, &ref);
if (ref != Ref::INVALID()) {
if (usedParentsAux.find(ref.num) == usedParentsAux.end()) {
usedParentsAux.insert(ref.num);
} else {
return;
}
}
if (!(funcs[i] = Function::parse(&obj2, &usedParentsAux))) {
return;
}
if (funcs[i]->getInputSize() != 1 || (i > 0 && funcs[i]->getOutputSize() != funcs[0]->getOutputSize())) {
error(errSyntaxError, -1, "Incompatible subfunctions in stitching function");
return;
}
}
//----- Bounds
obj1 = dict->lookup("Bounds");
if (!obj1.isArray() || obj1.arrayGetLength() != k - 1) {
error(errSyntaxError, -1, "Missing or invalid 'Bounds' entry in stitching function");
return;
}
bounds[0] = domain[0][0];
for (i = 1; i < k; ++i) {
Object obj2 = obj1.arrayGet(i - 1);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Invalid type in 'Bounds' array in stitching function");
return;
}
bounds[i] = obj2.getNum();
}
bounds[k] = domain[0][1];
//----- Encode
obj1 = dict->lookup("Encode");
if (!obj1.isArray() || obj1.arrayGetLength() != 2 * k) {
error(errSyntaxError, -1, "Missing or invalid 'Encode' entry in stitching function");
return;
}
for (i = 0; i < 2 * k; ++i) {
Object obj2 = obj1.arrayGet(i);
if (!obj2.isNum()) {
error(errSyntaxError, -1, "Invalid type in 'Encode' array in stitching function");
return;
}
encode[i] = obj2.getNum();
}
//----- pre-compute the scale factors
for (i = 0; i < k; ++i) {
if (bounds[i] == bounds[i + 1]) {
// avoid a divide-by-zero -- in this situation, function i will
// never be used anyway
scale[i] = 0;
} else {
scale[i] = (encode[2 * i + 1] - encode[2 * i]) / (bounds[i + 1] - bounds[i]);
}
}
n = funcs[0]->getOutputSize();
ok = true;
return;
}
StitchingFunction::StitchingFunction(const StitchingFunction *func) : Function(func)
{
k = func->k;
funcs = (Function **)gmallocn(k, sizeof(Function *));
for (int i = 0; i < k; ++i) {
funcs[i] = func->funcs[i]->copy();
}
bounds = (double *)gmallocn(k + 1, sizeof(double));
memcpy(bounds, func->bounds, (k + 1) * sizeof(double));
encode = (double *)gmallocn(2 * k, sizeof(double));
memcpy(encode, func->encode, 2 * k * sizeof(double));
scale = (double *)gmallocn(k, sizeof(double));
memcpy(scale, func->scale, k * sizeof(double));
ok = func->ok;
}
StitchingFunction::~StitchingFunction()
{
int i;
if (funcs) {
for (i = 0; i < k; ++i) {
if (funcs[i]) {
delete funcs[i];
}
}
}
gfree(funcs);
gfree(bounds);
gfree(encode);
gfree(scale);
}
void StitchingFunction::transform(const double *in, double *out) const
{
double x;
int i;
if (in[0] < domain[0][0]) {
x = domain[0][0];
} else if (in[0] > domain[0][1]) {
x = domain[0][1];
} else {
x = in[0];
}
for (i = 0; i < k - 1; ++i) {
if (x < bounds[i + 1]) {
break;
}
}
x = encode[2 * i] + (x - bounds[i]) * scale[i];
funcs[i]->transform(&x, out);
}
//------------------------------------------------------------------------
// PostScriptFunction
//------------------------------------------------------------------------
enum PSOp
{
psOpAbs,
psOpAdd,
psOpAnd,
psOpAtan,
psOpBitshift,
psOpCeiling,
psOpCopy,
psOpCos,
psOpCvi,
psOpCvr,
psOpDiv,
psOpDup,
psOpEq,
psOpExch,
psOpExp,
psOpFalse,
psOpFloor,
psOpGe,
psOpGt,
psOpIdiv,
psOpIndex,
psOpLe,
psOpLn,
psOpLog,
psOpLt,
psOpMod,
psOpMul,
psOpNe,
psOpNeg,
psOpNot,
psOpOr,
psOpPop,
psOpRoll,
psOpRound,
psOpSin,
psOpSqrt,
psOpSub,
psOpTrue,
psOpTruncate,
psOpXor,
psOpIf,
psOpIfelse,
psOpReturn
};
// Note: 'if' and 'ifelse' are parsed separately.
// The rest are listed here in alphabetical order.
// The index in this table is equivalent to the entry in PSOp.
static const char *psOpNames[] = { "abs", "add", "and", "atan", "bitshift", "ceiling", "copy", "cos", "cvi", "cvr", "div", "dup", "eq", "exch", "exp", "false", "floor", "ge", "gt", "idiv",
"index", "le", "ln", "log", "lt", "mod", "mul", "ne", "neg", "not", "or", "pop", "roll", "round", "sin", "sqrt", "sub", "true", "truncate", "xor" };
#define nPSOps (sizeof(psOpNames) / sizeof(char *))
enum PSObjectType
{
psBool,
psInt,
psReal,
psOperator,
psBlock
};
// In the code array, 'if'/'ifelse' operators take up three slots
// plus space for the code in the subclause(s).
//
// +---------------------------------+
// | psOperator: psOpIf / psOpIfelse |
// +---------------------------------+
// | psBlock: ptr=<A> |
// +---------------------------------+
// | psBlock: ptr=<B> |
// +---------------------------------+
// | if clause |
// | ... |
// | psOperator: psOpReturn |
// +---------------------------------+
// <A> | else clause |
// | ... |
// | psOperator: psOpReturn |
// +---------------------------------+
// <B> | ... |
//
// For 'if', pointer <A> is present in the code stream but unused.
struct PSObject
{
PSObjectType type;
union {
bool booln; // boolean (stack only)
int intg; // integer (stack and code)
double real; // real (stack and code)
PSOp op; // operator (code only)
int blk; // if/ifelse block pointer (code only)
};
};
#define psStackSize 100
class PSStack
{
public:
PSStack() { sp = psStackSize; }
void clear() { sp = psStackSize; }
void pushBool(bool booln)
{
if (checkOverflow()) {
stack[--sp].type = psBool;
stack[sp].booln = booln;
}
}
void pushInt(int intg)
{
if (checkOverflow()) {
stack[--sp].type = psInt;
stack[sp].intg = intg;
}
}
void pushReal(double real)
{
if (checkOverflow()) {
stack[--sp].type = psReal;
stack[sp].real = real;
}
}
bool popBool()
{
if (checkUnderflow() && checkType(psBool, psBool)) {
return stack[sp++].booln;
}
return false;
}
int popInt()
{
if (checkUnderflow() && checkType(psInt, psInt)) {
return stack[sp++].intg;
}
return 0;
}
double popNum()
{
double ret;
if (checkUnderflow() && checkType(psInt, psReal)) {
ret = (stack[sp].type == psInt) ? (double)stack[sp].intg : stack[sp].real;
++sp;
return ret;
}
return 0;
}
bool empty() { return sp == psStackSize; }
bool topIsInt() { return sp < psStackSize && stack[sp].type == psInt; }
bool topTwoAreInts() { return sp < psStackSize - 1 && stack[sp].type == psInt && stack[sp + 1].type == psInt; }
bool topIsReal() { return sp < psStackSize && stack[sp].type == psReal; }
bool topTwoAreNums() { return sp < psStackSize - 1 && (stack[sp].type == psInt || stack[sp].type == psReal) && (stack[sp + 1].type == psInt || stack[sp + 1].type == psReal); }
void copy(int n);
void roll(int n, int j);
void index(int i)
{
if (!checkOverflow()) {
return;
}
--sp;
if (unlikely(sp + i + 1 >= psStackSize)) {
error(errSyntaxError, -1, "Stack underflow in PostScript function");
return;
}
if (unlikely(sp + i + 1 < 0)) {
error(errSyntaxError, -1, "Stack overflow in PostScript function");