Java Code Examples for javax.imageio.metadata.IIOMetadata

The following are top voted examples for showing how to use javax.imageio.metadata.IIOMetadata. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: imageio-jnr   File: TurboJpegImageWriter.java   Source Code and License 7 votes vote down vote up
@Override
public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param) throws IOException {
  RenderedImage img = image.getRenderedImage();
  if (stream == null) {
    throw new IOException("Set an output first!");
  }
  if (param == null) {
    param = getDefaultWriteParam();
  }
  Rectangle sourceRegion = new Rectangle(0, 0, img.getWidth(), img.getHeight());
  if (param.getSourceRegion() != null) {
    sourceRegion = sourceRegion.intersection(param.getSourceRegion());
  }
  Raster raster = img.getData(sourceRegion);
  int quality = 85;
  if (param.getCompressionMode() == ImageWriteParam.MODE_EXPLICIT) {
    quality = (int) (param.getCompressionQuality() * 100);
  }
  try {
    stream.write(lib.encode(raster, quality).array());
  } catch (TurboJpegException e) {
    throw new IOException(e);
  }
}
 
Example 2
Project: jdk8u-jdk   File: ShortHistogramTest.java   Source Code and License 6 votes vote down vote up
protected File writeImageWithHist(BufferedImage bi) throws IOException {
    File f = File.createTempFile("hist_", ".png", new File("."));

    ImageWriter writer = ImageIO.getImageWritersByFormatName("PNG").next();

    ImageOutputStream ios = ImageIO.createImageOutputStream(f);
    writer.setOutput(ios);

    ImageWriteParam param = writer.getDefaultWriteParam();
    ImageTypeSpecifier type = new ImageTypeSpecifier(bi);

    IIOMetadata imgMetadata = writer.getDefaultImageMetadata(type, param);

    /* add hIST node to image metadata */
    imgMetadata = upgradeMetadata(imgMetadata, bi);

    IIOImage iio_img = new IIOImage(bi,
                                    null, // no thumbnails
                                    imgMetadata);

    writer.write(iio_img);
    ios.flush();
    ios.close();
    return f;
}
 
Example 3
Project: openjdk-jdk10   File: CompressionModeTest.java   Source Code and License 6 votes vote down vote up
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.");
    }
}
 
Example 4
Project: openjdk-jdk10   File: WriteMetadataTest.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args) {
    ImageWriter w = ImageIO.getImageWritersByFormatName(format).next();
    if (w == null) {
        throw new RuntimeException("No available writers for format " + format);
    }
    ImageWriteParam p = w.getDefaultWriteParam();

    ImageTypeSpecifier t =
            ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);

    IIOMetadata m = w.getDefaultImageMetadata(t, p);
    System.out.println("Default image metadata is " + m);
    testWritableMetadata(m);

    IIOMetadata sm = w.getDefaultStreamMetadata(p);
    System.out.println("Default stream metadata is " + sm);
    testWritableMetadata(sm);
}
 
Example 5
Project: OpenJSharp   File: GIFImageWriter.java   Source Code and License 6 votes vote down vote up
private void writeHeader(IIOMetadata streamMetadata, int bitsPerPixel)
  throws IOException {

    GIFWritableStreamMetadata sm;
    if (streamMetadata instanceof GIFWritableStreamMetadata) {
        sm = (GIFWritableStreamMetadata)streamMetadata;
    } else {
        sm = new GIFWritableStreamMetadata();
        Node root =
            streamMetadata.getAsTree(STREAM_METADATA_NAME);
        sm.setFromTree(STREAM_METADATA_NAME, root);
    }

    writeHeader(sm.version,
                sm.logicalScreenWidth,
                sm.logicalScreenHeight,
                sm.colorResolution,
                sm.pixelAspectRatio,
                sm.backgroundColorIndex,
                sm.sortFlag,
                bitsPerPixel,
                sm.globalColorTable);
}
 
