Search in sources :

Example 46 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class AnimationTest method initWriter.

private ImageWriter initWriter() throws IOException {
    ImageOutputStream ios = ImageIO.createImageOutputStream(new File(fname));
    writer = ImageIO.getImageWritersByFormatName("GIF").next();
    writer.setOutput(ios);
    return writer;
}
Also used : File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 47 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class WritingColorChangeTest method doModification.

private BufferedImage doModification(BufferedImage src) {
    try {
        BufferedImage dst = null;
        if (!writer.getOriginatingProvider().canEncodeImage(src)) {
            throw new RuntimeException(writingFormat + " writer does not support the image type " + type);
        }
        System.out.println(writingFormat + " writer claims it can encode the image " + type);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
        writer.setOutput(ios);
        writer.write(src);
        ios.close();
        baos.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        dst = ImageIO.read(bais);
        return dst;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 48 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream 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 49 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream 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 50 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.

the class UshortGrayTest method main.

public static void main(String[] args) {
    Iterator iter;
    BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_USHORT_GRAY);
    // Part 1: ensure that JPEGImageWriter throws an exception if it
    // encounters an image with 16-bit samples
    ImageWriter writer = null;
    iter = ImageIO.getImageWritersByFormatName("jpeg");
    if (iter.hasNext()) {
        writer = (ImageWriter) iter.next();
    } else {
        throw new RuntimeException("No JPEG reader found");
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = null;
    boolean exceptionThrown = false;
    try {
        ios = ImageIO.createImageOutputStream(baos);
    } catch (IOException ioe) {
        throw new RuntimeException("Could not create ImageOutputStream");
    }
    try {
        writer.setOutput(ios);
        writer.write(bi);
    } catch (IOException ioe) {
        exceptionThrown = true;
    }
    if (!exceptionThrown) {
        throw new RuntimeException("JPEG writer should not be able to " + "write USHORT_GRAY images");
    }
    // Part 2: ensure that JPEGImageWriterSpi.canEncodeImage() returns
    // false for images with 16-bit samples
    ImageTypeSpecifier its = ImageTypeSpecifier.createFromRenderedImage(bi);
    iter = ImageIO.getImageWriters(its, "jpeg");
    if (iter.hasNext()) {
        throw new RuntimeException("JPEG writer should not be available" + " for USHORT_GRAY images");
    }
}
Also used : Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Aggregations

ImageOutputStream (javax.imageio.stream.ImageOutputStream)64 ImageWriter (javax.imageio.ImageWriter)41 BufferedImage (java.awt.image.BufferedImage)37 ByteArrayOutputStream (java.io.ByteArrayOutputStream)36 IIOImage (javax.imageio.IIOImage)33 IOException (java.io.IOException)26 ImageWriteParam (javax.imageio.ImageWriteParam)26 File (java.io.File)24 IIOMetadata (javax.imageio.metadata.IIOMetadata)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)12 Iterator (java.util.Iterator)9 ImageInputStream (javax.imageio.stream.ImageInputStream)9 ImageReader (javax.imageio.ImageReader)7 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)7 Graphics2D (java.awt.Graphics2D)5 FileOutputStream (java.io.FileOutputStream)5 Color (java.awt.Color)4 Graphics (java.awt.Graphics)4 OutputStream (java.io.OutputStream)4