Search in sources :

Example 6 with IIOImage

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

the class ImageReaderReadAll method main.

public static void main(String[] argv) {
    ImageReader ireader;
    ImageReadParam irp;
    IIOImage image;
    BufferedImage bi;
    BufferedImage bi_1;
    BufferedImage bi_2;
    ireader = new DummyImageReaderImpl(null);
    MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream(new ByteArrayInputStream(ba));
    ireader.setInput(mciis);
    irp = new ImageReadParam();
    irp.setDestination(new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR));
    try {
        image = ireader.readAll(0, irp);
        bi_1 = ireader.read(0, irp);
        bi_2 = ireader.read(0);
    } catch (java.io.IOException ee) {
        throw new RuntimeException("Unexpected exception: " + ee);
    }
    bi = (BufferedImage) image.getRenderedImage();
    if (bi.getType() != bi_1.getType()) {
        throw new RuntimeException("Images have different type!");
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 7 with IIOImage

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

the class BMPWriteParamTest method getWriteResult.

private static BufferedImage getWriteResult(BufferedImage img, String format) throws IOException {
    ImageWriter iw = null;
    Iterator writers = ImageIO.getImageWritersByFormatName(format);
    while (writers.hasNext()) {
        iw = (ImageWriter) writers.next();
        System.out.println(format + " -> " + iw.toString());
    }
    if (iw == null) {
        throw new RuntimeException("No available Image writer for " + format);
    }
    ImageWriteParam param = iw.getDefaultWriteParam();
    param.setSourceRegion(new Rectangle(10, 10, 31, 31));
    param.setSourceSubsampling(3, 3, 0, 0);
    IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOImage iio_img = new IIOImage(img, null, meta);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    iw.setOutput(ios);
    iw.write(meta, iio_img, param);
    ios.flush();
    byte[] ba_image = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);
    ImageReader ir = null;
    Iterator readers = ImageIO.getImageReadersByFormatName(format);
    while (readers.hasNext()) {
        ir = (ImageReader) readers.next();
        System.out.println(format + " -> " + ir.toString());
    }
    if (ir == null) {
        throw new RuntimeException("No available Image reader for " + format);
    }
    ir.setInput(ImageIO.createImageInputStream(bais));
    BufferedImage res = ir.read(0);
    return res;
}
Also used : ImageWriter(javax.imageio.ImageWriter) Rectangle(java.awt.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) Iterator(java.util.Iterator) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 8 with IIOImage

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

the class RleEncodingTest method doTest.

private static void doTest(BufferedImage src, String compressionType, int compressionMode) throws IOException {
    ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix("bmp").next();
    if (iw == null) {
        throw new RuntimeException("No available writer. Test failed.");
    }
    IIOImage iioImg = new IIOImage(src, null, null);
    ImageWriteParam param = iw.getDefaultWriteParam();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    iw.setOutput(ios);
    System.out.println("Compression Type is " + compressionType);
    System.out.println("Compression Mode is " + compressionMode);
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType(compressionType);
    if (compressionMode != ImageWriteParam.MODE_EXPLICIT) {
        param.setCompressionMode(compressionMode);
    }
    try {
        iw.write(null, iioImg, param);
    } catch (IOException e) {
        int bpp = src.getColorModel().getPixelSize();
        if (compressionMode == ImageWriteParam.MODE_EXPLICIT) {
            if ((compressionType.equals("BI_RLE4") && bpp != 4) || (compressionType.equals("BI_RLE8") && bpp != 8)) {
                System.out.println("Can not encode " + bpp + "bpp image as" + compressionType);
                return;
            } else {
                throw new RuntimeException("Unable to encode " + bpp + "bpp image as " + compressionType + ". Test failed");
            }
        }
    }
    baos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    BufferedImage dst = ImageIO.read(iis);
    int w = src.getWidth();
    int h = src.getHeight();
    Object dstPixel = dst.getRaster().getDataElements(w / 2, h / 2, null);
    Object srcPixel = src.getRaster().getDataElements(w / 2, h / 2, null);
    if ((src.getColorModel().getRed(srcPixel) != dst.getColorModel().getRed(dstPixel)) || (src.getColorModel().getGreen(srcPixel) != dst.getColorModel().getGreen(dstPixel)) || (src.getColorModel().getBlue(srcPixel) != dst.getColorModel().getBlue(dstPixel)) || (src.getColorModel().getAlpha(srcPixel) != dst.getColorModel().getAlpha(dstPixel))) {
        showPixel(src, w / 2, h / 2);
        showPixel(dst, w / 2, h / 2);
        throw new RuntimeException("Colors are different: " + Integer.toHexString(src.getColorModel().getRGB(srcPixel)) + " and " + Integer.toHexString(dst.getColorModel().getRGB(dstPixel)));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 9 with IIOImage

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

the class TestCompressionBI_BITFIELDS method writeAndRead.

public BufferedImage writeAndRead(BufferedImage src) throws IOException {
    writer.reset();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writer.setOutput(ImageIO.createImageOutputStream(baos));
    ImageWriteParam wparam = prepareWriteParam(src);
    IIOImage img = new IIOImage(src, null, null);
    writer.write(null, img, wparam);
    if (doSave) {
        // save images to file in order to be able to check
        // that image is well-formed using standard windows tools.
        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));
    return reader.read(0);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) IIOImage(javax.imageio.IIOImage)

Example 10 with IIOImage

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

the class RLECompressionTest method doTest.

public void doTest(int type) throws IOException {
    BufferedImage src = getTestImage(type);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
    writer.setOutput(ios);
    ImageWriteParam wparam = writer.getDefaultWriteParam();
    wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    switch(type) {
        case TEST_RLE8:
            wparam.setCompressionType("BI_RLE8");
            break;
        case TEST_RLE4:
            wparam.setCompressionType("BI_RLE4");
            break;
        default:
            throw new IllegalArgumentException("Wrong test type: " + type);
    }
    writer.write(null, new IIOImage(src, null, null), wparam);
    ios.close();
    baos.close();
    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    BufferedImage dst = ImageIO.read(bais);
    checkResult(src, dst);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) 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)

Aggregations

IIOImage (javax.imageio.IIOImage)123 ImageWriter (javax.imageio.ImageWriter)92 ImageWriteParam (javax.imageio.ImageWriteParam)79 ImageOutputStream (javax.imageio.stream.ImageOutputStream)79 BufferedImage (java.awt.image.BufferedImage)66 File (java.io.File)45 IOException (java.io.IOException)41 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 IIOMetadata (javax.imageio.metadata.IIOMetadata)32 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)25 ByteArrayInputStream (java.io.ByteArrayInputStream)22 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)17 ImageReader (javax.imageio.ImageReader)16 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)16 Graphics2D (java.awt.Graphics2D)14 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)14 Test (org.junit.Test)14 FileOutputStream (java.io.FileOutputStream)13 Iterator (java.util.Iterator)11 RenderedImage (java.awt.image.RenderedImage)10