Example 6
Project: openjdk-jdk10   File: TIFFImageWriter.java   Source Code and License 6 votes vote down vote up
public void prepareInsertEmpty(int imageIndex,
                               ImageTypeSpecifier imageType,
                               int width,
                               int height,
                               IIOMetadata imageMetadata,
                               List<? extends BufferedImage> thumbnails,
                               ImageWriteParam param) throws IOException {
    checkParamsEmpty(imageType, width, height, thumbnails);

    this.isInsertingEmpty = true;

    SampleModel emptySM = imageType.getSampleModel();
    RenderedImage emptyImage =
        new EmptyImage(0, 0, width, height,
                       0, 0, emptySM.getWidth(), emptySM.getHeight(),
                       emptySM, imageType.getColorModel());

    insert(imageIndex, new IIOImage(emptyImage, null, imageMetadata),
           param, false);
}
 
Example 7
Project: openjdk-jdk10   File: UserPluginMetadataFormatTest.java   Source Code and License 6 votes vote down vote up
public IIOMetadata getImageMetadata(int imageIndex) throws IOException {

            if (input == null)
                throw new IllegalStateException();
            if (imageIndex >= 5 || imageIndex < 0)
                throw new IndexOutOfBoundsException();
            if (seekForwardOnly) {
                if (imageIndex < minIndex)
                    throw new IndexOutOfBoundsException();
                minIndex = imageIndex;
            }
            System.out.println("Current format class name " + DummyIIOMetadataFormatImpl.class.getName());
            return new DummyIIOMetadataImpl(true,
                                            DummyIIOMetadataFormatImpl.nativeMetadataFormatName,
                                            DummyIIOMetadataFormatImpl.class.getName(),
                                            null, null);
        }
 
Example 8
Project: openjdk-jdk10   File: JpegMetadataColorSpaceTest.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    String fileName = "nomarkers.jpg";
    String sep = System.getProperty("file.separator");
    String dir = System.getProperty("test.src", ".");
    String filePath = dir+sep+fileName;
    System.out.println("Test file: " + filePath);
    File file = new File(filePath);
    ImageInputStream stream = ImageIO.createImageInputStream(file);
    Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);

    if(readers.hasNext()) {
        ImageReader reader = readers.next();
        reader.setInput(stream);
        IIOMetadata metadata = reader.getImageMetadata(0);

        IIOMetadataNode standardTree = (IIOMetadataNode)
            metadata.getAsTree
            (IIOMetadataFormatImpl.standardMetadataFormatName);
        IIOMetadataNode colorSpaceType = (IIOMetadataNode)
            standardTree.getElementsByTagName("ColorSpaceType").item(0);
        String colorSpaceName = colorSpaceType.getAttribute("name");
        if(colorSpaceName.equals("RGB"))
            throw new RuntimeException("Identified incorrect ColorSpace");
    }
}
 
Example 9
Project: openjdk-jdk10   File: TIFFImageReader.java   Source Code and License 6 votes vote down vote up
public IIOMetadata getImageMetadata(int imageIndex) throws IIOException {
    seekToImage(imageIndex);
    TIFFImageMetadata im
            = new TIFFImageMetadata(imageMetadata.getRootIFD().getTagSetList());
    Node root
            = imageMetadata.getAsTree(TIFFImageMetadata.NATIVE_METADATA_FORMAT_NAME);
    im.setFromTree(TIFFImageMetadata.NATIVE_METADATA_FORMAT_NAME, root);
    return im;
}
 
Example 10
Project: openjdk-jdk10   File: GIFImageWriter.java   Source Code and License 6 votes vote down vote up
public void write(IIOMetadata sm,
                  IIOImage iioimage,
                  ImageWriteParam p) throws IOException {
    if (stream == null) {
        throw new IllegalStateException("output == null!");
    }
    if (iioimage == null) {
        throw new IllegalArgumentException("iioimage == null!");
    }
    if (iioimage.hasRaster()) {
        throw new UnsupportedOperationException("canWriteRasters() == false!");
    }

    resetLocal();

    GIFWritableStreamMetadata streamMetadata;
    if (sm == null) {
        streamMetadata =
            (GIFWritableStreamMetadata)getDefaultStreamMetadata(p);
    } else {
        streamMetadata =
            (GIFWritableStreamMetadata)convertStreamMetadata(sm, p);
    }

    write(true, true, streamMetadata, iioimage, p);
}
 
