Search in sources :

Example 41 with MemoryCacheImageInputStream

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

Example 42 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project limelight by slagyr.

the class ImagePanel method setData.

public void setData(byte[] bytes) throws Exception {
    ImageInputStream imageInput = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes));
    setImage(ImageIO.read(imageInput));
    filename = "[DATA]";
    markAsNeedingLayout();
    getParent().markAsNeedingLayout();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream)

Example 43 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project zm-mailbox by Zimbra.

the class NativeFormatter method getResizedImageData.

/**
 * If the image stored in the {@code MimePart} exceeds the given width,
 * shrinks the image and returns the shrunk data.  If the
 * image width is smaller than {@code maxWidth} or resizing is not supported,
 * returns {@code null}.
 */
public static byte[] getResizedImageData(InputStream in, String contentType, String fileName, Integer maxWidth, Integer maxHeight) throws IOException {
    ImageReader reader = null;
    ImageWriter writer = null;
    if (maxWidth == null)
        maxWidth = LC.max_image_size_to_resize.intValue();
    if (maxHeight == null)
        maxHeight = LC.max_image_size_to_resize.intValue();
    try {
        // Get ImageReader for stream content.
        reader = ImageUtil.getImageReader(contentType, fileName);
        if (reader == null) {
            log.debug("No ImageReader available.");
            return null;
        }
        // Read message content.
        reader.setInput(new MemoryCacheImageInputStream(in));
        BufferedImage img = reader.read(0);
        int width = img.getWidth(), height = img.getHeight();
        if (width <= maxWidth && height <= maxHeight) {
            log.debug("Image %dx%d is less than max %dx%d.  Not resizing.", width, height, maxWidth, maxHeight);
            return RETURN_CODE_NO_RESIZE.getBytes();
        }
        // Resize.
        writer = ImageIO.getImageWriter(reader);
        if (writer == null) {
            log.debug("No ImageWriter available.");
            return null;
        }
        double ratio = Math.min((double) maxWidth / width, (double) maxHeight / height);
        width *= ratio;
        height *= ratio;
        BufferedImage small = ImageUtil.resize(img, width, height);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        writer.setOutput(new MemoryCacheImageOutputStream(out));
        writer.write(small);
        return out.toByteArray();
    } finally {
        ByteUtil.closeStream(in);
        if (reader != null) {
            reader.dispose();
        }
        if (writer != null) {
            writer.dispose();
        }
    }
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ImageWriter(javax.imageio.ImageWriter) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage)

Example 44 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project OsmAnd-tools by osmandapp.

the class NativeJavaRendering method renderImage.

public BufferedImage renderImage(RenderingImageContext ctx) throws IOException {
    ByteBuffer bitmapBuffer = render(ctx);
    InputStream inputStream = new InputStream() {

        int nextInd = 0;

        @Override
        public int read() throws IOException {
            if (nextInd >= bitmapBuffer.capacity()) {
                return -1;
            }
            byte b = bitmapBuffer.get(nextInd++);
            if (b < 0) {
                return b + 256;
            } else {
                return b;
            }
        }
    };
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("png");
    ImageReader reader = readers.next();
    reader.setInput(new MemoryCacheImageInputStream(inputStream), true);
    BufferedImage img = reader.read(0);
    return img;
}
Also used : MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageReader(javax.imageio.ImageReader) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage)

Example 45 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project openj9 by eclipse.

the class ZipFileResolver method getLibrary.

public LibraryDataSource getLibrary(String fileName, boolean silent) throws FileNotFoundException {
    if (zipfile == null) {
        throw new FileNotFoundException(fileName);
    }
    ZipEntry entry = null;
    try {
        zip = new ZipFile(zipfile);
        if (entryNames == null) {
            entryNames = new ArrayList<String>();
            Enumeration<? extends ZipEntry> entries = zip.entries();
            while (entries.hasMoreElements()) {
                ZipEntry nextEntry = entries.nextElement();
                entryNames.add(nextEntry.getName());
                if (nextEntry.getName().equals(fileName)) {
                    // we found the name we were asked to resolve
                    entry = nextEntry;
                }
            }
            if (entry == null) {
                // didn't find the named entry whilst building the cache
                throw new FileNotFoundException(fileName);
            }
        } else {
            if (entryNames.contains(fileName)) {
                entry = zip.getEntry(fileName);
            } else {
                throw new FileNotFoundException(fileName);
            }
        }
        MemoryCacheImageInputStream stream = new MemoryCacheImageInputStream(zip.getInputStream(entry));
        log.fine("Resolved library " + fileName + " in zip file " + zipfile.getAbsolutePath());
        return new LibraryDataSource(fileName, stream);
    } catch (IOException e) {
        log.log(Level.FINE, "Error resolving library in zip file " + zipfile.getAbsolutePath(), e);
        throw new FileNotFoundException(fileName + " (due to underlying IOException)");
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IOException(java.io.IOException)

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