Search in sources :

Example 56 with IIOException

use of javax.imageio.IIOException in project imageio-ext by geosolutions-it.

the class TIFFBaseJPEGCompressor method getImageMetadata.

/**
 * Retrieves image metadata with non-core nodes removed.
 */
private IIOMetadata getImageMetadata(boolean pruneTables) throws IIOException {
    if (DEBUG) {
        System.out.println("getImageMetadata(" + pruneTables + ")");
    }
    if (JPEGImageMetadata == null && IMAGE_METADATA_NAME.equals(JPEGWriter.getOriginatingProvider().getNativeImageMetadataFormatName())) {
        TIFFImageWriter tiffWriter = (TIFFImageWriter) this.writer;
        // Get default image metadata.
        JPEGImageMetadata = JPEGWriter.getDefaultImageMetadata(tiffWriter.imageType, JPEGParam);
        // Get the DOM tree.
        Node tree = JPEGImageMetadata.getAsTree(IMAGE_METADATA_NAME);
        // Remove unwanted marker segments.
        try {
            pruneNodes(tree, pruneTables);
        } catch (IllegalArgumentException e) {
            throw new IIOException("Error pruning unwanted nodes", e);
        }
        // Set the DOM back into the metadata.
        try {
            JPEGImageMetadata.setFromTree(IMAGE_METADATA_NAME, tree);
        } catch (IIOInvalidTreeException e) {
            // present of JPEGTables field.
            throw new IIOException("Cannot set pruned image metadata!", e);
        }
    }
    return JPEGImageMetadata;
}
Also used : IIOInvalidTreeException(javax.imageio.metadata.IIOInvalidTreeException) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) Node(org.w3c.dom.Node) IIOException(javax.imageio.IIOException)

Example 57 with IIOException

use of javax.imageio.IIOException in project acs-aem-commons by Adobe-Consulting-Services.

the class AbstractRenditionModifyingProcess method execute.

public final void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaData, WorkflowHelper workflowHelper) throws WorkflowException {
    String[] args = workflowHelper.buildArguments(metaData);
    final String renditionName = workflowHelper.getValuesFromArgs(ARG_RENDITION_NAME, args).size() > 0 ? workflowHelper.getValuesFromArgs(ARG_RENDITION_NAME, args).get(0) : null;
    // image quality: from 0 t0 100%
    final String qualityStr = workflowHelper.getValuesFromArgs(ARG_QUALITY, args).size() > 0 ? workflowHelper.getValuesFromArgs(ARG_QUALITY, args).get(0) : DEFAULT_QUALITY;
    if (renditionName == null) {
        log.warn("Rendition name was not configured in arguments. Skipping.");
        return;
    }
    final WorkflowHelper.AssetResourceResolverPair pair = workflowHelper.getAssetFromPayload(workItem, workflowSession);
    if (pair == null) {
        return;
    }
    final Rendition rendition = pair.asset.getRendition(new PrefixRenditionPicker(renditionName));
    if (rendition == null) {
        log.warn("Rendition name {} was not available for asset {}. Skipping.", renditionName, pair.asset);
        return;
    }
    Layer layer = null;
    try {
        layer = new Layer(rendition.getStream());
        layer = processLayer(layer, rendition, workflowSession, args);
        String mimetype = layer.getMimeType();
        double quality = mimetype.equals("image/gif") ? workflowHelper.getQuality(MAX_GIF_QUALITY, qualityStr) : workflowHelper.getQuality(1.0, qualityStr);
        saveImage(pair.asset, rendition, layer, mimetype, quality, workflowHelper);
    } catch (IIOException e) {
        log.warn("Unable to load image layer from " + rendition.getPath(), e);
    } catch (IOException e) {
        log.warn("Unable to load image layer from " + rendition.getPath(), e);
    } finally {
        if (layer != null) {
            layer.dispose();
            layer = null;
        }
        pair.resourceResolver.close();
    }
}
Also used : Rendition(com.day.cq.dam.api.Rendition) WorkflowHelper(com.adobe.acs.commons.util.WorkflowHelper) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) IIOException(javax.imageio.IIOException) PrefixRenditionPicker(com.day.cq.dam.commons.util.PrefixRenditionPicker) Layer(com.day.image.Layer)

