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.ByteArrayOutputStream;
  32 import java.io.IOException;
  33 import java.io.InputStream;
  34 import javax.imageio.ImageIO;
  35 import static org.junit.Assert.assertNotNull;
  36 import static org.junit.Assert.assertTrue;
  37 import static org.junit.Assert.fail;
  38 import org.junit.Test;
  39 
  40 public class ImageLoaderScalingTest {
  41     private static final int testWidth = 170, testHeight = 62;
  42 
  43     Image loadImage(InputStream stream,
  44             int width, int height, boolean preserveRatio, boolean smooth)
  45     {
  46         try {
  47             ImageFrame[] frames = ImageStorage.loadAll(stream, null,
  48                     width, height, preserveRatio, 1.0f, smooth);
  49             assertTrue(frames.length > 0);
  50             assertNotNull(frames[0]);
  51             return Image.convertImageFrame(frames[0]);
  52         } catch (ImageStorageException e) {
  53             if (e.getCause() != null) {
  54                 e.getCause().printStackTrace();
  55                 fail("unexpected ImageStorageException: " + e.getCause());
  56             } else {
  57                 fail("unexpected ImageStorageException: " + e);
  58             }
  59         }
  60         return null;
  61     }
  62 
  63     ByteArrayInputStream getJPGStream(BufferedImage bImg) {
  64         ByteArrayOutputStream out = new ByteArrayOutputStream();
  65         try {
  66             ImageIO.write(bImg, "jpg", out);
  67         } catch (IOException e) {
  68             fail("unexpected IOException: " + e);
  69         }
  70         return new ByteArrayInputStream(out.toByteArray());
  71     }
  72 
  73     Image getImage(BufferedImage bImg,
  74             int width, int height, boolean preserveRatio, boolean smooth)
  75     {
  76         return loadImage(getJPGStream(bImg), width, height, preserveRatio, smooth);
  77     }
  78 
  79     BufferedImage createImage(int w, int h) {
  80         return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  81     }
  82 
  83     void testResize(int w, int h) {
  84         BufferedImage bImg = createImage(testWidth, testHeight);
  85         assertNotNull(getImage(bImg, w, h, false, false));
  86         assertNotNull(getImage(bImg, w, h, false, true));
  87         assertNotNull(getImage(bImg, w, h, true, false));
  88         assertNotNull(getImage(bImg, w, h, true, true));
  89     }
  90 
  91     @Test
  92     public void testNoScale() {
  93         testResize(0, 0);
  94     }
  95 
  96     @Test
  97     public void testScale() {
  98         testResize(testWidth * 2, testHeight * 2);
  99         testResize(testWidth / 2, testHeight / 2);
 100     }
 101 
 102     @Test
 103     public void testRT20295() {
 104         ByteArrayInputStream in = getJPGStream(createImage(100, 62));
 105         // (62.0 / 78.0) * 78 != 62
 106         assertNotNull(loadImage(in, 100, 78, false, false));
 107     }
 108 }