FunctionCacheImpl.java 10.7 KB
/*
 * 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);
        }
    }
}