Search in sources :

Example 16 with ImageOutputStream

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

the class Write3ByteBgrTest method writeImage.

private BufferedImage writeImage(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);
        IIOImage img = new IIOImage(src.getRaster(), null, null);
        writer.write(img);
        ios.close();
        baos.close();
        // save to file
        File f = new File("test" + src.getType() + ".bmp");
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(baos.toByteArray());
        fos.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        dst = ImageIO.read(bais);
        return dst;
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 17 with ImageOutputStream

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

the class WriteProgressListenerTest method doTest.

public void doTest() {
    try {
        System.out.println("Progress test for " + compression_type);
        BufferedImage bi = new BufferedImage(20, 300, BufferedImage.TYPE_INT_RGB);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
        Iterator iter = ImageIO.getImageWritersByFormatName(format);
        if (!iter.hasNext()) {
            throw new RuntimeException("No available writer for " + format);
        }
        ImageWriter writer = (ImageWriter) iter.next();
        writer.setOutput(ios);
        writer.addIIOWriteProgressListener(listener);
        IIOImage iio_img = new IIOImage(bi, null, null);
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionType(compression_type);
        writer.write(null, iio_img, param);
        if (!listener.isTestPassed) {
            throw new RuntimeException("Test for " + compression_type + " does not finish correctly!");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 18 with ImageOutputStream

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

the class LogicalScreenDimensionTest method main.

public static void main(String[] args) throws IOException {
    String format = "GIF";
    ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
    if (writer == null) {
        throw new RuntimeException("No available writers for " + format);
    }
    BufferedImage img = createTestImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);
    ImageWriteParam p = writer.getDefaultWriteParam();
    ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(img);
    IIOMetadata inImageMetadata = writer.getDefaultImageMetadata(type, p);
    IIOMetadata inStreamMetadata = writer.getDefaultStreamMetadata(p);
    // write and read image
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);
    writer.write(inStreamMetadata, new IIOImage(img, null, inImageMetadata), p);
    ios.flush();
    ios.close();
    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    ImageReader reader = ImageIO.getImageReader(writer);
    reader.setInput(iis);
    IIOMetadata outStreamMetadata = reader.getStreamMetadata();
    GIFStreamMetadata gifStreamMetadata = (GIFStreamMetadata) outStreamMetadata;
    if (gifStreamMetadata.logicalScreenWidth != img.getWidth() || gifStreamMetadata.logicalScreenHeight != img.getHeight()) {
        throw new RuntimeException("Test failed due to wrong logical screen dimension.");
    }
}
Also used : GIFStreamMetadata(com.sun.imageio.plugins.gif.GIFStreamMetadata) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 19 with ImageOutputStream

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

the class Frame method doTest.

public void doTest() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writer.reset();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);
    ImageWriteParam wparam = prepareWriteParam();
    IIOMetadata streamMetadata = prepareStreamMetadata(wparam);
    writer.prepareWriteSequence(streamMetadata);
    for (int i = 0; i < frames.length; i++) {
        BufferedImage src = frames[i].getImage();
        IIOMetadata imageMetadata = prepareImageMetadata(i, src, wparam);
        IIOImage img = new IIOImage(src, null, imageMetadata);
        writer.writeToSequence(img, wparam);
    }
    writer.endWriteSequence();
    ios.flush();
    ios.close();
    if (doSave) {
        File f = File.createTempFile("wr_test_", "." + format, new File("."));
        System.out.println("Save to file: " + f.getCanonicalPath());
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(baos.toByteArray());
        fos.flush();
        fos.close();
    }
    // read result
    reader.reset();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    reader.setInput(ImageIO.createImageInputStream(bais));
    int minIndex = reader.getMinIndex();
    int numImages = reader.getNumImages(true);
    for (int i = 0; i < numImages; i++) {
        BufferedImage dst = reader.read(i + minIndex);
        frames[i].checkResult(dst);
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 20 with ImageOutputStream

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

the class RGBImageTest method doTest.

protected void doTest() throws IOException {
    BufferedImage biSrc = getSrc();
    ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);
    ImageWriteParam writeParam = writer.getDefaultWriteParam();
    IIOMetadata imageMetadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(biSrc), writeParam);
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(writeParam);
    IIOImage iioImg = new IIOImage(biSrc, null, imageMetadata);
    writer.write(streamMetadata, iioImg, writeParam);
    ios.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    ImageReader reader = ImageIO.getImageReader(writer);
    reader.setInput(iis);
    BufferedImage dst = reader.read(0);
    // do test
    int x = dx / 2;
    int y = height / 2;
    for (int i = 0; i < usedColors.length; i++) {
        int dstRgb = dst.getRGB(x, y);
        System.out.println("dstColor: " + Integer.toHexString(dstRgb));
        int srcRgb = usedColors[i].getRGB();
        System.out.println("srcColor: " + Integer.toHexString(srcRgb));
        if (dstRgb != srcRgb) {
            throw new RuntimeException("wrong color " + i + ": " + Integer.toHexString(dstRgb));
        }
        x += dx;
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Aggregations

ImageOutputStream (javax.imageio.stream.ImageOutputStream)56 ImageWriter (javax.imageio.ImageWriter)35 BufferedImage (java.awt.image.BufferedImage)33 IIOImage (javax.imageio.IIOImage)31 ByteArrayOutputStream (java.io.ByteArrayOutputStream)29 ImageWriteParam (javax.imageio.ImageWriteParam)24 File (java.io.File)23 IOException (java.io.IOException)21 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 FileOutputStream (java.io.FileOutputStream)5 Graphics (java.awt.Graphics)4 Graphics2D (java.awt.Graphics2D)4 Color (java.awt.Color)3 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)3