Example 11
Project: openjdk-jdk10   File: ITXtTest.java   Source Code and License 6 votes vote down vote up
private static ITXtTest readFrom(File f) {
    try (ImageInputStream imageInputStream =
            ImageIO.createImageInputStream(f)) {

        ImageReader r = ImageIO.getImageReaders(imageInputStream).next();
        r.setInput(imageInputStream);

        IIOImage dst = r.readAll(0, null);

        // look for iTXt node
        IIOMetadata m = dst.getMetadata();
        Node root = m.getAsTree(m.getNativeMetadataFormatName());
        Node n = root.getFirstChild();
        while (n != null && !"iTXt".equals(n.getNodeName())) {
            n = n.getNextSibling();
        }
        if (n == null) {
            throw new RuntimeException("No iTXt node!");
        }
        ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
        return t;
    } catch (Throwable e) {
        throw new RuntimeException("Reading test failed.", e);
    }
}
 
Example 12
Project: jdk8u-jdk   File: JPEGImageReader.java   Source Code and License 6 votes vote down vote up
public IIOMetadata getImageMetadata(int imageIndex)
    throws IOException {
    setThreadLock();
    try {
        // imageMetadataIndex will always be either a valid index or
        // -1, in which case imageMetadata will not be null.
        // So we can leave checking imageIndex for gotoImage.
        if ((imageMetadataIndex == imageIndex)
            && (imageMetadata != null)) {
            return imageMetadata;
        }

        cbLock.check();

        gotoImage(imageIndex);

        imageMetadata = new JPEGMetadata(false, false, iis, this);

        imageMetadataIndex = imageIndex;

        return imageMetadata;
    } finally {
        clearThreadLock();
    }
}
 
