1 /*
   2  * Copyright (c) 2011, 2013, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @summary A collection of utilities for testing the contents of BufferedImages
  26  * @summary com.apple.junit.utils
  27  */
  28 
  29 package test.java.awt.regtesthelpers;
  30 
  31 import java.awt.image.BufferedImage;
  32 import java.io.*;
  33 
  34 public class BITestUtils {
  35 
  36     //
  37     //    A set of functions for determining the location
  38     //      of a drawing inside a buffered image.  These
  39     //      methods look for white pixel.
  40     //
  41 
  42     /* Get location of top white pixel */
  43 
  44     public static int getTopMost(BufferedImage bi) {
  45         for (int y = 0; y < bi.getHeight(); y++) {
  46             for (int x = 0; x < bi.getWidth(); x++) {
  47                 int rgb = bi.getRGB(x, y);
  48                 rgb = rgb & 0x00ffffff;
  49                 if (rgb == 0x00ffffff) {
  50                     return(y);
  51                 }
  52             }
  53         }
  54         return (-1);
  55     }
  56 
  57     /* Get location of bottom white pixel */
  58 
  59     public static int getBottomMost(BufferedImage bi) {
  60         for (int y = bi.getHeight()-1; y >= 0; y--) {
  61             for (int x = 0; x < bi.getWidth(); x++) {
  62                 int rgb = bi.getRGB(x, y);
  63                 rgb = rgb & 0x00ffffff;
  64                 if (rgb == 0x00ffffff) {
  65                     return(y);
  66                 }
  67             }
  68         }
  69         return (-1);
  70     }
  71 
  72     /* Get location of leftmost white pixel */
  73 
  74     public static int getLeftMost(BufferedImage bi) {
  75         for (int x = 0; x < bi.getWidth(); x++) {
  76             for (int y = 0; y < bi.getHeight(); y++) {
  77                 int rgb = bi.getRGB(x, y);
  78                 rgb = rgb & 0x00ffffff;
  79                 if (rgb == 0x00ffffff) {
  80                     return(x);
  81                 }
  82             }
  83         }
  84         return (-1);
  85     }
  86 
  87     /* Get location of rightmost white pixel */
  88 
  89     public static int getRightMost(BufferedImage bi) {
  90         for (int x = bi.getWidth()-1; x >=0 ; x--) {
  91             for (int y = 0; y < bi.getHeight(); y++) {
  92                 int rgb = bi.getRGB(x, y);
  93                 rgb = rgb & 0x00ffffff;
  94                 if (rgb == 0x00ffffff) {
  95                     return(x);
  96                 }
  97             }
  98         }
  99         return (-1);
 100     }
 101 
 102 
 103     //
 104     //    A collection of methods for displaying the contents
 105     //    of BufferedIages in human readable form.
 106     //
 107 
 108     public static void pixelDisplay(BufferedImage bi, String name)
 109     {
 110         System.err.println(name + " buffered image");
 111         for (int y = 0; y < bi.getHeight(); y++)
 112         {
 113             System.err.print("  ");
 114             for (int x = 0; x < bi.getWidth(); x++)
 115             {
 116                 int rgb = bi.getRGB(x, y);
 117                 rgb = rgb & 0x00ffffff;
 118                 String c = (rgb == 0xffffff ? "X" : (rgb == 0x000000 ? "." : "?"));
 119                 System.err.print(c + " ");
 120             }
 121             System.err.println("");
 122         }
 123 
 124     }
 125 
 126     static void pixelDisplayInt(BufferedImage img) {
 127         int width = img.getWidth();
 128         int height = img.getHeight();
 129         for (int y = 0; y < height; y+=1) {
 130             for (int x = 0; x < width; x+=1) {
 131                 System.out.print( img.getRGB(x,y) + ", ");
 132             }
 133             System.out.println();
 134         }
 135     }
 136 
 137     static void pixelDisplayHex(BufferedImage img) {
 138         int width = img.getWidth();
 139         int height = img.getHeight();
 140         for (int y = 0; y < height; y+=1) {
 141             for (int x = 0; x < width; x+=1) {
 142                 int pixelValue = img.getRGB(x,y);
 143                 System.out.print( "0x" + Hex(pixelValue) + ", ");
 144             }
 145             System.out.println();
 146         }
 147     }
 148 
 149     public static void pixelLogfileHex(BufferedImage img, String filename) throws Exception {
 150         File datafile = File.createTempFile(filename, ".txt");
 151         PrintStream streamer =    new PrintStream( new FileOutputStream( datafile ), true, "UTF-16");
 152         System.out.println("Creating log file :" + datafile.getCanonicalPath() );
 153 
 154         int width = img.getWidth();
 155         int height = img.getHeight();
 156         for (int y = 0; y < height; y+=1) {
 157             for (int x = 0; x < width; x+=1) {
 158                 int pixelValue = img.getRGB(x,y);
 159                 streamer.print( "0x" + Hex(pixelValue) + ", ");
 160             }
 161             streamer.println();
 162         }
 163 
 164     }
 165 
 166 
 167     // Note that this is used to '0' pad an integer properly
 168     static final String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
 169     public static String Hex( int x ) {
 170         String s = "";
 171         for (int i = 0; i < 8; i += 1) {
 172             s = hexDigits[x & 0xF] + s;
 173             x = x >> 4;
 174         }
 175         return s;
 176     }
 177 
 178 }