Example usage for javax.imageio.stream ImageInputStream length

List of usage examples for javax.imageio.stream ImageInputStream length

Introduction

In this page you can find the example usage for javax.imageio.stream ImageInputStream length.

Prototype

long length() throws IOException;

Source Link

Document

Returns the total length of the stream, if known.

Usage

From source file:net.rptools.tokentool.util.ImageUtil.java

private static ImageView getImage(ImageView thumbView, final Path filePath, final boolean overlayWanted,
        final int THUMB_SIZE) throws IOException {
    Image thumb = null;//  w  w  w.  j a  va2s  .co m
    String fileURL = filePath.toUri().toURL().toString();

    if (ImageUtil.SUPPORTED_IMAGE_FILE_FILTER.accept(null, fileURL)) {
        if (THUMB_SIZE <= 0)
            thumb = processMagenta(new Image(fileURL), COLOR_THRESHOLD, overlayWanted);
        else
            thumb = processMagenta(new Image(fileURL, THUMB_SIZE, THUMB_SIZE, true, true), COLOR_THRESHOLD,
                    overlayWanted);
    } else if (ImageUtil.PSD_FILE_FILTER.accept(null, fileURL)) {
        ImageInputStream is = null;
        PSDImageReader reader = null;
        int imageIndex = 1;

        // Mask layer should always be layer 1 and overlay image on layer 2. Note, layer 0 will be a combined layer composite
        if (overlayWanted)
            imageIndex = 2;

        File file = filePath.toFile();

        try {
            is = ImageIO.createImageInputStream(file);
            if (is == null || is.length() == 0) {
                log.info("Image from file " + file.getAbsolutePath() + " is null");
            }

            Iterator<ImageReader> iterator = ImageIO.getImageReaders(is);
            if (iterator == null || !iterator.hasNext()) {
                throw new IOException("Image file format not supported by ImageIO: " + filePath);
            }

            reader = (PSDImageReader) iterator.next();
            reader.setInput(is);
            BufferedImage thumbBI;
            thumbBI = reader.read(imageIndex);

            if (thumbBI != null) {
                int layerIndex = 0;
                if (overlayWanted)
                    layerIndex = 1;

                IIOMetadata metadata = reader.getImageMetadata(0);
                IIOMetadataNode root = (IIOMetadataNode) metadata
                        .getAsTree(PSDMetadata.NATIVE_METADATA_FORMAT_NAME);
                NodeList layerInfos = root.getElementsByTagName("LayerInfo");

                // Layer index corresponds to imageIndex - 1 in the reader
                IIOMetadataNode layerInfo = (IIOMetadataNode) layerInfos.item(layerIndex);

                // Get the width & height of the Mask layer so we can create the overlay the same size
                int width = reader.getWidth(0);
                int height = reader.getHeight(0);

                // Get layer offsets, PhotoShop PSD layers can have different widths/heights and all images start at 0,0 with a layer offset applied
                int x = Math.max(Integer.parseInt(layerInfo.getAttribute("left")), 0);
                int y = Math.max(Integer.parseInt(layerInfo.getAttribute("top")), 0);

                // Lets pad the overlay with transparency to make it the same size as the PSD canvas size
                thumb = resizeCanvas(SwingFXUtils.toFXImage(thumbBI, null), width, height, x, y);

                // Finally set ImageView to thumbnail size
                if (THUMB_SIZE > 0) {
                    thumbView.setFitWidth(THUMB_SIZE);
                    thumbView.setPreserveRatio(true);
                }
            }
        } catch (Exception e) {
            log.error("Processing: " + file.getAbsolutePath(), e);
        } finally {
            // Dispose reader in finally block to avoid memory leaks
            reader.dispose();
            is.close();
        }
    }

    thumbView.setImage(thumb);

    return thumbView;
}