Example 13
Project: openjdk-jdk10   File: OpaquePNGToGIFTest.java   Source Code and License 6 votes vote down vote up
public void createAnimatedImage(BufferedImage bi, IIOMetadata metadata,
        ImageWriter writer, File outputFile) {
    try {

        fos = new FileOutputStream(outputFile);
        ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
        System.out.println(ios);
        writer.setOutput(ios);

        ImageWriteParam param = writer.getDefaultWriteParam();
        IIOMetadata streamData = writer.getDefaultStreamMetadata(param);

        writer.prepareWriteSequence(streamData);
        ImageTypeSpecifier specify = new ImageTypeSpecifier(bi);
        IIOMetadata imgData = writer.convertImageMetadata(
                (IIOMetadata) metadata, specify, param);
        GIFImageMetadata gifData = setAnimationProperties(imgData);
        IIOImage iim = new IIOImage(bi, null, gifData);
        param.setProgressiveMode(param.MODE_DISABLED);
        writer.writeToSequence(iim, param);
        writer.endWriteSequence();
        ios.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 14
Project: openjdk-jdk10   File: GIFImageWriter.java   Source Code and License 6 votes vote down vote up
private void writeHeader(IIOMetadata streamMetadata, int bitsPerPixel)
  throws IOException {

    GIFWritableStreamMetadata sm;
    if (streamMetadata instanceof GIFWritableStreamMetadata) {
        sm = (GIFWritableStreamMetadata)streamMetadata;
    } else {
        sm = new GIFWritableStreamMetadata();
        Node root =
            streamMetadata.getAsTree(STREAM_METADATA_NAME);
        sm.setFromTree(STREAM_METADATA_NAME, root);
    }

    writeHeader(sm.version,
                sm.logicalScreenWidth,
                sm.logicalScreenHeight,
                sm.colorResolution,
                sm.pixelAspectRatio,
                sm.backgroundColorIndex,
                sm.sortFlag,
                bitsPerPixel,
                sm.globalColorTable);
}
 
Example 15
Project: jdk8u-jdk   File: CanWriteSequence.java   Source Code and License 6 votes vote down vote up
private static void test(final ImageWriter writer) throws Exception {
    final File file = File.createTempFile("temp", ".img");
    file.deleteOnExit();
    final FileOutputStream fos = new FileOutputStream(file);
    final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
    writer.setOutput(ios);
    final IIOMetadata data = writer.getDefaultStreamMetadata(null);

    if (writer.canWriteSequence()) {
        writer.prepareWriteSequence(data);
    } else {
        try {
            writer.prepareWriteSequence(data);
            throw new RuntimeException(
                    "UnsupportedOperationException was not thrown");
        } catch (final UnsupportedOperationException ignored) {
            // expected
        }
    }
    writer.dispose();
    ios.close();
}
 
Example 16
Project: jdk8u-jdk   File: GIFImageWriter.java   Source Code and License 6 votes vote down vote up
public void prepareWriteSequence(IIOMetadata streamMetadata)
  throws IOException {

    if (stream == null) {
        throw new IllegalStateException("Output is not set.");
    }

    resetLocal();

    // Save the possibly converted stream metadata as an instance variable.
    if (streamMetadata == null) {
        this.theStreamMetadata =
            (GIFWritableStreamMetadata)getDefaultStreamMetadata(null);
    } else {
        this.theStreamMetadata = new GIFWritableStreamMetadata();
        convertMetadata(STREAM_METADATA_NAME, streamMetadata,
                        theStreamMetadata);
    }

    this.isWritingSequence = true;
}
 
Example 17
Project: openjdk-jdk10   File: UshortOutOfMemoryTest.java   Source Code and License 6 votes vote down vote up
public void testGetAsTree() {
    ImageWriteParam p = w.getDefaultWriteParam();
    IIOMetadata m =
        w.getDefaultImageMetadata(ImageTypeSpecifier.createFromBufferedImageType(type), p);

    String format = m.getNativeMetadataFormatName();
    System.out.println("native format: " + format);

    int count = 0;
    try {
        while (count < 100) {
            System.out.println(" test " + count++);
            m.getAsTree(format);
        }
    } catch (OutOfMemoryError e) {
        System.gc();
        throw new RuntimeException("Test failed. Number of performed operations: " + count, e);
    }
}
 
Example 18
Project: jdk8u-jdk   File: GIFImageWriter.java   Source Code and License 6 votes vote down vote up
private void writeHeader(IIOMetadata streamMetadata, int bitsPerPixel)
  throws IOException {

    GIFWritableStreamMetadata sm;
    if (streamMetadata instanceof GIFWritableStreamMetadata) {
        sm = (GIFWritableStreamMetadata)streamMetadata;
    } else {
        sm = new GIFWritableStreamMetadata();
        Node root =
            streamMetadata.getAsTree(STREAM_METADATA_NAME);
        sm.setFromTree(STREAM_METADATA_NAME, root);
    }

    writeHeader(sm.version,
                sm.logicalScreenWidth,
                sm.logicalScreenHeight,
                sm.colorResolution,
                sm.pixelAspectRatio,
                sm.backgroundColorIndex,
                sm.sortFlag,
                bitsPerPixel,
                sm.globalColorTable);
}
 
Example 19
Project: jtk   File: IPanel.java   Source Code and License 5 votes vote down vote up
/**
 * Paints this panel to a PNG image with specified resolution and width.
 * The image height is computed so that the image has the same aspect 
 * ratio as this panel.
 * @param dpi the image resolution, in dots per inch.
 * @param win the image width, in inches.
 * @param fileName the name of the file to contain the PNG image.
 * @throws IOException when unable to save to file.
 */
public void paintToPng(double dpi, double win, String fileName) 
  throws IOException 
{
  BufferedImage image = paintToImage((int)ceil(dpi*win));
  // The two lines below are simple, but do not write resolution info to 
  // the PNG file. We want that info, especially for high-res images.
  //File file = new File(fileName);
  //ImageIO.write(image,"png",file);
  Iterator<ImageWriter> i = ImageIO.getImageWritersBySuffix("png");
  if (!i.hasNext())
    throw new IOException("cannot get a PNG image writer");
  ImageWriter iw = i.next();
  FileOutputStream fos = new FileOutputStream(fileName);
  ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
  iw.setOutput(ios);
  ImageWriteParam iwp = iw.getDefaultWriteParam();
  ImageTypeSpecifier its = new ImageTypeSpecifier(image);
  IIOMetadata imd = iw.getDefaultImageMetadata(its,iwp);
  String format = "javax_imageio_png_1.0";
  IIOMetadataNode tree = (IIOMetadataNode)imd.getAsTree(format);
  IIOMetadataNode node = new IIOMetadataNode("pHYs");
  String dpm = Integer.toString((int)ceil(dpi/0.0254));
  node.setAttribute("pixelsPerUnitXAxis",dpm);
  node.setAttribute("pixelsPerUnitYAxis",dpm);
  node.setAttribute("unitSpecifier","meter");
  tree.appendChild(node);
  imd.setFromTree(format,tree);
  iw.write(new IIOImage(image,null,imd));
  ios.flush();
  ios.close();
  fos.flush();
  fos.close();
  iw.dispose();
}
 
Example 20
Project: imagetozxspec   File: GifConverter.java   Source Code and License 5 votes vote down vote up
/**
 * Configures the per frame metadata
 * 
 * @param meta the default meta data
 * @param delayTime the amount of time a frame is to stay on screen in hundreds of a second (millisecond value/10)
 * @param imageIndex the index of this frame
 * @throws IIOInvalidTreeException if the meta data cannot be set
 */
private void configureMetaData(IIOMetadata meta, String delayTime, int imageIndex) throws IIOInvalidTreeException {
	String metaFormat = meta.getNativeMetadataFormatName();
	Node root = meta.getAsTree(metaFormat);
	Node child = root.getFirstChild();
	while (child != null) {
		if ("GraphicControlExtension".equals(child.getNodeName())) {
			break;
		}
		child = child.getNextSibling();
	}
	IIOMetadataNode gce = (IIOMetadataNode) child;
	gce.setAttribute("userDelay", "FALSE");
	gce.setAttribute("delayTime", delayTime);
	gce.setAttribute("disposalMethod", "none");

	if (imageIndex == 0) {
		IIOMetadataNode aes = new IIOMetadataNode("ApplicationExtensions");
		IIOMetadataNode ae = new IIOMetadataNode("ApplicationExtension");
		ae.setAttribute("applicationID", "NETSCAPE");
        ae.setAttribute("authenticationCode", "2.0");
		byte[] uo = new byte[] { 0x1, (byte) (LOOP_COUNT & 0xFF), (byte) ((LOOP_COUNT >> 8) & 0xFF) };
		ae.setUserObject(uo);
		aes.appendChild(ae);
		root.appendChild(aes);
	}
	meta.setFromTree(metaFormat, root);		
}
 
Example 21
Project: openjdk-jdk10   File: JPEGImageWriter.java   Source Code and License 5 votes vote down vote up
public Dimension[] getPreferredThumbnailSizes(ImageTypeSpecifier imageType,
                                              ImageWriteParam param,
                                              IIOMetadata streamMetadata,
                                              IIOMetadata imageMetadata) {
    if (jfifOK(imageType, param, streamMetadata, imageMetadata)) {
        return preferredThumbSizes.clone();
    }
    return null;
}
 
Example 22
Project: OpenJSharp   File: JPEGImageWriter.java   Source Code and License 5 votes vote down vote up
public IIOMetadata
    getDefaultImageMetadata(ImageTypeSpecifier imageType,
                            ImageWriteParam param) {
    setThreadLock();
    try {
        return new JPEGMetadata(imageType, param, this);
    } finally {
        clearThreadLock();
    }
}
 
Example 23
Project: OpenJSharp   File: JPEGImageWriter.java   Source Code and License 5 votes vote down vote up
public IIOMetadata convertStreamMetadata(IIOMetadata inData,
                                         ImageWriteParam param) {
    // There isn't much we can do.  If it's one of ours, then
    // return it.  Otherwise just return null.  We use it only
    // for tables, so we can't get a default and modify it,
    // as this will usually not be what is intended.
    if (inData instanceof JPEGMetadata) {
        JPEGMetadata jpegData = (JPEGMetadata) inData;
        if (jpegData.isStream) {
            return inData;
        }
    }
    return null;
}
 
Example 24
Project: openjdk-jdk10   File: PngDitDepthTest.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) throws IIOInvalidTreeException {

        // getting the writer for the png format
        Iterator iter = ImageIO.getImageWritersByFormatName("png");
        ImageWriter writer = (ImageWriter) iter.next();

        // creating a color model
        ColorModel colorModel = ColorModel.getRGBdefault();

        // creating a sample model
        SampleModel sampleModel = colorModel.createCompatibleSampleModel(640, 480);

        // creating a default metadata object
        IIOMetadata metaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(colorModel, sampleModel), null);
        String formatName = metaData.getNativeMetadataFormatName();

        // first call
        Node metaDataNode = metaData.getAsTree(formatName);
        try {
            metaData.setFromTree(formatName, metaDataNode);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        // second call (bitdepht is already set to an invalid value)
        metaDataNode = metaData.getAsTree(formatName);

        metaData.setFromTree(formatName, metaDataNode);

    }
 
Example 25
Project: OpenJSharp   File: JPEGImageWriter.java   Source Code and License 5 votes vote down vote up
public void write(IIOMetadata streamMetadata,
                  IIOImage image,
                  ImageWriteParam param) throws IOException {
    setThreadLock();
    try {
        cbLock.check();

        writeOnThread(streamMetadata, image, param);
    } finally {
        clearThreadLock();
    }
}
 
Example 26
Project: openjdk-jdk10   File: GIFImageReader.java   Source Code and License 5 votes vote down vote up
public IIOMetadata getImageMetadata(int imageIndex) throws IIOException {
    checkIndex(imageIndex);

    int index = locateImage(imageIndex);
    if (index != imageIndex) {
        throw new IndexOutOfBoundsException("Bad image index!");
    }
    readMetadata();
    return imageMetadata;
}
 
Example 27
Project: jdk8u-jdk   File: ITXtTest.java   Source Code and License 5 votes vote down vote up
private static void writeTo(File f, ITXtTest t) {
    BufferedImage src = createBufferedImage();
    try {
        ImageOutputStream imageOutputStream =
            ImageIO.createImageOutputStream(f);

        ImageTypeSpecifier imageTypeSpecifier =
            new ImageTypeSpecifier(src);
        ImageWriter imageWriter =
            ImageIO.getImageWritersByFormatName("PNG").next();

        imageWriter.setOutput(imageOutputStream);

        IIOMetadata m =
            imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);

        String format = m.getNativeMetadataFormatName();
        Node root = m.getAsTree(format);

        IIOMetadataNode iTXt = t.getNode();
        root.appendChild(iTXt);
        m.setFromTree(format, root);

        imageWriter.write(new IIOImage(src, null, m));
        imageOutputStream.close();
        System.out.println("Writing done.");
    } catch (Throwable e) {
        throw new RuntimeException("Writing test failed.", e);
    }
}
 
