Search in sources :

Example 81 with ImageTypeSpecifier

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

the class HDF4Test method rasterBandsProperties.

/**
 * This test method retrieves properties from each band of the sample
 * dataset
 */
@Test
public void rasterBandsProperties() throws FileNotFoundException, IOException {
    if (!isLinux && isDriverAvailable)
        return;
    try {
        ImageReader reader = new HDF4ImageReaderSpi().createReaderInstance();
        String fileName = "TOVS_DAILY_AM_870330_NG.HDF";
        File file = TestData.file(this, fileName);
        reader.setInput(file);
        final int numImages = 3;
        final int startIndex = 2;
        int bands;
        Iterator<ImageTypeSpecifier> it;
        SampleModel sm;
        for (int i = startIndex; i < startIndex + numImages; i++) {
            it = reader.getImageTypes(i);
            ImageTypeSpecifier its;
            if (it.hasNext()) {
                its = (ImageTypeSpecifier) it.next();
                sm = its.getSampleModel();
                bands = sm.getNumBands();
                double d;
                StringBuffer sb = new StringBuffer("RasterBands properties retrieval").append(" Image: ").append(i);
                HDF4ImageReader mReader = (HDF4ImageReader) reader;
                for (int j = 0; j < bands; j++) {
                    sb.append(" \n\t Band: ").append(j).append(" --- ");
                    try {
                        d = mReader.getNoDataValue(i, j);
                        sb.append("NoDataV=").append(d);
                    } catch (IllegalArgumentException iae) {
                        sb.append(" NoDataV=NotAvailable");
                    }
                    try {
                        d = mReader.getOffset(i, j);
                        sb.append(" Off=").append(d);
                    } catch (IllegalArgumentException iae) {
                        sb.append(" Off=NotAvailable");
                    }
                    try {
                        d = mReader.getScale(i, j);
                        sb.append(" Scale=").append(d);
                    } catch (IllegalArgumentException iae) {
                        sb.append(" Scale=NotAvailable");
                    }
                    try {
                        d = mReader.getMinimum(i, j);
                        sb.append(" Min=").append(d);
                    } catch (IllegalArgumentException iae) {
                        sb.append(" Min=NotAvailable");
                    }
                    try {
                        d = mReader.getMaximum(i, j);
                        sb.append(" MAX=").append(d);
                    } catch (IllegalArgumentException iae) {
                        sb.append(" MAX=NotAvailable");
                    }
                }
                LOGGER.info(sb.toString());
            }
        }
        reader.dispose();
    } catch (FileNotFoundException fnfe) {
        warningMessage();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) SampleModel(java.awt.image.SampleModel) ImageReader(javax.imageio.ImageReader) File(java.io.File) Test(org.junit.Test) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest)

Example 82 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project OpenOLAT by OpenOLAT.

the class ImageHelperImpl method calcScaledSize.

