Search in sources :

Example 6 with PDRange

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

the class PDLab method toRGBImage.

// 
// WARNING: this method is performance sensitive, modify with care!
// 
@Override
public BufferedImage toRGBImage(WritableRaster raster) throws IOException {
    int width = raster.getWidth();
    int height = raster.getHeight();
    BufferedImage rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster rgbRaster = rgbImage.getRaster();
    PDRange aRange = getARange();
    PDRange bRange = getBRange();
    float minA = aRange.getMin();
    float maxA = aRange.getMax();
    float minB = bRange.getMin();
    float maxB = bRange.getMax();
    float deltaA = maxA - minA;
    float deltaB = maxB - minB;
    // always three components: ABC
    float[] abc = new float[3];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            raster.getPixel(x, y, abc);
            // 0..255 -> 0..1
            abc[0] /= 255;
            abc[1] /= 255;
            abc[2] /= 255;
            // scale to range
            abc[0] *= 100;
            abc[1] = minA + abc[1] * deltaA;
            abc[2] = minB + abc[2] * deltaB;
            float[] rgb = toRGB(abc);
            // 0..1 -> 0..255
            rgb[0] *= 255;
            rgb[1] *= 255;
            rgb[2] *= 255;
            rgbRaster.setPixel(x, y, rgb);
        }
    }
    return rgbImage;
}
Also used : PDRange(org.apache.pdfbox.pdmodel.common.PDRange) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage)

Example 7 with PDRange

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

the class FDFIconFit method getFractionalSpaceToAllocate.

/**
 * This is guaranteed to never return null.<br>
 *
 * To quote the PDF Spec "An array of two numbers between 0.0 and 1.0 indicating the fraction of leftover space to
 * allocate at the left and bottom of the icon. A value of [0.0 0.0] positions the icon at the bottom-left corner of
 * the annotation rectangle; a value of [0.5 0.5] centers it within the rectangle. This entry is used only if the
 * icon is scaled proportionally. Default value: [0.5 0.5]."
 *
 * @return The fractional space to allocate.
 */
public PDRange getFractionalSpaceToAllocate() {
    PDRange retval = null;
    COSArray array = fit.getCOSArray(COSName.A);
    if (array == null) {
        retval = new PDRange();
        retval.setMin(.5f);
        retval.setMax(.5f);
        setFractionalSpaceToAllocate(retval);
    } else {
        retval = new PDRange(array);
    }
    return retval;
}
Also used : PDRange(org.apache.pdfbox.pdmodel.common.PDRange) COSArray(org.apache.pdfbox.cos.COSArray)

Example 8 with PDRange

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

the class PatchMeshesShadingContext method collectPatches.

/**
 * Create a patch list from a data stream, the returned list contains all the patches contained
 * in the data stream.
 *
 * @param shadingType the shading type
 * @param xform transformation for user to device space
 * @param matrix the pattern matrix concatenated with that of the parent content stream
 * @param controlPoints number of control points, 12 for type 6 shading and 16 for type 7 shading
 * @return the obtained patch list
 * @throws IOException when something went wrong
 */