Example 28
Project: openjdk-jdk10   File: TIFFImageReadParamTest.java   Source Code and License 5 votes vote down vote up
private void writeImage() throws Exception {

        OutputStream s = new BufferedOutputStream(new FileOutputStream(FILENAME));
        try (ImageOutputStream ios = ImageIO.createImageOutputStream(s)) {
            ImageWriter writer = getTIFFWriter();
            writer.setOutput(ios);

            BufferedImage img =
                new BufferedImage(SZ, SZ, BufferedImage.TYPE_INT_RGB);
            Graphics g = img.getGraphics();
            g.setColor(C);
            g.fillRect(0, 0, SZ, SZ);
            g.dispose();

            IIOMetadata metadata = writer.getDefaultImageMetadata(
                new ImageTypeSpecifier(img), writer.getDefaultWriteParam());

            TIFFDirectory dir = TIFFDirectory.createFromMetadata(metadata);

            // add some extension tags
            addASCIIField(dir, "GeoAsciiParamsTag", GEO_DATA, GEO_N);
            addASCIIField(dir, "DateTimeOriginal", EXIF_DATA, EXIF_N);
            addASCIIField(dir, "GPSStatus", GPS_DATA, GPS_N);

            dir.addTIFFField(new TIFFField(new TIFFTag(
                "CleanFaxData", FAX_N, 1 << TIFFTag.TIFF_SHORT), FAX_DATA));

            IIOMetadata data = dir.getAsMetadata();

            writer.write(new IIOImage(img, null, data));

            ios.flush();
            writer.dispose();
        }
    }
 
