Search in sources :

Example 41 with ImageWriteParam

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

the class DisableCompressionTest method testFormat.

protected static void testFormat(String format) {
    ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
    if (writer == null) {
        throw new RuntimeException("No writer for " + format);
    }
    ImageWriteParam param = writer.getDefaultWriteParam();
    int[] supported_modes = new int[] { ImageWriteParam.MODE_COPY_FROM_METADATA, ImageWriteParam.MODE_DEFAULT, ImageWriteParam.MODE_EXPLICIT };
    for (int mode : supported_modes) {
        String mode_name = getModeName(mode);
        System.out.println("Test mode " + mode_name + "...");
        // we know that GIF image writer supports compression
        // and supports any compression mode form supportd_modes
        // If exception would be thrown here then test failed.
        param.setCompressionMode(mode);
        // now we are trying to disable compression.
        // This operation is not supported because GIF image writer
        // does not provide uncompressed output.
        // The expected behaviour is that UnsupportedOperationException
        // will be thrown here and current compression mode will not be
        // changed.
        boolean gotException = false;
        try {
            param.setCompressionMode(ImageWriteParam.MODE_DISABLED);
        } catch (UnsupportedOperationException e) {
            gotException = true;
        } catch (Throwable e) {
            throw new RuntimeException("Test failed due to unexpected exception", e);
        }
        if (!gotException) {
            throw new RuntimeException("Test failed.");
        }
        if (param.getCompressionMode() != mode) {
            throw new RuntimeException("Param state was changed.");
        }
        System.out.println("Test passed.");
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam)

Example 42 with ImageWriteParam

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

the class EmbeddedFormatTest method doTest.

public void doTest(String compression, int bi_type) throws IOException {
    System.out.println("Test " + compression + " on " + getImageTypeName(bi_type));
    BufferedImage src = createTestImage(bi_type);
    writer.reset();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);
    ImageWriteParam wparam = prepareWriteParam(compression);
    writer.write(null, new IIOImage(src, null, null), wparam);
    ios.flush();
    ios.close();
    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    reader.reset();
    reader.setInput(iis);
    BufferedImage dst = reader.read(0);
    checkResult(dst);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 43 with ImageWriteParam

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

the class TestCompressionBI_BITFIELDS method prepareWriteParam.

protected ImageWriteParam prepareWriteParam(BufferedImage src) {
    ImageWriteParam wparam = writer.getDefaultWriteParam();
    wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    wparam.setCompressionType("BI_BITFIELDS");
    return wparam;
}
Also used : ImageWriteParam(javax.imageio.ImageWriteParam)

Example 44 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 45 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)

Aggregations

ImageWriteParam (javax.imageio.ImageWriteParam)52 IIOImage (javax.imageio.IIOImage)35 ImageWriter (javax.imageio.ImageWriter)35 BufferedImage (java.awt.image.BufferedImage)28 ImageOutputStream (javax.imageio.stream.ImageOutputStream)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 IIOMetadata (javax.imageio.metadata.IIOMetadata)15 IOException (java.io.IOException)14 File (java.io.File)13 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Iterator (java.util.Iterator)10 Rectangle (java.awt.Rectangle)6 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)6 ImageReader (javax.imageio.ImageReader)5 ImageInputStream (javax.imageio.stream.ImageInputStream)5 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)5 Graphics2D (java.awt.Graphics2D)3 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)3 Color (java.awt.Color)2