@SuppressWarnings({ "squid:S2583", "squid:S1166" })
final List<Patch> collectPatches(PDShadingType6 shadingType, AffineTransform xform, Matrix matrix, int controlPoints) throws IOException {
    COSDictionary dict = shadingType.getCOSObject();
    if (!(dict instanceof COSStream)) {
        return Collections.emptyList();
    }
    PDRange rangeX = shadingType.getDecodeForParameter(0);
    PDRange rangeY = shadingType.getDecodeForParameter(1);
    if (Float.compare(rangeX.getMin(), rangeX.getMax()) == 0 || Float.compare(rangeY.getMin(), rangeY.getMax()) == 0) {
        return Collections.emptyList();
    }
    int bitsPerFlag = shadingType.getBitsPerFlag();
    PDRange[] colRange = new PDRange[numberOfColorComponents];
    for (int i = 0; i < numberOfColorComponents; ++i) {
        colRange[i] = shadingType.getDecodeForParameter(2 + i);
        if (colRange[i] == null) {
            throw new IOException("Range missing in shading /Decode entry");
        }
    }
    List<Patch> list = new ArrayList<>();
    long maxSrcCoord = (long) Math.pow(2, bitsPerCoordinate) - 1;
    long maxSrcColor = (long) Math.pow(2, bitsPerColorComponent) - 1;
    COSStream cosStream = (COSStream) dict;
    try (ImageInputStream mciis = new MemoryCacheImageInputStream(cosStream.createInputStream())) {
        Point2D[] implicitEdge = new Point2D[4];
        float[][] implicitCornerColor = new float[2][numberOfColorComponents];
        byte flag = 0;
        try {
            flag = (byte) (mciis.readBits(bitsPerFlag) & 3);
        } catch (EOFException ex) {
            LOG.error(ex);
        }
        boolean eof = false;
        while (!eof) {
            try {
                boolean isFree = (flag == 0);
                Patch current = readPatch(mciis, isFree, implicitEdge, implicitCornerColor, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform, controlPoints);
                if (current == null) {
                    break;
                }
                list.add(current);
                flag = (byte) (mciis.readBits(bitsPerFlag) & 3);
                switch(flag) {
                    case 0:
                        break;
                    case 1:
                        implicitEdge = current.getFlag1Edge();
                        implicitCornerColor = current.getFlag1Color();
                        break;
                    case 2:
                        implicitEdge = current.getFlag2Edge();
                        implicitCornerColor = current.getFlag2Color();
                        break;
                    case 3:
                        implicitEdge = current.getFlag3Edge();
                        implicitCornerColor = current.getFlag3Color();
                        break;
                    default:
                        LOG.warn("bad flag: " + flag);
                        break;
                }
            } catch (EOFException ex) {
                eof = true;
            }
        }
    }
    return list;
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDRange(org.apache.pdfbox.pdmodel.common.PDRange) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ArrayList(java.util.ArrayList) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IOException(java.io.IOException) Point(java.awt.Point) Point2D(java.awt.geom.Point2D) EOFException(java.io.EOFException)

Example 9 with PDRange

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

the class Type4ShadingContext method collectTriangles.

@SuppressWarnings("squid:S1166")
private List<ShadedTriangle> collectTriangles(PDShadingType4 freeTriangleShadingType, AffineTransform xform, Matrix matrix) throws IOException {
    COSDictionary dict = freeTriangleShadingType.getCOSObject();
    if (!(dict instanceof COSStream)) {
        return Collections.emptyList();
    }
    PDRange rangeX = freeTriangleShadingType.getDecodeForParameter(0);
    PDRange rangeY = freeTriangleShadingType.getDecodeForParameter(1);
    if (Float.compare(rangeX.getMin(), rangeX.getMax()) == 0 || Float.compare(rangeY.getMin(), rangeY.getMax()) == 0) {
        return Collections.emptyList();
    }
    PDRange[] colRange = new PDRange[numberOfColorComponents];
    for (int i = 0; i < numberOfColorComponents; ++i) {
        colRange[i] = freeTriangleShadingType.getDecodeForParameter(2 + i);
    }
    List<ShadedTriangle> list = new ArrayList<>();
    long maxSrcCoord = (long) Math.pow(2, bitsPerCoordinate) - 1;
    long maxSrcColor = (long) Math.pow(2, bitsPerColorComponent) - 1;
    COSStream stream = (COSStream) dict;
    try (ImageInputStream mciis = new MemoryCacheImageInputStream(stream.createInputStream())) {
        byte flag = (byte) 0;
        try {
            flag = (byte) (mciis.readBits(bitsPerFlag) & 3);
        } catch (EOFException ex) {
            LOG.error(ex);
        }
        boolean eof = false;
        while (!eof) {
            Vertex p0, p1, p2;
            Point2D[] ps;
            float[][] cs;
            int lastIndex;
            try {
                switch(flag) {
                    case 0:
                        p0 = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform);
                        flag = (byte) (mciis.readBits(bitsPerFlag) & 3);
                        if (flag != 0) {
                            LOG.error("bad triangle: " + flag);
                        }
                        p1 = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform);
                        mciis.readBits(bitsPerFlag);
                        if (flag != 0) {
                            LOG.error("bad triangle: " + flag);
                        }
                        p2 = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform);
                        ps = new Point2D[] { p0.point, p1.point, p2.point };
                        cs = new float[][] { p0.color, p1.color, p2.color };
                        list.add(new ShadedTriangle(ps, cs));
                        flag = (byte) (mciis.readBits(bitsPerFlag) & 3);
                        break;
                    case 1:
                    case 2:
                        lastIndex = list.size() - 1;
                        if (lastIndex < 0) {
                            LOG.error("broken data stream: " + list.size());
                        } else {
                            ShadedTriangle preTri = list.get(lastIndex);
                            p2 = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform);
                            ps = new Point2D[] { flag == 1 ? preTri.corner[1] : preTri.corner[0], preTri.corner[2], p2.point };
                            cs = new float[][] { flag == 1 ? preTri.color[1] : preTri.color[0], preTri.color[2], p2.color };
                            list.add(new ShadedTriangle(ps, cs));
                            flag = (byte) (mciis.readBits(bitsPerFlag) & 3);
                        }
                        break;
                    default:
                        LOG.warn("bad flag: " + flag);
                        break;
                }
            } catch (EOFException ex) {
                eof = true;
            }
        }
    }
    return list;
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDRange(org.apache.pdfbox.pdmodel.common.PDRange) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ArrayList(java.util.ArrayList) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) Point2D(java.awt.geom.Point2D) EOFException(java.io.EOFException)

