FunctionCacheImpl.java
10.7 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.stream.InputByteStream
* com.adobe.internal.pdftoolkit.core.cos.CosArray
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.cos.CosStream
* com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFRuntimeException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
*/
package com.adobe.internal.pdftoolkit.pdf.graphics.impl;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.pdftoolkit.color.Function;
import com.adobe.internal.pdftoolkit.color.FunctionCache;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFRuntimeException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFDomain;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFFunction;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFFunctionType0;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFFunctionType2;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFFunctionType3;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFFunctionsArray;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFRange;
import com.adobe.internal.pdftoolkit.pdf.graphics.impl.ExtendedPostScriptTokenManager;
import com.adobe.internal.pdftoolkit.pdf.graphics.impl.FunctionManager;
import com.adobe.internal.pdftoolkit.pdf.graphics.impl.PDFFunctionIdentity;
import com.adobe.internal.pdftoolkit.pdf.graphics.impl.PDFFunctionUtils;
import com.adobe.internal.pdftoolkit.pdf.graphics.impl.PostScriptCalculator;
import com.adobe.internal.pdftoolkit.pdf.graphics.impl.PostScriptCalculatorTokenManager;
import com.adobe.internal.pdftoolkit.pdf.graphics.impl.SimpleCharStream;
import java.io.IOException;
import java.io.InputStream;
public class FunctionCacheImpl
implements FunctionCache {
private Function function;
private byte[] sampledData = null;
private double[] domain;
private double[] range;
private double[] decode;
private double[] encode;
private double[] size;
private int bps = -1;
private int valToBeInterpolated = -1;
private int functionType = -1;
private PostScriptCalculator postScriptCalculator = null;
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Enabled force condition propagation
* Lifted jumps to return sites
*/
public FunctionCacheImpl(Function function) throws PDFIOException, PDFSecurityException, PDFInvalidDocumentException {
this.function = function;
if (function instanceof PDFFunctionIdentity) {
return;
}
PDFFunction pdfFunction = (PDFFunction)function;
this.functionType = pdfFunction.getFunctionType();
PDFDomain pdfDomain = pdfFunction.getFunctionDomain();
if (pdfDomain != null) {
this.domain = pdfDomain.getCosArray().getArrayDouble();
}
try {
PDFFunctionUtils.validateDomainORRangeArray(this.domain);
}
catch (PDFInvalidParameterException e1) {
throw new PDFInvalidDocumentException("Invalid domain entry.");
}
PDFRange pdfRange = pdfFunction.getFunctionRange();
if (pdfRange != null) {
this.range = pdfRange.getCosArray().getArrayDouble();
}
try {
if (this.functionType == 0) {
InputByteStream is = null;
try {
is = pdfFunction.getCosStream().getStreamDecoded();
this.sampledData = new byte[(int)is.length()];
is.read(this.sampledData);
this.decode = ((PDFFunctionType0)function).getDecode() != null ? ((PDFFunctionType0)function).getDecode().getArrayDouble() : this.range;
this.size = ((PDFFunctionType0)function).getSize().getArrayDouble();
this.encode = null;
if (((PDFFunctionType0)function).getEncode() != null) {
this.encode = ((PDFFunctionType0)function).getEncode().getArrayDouble();
} else {
this.encode = new double[this.size.length * 2];
for (int i = 0; i < this.size.length; ++i) {
this.encode[2 * i] = 0.0;
this.encode[2 * i + 1] = this.size[i];
}
}
this.bps = ((PDFFunctionType0)function).getBitsPerSample();
this.valToBeInterpolated = (2 << this.bps - 1) - 1;
}
finally {
try {
if (is != null) {
is.close();
}
}
catch (IOException e) {
throw new PDFRuntimeException(null, (Throwable)e);
}
}
}
if (this.functionType != 4) return;
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
}
/*
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
*/
public double[] apply(double[] inputData) {
try {
if (this.functionType == 0) {
return FunctionManager.applyFunction0(inputData, this.sampledData, this.domain, this.range, this.decode, this.encode, this.size, this.bps, this.valToBeInterpolated);
}
if (this.functionType == 2) {
CosArray C0 = ((PDFFunctionType2)this.function).getC0();
CosArray C1 = ((PDFFunctionType2)this.function).getC1();
double N = ((PDFFunctionType2)this.function).getInterpolationExponent();
int n = 1;
if (this.range != null) {
n = this.range.length / 2;
} else if (C0 != null) {
n = C0.size();
} else if (C1 != null) {
n = C1.size();
}
double[] c0Array = null;
double[] c1Array = null;
if (C0 == null) {
c0Array = FunctionManager.getDefaultArray(0.0, n);
} else {
c0Array = C0.getArrayDouble();
if (c0Array.length != n) {
throw new PDFInvalidDocumentException("The length of the C0 Array shoule be " + n);
}
}
if (C1 == null) {
c1Array = FunctionManager.getDefaultArray(1.0, n);
return FunctionManager.applyFunction2(inputData, c0Array, c1Array, N, n, this.range);
}
c1Array = C1.getArrayDouble();
if (c1Array.length == n) return FunctionManager.applyFunction2(inputData, c0Array, c1Array, N, n, this.range);
throw new PDFInvalidDocumentException("The length of the C1 Array shoule be " + n);
}
if (this.functionType == 3) {
return FunctionManager.applyFunction3(this.domain, ((PDFFunctionType3)this.function).getBounds().getArrayDouble(), ((PDFFunctionType3)this.function).getEncode().getArrayDouble(), inputData, FunctionManager.pdfFunctionsArrayToInterfaceArray(((PDFFunctionType3)this.function).getFunctions()));
}
if (this.functionType != 4) {
if (!(this.function instanceof PDFFunctionIdentity)) return null;
return this.function.apply(inputData);
}
InputByteStream decodedData = null;
InputStream byteStream = null;
try {
if (this.postScriptCalculator == null) {
decodedData = ((PDFFunction)this.function).getCosStream().getStreamDecoded();
byteStream = decodedData.toInputStream();
this.postScriptCalculator = new PostScriptCalculator(new ExtendedPostScriptTokenManager(new SimpleCharStream(byteStream, null, 1, 1)));
}
double[] N = FunctionManager.applyFunction4(this.range, this.domain, this.postScriptCalculator, inputData);
return N;
}
catch (IOException e) {
throw new PDFRuntimeException("Exception occured while applying function type 4 with object no = " + ((PDFFunction)this.function).getCosObject().getObjNum(), (Throwable)e);
}
finally {
if (byteStream != null) {
try {
byteStream.close();
}
catch (IOException e) {
throw new PDFRuntimeException("Exception occured while closing stream during application of function type 4 with object no = " + ((PDFFunction)this.function).getCosObject().getObjNum(), (Throwable)e);
}
}
if (decodedData != null) {
try {
decodedData.close();
}
catch (IOException e) {
throw new PDFRuntimeException("Exception occured while closing stream during application of function type 4 with object no = " + ((PDFFunction)this.function).getCosObject().getObjNum(), (Throwable)e);
}
}
}
}
catch (PDFCosParseException e) {
throw new PDFRuntimeException((PDFException)e);
}
catch (PDFIOException e) {
throw new PDFRuntimeException((PDFException)e);
}
catch (PDFSecurityException e) {
throw new PDFRuntimeException((PDFException)e);
}
catch (PDFInvalidDocumentException e) {
throw new PDFRuntimeException((PDFException)e);
}
}
}