Search in sources :

Example 11 with PDRange

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

the class PDShadingType4 method collectTriangles.

@SuppressWarnings("squid:S1166")
@Override
List<ShadedTriangle> collectTriangles(AffineTransform xform, Matrix matrix) throws IOException {
    int bitsPerFlag = getBitsPerFlag();
    COSDictionary dict = getCOSObject();
    if (!(dict instanceof COSStream)) {
        return Collections.emptyList();
    }
    PDRange rangeX = getDecodeForParameter(0);
    PDRange rangeY = getDecodeForParameter(1);
    if (rangeX == null || rangeY == null || Float.compare(rangeX.getMin(), rangeX.getMax()) == 0 || Float.compare(rangeY.getMin(), rangeY.getMax()) == 0) {
        return Collections.emptyList();
    }
    PDRange[] colRange = new PDRange[getNumberOfColorComponents()];
    for (int i = 0; i < colRange.length; ++i) {
        colRange[i] = getDecodeForParameter(2 + i);
        if (colRange[i] == null) {
            throw new IOException("Range missing in shading /Decode entry");
        }
    }
    List<ShadedTriangle> list = new ArrayList<>();
    long maxSrcCoord = (long) Math.pow(2, getBitsPerCoordinate()) - 1;
    long maxSrcColor = (long) Math.pow(2, getBitsPerComponent()) - 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;
            Vertex p1;
            Vertex 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) IOException(java.io.IOException) Paint(java.awt.Paint) Point2D(java.awt.geom.Point2D) EOFException(java.io.EOFException)

Example 12 with PDRange

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

the class PDShadingType5 method collectTriangles.

@SuppressWarnings("squid:S1166")
@Override
List<ShadedTriangle> collectTriangles(AffineTransform xform, Matrix matrix) throws IOException {
    COSDictionary dict = getCOSObject();
    if (!(dict instanceof COSStream)) {
        return Collections.emptyList();
    }
    PDRange rangeX = getDecodeForParameter(0);
    PDRange rangeY = getDecodeForParameter(1);
    if (rangeX == null || rangeY == null || Float.compare(rangeX.getMin(), rangeX.getMax()) == 0 || Float.compare(rangeY.getMin(), rangeY.getMax()) == 0) {
        return Collections.emptyList();
    }
    int numPerRow = getVerticesPerRow();
    PDRange[] colRange = new PDRange[getNumberOfColorComponents()];
    for (int i = 0; i < colRange.length; ++i) {
        colRange[i] = getDecodeForParameter(2 + i);
        if (colRange[i] == null) {
            throw new IOException("Range missing in shading /Decode entry");
        }
    }
    List<Vertex> vlist = new ArrayList<>();
    long maxSrcCoord = (long) Math.pow(2, getBitsPerCoordinate()) - 1;
    long maxSrcColor = (long) Math.pow(2, getBitsPerComponent()) - 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 rowNum = vlist.size() / numPerRow;
    if (rowNum < 2) {
        // must have at least two rows; if not, return empty list
        return Collections.emptyList();
    }
    Vertex[][] latticeArray = new Vertex[rowNum][numPerRow];
    for (int i = 0; i < rowNum; i++) {
        for (int j = 0; j < numPerRow; j++) {
            latticeArray[i][j] = vlist.get(i * numPerRow + j);
        }
    }
    return createShadedTriangleList(rowNum, numPerRow, latticeArray);
}
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) Paint(java.awt.Paint) EOFException(java.io.EOFException)

Example 13 with PDRange

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

the class PDMeshBasedShadingType method collectPatches.

