Path: blob/master/test/jdk/javax/imageio/plugins/bmp/BMPPixelSpacingTest.java
41153 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 718275826* @summary Test verifies whether we are getting correct Horizontal27* & Vertical Physical pixel spacing for active BMP image28* through stored metadata or not.29* @run main BMPPixelSpacingTest30*/3132import java.io.ByteArrayInputStream;33import java.util.Iterator;34import javax.imageio.ImageIO;35import javax.imageio.ImageReader;36import javax.imageio.metadata.IIOMetadata;37import javax.imageio.stream.ImageInputStream;38import org.w3c.dom.NamedNodeMap;39import org.w3c.dom.Node;40import org.w3c.dom.NodeList;4142public class BMPPixelSpacingTest {4344public static void main(String[] args) throws Exception {45// Header contaning X & Y pixels-per-meter more than value 146byte[] bmpHeaderData = { (byte) 0x42, (byte) 0x4d, (byte) 0x7e,47(byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,48(byte) 0x00, (byte) 0x00, (byte) 0x3e, (byte) 0x00, (byte) 0x00,49(byte) 0x00, (byte) 0x28, (byte) 0x00, (byte) 0x00, (byte) 0x00,50(byte) 0x64, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x64,51(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,52(byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,53(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,54(byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02,55(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,56(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0xff,57(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,58(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,59(byte) 0xff };6061ImageInputStream imageInput = ImageIO.62createImageInputStream(new ByteArrayInputStream(bmpHeaderData));6364for (Iterator<ImageReader> it = ImageIO.getImageReaders(imageInput);65it.hasNext(); ) {66ImageReader reader = it.next();67reader.setInput(imageInput);68IIOMetadata metadata = reader.getImageMetadata(0);6970Node rootNode = metadata.getAsTree("javax_imageio_1.0");71NodeList nl = rootNode.getChildNodes();7273//Parse until you get Dimension child node74for (int i = 0; i < nl.getLength(); i++) {75Node node = nl.item(i);76if ((node.getNodeName()).equals("Dimension")) {77//get childnode list under Dimension node78NodeList cl = node.getChildNodes();79//Corresponding node indices under Dimension node80int horizontalNodeIndex = 1;81int verticalNodeIndex = 2;82Node horizontalNode = cl.item(horizontalNodeIndex);83Node verticalNode = cl.item(verticalNodeIndex);8485//get attributes for horizontal and vertical nodes86NamedNodeMap horizontalAttr = horizontalNode.87getAttributes();88NamedNodeMap verticalAttr = verticalNode.getAttributes();8990//since they have only one attribute index is 091int attributeIndex = 0;92Node horizontalValue = horizontalAttr.item(attributeIndex);93Node verticalValue = verticalAttr.item(attributeIndex);94float horizontalNodeValue = Float.95parseFloat((horizontalValue.getNodeValue()));96float verticalNodeValue = Float.97parseFloat((verticalValue.getNodeValue()));9899float expectedHorizontalValue, expectedVerticalValue;100// in test metadata xPixelsPerMeter & yPixelsPerMeter is 2101expectedHorizontalValue = expectedVerticalValue =1021000.0F / 2;103//expected and returned values should be same104if ((Float.compare(horizontalNodeValue,105expectedHorizontalValue) != 0) ||106(Float.compare(verticalNodeValue,107expectedVerticalValue) != 0)) {108throw new RuntimeException("Invalid pixel spacing");109}110}111}112}113}114}115116117