Example 10 with PDRange

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

the class Type5ShadingContext method collectTriangles.

@SuppressWarnings("squid:S1166")
private List<ShadedTriangle> collectTriangles(PDShadingType5 latticeTriangleShadingType, AffineTransform xform, Matrix matrix) throws IOException {
    COSDictionary dict = latticeTriangleShadingType.getCOSObject();
    if (!(dict instanceof COSStream)) {
        return Collections.emptyList();
    }
    PDRange rangeX = latticeTriangleShadingType.getDecodeForParameter(0);
    PDRange rangeY = latticeTriangleShadingType.getDecodeForParameter(1);
    if (Float.compare(rangeX.getMin(), rangeX.getMax()) == 0 || Float.compare(rangeY.getMin(), rangeY.getMax()) == 0) {
        return Collections.emptyList();
    }
    int numPerRow = latticeTriangleShadingType.getVerticesPerRow();
    PDRange[] colRange = new PDRange[numberOfColorComponents];
    for (int i = 0; i < numberOfColorComponents; ++i) {
        colRange[i] = latticeTriangleShadingType.getDecodeForParameter(2 + i);
    }
    List<Vertex> vlist = new ArrayList<>();
    long maxSrcCoord = (long) Math.pow(2, bitsPerCoordinate) - 1;
    long maxSrcColor = (long) Math.pow(2, bitsPerColorComponent) - 1;
    COSStream cosStream = (COSStream) dict;
    try (ImageInputStream mciis = new MemoryCacheImageInputStream(cosStream.createInputStream())) {
        boolean eof = false;
        while (!eof) {
            Vertex p;
            try {
                p = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform);
                vlist.add(p);
            } catch (EOFException ex) {
                eof = true;
            }
        }
    }
    int sz = vlist.size(), rowNum = sz / numPerRow;
    Vertex[][] latticeArray = new Vertex[rowNum][numPerRow];
    List<ShadedTriangle> list = new ArrayList<>();
    if (rowNum < 2) {
        // must have at least two rows; if not, return empty list
        return list;
    }
    for (int i = 0; i < rowNum; i++) {
        for (int j = 0; j < numPerRow; j++) {
            latticeArray[i][j] = vlist.get(i * numPerRow + j);
        }
    }
    for (int i = 0; i < rowNum - 1; i++) {
        for (int j = 0; j < numPerRow - 1; j++) {
            Point2D[] ps = new Point2D[] { latticeArray[i][j].point, latticeArray[i][j + 1].point, latticeArray[i + 1][j].point };
            float[][] cs = new float[][] { latticeArray[i][j].color, latticeArray[i][j + 1].color, latticeArray[i + 1][j].color };
            list.add(new ShadedTriangle(ps, cs));
            ps = new Point2D[] { latticeArray[i][j + 1].point, latticeArray[i + 1][j].point, latticeArray[i + 1][j + 1].point };
            cs = new float[][] { latticeArray[i][j + 1].color, latticeArray[i + 1][j].color, latticeArray[i + 1][j + 1].color };
            list.add(new ShadedTriangle(ps, cs));
        }
    }
    return list;
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDRange(org.apache.pdfbox.pdmodel.common.PDRange) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ArrayList(java.util.ArrayList) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) Point2D(java.awt.geom.Point2D) EOFException(java.io.EOFException)

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