Example 29
Project: OpenJSharp   File: GIFImageWriter.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a default stream metadata object and merges in the
 * supplied metadata.
 */
public IIOMetadata convertStreamMetadata(IIOMetadata inData,
                                         ImageWriteParam param) {
    if (inData == null) {
        throw new IllegalArgumentException("inData == null!");
    }

    IIOMetadata sm = getDefaultStreamMetadata(param);

    convertMetadata(STREAM_METADATA_NAME, inData, sm);

    return sm;
}
 
Example 30
Project: OpenJSharp   File: GIFImageWriter.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a default image metadata object and merges in the
 * supplied metadata.
 */
public IIOMetadata convertImageMetadata(IIOMetadata inData,
                                        ImageTypeSpecifier imageType,
                                        ImageWriteParam param) {
    if (inData == null) {
        throw new IllegalArgumentException("inData == null!");
    }
    if (imageType == null) {
        throw new IllegalArgumentException("imageType == null!");
    }

    GIFWritableImageMetadata im =
        (GIFWritableImageMetadata)getDefaultImageMetadata(imageType,
                                                          param);

    // Save interlace flag state.

    boolean isProgressive = im.interlaceFlag;

    convertMetadata(IMAGE_METADATA_NAME, inData, im);

    // Undo change to interlace flag if not MODE_COPY_FROM_METADATA.

    if (param != null && param.canWriteProgressive() &&
        param.getProgressiveMode() != param.MODE_COPY_FROM_METADATA) {
        im.interlaceFlag = isProgressive;
    }

    return im;
}
 
