Search in sources :

Example 36 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project bioformats by openmicroscopy.

the class JAIIIOServiceImpl method readImage.

/* @see JAIIIOService#readImage(InputStream, JPEG2000CodecOptions) */
@Override
public BufferedImage readImage(InputStream in, JPEG2000CodecOptions options) throws IOException, ServiceException {
    J2KImageReader reader = getReader();
    MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream(in);
    reader.setInput(mciis, false, true);
    J2KImageReadParam param = (J2KImageReadParam) reader.getDefaultReadParam();
    if (options.resolution != null) {
        param.setResolution(options.resolution.intValue());
    }
    BufferedImage image = reader.read(0, param);
    mciis.close();
    reader.dispose();
    return image;
}
Also used : J2KImageReadParam(com.sun.media.imageio.plugins.jpeg2000.J2KImageReadParam) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) J2KImageReader(com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader) BufferedImage(java.awt.image.BufferedImage)

Example 37 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project CodenameOne by codenameone.

the class JavaSEPort method createImage.

/**
 * @inheritDoc
 */
public Object createImage(String path) throws IOException {
    if (exists(path)) {
        InputStream is = null;
        try {
            is = openInputStream(path);
            return createImage(is);
        } finally {
            is.close();
        }
    }
    try {
        InputStream i = getResourceAsStream(clsInstance, path);
        // prevents a security exception due to a JDK bug which for some stupid reason chooses
        // to create a temporary file in the spi of Image IO
        BufferedImage b = ImageIO.read(new MemoryCacheImageInputStream(i));
        if (perfMonitor != null) {
            b = cloneTrackableBufferedImage(b);
            perfMonitor.printToLog("Created path image " + path + " width: " + b.getWidth() + " height: " + b.getHeight() + " size (bytes) " + (b.getWidth() * b.getHeight() * 4));
        }
        return b;
    } catch (Throwable t) {
        t.printStackTrace();
        throw new IOException(t.toString());
    }
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) BufferedImage(java.awt.image.BufferedImage)

Example 38 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project pdfbox by apache.

the class PDFunctionType0 method getSamples.

/**
 * Get all sample values of this function.
 *
 * @return an array with all samples.
 */
private int[][] getSamples() {
    if (samples == null) {
        int arraySize = 1;
        int numberOfInputValues = getNumberOfInputParameters();
        int numberOfOutputValues = getNumberOfOutputParameters();
        COSArray sizes = getSize();
        for (int i = 0; i < numberOfInputValues; i++) {
            arraySize *= sizes.getInt(i);
        }
        samples = new int[arraySize][numberOfOutputValues];
        int bitsPerSample = getBitsPerSample();
        int index = 0;
        try {
            // Successive values are adjacent in the bit stream; there is no padding at byte boundaries.
            try (ImageInputStream mciis = new MemoryCacheImageInputStream(getPDStream().createInputStream())) {
                for (int i = 0; i < arraySize; i++) {
                    for (int k = 0; k < numberOfOutputValues; k++) {
                        // TODO will this cast work properly for 32 bitsPerSample or should we use long[]?
                        samples[index][k] = (int) mciis.readBits(bitsPerSample);
                    }
                    index++;
                }
            }
        } catch (IOException exception) {
            LOG.error("IOException while reading the sample values of this function.", exception);
        }
    }
    return samples;
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IOException(java.io.IOException)

Example 39 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream 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 40 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream 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)

Aggregations

MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)54 ImageInputStream (javax.imageio.stream.ImageInputStream)33 ByteArrayInputStream (java.io.ByteArrayInputStream)23 ImageReader (javax.imageio.ImageReader)22 IOException (java.io.IOException)19 InputStream (java.io.InputStream)19 BufferedImage (java.awt.image.BufferedImage)17 FileInputStream (java.io.FileInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 COSDictionary (org.apache.pdfbox.cos.COSDictionary)8 EOFException (java.io.EOFException)7 ArrayList (java.util.ArrayList)6 COSStream (org.apache.pdfbox.cos.COSStream)6 PDRange (org.apache.pdfbox.pdmodel.common.PDRange)6 Point (java.awt.Point)5 Point2D (java.awt.geom.Point2D)5 ImageReadParam (javax.imageio.ImageReadParam)5 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)5 Paint (java.awt.Paint)4 Size (org.olat.core.commons.services.image.Size)4