1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.iio;
  27 
  28 import com.sun.prism.Image;
  29 import java.awt.image.BufferedImage;
  30 import java.io.ByteArrayInputStream;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import static org.junit.Assert.*;
  34 import org.junit.Test;
  35 
  36 public class ImageLoaderScalingTest {
  37     // if true, the test will write original and scaled PNG files to the current directory
  38     private static final boolean writeFiles = false;
  39 
  40     private BufferedImage createImage(int w, int h) {
  41         BufferedImage bImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  42         ImageTestHelper.drawImageRandom(bImg);
  43         return bImg;
  44     }
  45 
  46     private Image loadImage(InputStream stream, int width, int height)
  47             throws Exception
  48     {
  49         ImageFrame[] imgFrames =
  50             ImageStorage.loadAll(stream, null, width, height, false, 1.0f, false);
  51         assertNotNull(imgFrames);
  52         assertTrue(imgFrames.length > 0);
  53         return Image.convertImageFrame(imgFrames[0]);
  54     }
  55 
  56     private void compare(Image img, BufferedImage bImg) {
  57         assertNotNull(img);
  58         assertNotNull(bImg);
  59         int w = img.getWidth(), h = img.getHeight();
  60         double scaleX = (double)bImg.getWidth() / w;
  61         double scaleY = (double)bImg.getHeight() / h;
  62         for (int y = 0; y < h; y++) {
  63             int srcY = (int) Math.floor((y + 0.5) * scaleY);
  64             for (int x = 0; x < w; x++) {
  65                 int srcX = (int) Math.floor((x + 0.5) * scaleX);
  66                 int expected = bImg.getRGB(srcX, srcY);
  67                 int actual = img.getArgb(x, y);
  68                 if (expected != actual) {
  69                     if (writeFiles) {
  70                         writeImages(img, bImg);
  71                     }
  72                     throw new org.junit.ComparisonFailure(
  73                         "pixel " + x + ", " + y + " does not match",
  74                         String.format("0x%08X", expected),
  75                         String.format("0x%08X", actual)
  76                     );
  77                 }
  78             }
  79         }
  80     }
  81 
  82     private void writePNGFile(BufferedImage bImg, String fileName) {
  83         try {
  84             ImageTestHelper.writeImage(bImg, fileName, "png", null);
  85         } catch (IOException e) {
  86             System.out.println("writePNGFile " + fileName + " failed: " + e);
  87         }
  88     }
  89 
  90     private void writeImages(Image img, BufferedImage bImg) {
  91         int w = img.getWidth();
  92         int h = img.getHeight();
  93         writePNGFile(bImg, "out"+w+"x"+h+"OrigJDK.png");
  94         int pixels[] = new int[w * h];
  95         img.getPixels(0, 0, w, h,
  96                 javafx.scene.image.PixelFormat.getIntArgbPreInstance(),
  97                 pixels, 0, w);
  98         BufferedImage fxImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  99         fxImg.setRGB(0, 0, w, h, pixels, 0, w);
 100         writePNGFile(fxImg, "out"+w+"x"+h+"ScaledFX.png");
 101     }
 102 
 103     private ByteArrayInputStream writePNGStream(BufferedImage bImg)
 104             throws IOException
 105     {
 106         return ImageTestHelper.writeImageToStream(bImg, "png", null);
 107     }
 108 
 109     private void scaleAndCompareImage(BufferedImage bImg, int width, int height)
 110             throws Exception
 111     {
 112         ByteArrayInputStream in = writePNGStream(bImg);                
 113         Image img = loadImage(in, width, height);
 114         compare(img, bImg);
 115     }
 116 
 117     private void testScale(int w1, int h1, int w2, int h2) throws Exception {
 118         BufferedImage bImg = createImage(w1, h1);
 119         scaleAndCompareImage(bImg, w2, h2);
 120     }
 121 
 122     @Test
 123     public void testNoScale() throws Exception {
 124         testScale(100, 100, 100, 100);
 125     }
 126 
 127     @Test
 128     public void testAllTheScales() throws Exception {
 129         BufferedImage bImg = createImage(10, 10);
 130         for (int h = 2; h < 20; h++) {
 131             for (int w = 2; w < 20; w++) {
 132                 scaleAndCompareImage(bImg, w, h);
 133                 testScale(w, h, 10, 10);
 134             }
 135         }
 136     }
 137 
 138     @Test
 139     public void testRT20295() throws Exception {
 140         // (62.0 / 78.0) * 78 != 62
 141         testScale(100, 62, 100, 78);
 142     }
 143 }