Search in sources :

Example 1 with PDRange

use of org.apache.pdfbox.pdmodel.common.PDRange in project pdfbox by apache.

the class PDFunctionType0 method getDecodeForParameter.

/**
 * Get the decode for the input parameter.
 *
 * @param paramNum The function parameter number.
 *
 * @return The decode parameter range or null if none is set.
 */
public PDRange getDecodeForParameter(int paramNum) {
    PDRange retval = null;
    COSArray decodeValues = getDecodeValues();
    if (decodeValues != null && decodeValues.size() >= paramNum * 2 + 1) {
        retval = new PDRange(decodeValues, paramNum);
    }
    return retval;
}
Also used : PDRange(org.apache.pdfbox.pdmodel.common.PDRange) COSArray(org.apache.pdfbox.cos.COSArray)

Example 2 with PDRange

use of org.apache.pdfbox.pdmodel.common.PDRange in project pdfbox by apache.

the class PDFunctionType0 method getEncodeForParameter.

/**
 * Get the encode for the input parameter.
 *
 * @param paramNum The function parameter number.
 *
 * @return The encode parameter range or null if none is set.
 */
public PDRange getEncodeForParameter(int paramNum) {
    PDRange retval = null;
    COSArray encodeValues = getEncodeValues();
    if (encodeValues != null && encodeValues.size() >= paramNum * 2 + 1) {
        retval = new PDRange(encodeValues, paramNum);
    }
    return retval;
}
Also used : PDRange(org.apache.pdfbox.pdmodel.common.PDRange) COSArray(org.apache.pdfbox.cos.COSArray)

Example 3 with PDRange

use of org.apache.pdfbox.pdmodel.common.PDRange in project pdfbox by apache.

the class PDFunctionType3 method eval.

/**
 * {@inheritDoc}
 */
@Override
public float[] eval(float[] input) throws IOException {
    // This function is known as a "stitching" function. Based on the input, it decides which child function to call.
    // All functions in the array are 1-value-input functions
    // See PDF Reference section 3.9.3.
    PDFunction function = null;
    float x = input[0];
    PDRange domain = getDomainForInput(0);
    // clip input value to domain
    x = clipToRange(x, domain.getMin(), domain.getMax());
    if (functionsArray == null) {
        COSArray ar = getFunctions();
        functionsArray = new PDFunction[ar.size()];
        for (int i = 0; i < ar.size(); ++i) {
            functionsArray[i] = PDFunction.create(ar.getObject(i));
        }
    }
    if (functionsArray.length == 1) {
        // This doesn't make sense but it may happen ...
        function = functionsArray[0];
        PDRange encRange = getEncodeForParameter(0);
        x = interpolate(x, domain.getMin(), domain.getMax(), encRange.getMin(), encRange.getMax());
    } else {
        if (boundsValues == null) {
            boundsValues = getBounds().toFloatArray();
        }
        int boundsSize = boundsValues.length;
        // create a combined array containing the domain and the bounds values
        // domain.min, bounds[0], bounds[1], ...., bounds[boundsSize-1], domain.max
        float[] partitionValues = new float[boundsSize + 2];
        int partitionValuesSize = partitionValues.length;
        partitionValues[0] = domain.getMin();
        partitionValues[partitionValuesSize - 1] = domain.getMax();
        System.arraycopy(boundsValues, 0, partitionValues, 1, boundsSize);
        // find the partition
        for (int i = 0; i < partitionValuesSize - 1; i++) {
            if (x >= partitionValues[i] && (x < partitionValues[i + 1] || (i == partitionValuesSize - 2 && Float.compare(x, partitionValues[i + 1]) == 0))) {
                function = functionsArray[i];
                PDRange encRange = getEncodeForParameter(i);
                x = interpolate(x, partitionValues[i], partitionValues[i + 1], encRange.getMin(), encRange.getMax());
                break;
            }
        }
        if (function == null) {
            throw new IOException("partition not found in type 3 function");
        }
    }
    float[] functionValues = new float[] { x };
    // calculate the output values using the chosen function
    float[] functionResult = function.eval(functionValues);
    // clip to range if available
    return clipToRange(functionResult);
}
Also used : PDRange(org.apache.pdfbox.pdmodel.common.PDRange) COSArray(org.apache.pdfbox.cos.COSArray) IOException(java.io.IOException)

Example 4 with PDRange

use of org.apache.pdfbox.pdmodel.common.PDRange in project pdfbox by apache.

the class PDFunctionType4 method eval.

/**
 * {@inheritDoc}
 */
@Override
public float[] eval(float[] input) throws IOException {
    // Setup the input values
    ExecutionContext context = new ExecutionContext(OPERATORS);
    for (int i = 0; i < input.length; i++) {
        PDRange domain = getDomainForInput(i);
        float value = clipToRange(input[i], domain.getMin(), domain.getMax());
        context.getStack().push(value);
    }
    // Execute the type 4 function.
    instructions.execute(context);
    // Extract the output values
    int numberOfOutputValues = getNumberOfOutputParameters();
    int numberOfActualOutputValues = context.getStack().size();
    if (numberOfActualOutputValues < numberOfOutputValues) {
        throw new IllegalStateException("The type 4 function returned " + numberOfActualOutputValues + " values but the Range entry indicates that " + numberOfOutputValues + " values be returned.");
    }
    float[] outputValues = new float[numberOfOutputValues];
    for (int i = numberOfOutputValues - 1; i >= 0; i--) {
        PDRange range = getRangeForOutput(i);
        outputValues[i] = context.popReal();
        outputValues[i] = clipToRange(outputValues[i], range.getMin(), range.getMax());
    }
    // Return the resulting array
    return outputValues;
}
Also used : ExecutionContext(org.apache.pdfbox.pdmodel.common.function.type4.ExecutionContext) PDRange(org.apache.pdfbox.pdmodel.common.PDRange)

Example 5 with PDRange

use of org.apache.pdfbox.pdmodel.common.PDRange in project pdfbox by apache.

the class PDLab method getDefaultDecode.

@Override
public float[] getDefaultDecode(int bitsPerComponent) {
    PDRange a = getARange();
    PDRange b = getBRange();
    return new float[] { 0, 100, a.getMin(), a.getMax(), b.getMin(), b.getMax() };
}
Also used : PDRange(org.apache.pdfbox.pdmodel.common.PDRange)

Aggregations

PDRange (org.apache.pdfbox.pdmodel.common.PDRange)16 COSDictionary (org.apache.pdfbox.cos.COSDictionary)7 EOFException (java.io.EOFException)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 ImageInputStream (javax.imageio.stream.ImageInputStream)6 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)6 COSArray (org.apache.pdfbox.cos.COSArray)6 COSStream (org.apache.pdfbox.cos.COSStream)6 Point2D (java.awt.geom.Point2D)5 Paint (java.awt.Paint)2 Point (java.awt.Point)1 BufferedImage (java.awt.image.BufferedImage)1 WritableRaster (java.awt.image.WritableRaster)1 ExecutionContext (org.apache.pdfbox.pdmodel.common.function.type4.ExecutionContext)1 Test (org.junit.jupiter.api.Test)1