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 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.IOException;
  36 import java.util.Iterator;
  37 import java.util.Random;
  38 import javax.imageio.IIOImage;
  39 import javax.imageio.ImageIO;
  40 import javax.imageio.ImageWriteParam;
  41 import javax.imageio.ImageWriter;
  42 import javax.imageio.stream.ImageOutputStream;
  43 
  44 public class ImageTestHelper {
  45 
  46     static void writeImage(BufferedImage bImg, String fileName, String format, String compression)
  47             throws IOException
  48     {
  49         if (fileName != null) {
  50             File file = new File(fileName);
  51             file.delete();
  52             writeImage(bImg, file, format, compression);
  53         }
  54     }
  55 
  56     static void writeImage(BufferedImage bImg, Object out, String format, String compression)
  57             throws IOException
  58     {
  59         try (ImageOutputStream ios = ImageIO.createImageOutputStream(out)) {
  60             Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(format);
  61             ImageWriter writer = iter.next();
  62             ImageWriteParam iwp = writer.getDefaultWriteParam();
  63             if (compression != null) {
  64                 iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  65                 iwp.setCompressionType(compression);
  66             }
  67             writer.setOutput(ios);
  68             try {
  69                 writer.write(null, new IIOImage(bImg, null, null), iwp);
  70             } finally {
  71                 writer.dispose();
  72                 ios.flush();
  73             }
  74         }
  75     }
  76 
  77     static ByteArrayInputStream writeImageToStream(BufferedImage bImg,
  78             String format, String compression) throws IOException
  79     {
  80         ByteArrayOutputStream out = new ByteArrayOutputStream();
  81         writeImage(bImg, out, format, compression);
  82         return new ByteArrayInputStream(out.toByteArray());
  83     }
  84 
  85     static void drawImageGradient(BufferedImage bImg) {
  86         int w = bImg.getWidth();
  87         int h = bImg.getHeight();
  88         Graphics2D graphics = bImg.createGraphics();
  89         GradientPaint g = new GradientPaint(0, 0, Color.RED, w, h, Color.GREEN);
  90         graphics.setPaint(g);
  91         graphics.fillRect(0, 0, w, h);
  92     }
  93 
  94     static void drawImageRandom(BufferedImage bImg) {
  95         int w = bImg.getWidth();
  96         int h = bImg.getHeight();
  97         Random r = new Random(1);
  98         for (int y = 0; y < h; y++) {
  99             for (int x = 0; x < w; x++) {
 100                 bImg.setRGB(x, y, r.nextInt(1 << 24));
 101             }
 102         }
 103     }
 104 
 105     static void drawImageHue(BufferedImage bImg) {
 106         int w = bImg.getWidth();
 107         int h = bImg.getHeight();
 108         for (int y = 0; y < h; y++) {
 109             float s = 2.0f * y / h;
 110             if (s > 1) {
 111                 s = 1;
 112             }
 113             float b = 2.0f * (h - y) / h;
 114             if (b > 1) {
 115                 b = 1;
 116             }
 117             for (int x = 0; x < w; x++) {
 118                 float hue = (float) x / w;
 119                 bImg.setRGB(x, y, Color.HSBtoRGB(hue, s, b));
 120             }
 121         }
 122     }
 123 
 124     static void drawImageAll(BufferedImage bImg) {
 125         int w = bImg.getWidth();
 126         int h = bImg.getHeight();
 127         //if (h*w < (1<<24)) return;
 128         for (int y = 0; y < h; y++) {
 129             for (int x = 0; x < w; x++) {
 130                 bImg.setRGB(x, y, y * h + x);
 131             }
 132         }
 133     }
 134 }