Search in sources :

Example 96 with ImageWriter

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

the class XDataTransferer method getPlatformMappingsForFlavor.

/*
     * The XDnD protocol prescribes that the Atoms used as targets for data
     * transfer should have string names that represent the corresponding MIME
     * types.
     * To meet this requirement we return a list of formats that represent
     * MIME types to which the data in this flavor can be translated by the Data
     * Transfer subsystem.
     */
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);
    if (df == null) {
        return natives;
    }
    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;
    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }
    // doesn't require translation.
    if (df.getRepresentationClass() != null && (df.isRepresentationClassInputStream() || df.isRepresentationClassByteBuffer() || byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }
    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers = ImageIO.getImageWritersByMIMEType(mimeTypes[i]);
                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter) writers.next();
                    ImageWriterSpi writerSpi = imageWriter.getOriginatingProvider();
                    if (writerSpi != null && writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }
        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }
        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }
    return natives;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi)

Example 97 with ImageWriter

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

the class LUTCompareTest method createTestImage.

/* Create test image with two frames:
     *  1) with {red, red} palette
     *  2) with {blue, red } palette
     */
private static Image createTestImage() throws IOException {
    BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
    BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });
    ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
    ImageWriteParam param = writer.getDefaultWriteParam();
    writer.setOutput(ios);
    writer.prepareWriteSequence(null);
    writer.writeToSequence(new IIOImage(frame1, null, null), param);
    writer.writeToSequence(new IIOImage(frame2, null, null), param);
    writer.endWriteSequence();
    writer.reset();
    writer.dispose();
    ios.flush();
    ios.close();
    return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
}
Also used : ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 98 with ImageWriter

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

the class BmpBigDestinationTest method main.

public static void main(String[] args) {
    try {
        BufferedImage src = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = src.createGraphics();
        g.setColor(Color.red);
        g.fillRect(0, 0, 100, 100);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName(format).next();
        if (iw == null) {
            throw new RuntimeException("No writer available. Test failed.");
        }
        iw.setOutput(ImageIO.createImageOutputStream(baos));
        iw.write(src);
        byte[] data = baos.toByteArray();
        ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName(format).next();
        ir.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(data)));
        Iterator specifiers = ir.getImageTypes(0);
        ImageTypeSpecifier typeSpecifier = null;
        if (specifiers.hasNext()) {
            typeSpecifier = (ImageTypeSpecifier) specifiers.next();
        }
        ImageReadParam param = new ImageReadParam();
        BufferedImage dst = typeSpecifier.createBufferedImage(200, 200);
        param.setDestination(dst);
        ir.read(0, param);
        checkResults(src, dst);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Unexpected exception. Test failed.");
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageWriter(javax.imageio.ImageWriter) Iterator(java.util.Iterator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Graphics2D(java.awt.Graphics2D)

Example 99 with ImageWriter

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

the class CompressionModeTest method doTest.

private static void doTest(int mode) {
    String fileFormat = "bmp";
    try {
        ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix(fileFormat).next();
        if (iw == null) {
            throw new RuntimeException("No available image writer for " + fileFormat + " Test failed.");
        }
        File file = new File("image." + fileFormat);
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        iw.setOutput(ios);
        BufferedImage bimg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics g = bimg.getGraphics();
        g.setColor(Color.green);
        g.fillRect(0, 0, 100, 100);
        ImageWriteParam param = iw.getDefaultWriteParam();
        param.setCompressionMode(mode);
        IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bimg), param);
        IIOImage iioImg = new IIOImage(bimg, null, meta);
        iw.write(null, iioImg, param);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed.");
    }
}
Also used : Graphics(java.awt.Graphics) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Example 100 with ImageWriter

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

the class TopDownTest method writeWithCompression.

private static void writeWithCompression(BufferedImage src, String compression) throws IOException {
    System.out.println("Compression: " + compression);
    ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
    if (writer == null) {
        throw new RuntimeException("Test failed: no bmp writer available");
    }
    File fout = File.createTempFile(compression + "_", ".bmp", new File("."));
    ImageOutputStream ios = ImageIO.createImageOutputStream(fout);
    writer.setOutput(ios);
    BMPImageWriteParam param = (BMPImageWriteParam) writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType(compression);
    param.setTopDown(true);
    writer.write(null, new IIOImage(src, null, null), param);
    writer.dispose();
    ios.flush();
    ios.close();
    BufferedImage dst = ImageIO.read(fout);
    verify(dst);
}
Also used : ImageWriter(javax.imageio.ImageWriter) BMPImageWriteParam(javax.imageio.plugins.bmp.BMPImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Aggregations

ImageWriter (javax.imageio.ImageWriter)171 ImageWriteParam (javax.imageio.ImageWriteParam)103 IIOImage (javax.imageio.IIOImage)92 ImageOutputStream (javax.imageio.stream.ImageOutputStream)85 BufferedImage (java.awt.image.BufferedImage)80 File (java.io.File)73 IOException (java.io.IOException)53 ByteArrayOutputStream (java.io.ByteArrayOutputStream)44 Test (org.junit.Test)37 IIOMetadata (javax.imageio.metadata.IIOMetadata)36 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)30 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)28 RenderedOp (javax.media.jai.RenderedOp)28 ImageReadParam (javax.imageio.ImageReadParam)26 Iterator (java.util.Iterator)25 ImageReader (javax.imageio.ImageReader)24 FileImageOutputStreamExtImpl (it.geosolutions.imageio.stream.output.FileImageOutputStreamExtImpl)20 ByteArrayInputStream (java.io.ByteArrayInputStream)20 Graphics2D (java.awt.Graphics2D)19 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)17