Example 31
Project: openjdk-jdk10   File: DestTypeTest.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    BufferedImage bi_rgb = createTestImage(BufferedImage.TYPE_INT_RGB);

    DestTypeTest bug = new DestTypeTest();
    byte[] rgb_data = bug.writeTest(bi_rgb);

    System.out.println("rgb jpeg data length is " + rgb_data.length);

    BufferedImage bi_argb = createTestImage(BufferedImage.TYPE_INT_ARGB);

    ImageWriteParam p = bug.getWriteParam();
    IIOMetadata m = bug.getMetadata(p);

    byte[] submeta_data = bug.writeTest(bi_argb, p, m);
    System.out.println("desttype and metadata jpeg data length is " + submeta_data.length);

    p = bug.getWriteParam();
    byte[] subbanded_data = bug.writeTest(bi_argb, p);
    System.out.println("desttype jpeg data length is " + subbanded_data.length);

    if (submeta_data.length > rgb_data.length) {
        throw new  RuntimeException("Too big result jpeg: " + submeta_data.length +
                                    "(rgb image size is " + rgb_data.length + ")");
    }
    if (subbanded_data.length > rgb_data.length) {
        throw new  RuntimeException("Too big result jpeg: " + subbanded_data.length +
                                    "(rgb image size is " + rgb_data.length + ")");
    }
}
 
Example 32
Project: openjdk-jdk10   File: JPEGImageWriter.java   Source Code and License 5 votes vote down vote up
public IIOMetadata convertStreamMetadata(IIOMetadata inData,
                                         ImageWriteParam param) {
    // There isn't much we can do.  If it's one of ours, then
    // return it.  Otherwise just return null.  We use it only
    // for tables, so we can't get a default and modify it,
    // as this will usually not be what is intended.
    if (inData instanceof JPEGMetadata) {
        JPEGMetadata jpegData = (JPEGMetadata) inData;
        if (jpegData.isStream) {
            return inData;
        }
    }
    return null;
}
 
Example 33
Project: openjdk-jdk10   File: GIFImageWriter.java   Source Code and License 5 votes vote down vote up
/**
 * Merges {@code inData} into {@code outData}. The supplied
 * metadata format name is attempted first and failing that the standard
 * metadata format name is attempted.
 */
