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 test.com.sun.javafx.iio;
  27 
  28 import java.awt.Color;
  29 import java.awt.GradientPaint;
  30 import java.awt.Graphics2D;
  31 import java.awt.image.BufferedImage;
  32 import java.io.ByteArrayInputStream;
  33 import java.io.ByteArrayOutputStream;
  34 import java.io.File;
  35 import java.io.FilterInputStream;
  36 import java.io.IOException;
  37 import java.io.InputStream;
  38 import java.util.Iterator;
  39 import java.util.Random;
  40 import javax.imageio.IIOImage;
  41 import javax.imageio.ImageIO;
  42 import javax.imageio.ImageWriteParam;
  43 import javax.imageio.ImageWriter;
  44 import javax.imageio.stream.ImageOutputStream;
  45 
  46 public class ImageTestHelper {
  47 
  48     public static void writeImage(BufferedImage bImg, String fileName, String format, String compression)
  49             throws IOException
  50     {
  51         if (fileName != null) {
  52             File file = new File(fileName);
  53             file.delete();
  54             writeImage(bImg, file, format, compression);
  55         }
  56     }
  57 
  58     public static void writeImage(BufferedImage bImg, Object out, String format, String compression)
  59             throws IOException
  60     {
  61         try (ImageOutputStream ios = ImageIO.createImageOutputStream(out)) {
  62             Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(format);
  63             ImageWriter writer = iter.next();
  64             ImageWriteParam iwp = writer.getDefaultWriteParam();
  65             if (compression != null) {
  66                 iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  67                 iwp.setCompressionType(compression);
  68             }
  69             writer.setOutput(ios);
  70             try {
  71                 writer.write(null, new IIOImage(bImg, null, null), iwp);
  72             } finally {
  73                 writer.dispose();
  74                 ios.flush();
  75             }
  76         }
  77     }
  78 
  79     public static ByteArrayInputStream writeImageToStream(BufferedImage bImg,
  80             String format, String compression) throws IOException
  81     {
  82         ByteArrayOutputStream out = new ByteArrayOutputStream();
  83         writeImage(bImg, out, format, compression);
  84         return new ByteArrayInputStream(out.toByteArray());
  85     }
  86 
  87     public static void drawImageGradient(BufferedImage bImg) {
  88         int w = bImg.getWidth();
  89         int h = bImg.getHeight();
  90         Graphics2D graphics = bImg.createGraphics();
  91         GradientPaint g = new GradientPaint(0, 0, Color.RED, w, h, Color.GREEN);
  92         graphics.setPaint(g);
  93         graphics.fillRect(0, 0, w, h);
  94     }
  95 
  96     public static void drawImageRandom(BufferedImage bImg) {
  97         int w = bImg.getWidth();
  98         int h = bImg.getHeight();
  99         Random r = new Random(1);
 100         for (int y = 0; y < h; y++) {
 101             for (int x = 0; x < w; x++) {
 102                 bImg.setRGB(x, y, r.nextInt(1 << 24));
 103             }
 104         }
 105     }
 106 
 107     public static void drawImageHue(BufferedImage bImg) {
 108         int w = bImg.getWidth();
 109         int h = bImg.getHeight();
 110         for (int y = 0; y < h; y++) {
 111             float s = 2.0f * y / h;
 112             if (s > 1) {
 113                 s = 1;
 114             }
 115             float b = 2.0f * (h - y) / h;
 116             if (b > 1) {
 117                 b = 1;
 118             }
 119             for (int x = 0; x < w; x++) {
 120                 float hue = (float) x / w;
 121                 bImg.setRGB(x, y, Color.HSBtoRGB(hue, s, b));
 122             }
 123         }
 124     }
 125 
 126     public static void drawImageAll(BufferedImage bImg) {
 127         int w = bImg.getWidth();
 128         int h = bImg.getHeight();
 129         //if (h*w < (1<<24)) return;
 130         for (int y = 0; y < h; y++) {
 131             for (int x = 0; x < w; x++) {
 132                 bImg.setRGB(x, y, y * h + x);
 133             }
 134         }
 135     }
 136 
 137     public static InputStream createTestImageStream(String format)
 138             throws IOException
 139     {
 140         BufferedImage bImg = new BufferedImage(509, 157, BufferedImage.TYPE_INT_RGB);
 141         ImageTestHelper.drawImageRandom(bImg);
 142         return ImageTestHelper.writeImageToStream(bImg, format, null);
 143     }
 144 
 145     public static InputStream createStutteringInputStream(InputStream in) {
 146         return new FilterInputStream(in) {
 147 
 148             private final Random rnd = new Random(0);
 149             private int numReadStutters = 10;
 150             private int numSkipStutters = 10;
 151 
 152             @Override
 153             public int read(byte[] b, int off, int len) throws IOException {
 154                 if (numReadStutters > 0 && rnd.nextBoolean()) {
 155                     numReadStutters--;
 156                     return 0;
 157                 }
 158                 return in.read(b, off, 1);
 159             }
 160 
 161             @Override
 162             public long skip(long n) throws IOException {
 163                 if (numSkipStutters > 0 && rnd.nextBoolean()) {
 164                     numSkipStutters--;
 165                     return 0;
 166                 }
 167                 return in.skip(1);
 168             }
 169         };
 170     }
 171 
 172     public static ByteArrayInputStream constructStreamFromInts(int[] ints) {
 173         byte[] bytes = new byte[ints.length];
 174         for (int i = 0; i < ints.length; i++) {
 175             bytes[i] = (byte)ints[i];
 176         }
 177         return new ByteArrayInputStream(bytes);
 178     }
 179 }