private static SizeAndBufferedImage calcScaledSize(ImageInputStream stream, String suffix, int maxWidth, int maxHeight, boolean fill) {
    Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
    if (iter.hasNext()) {
        ImageReader reader = iter.next();
        try {
            reader.setInput(stream, true, true);
            int width = reader.getWidth(reader.getMinIndex());
            int height = reader.getHeight(reader.getMinIndex());
            Size size = new Size(width, height, false);
            Size scaledSize = computeScaledSize(width, height, maxWidth, maxHeight, fill);
            SizeAndBufferedImage all = new SizeAndBufferedImage(size, scaledSize);
            int readerMinIndex = reader.getMinIndex();
            ImageReadParam param = reader.getDefaultReadParam();
            Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
            while (imageTypes.hasNext()) {
                try {
                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                    int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
                    if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
                        param.setDestinationType(imageTypeSpecifier);
                    }
                    double memoryKB = (width * height * 4) / 1024d;
                    if (memoryKB > 2000) {
                        // check limit at 20MB
                        double free = Runtime.getRuntime().freeMemory() / 1024d;
                        if (free > memoryKB) {
                            all.setImage(reader.read(readerMinIndex, param));
                        } else {
                            // make sub sampling to save memory
                            int ratio = (int) Math.round(Math.sqrt(memoryKB / free));
                            param.setSourceSubsampling(ratio, ratio, 0, 0);
                            all.setImage(reader.read(readerMinIndex, param));
                        }
                    } else {
                        all.setImage(reader.read(readerMinIndex, param));
                    }
                    return all;
                } catch (IllegalArgumentException e) {
                    log.warn(e.getMessage(), e);
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            reader.dispose();
        }
    } else {
        log.error("No reader found for given format: " + suffix, null);
    }
    return null;
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) Size(org.olat.core.commons.services.image.Size) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 83 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project polymap4-core by Polymap4.

the class ImageEncodeProcessor method imageioEncodeJPEG.

private void imageioEncodeJPEG(Image image, ChunkedResponseOutputStream out) throws IOException {
    // this code is from http://forums.sun.com/thread.jspa?threadID=5197061
    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
    ImageWriteParam param = writer.getDefaultWriteParam();
    param.setSourceBands(new int[] { 0, 1, 2 });
    ColorModel cm = new DirectColorModel(24, // Red
    0x00ff0000, // Green
    0x0000ff00, // Blue
    0x000000ff, // Alpha
    0x0);
    param.setDestinationType(new ImageTypeSpecifier(cm, cm.createCompatibleSampleModel(1, 1)));
    ImageOutputStream imageOut = ImageIO.createImageOutputStream(out);
    writer.setOutput(imageOut);
    writer.write(null, new IIOImage((RenderedImage) image, null, null), param);
    writer.dispose();
    imageOut.close();
}
Also used : DirectColorModel(java.awt.image.DirectColorModel) ColorModel(java.awt.image.ColorModel) ImageWriter(javax.imageio.ImageWriter) DirectColorModel(java.awt.image.DirectColorModel) ImageWriteParam(javax.imageio.ImageWriteParam) RenderedImage(java.awt.image.RenderedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 84 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project tess4j by nguyenq.

the class ImageIOHelper method mergeTiff.

/**
 * Merges multiple images into one multi-page TIFF image.
 *
 * @param imageList a list of <code>IIOImage</code> objects
 * @param outputTiff the output TIFF file
 * @param compressionType valid values: LZW, CCITT T.6, PackBits
 * @throws IOException
 */
public static void mergeTiff(List<IIOImage> imageList, File outputTiff, String compressionType) throws IOException {
    if (imageList == null || imageList.isEmpty()) {
        // if no image
        return;
    }
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    if (!writers.hasNext()) {
        throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
    }
    ImageWriter writer = writers.next();
    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    // tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED); // comment out to preserve original sizes
    if (compressionType != null) {
        tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        tiffWriteParam.setCompressionType(compressionType);
    }
    // Get the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
    ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff);
    writer.setOutput(ios);
    int dpiX = 300;
    int dpiY = 300;
    for (IIOImage iioImage : imageList) {
        // Get the default image metadata.
        ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromRenderedImage(iioImage.getRenderedImage());
        IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
        imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
        iioImage.setMetadata(imageMetadata);
    }
    IIOImage firstIioImage = imageList.remove(0);
    writer.write(streamMetadata, firstIioImage, tiffWriteParam);
    int i = 1;
    for (IIOImage iioImage : imageList) {
        writer.writeInsert(i++, iioImage, tiffWriteParam);
    }
    ios.close();
    writer.dispose();
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) TIFFImageWriteParam(com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Example 85 with ImageTypeSpecifier

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

the class JPEGFactory method encodeImageToJPEGStream.

private static void encodeImageToJPEGStream(BufferedImage image, float quality, int dpi, OutputStream out) throws IOException {
    // encode to JPEG
    ImageOutputStream ios = null;
    ImageWriter imageWriter = null;
    try {
        // find JAI writer
        imageWriter = getJPEGImageWriter();
        ios = ImageIO.createImageOutputStream(out);
        imageWriter.setOutput(ios);
        // add compression
        JPEGImageWriteParam jpegParam = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
        jpegParam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
        jpegParam.setCompressionQuality(quality);
        // add metadata
        ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(image);
        IIOMetadata data = imageWriter.getDefaultImageMetadata(imageTypeSpecifier, jpegParam);
        Element tree = (Element) data.getAsTree("javax_imageio_jpeg_image_1.0");
        Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
        jfif.setAttribute("Xdensity", Integer.toString(dpi));
        jfif.setAttribute("Ydensity", Integer.toString(dpi));
        // 1 = dots/inch
        jfif.setAttribute("resUnits", "1");
        // write
        imageWriter.write(data, new IIOImage(image, null, null), jpegParam);
    } finally {
        // clean up
        IOUtils.closeQuietly(out);
        if (ios != null) {
            ios.close();
        }
        if (imageWriter != null) {
            imageWriter.dispose();
        }
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) Element(org.w3c.dom.Element) ImageWriter(javax.imageio.ImageWriter) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Aggregations

ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)88 BufferedImage (java.awt.image.BufferedImage)36 IIOMetadata (javax.imageio.metadata.IIOMetadata)32 ImageWriter (javax.imageio.ImageWriter)29 IIOImage (javax.imageio.IIOImage)23 IOException (java.io.IOException)22 ImageOutputStream (javax.imageio.stream.ImageOutputStream)22 ColorModel (java.awt.image.ColorModel)20 ImageReader (javax.imageio.ImageReader)20 SampleModel (java.awt.image.SampleModel)18 ImageWriteParam (javax.imageio.ImageWriteParam)18 ImageReadParam (javax.imageio.ImageReadParam)16 Rectangle (java.awt.Rectangle)14 File (java.io.File)13 Iterator (java.util.Iterator)12 ColorSpace (java.awt.color.ColorSpace)11 IndexColorModel (java.awt.image.IndexColorModel)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Point (java.awt.Point)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9