< prev index next >

modules/graphics/src/test/java/com/sun/javafx/iio/ImageLoaderScalingTest.java

Print this page




  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 }


  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.Ignore;
  35 import org.junit.Test;
  36 
  37 public class ImageLoaderScalingTest {
  38     // if true, the test will write original and scaled PNG files to the current directory
  39     private static final boolean writeFiles = false;
  40 
  41     private BufferedImage createImage(int w, int h) {
  42         BufferedImage bImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  43         ImageTestHelper.drawImageRandom(bImg);
  44         return bImg;
  45     }
  46 
  47     private Image loadImage(InputStream stream, int width, int height)
  48             throws Exception
  49     {
  50         ImageFrame[] imgFrames =
  51             ImageStorage.loadAll(stream, null, width, height, false, 1.0f, false);
  52         assertNotNull(imgFrames);
  53         assertTrue(imgFrames.length > 0);
  54         return Image.convertImageFrame(imgFrames[0]);
  55     }
  56 
  57     private void compare(Image img, Image expectedImg) {
  58         assertNotNull(img);
  59         assertNotNull(expectedImg);
  60         int w = img.getWidth(), h = img.getHeight();
  61         double scaleX = (double)expectedImg.getWidth() / w;
  62         double scaleY = (double)expectedImg.getHeight() / h;
  63         for (int y = 0; y < h; y++) {
  64             int srcY = (int) Math.floor((y + 0.5) * scaleY);
  65             for (int x = 0; x < w; x++) {
  66                 int srcX = (int) Math.floor((x + 0.5) * scaleX);
  67                 int expected = expectedImg.getArgb(srcX, srcY);
  68                 int actual = img.getArgb(x, y);
  69                 if (expected != actual) {
  70                     if (writeFiles) {
  71                         writeImages(img, expectedImg);
  72                     }
  73                     throw new org.junit.ComparisonFailure(
  74                         "pixel " + x + ", " + y + " does not match",
  75                         String.format("0x%08X", expected),
  76                         String.format("0x%08X", actual)
  77                     );
  78                 }
  79             }
  80         }
  81     }
  82 
  83     private void writeImage(Image img, String fileName) {








  84         int w = img.getWidth();
  85         int h = img.getHeight();

  86         int pixels[] = new int[w * h];
  87         img.getPixels(0, 0, w, h,
  88                 javafx.scene.image.PixelFormat.getIntArgbPreInstance(),
  89                 pixels, 0, w);
  90         BufferedImage bImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  91         bImg.setRGB(0, 0, w, h, pixels, 0, w);
  92         try {
  93             ImageTestHelper.writeImage(bImg, fileName, "png", null);
  94         } catch (IOException e) {
  95             System.err.println("writeImage " + fileName + " failed: " + e);
  96         }
  97     }
  98 
  99     private void writeImages(Image img, Image expectedImg) {
 100         int w = img.getWidth();
 101         int h = img.getHeight();
 102         writeImage(expectedImg, "out"+w+"x"+h+"Orig.png");
 103         writeImage(img, "out"+w+"x"+h+"Scaled.png");
 104     }
 105 
 106     private void scaleAndCompareImage(BufferedImage bImg, String format,
 107             int width, int height) throws Exception
 108     {
 109         ByteArrayInputStream in = ImageTestHelper.writeImageToStream(bImg, format, null);
 110         Image expectedImg = loadImage(in, 0, 0);
 111         in.reset();
 112         Image img = loadImage(in, width, height);
 113         compare(img, expectedImg);
 114     }
 115 
 116     private void testScale(String format, int w1, int h1, int w2, int h2) throws Exception {
 117         BufferedImage bImg = createImage(w1, h1);
 118         scaleAndCompareImage(bImg, format, w2, h2);
 119     }
 120 
 121     @Test
 122     public void testNoScalePNG() throws Exception {
 123         testScale("png", 100, 100, 100, 100);
 124     }
 125 
 126     @Test
 127     public void testNoScaleBMP() throws Exception {
 128         testScale("bmp", 100, 100, 100, 100);
 129     }
 130 
 131     @Test
 132     public void testNoScaleJPG() throws Exception {
 133         testScale("jpg", 100, 100, 100, 100);
 134     }
 135 
 136     @Test
 137     public void testNoScaleGIF() throws Exception {
 138         testScale("gif", 100, 100, 100, 100);
 139     }
 140 
 141     @Test
 142     public void testAllTheScalesPNG() throws Exception {
 143         testAllTheScales("png");
 144     }
 145 
 146     @Test
 147     public void testAllTheScalesBMP() throws Exception {
 148         testAllTheScales("bmp");
 149     }
 150 
 151     @Ignore // libjpeg can scale the image itself and results are unpredictable
 152     @Test
 153     public void testAllTheScalesJPG() throws Exception {
 154         testAllTheScales("jpg");
 155     }
 156 
 157     @Test
 158     public void testAllTheScalesGIF() throws Exception {
 159         testAllTheScales("gif");
 160     }
 161 
 162     public void testAllTheScales(String format) throws Exception {
 163         BufferedImage bImg = createImage(10, 10);
 164         for (int h = 2; h < 20; h++) {
 165             for (int w = 2; w < 20; w++) {
 166                 scaleAndCompareImage(bImg, format, w, h);
 167                 testScale(format, w, h, 10, 10);
 168             }
 169         }
 170     }
 171 


 172     // (62.0 / 78.0) * 78 != 62
 173     @Test
 174     public void testRT20295_PNG() throws Exception {
 175         testScale("png", 100, 62, 100, 78);
 176     }
 177 
 178     @Test
 179     public void testRT20295_BMP() throws Exception {
 180         testScale("bmp", 100, 62, 100, 78);
 181     }
 182 
 183     @Test
 184     public void testRT20295_JPG() throws Exception {
 185         testScale("jpg", 100, 62, 100, 78);
 186     }
 187 
 188     @Test
 189     public void testRT20295_GIF() throws Exception {
 190         testScale("gif", 100, 62, 100, 78);
 191     }
 192 }
< prev index next >