private void convertMetadata(String metadataFormatName,
                             IIOMetadata inData,
                             IIOMetadata outData) {
    String formatName = null;

    String nativeFormatName = inData.getNativeMetadataFormatName();
    if (nativeFormatName != null &&
        nativeFormatName.equals(metadataFormatName)) {
        formatName = metadataFormatName;
    } else {
        String[] extraFormatNames = inData.getExtraMetadataFormatNames();

        if (extraFormatNames != null) {
            for (int i = 0; i < extraFormatNames.length; i++) {
                if (extraFormatNames[i].equals(metadataFormatName)) {
                    formatName = metadataFormatName;
                    break;
                }
            }
        }
    }

    if (formatName == null &&
        inData.isStandardMetadataFormatSupported()) {
        formatName = STANDARD_METADATA_NAME;
    }

    if (formatName != null) {
        try {
            Node root = inData.getAsTree(formatName);
            outData.mergeTree(formatName, root);
        } catch(IIOInvalidTreeException e) {
            // ignore
        }
    }
}
 
Example 34
Project: OpenJSharp   File: PNGImageWriter.java   Source Code and License 5 votes vote down vote up
public IIOMetadata convertImageMetadata(IIOMetadata inData,
                                        ImageTypeSpecifier imageType,
                                        ImageWriteParam param) {
    // TODO - deal with imageType
    if (inData instanceof PNGMetadata) {
        return (PNGMetadata)((PNGMetadata)inData).clone();
    } else {
        return new PNGMetadata(inData);
    }
}
 
Example 35
Project: jdk8u-jdk   File: BMPImageWriter.java   Source Code and License 5 votes vote down vote up
public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType,
                                           ImageWriteParam param) {
    BMPMetadata meta = new BMPMetadata();
    meta.bmpVersion = VERSION_3;
    meta.compression = getPreferredCompressionType(imageType);
    if (param != null
        && param.getCompressionMode() == ImageWriteParam.MODE_EXPLICIT) {
        meta.compression = BMPCompressionTypes.getType(param.getCompressionType());
    }
    meta.bitsPerPixel = (short)imageType.getColorModel().getPixelSize();
    return meta;
}
 
Example 36
Project: openjdk-jdk10   File: GIFImageWriter.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a default stream metadata object and merges in the
 * supplied metadata.
 */
public IIOMetadata convertStreamMetadata(IIOMetadata inData,
                                         ImageWriteParam param) {
    if (inData == null) {
        throw new IllegalArgumentException("inData == null!");
    }

    IIOMetadata sm = getDefaultStreamMetadata(param);

    convertMetadata(STREAM_METADATA_NAME, inData, sm);

    return sm;
}
 
Example 37
Project: openjdk-jdk10   File: MergeTreeTest.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    ImageWriter iw =
        (ImageWriter)ImageIO.getImageWritersByFormatName("jpeg").next();

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);

    ImageOutputStream ios =
        ImageIO.createImageOutputStream(new File("MergeTreeTest.jpeg"));
    iw.setOutput(ios);

    IIOMetadata meta = iw.getDefaultImageMetadata(type, null);

    boolean isFailed = false;

    String[] fmts = meta.getMetadataFormatNames();
    for (int i=0; i<fmts.length; i++) {
        System.out.print("Format: " + fmts[i] + " ... ");
        Node root = meta.getAsTree(fmts[i]);
        try {
            meta.mergeTree(fmts[i], root);
        } catch (NullPointerException e) {
            throw new RuntimeException("Test failed for format " + fmts[i], e);
        }
        System.out.println("PASSED");
    }
}
 
Example 38
Project: jdk8u-jdk   File: JPEGImageWriter.java   Source Code and License 5 votes vote down vote up
public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param) {
    setThreadLock();
    try {
        return new JPEGMetadata(param, this);
    } finally {
        clearThreadLock();
    }
}
 
Example 39
Project: openjdk-jdk10   File: PNGImageWriter.java   Source Code and License 5 votes vote down vote up
@Override
public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType,
                                           ImageWriteParam param) {
    PNGMetadata m = new PNGMetadata();
    m.initialize(imageType, imageType.getSampleModel().getNumBands());
    return m;
}
 
Example 40
Project: openjdk-jdk10   File: BMPCompressionTest.java   Source Code and License 5 votes vote down vote up
private Test(BMPImageWriteParam p, IIOMetadata md, BufferedImage i) {
    param = p;
    meta = md;
    img = i;


    description = "Compression type is " + p.getCompressionType();
}