/**
 * Create a patch list from a data stream, the returned list contains all the patches contained in the data stream.
 *
 * @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(AffineTransform xform, Matrix matrix, int controlPoints) throws IOException {
    COSDictionary dict = getCOSObject();
    if (!(dict instanceof COSStream)) {
        return Collections.emptyList();
    }
    PDRange rangeX = getDecodeForParameter(0);
    PDRange rangeY = getDecodeForParameter(1);
    if (rangeX == null || rangeY == null || Float.compare(rangeX.getMin(), rangeX.getMax()) == 0 || Float.compare(rangeY.getMin(), rangeY.getMax()) == 0) {
        return Collections.emptyList();
    }
    int bitsPerFlag = getBitsPerFlag();
    PDRange[] colRange = new PDRange[getNumberOfColorComponents()];
    for (int i = 0; i < colRange.length; ++i) {
        colRange[i] = 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, getBitsPerCoordinate()) - 1;
    long maxSrcColor = (long) Math.pow(2, getBitsPerComponent()) - 1;
    COSStream cosStream = (COSStream) dict;
    try (ImageInputStream mciis = new MemoryCacheImageInputStream(cosStream.createInputStream())) {
        Point2D[] implicitEdge = new Point2D[4];
        float[][] implicitCornerColor = new float[2][colRange.length];
        byte flag = 0;
        try {
            flag = (byte) (mciis.readBits(bitsPerFlag) & 3);
        } catch (EOFException ex) {
            LOG.error(ex);
            return list;
        }
        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) Point2D(java.awt.geom.Point2D) EOFException(java.io.EOFException)

Example 14 with PDRange

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

the class PDTriangleBasedShadingType 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 15 with PDRange

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

the class PDLabTest method testLAB.

/**
 * This test checks that getting default values do not alter the object,
 * and checks getters and setters.
 */
@Test
void testLAB() {
    PDLab pdLab = new PDLab();
    COSArray cosArray = (COSArray) pdLab.getCOSObject();
    COSDictionary dict = (COSDictionary) cosArray.getObject(1);
    // test with default values
    assertEquals("Lab", pdLab.getName());
    assertEquals(3, pdLab.getNumberOfComponents());
    assertNotNull(pdLab.getInitialColor());
    assertTrue(Arrays.equals(new float[] { 0, 0, 0 }, pdLab.getInitialColor().getComponents()));
    assertEquals(0f, pdLab.getBlackPoint().getX(), 0f);
    assertEquals(0f, pdLab.getBlackPoint().getY(), 0f);
    assertEquals(0f, pdLab.getBlackPoint().getZ(), 0f);
    assertEquals(1f, pdLab.getWhitepoint().getX(), 0f);
    assertEquals(1f, pdLab.getWhitepoint().getY(), 0f);
    assertEquals(1f, pdLab.getWhitepoint().getZ(), 0f);
    assertEquals(-100f, pdLab.getARange().getMin(), 0f);
    assertEquals(100f, pdLab.getARange().getMax(), 0f);
    assertEquals(-100f, pdLab.getBRange().getMin(), 0f);
    assertEquals(100f, pdLab.getBRange().getMax(), 0f);
    assertEquals(0, dict.size(), "read operations should not change the size of /Lab objects");
    // rev 1571125 did a stack overflow here
    dict.toString();
    // test setting specific values
    PDRange pdRange = new PDRange();
    pdRange.setMin(-1);
    pdRange.setMax(2);
    pdLab.setARange(pdRange);
    pdRange = new PDRange();
    pdRange.setMin(3);
    pdRange.setMax(4);
    pdLab.setBRange(pdRange);
    assertEquals(-1f, pdLab.getARange().getMin(), 0f);
    assertEquals(2f, pdLab.getARange().getMax(), 0f);
    assertEquals(3f, pdLab.getBRange().getMin(), 0f);
    assertEquals(4f, pdLab.getBRange().getMax(), 0f);
    PDTristimulus pdTristimulus = new PDTristimulus();
    pdTristimulus.setX(5);
    pdTristimulus.setY(6);
    pdTristimulus.setZ(7);
    pdLab.setWhitePoint(pdTristimulus);
    pdTristimulus = new PDTristimulus();
    pdTristimulus.setX(8);
    pdTristimulus.setY(9);
    pdTristimulus.setZ(10);
    pdLab.setBlackPoint(pdTristimulus);
    assertEquals(5f, pdLab.getWhitepoint().getX(), 0f);
    assertEquals(6f, pdLab.getWhitepoint().getY(), 0f);
    assertEquals(7f, pdLab.getWhitepoint().getZ(), 0f);
    assertEquals(8f, pdLab.getBlackPoint().getX(), 0f);
    assertEquals(9f, pdLab.getBlackPoint().getY(), 0f);
    assertEquals(10f, pdLab.getBlackPoint().getZ(), 0f);
    assertTrue(Arrays.equals(new float[] { 0, 0, 3 }, pdLab.getInitialColor().getComponents()));
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDRange(org.apache.pdfbox.pdmodel.common.PDRange) Test(org.junit.jupiter.api.Test)

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