Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/MagentaEXIFTest.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 8071707 624337626* @summary Test verifies that EXIF images with differing sampling factors27* are written correctly28*29* @run main MagentaEXIFTest30*/3132import java.awt.Color;33import java.awt.Graphics2D;34import java.awt.image.BufferedImage;3536import java.io.ByteArrayInputStream;37import java.io.ByteArrayOutputStream;3839import javax.imageio.IIOImage;40import javax.imageio.ImageIO;41import javax.imageio.ImageReader;42import javax.imageio.ImageTypeSpecifier;43import javax.imageio.ImageWriteParam;44import javax.imageio.ImageWriter;45import javax.imageio.metadata.IIOInvalidTreeException;46import javax.imageio.metadata.IIOMetadata;47import javax.imageio.metadata.IIOMetadataNode;48import javax.imageio.stream.ImageInputStream;49import javax.imageio.stream.ImageOutputStream;5051import org.w3c.dom.Attr;52import org.w3c.dom.NamedNodeMap;53import org.w3c.dom.Node;54import org.w3c.dom.NodeList;555657public class MagentaEXIFTest {5859public static void main(final String[] argv) throws Exception {6061IIOMetadata jpegmetadata = null;62ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();63try {64jpegmetadata = createJPEGMetadata(jpgWriter);65} catch (Exception e) {66throw new RuntimeException(e);67}6869ByteArrayOutputStream baos = new ByteArrayOutputStream();70ImageOutputStream output = ImageIO.createImageOutputStream(baos);71jpgWriter.setOutput(output);72int w=100, h=100;73BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);74Graphics2D g2d = bi.createGraphics();75g2d.setColor(Color.white);76g2d.fillRect(0, 0, w, h);77IIOImage image = new IIOImage(bi, null, jpegmetadata);78jpgWriter.write(null, image, null);79jpgWriter.dispose();8081baos.flush();82ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());83ImageInputStream iis = ImageIO.createImageInputStream(bais);84bi = ImageIO.read(iis);85for (int i=0; i<bi.getWidth(); i++) {86for(int j=0; j<bi.getHeight(); j++) {87if (bi.getRGB(i, j) != Color.white.getRGB()) {88throw new RuntimeException("Wrong color : " + Integer.toHexString(bi.getRGB(i, j)));89}90}91}9293}949596static void displayMetadata(Node node, int level) {97for (int i = 0; i < level; i++) System.out.print(" ");98System.out.print("<" + node.getNodeName());99NamedNodeMap map = node.getAttributes();100if (map != null) { // print attribute values101int length = map.getLength();102for (int i = 0; i < length; i++) {103Node attr = map.item(i);104System.out.print(" " + attr.getNodeName() +105"=\"" + attr.getNodeValue() + "\"");106}107}108109Node child = node.getFirstChild();110if (child != null) {111System.out.println(">"); // close current tag112while (child != null) { // emit child tags recursively113displayMetadata(child, level + 1);114child = child.getNextSibling();115}116for (int i = 0; i < level; i++) System.out.print(" ");117System.out.println("</" + node.getNodeName() + ">");118} else {119System.out.println("/>");120}121}122123/*124* Construct a JPEG IIOMetadata that has had the JFIF marker removed and125* an APP1 EXIF marker added, and further massaged so that we have differing126* horizontal and vertical sampling factors for one channel.127*/128static IIOMetadata createJPEGMetadata(ImageWriter iw) throws IIOInvalidTreeException {129String jpegMDName = "javax_imageio_jpeg_image_1.0";130ImageWriter imgWriter = ImageIO.getImageWritersByFormatName("jpg").next();131BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);132ImageTypeSpecifier ist = new ImageTypeSpecifier(bi);133IIOMetadata metadata = imgWriter.getDefaultImageMetadata(ist, null);134135IIOMetadataNode root = new IIOMetadataNode(jpegMDName);136IIOMetadataNode header = new IIOMetadataNode("JPEGvariety");137IIOMetadataNode sequence = new IIOMetadataNode("markerSequence");138139root.appendChild(header);140root.appendChild(sequence);141142IIOMetadataNode app1 = new IIOMetadataNode("unknown");143app1.setUserObject(new byte[255]);144app1.setAttribute("MarkerTag", "255");145sequence.appendChild(app1);146147IIOMetadataNode sof = new IIOMetadataNode("sof");148sof.setAttribute("process", "0");149sof.setAttribute("samplePrecision", "8");150sof.setAttribute("numLines", "100");151sof.setAttribute("samplesPerLine", "100");152sof.setAttribute("numFrameComponents", "3");153IIOMetadataNode c1 = new IIOMetadataNode("componentSpec");154c1.setAttribute("componentId", "1");155c1.setAttribute("HsamplingFactor", "1");156c1.setAttribute("VsamplingFactor", "2");157c1.setAttribute("QtableSelector", "1");158sof.appendChild(c1);159IIOMetadataNode c2 = new IIOMetadataNode("componentSpec");160c2.setAttribute("componentId", "2");161c2.setAttribute("HsamplingFactor", "1");162c2.setAttribute("VsamplingFactor", "1");163c2.setAttribute("QtableSelector", "1");164sof.appendChild(c2);165IIOMetadataNode c3 = new IIOMetadataNode("componentSpec");166c3.setAttribute("componentId", "3");167c3.setAttribute("HsamplingFactor", "1");168c3.setAttribute("VsamplingFactor", "1");169c3.setAttribute("QtableSelector", "1");170sof.appendChild(c3);171sequence.appendChild(sof);172metadata.setFromTree(jpegMDName, root);173IIOMetadata def = imgWriter.getDefaultImageMetadata(ist, null);174metadata.mergeTree(jpegMDName, def.getAsTree(jpegMDName));175Node tree = metadata.getAsTree(jpegMDName);176Node variety = tree.getFirstChild();177Node jfif = variety.getFirstChild();178variety.removeChild(jfif);179sequence = (IIOMetadataNode)tree.getLastChild();180NodeList markers = sequence.getChildNodes();181IIOMetadataNode n, sofNode=null;182for (int i=0;i<markers.getLength();i++) {183n = (IIOMetadataNode)markers.item(i);184if (n.getNodeName().equals("sof")) {185sofNode = n;186break;187}188}189IIOMetadataNode componentSpec = (IIOMetadataNode)sofNode.getFirstChild();190Attr attr = componentSpec.getAttributeNode("HsamplingFactor");191attr.setValue("1");192attr = componentSpec.getAttributeNode("VsamplingFactor");193attr.setValue("2");194metadata.setFromTree(jpegMDName, tree);195String[] names = metadata.getMetadataFormatNames();196int length = names.length;197for (int i = 0; i < length; i++) {198System.out.println( "Format name: " + names[ i ] );199displayMetadata(metadata.getAsTree(names[i]), 0);200}201202return metadata;203}204}205206207