Example 58 with IIOException

use of javax.imageio.IIOException in project pdfbox by apache.

the class DCTFilter method decode.

@Override
public DecodeResult decode(InputStream encoded, OutputStream decoded, COSDictionary parameters, int index, DecodeOptions options) throws IOException {
    ImageReader reader = findImageReader("JPEG", "a suitable JAI I/O image filter is not installed");
    try (ImageInputStream iis = ImageIO.createImageInputStream(encoded)) {
        // skip one LF if there
        if (iis.read() != 0x0A) {
            iis.seek(0);
        }
        reader.setInput(iis);
        ImageReadParam irp = reader.getDefaultReadParam();
        irp.setSourceSubsampling(options.getSubsamplingX(), options.getSubsamplingY(), options.getSubsamplingOffsetX(), options.getSubsamplingOffsetY());
        irp.setSourceRegion(options.getSourceRegion());
        options.setFilterSubsampled(true);
        String numChannels = getNumChannels(reader);
        // get the raster using horrible JAI workarounds
        ImageIO.setUseCache(false);
        Raster raster;
        // after "can't get metadata" because "no meta" file was CMYK
        if ("3".equals(numChannels) || numChannels.isEmpty()) {
            try {
                // I'd like to use ImageReader#readRaster but it is buggy and can't read RGB correctly
                BufferedImage image = reader.read(0, irp);
                raster = image.getRaster();
            } catch (IIOException e) {
                // JAI can't read CMYK JPEGs using ImageReader#read or ImageIO.read but
                // fortunately ImageReader#readRaster isn't buggy when reading 4-channel files
                LOG.debug("Couldn't read use read() for RGB image - using readRaster() as fallback", e);
                raster = reader.readRaster(0, irp);
            }
        } else {
            // JAI can't read CMYK JPEGs using ImageReader#read or ImageIO.read but
            // fortunately ImageReader#readRaster isn't buggy when reading 4-channel files
            raster = reader.readRaster(0, irp);
        }
        // special handling for 4-component images
        if (raster.getNumBands() == 4) {
            // get APP14 marker
            Integer transform;
            try {
                transform = getAdobeTransform(reader.getImageMetadata(0));
            } catch (IIOException | NegativeArraySizeException e) {
                // we really tried asking nicely, now we're using brute force.
                LOG.debug("Couldn't read usíng getAdobeTransform() - using getAdobeTransformByBruteForce() as fallback", e);
                transform = getAdobeTransformByBruteForce(iis);
            }
            int colorTransform = transform != null ? transform : 0;
            // 0 = Unknown (RGB or CMYK), 1 = YCbCr, 2 = YCCK
            switch(colorTransform) {
                case 0:
                    // already CMYK
                    break;
                case 1:
                    raster = fromYCbCrtoCMYK(raster);
                    break;
                case 2:
                    raster = fromYCCKtoCMYK(raster);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown colorTransform");
            }
        } else if (raster.getNumBands() == 3) {
            // BGR to RGB
            raster = fromBGRtoRGB(raster);
        }
        DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
        decoded.write(dataBuffer.getData());
    } finally {
        reader.dispose();
    }
    return new DecodeResult(parameters);
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) IIOException(javax.imageio.IIOException) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) ImageReadParam(javax.imageio.ImageReadParam) ImageReader(javax.imageio.ImageReader)

Example 59 with IIOException

use of javax.imageio.IIOException in project robolectric by robolectric.

the class ImageUtil method getImageFromStream.

