Path: blob/master/test/jdk/javax/imageio/plugins/png/ImageCompare.java
41155 views
/*1* Copyright (c) 2000, 2017, 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*/2223import java.awt.image.BufferedImage;24import java.awt.image.ColorModel;25import java.awt.image.Raster;2627// Utility to compare two BufferedImages for RGB equality28public class ImageCompare {2930public static void compare(BufferedImage oldimg,31BufferedImage newimg) {32int width = oldimg.getWidth();33int height = oldimg.getHeight();34if (newimg.getWidth() != width || newimg.getHeight() != height) {35throw new RuntimeException("Dimensions changed!");36}3738Raster oldras = oldimg.getRaster();39ColorModel oldcm = oldimg.getColorModel();40Raster newras = newimg.getRaster();41ColorModel newcm = newimg.getColorModel();4243for (int j = 0; j < height; j++) {44for (int i = 0; i < width; i++) {45Object oldpixel = oldras.getDataElements(i, j, null);46int oldrgb = oldcm.getRGB(oldpixel);47int oldalpha = oldcm.getAlpha(oldpixel);4849Object newpixel = newras.getDataElements(i, j, null);50int newrgb = newcm.getRGB(newpixel);51int newalpha = newcm.getAlpha(newpixel);5253if (newrgb != oldrgb ||54newalpha != oldalpha) {55throw new RuntimeException("Pixels differ at " + i +56", " + j);57}58}59}60}61}626364