Search in sources :

Example 76 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.

the class RasterWithMinXTest method main.

public static void main(String[] args) {
    String format = "jpeg";
    // Set output file.
    ImageOutputStream output = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());
    // Create image.
    BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_3BYTE_BGR);
    // Populate image.
    int[] rgbArray = new int[256];
    for (int i = 0; i < 256; i++) {
        Arrays.fill(rgbArray, i);
        bi.setRGB(0, i, 256, 1, rgbArray, 0, 256);
    }
    // create translated raster in order to get non-zero minX and minY
    WritableRaster r = (WritableRaster) bi.getRaster().createTranslatedChild(64, 64);
    Iterator i = ImageIO.getImageWritersByFormatName(format);
    ImageWriter iw = null;
    while (i.hasNext() && iw == null) {
        Object o = i.next();
        if (o instanceof com.sun.imageio.plugins.jpeg.JPEGImageWriter) {
            iw = (ImageWriter) o;
        }
    }
    if (iw == null) {
        throw new RuntimeException("No available image writer");
    }
    ImageWriteParam iwp = iw.getDefaultWriteParam();
    IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
    IIOImage img = new IIOImage(r, null, metadata);
    iw.setOutput(output);
    try {
        iw.write(img);
    } catch (RasterFormatException e) {
        e.printStackTrace();
        throw new RuntimeException("RasterException occurs. Test Failed!");
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Unexpected Exception");
    }
    // test case of theImageWriteParam with non-null sourceRegion
    iwp.setSourceRegion(new Rectangle(32, 32, 192, 192));
    metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
    try {
        iw.write(metadata, img, iwp);
    } catch (RasterFormatException e) {
        e.printStackTrace();
        throw new RuntimeException("SetSourceRegion causes the RasterException. Test Failed!");
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Unexpected Exception");
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) Rectangle(java.awt.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) RasterFormatException(java.awt.image.RasterFormatException) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) WritableRaster(java.awt.image.WritableRaster) Iterator(java.util.Iterator) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) RasterFormatException(java.awt.image.RasterFormatException)

Example 77 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.

the class DestTypeTest method getWriteParam.

public ImageWriteParam getWriteParam() {
    ImageWriteParam p = w.getDefaultWriteParam();
    p.setSourceBands(new int[] { 0, 1, 2 });
    ImageTypeSpecifier type = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
    p.setDestinationType(type);
    return p;
}
Also used : ImageWriteParam(javax.imageio.ImageWriteParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 78 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.

the class GIFImageWriteParam method computeRegions.

/**
     * Compute the source region and destination dimensions taking any
     * parameter settings into account.
     */
private static void computeRegions(Rectangle sourceBounds, Dimension destSize, ImageWriteParam p) {
    ImageWriteParam param;
    int periodX = 1;
    int periodY = 1;
    if (p != null) {
        int[] sourceBands = p.getSourceBands();
        if (sourceBands != null && (sourceBands.length != 1 || sourceBands[0] != 0)) {
            throw new IllegalArgumentException("Cannot sub-band image!");
        }
        // Get source region and subsampling factors
        Rectangle sourceRegion = p.getSourceRegion();
        if (sourceRegion != null) {
            // Clip to actual image bounds
            sourceRegion = sourceRegion.intersection(sourceBounds);
            sourceBounds.setBounds(sourceRegion);
        }
        // Adjust for subsampling offsets
        int gridX = p.getSubsamplingXOffset();
        int gridY = p.getSubsamplingYOffset();
        sourceBounds.x += gridX;
        sourceBounds.y += gridY;
        sourceBounds.width -= gridX;
        sourceBounds.height -= gridY;
        // Get subsampling factors
        periodX = p.getSourceXSubsampling();
        periodY = p.getSourceYSubsampling();
    }
    // Compute output dimensions
    destSize.setSize((sourceBounds.width + periodX - 1) / periodX, (sourceBounds.height + periodY - 1) / periodY);
    if (destSize.width <= 0 || destSize.height <= 0) {
        throw new IllegalArgumentException("Empty source region!");
    }
}
Also used : Rectangle(java.awt.Rectangle) ImageWriteParam(javax.imageio.ImageWriteParam)

Example 79 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.

the class ImageWriteParamMisc method test4434870.

private static void test4434870() {
    ImageWriteParam iwp = new ImageWriteParam4434870();
    try {
        Dimension[] dimensions = iwp.getPreferredTileSizes();
        iwp.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setTiling(100, 100, 0, 0);
        throw new RuntimeException("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
}
Also used : Dimension(java.awt.Dimension) ImageWriteParam(javax.imageio.ImageWriteParam)

Example 80 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.

the class UshortOutOfMemoryTest method testGetAsTree.

public void testGetAsTree() {
    ImageWriteParam p = w.getDefaultWriteParam();
    IIOMetadata m = w.getDefaultImageMetadata(ImageTypeSpecifier.createFromBufferedImageType(type), p);
    String format = m.getNativeMetadataFormatName();
    System.out.println("native format: " + format);
    int count = 0;
    try {
        while (count < 100) {
            System.out.println(" test " + count++);
            m.getAsTree(format);
        }
    } catch (OutOfMemoryError e) {
        System.gc();
        throw new RuntimeException("Test failed. Number of performed operations: " + count, e);
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriteParam(javax.imageio.ImageWriteParam)

Aggregations

ImageWriteParam (javax.imageio.ImageWriteParam)120 ImageWriter (javax.imageio.ImageWriter)100 IIOImage (javax.imageio.IIOImage)74 ImageOutputStream (javax.imageio.stream.ImageOutputStream)53 BufferedImage (java.awt.image.BufferedImage)51 File (java.io.File)51 IOException (java.io.IOException)38 Test (org.junit.Test)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 ImageReadParam (javax.imageio.ImageReadParam)24 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)23 RenderedOp (javax.media.jai.RenderedOp)23 IIOMetadata (javax.imageio.metadata.IIOMetadata)22 FileImageOutputStreamExtImpl (it.geosolutions.imageio.stream.output.FileImageOutputStreamExtImpl)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 Graphics2D (java.awt.Graphics2D)12 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)12 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)12 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)12