static RobolectricBufferedImage getImageFromStream(String fileName, InputStream is) {
    if (!initialized) {
        // Stops ImageIO from creating temp files when reading images
        // from input stream.
        ImageIO.setUseCache(false);
        initialized = true;
    }
    String format = null;
    try {
        ImageInputStream imageStream = createImageInputStream(is);
        Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
        if (!readers.hasNext()) {
            return null;
        }
        ImageReader reader = readers.next();
        try {
            reader.setInput(imageStream);
            format = reader.getFormatName();
            int minIndex = reader.getMinIndex();
            BufferedImage image = reader.read(minIndex);
            return RobolectricBufferedImage.create(image, ("image/" + format).toLowerCase());
        } finally {
            reader.dispose();
        }
    } catch (IOException e) {
        Throwable cause = e.getCause();
        if (FORMAT_NAME_PNG.equalsIgnoreCase(format) && cause instanceof IIOException && cause.getMessage() != null && cause.getMessage().contains("Invalid chunk length")) {
            String pngFileName = "(" + (fileName == null ? "not given PNG file name" : fileName) + ")";
            System.err.println("The PNG file" + pngFileName + " cannot be decoded. This may be due to an OpenJDK issue with certain PNG files." + " See https://github.com/robolectric/robolectric/issues/6812 for more details.");
        }
        throw new RuntimeException(e);
    }
}
Also used : ImageIO.createImageInputStream(javax.imageio.ImageIO.createImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IIOException(javax.imageio.IIOException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) Point(android.graphics.Point) BufferedImage(java.awt.image.BufferedImage)

Example 60 with IIOException

use of javax.imageio.IIOException in project plantuml-server by plantuml.

the class ProxyServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    final String fmt = request.getParameter("fmt");
    final String source = request.getParameter("src");
    final String index = request.getParameter("idx");
    final URL srcUrl;
    // Check if the src URL is valid
    try {
        srcUrl = new URL(source);
    } catch (MalformedURLException mue) {
        mue.printStackTrace();
        return;
    }
    // generate the response
    String diagmarkup = getSource(srcUrl);
    SourceStringReader reader = new SourceStringReader(diagmarkup);
    int n = index == null ? 0 : Integer.parseInt(index);
    List<BlockUml> blocks = reader.getBlocks();
    BlockUml block = blocks.get(n);
    Diagram diagram = block.getDiagram();
    UmlSource umlSrc = diagram.getSource();
    String uml = umlSrc.getPlainString();
    // System.out.println("uml=" + uml);
    // generate the response
    DiagramResponse dr = new DiagramResponse(response, getOutputFormat(fmt), request);
    try {
        dr.sendDiagram(uml, 0);
    } catch (IIOException iioe) {
    // Browser has closed the connection, so the HTTP OutputStream is closed
    // Silently catch the exception to avoid annoying log
    }
    dr = null;
}
Also used : UmlSource(net.sourceforge.plantuml.core.UmlSource) MalformedURLException(java.net.MalformedURLException) SourceStringReader(net.sourceforge.plantuml.SourceStringReader) BlockUml(net.sourceforge.plantuml.BlockUml) IIOException(javax.imageio.IIOException) URL(java.net.URL) Diagram(net.sourceforge.plantuml.core.Diagram)

Aggregations

IIOException (javax.imageio.IIOException)60 Point (java.awt.Point)23 IOException (java.io.IOException)23 BufferedImage (java.awt.image.BufferedImage)12 Rectangle (java.awt.Rectangle)11 IndexColorModel (java.awt.image.IndexColorModel)7 SampleModel (java.awt.image.SampleModel)7 WritableRaster (java.awt.image.WritableRaster)7 Iterator (java.util.Iterator)7 ColorModel (java.awt.image.ColorModel)6 ArrayList (java.util.ArrayList)6 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)6 ImageInputStream (javax.imageio.stream.ImageInputStream)6 TIFFField (it.geosolutions.imageio.plugins.tiff.TIFFField)5 ColorSpace (java.awt.color.ColorSpace)5 ComponentSampleModel (java.awt.image.ComponentSampleModel)5 DataBufferByte (java.awt.image.DataBufferByte)5 ImageReader (javax.imageio.ImageReader)5 IIOMetadata (javax.imageio.metadata.IIOMetadata)5 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)4