1 /*
   2  * Copyright (c) 2007, 2017, 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 package org.jemmy.image;
  26 
  27 import java.awt.Color;
  28 
  29 import java.awt.image.BufferedImage;
  30 
  31 import java.io.InputStream;
  32 import java.io.IOException;
  33 import java.io.FileInputStream;
  34 
  35 import org.jemmy.JemmyException;
  36 
  37 import java.util.zip.DataFormatException;
  38 import java.util.zip.Inflater;
  39 
  40 /**
  41  * Allows to load PNG graphical file.
  42  * @author Alexandre Iline
  43  */
  44 public class PNGDecoder extends Object {
  45 
  46     InputStream in;
  47 
  48     /**
  49      * Constructs a PNGDecoder object.
  50      * @param in input stream to read PNG image from.
  51      */
  52     public PNGDecoder(InputStream in) {
  53         this.in = in;
  54     }
  55 
  56     byte read() throws IOException {
  57         byte b = (byte)in.read();
  58         return(b);
  59     }
  60 
  61     int readInt() throws IOException {
  62         byte b[] = read(4);
  63         return(((b[0]&0xff)<<24) +
  64                ((b[1]&0xff)<<16) +
  65                ((b[2]&0xff)<<8) +
  66                ((b[3]&0xff)));
  67     }
  68 
  69     byte[] read(int count) throws IOException {
  70         byte[] result = new byte[count];
  71         for(int i = 0; i < count; i++) {
  72             result[i] = read();
  73         }
  74         return(result);
  75     }
  76 
  77     boolean compare(byte[] b1, byte[] b2) {
  78         if(b1.length != b2.length) {
  79             return(false);
  80         }
  81         for(int i = 0; i < b1.length; i++) {
  82             if(b1[i] != b2[i]) {
  83                 return(false);
  84             }
  85         }
  86         return(true);
  87     }
  88 
  89     void checkEquality(byte[] b1, byte[] b2) {
  90         if(!compare(b1, b2)) {
  91             throw(new JemmyException("Format error"));
  92         }
  93     }
  94 
  95     /**
  96      * Decodes image from an input stream passed into constructor.
  97      * @return a BufferedImage object
  98      * @throws IOException
  99      */
 100     public BufferedImage decode() throws IOException {
 101         return decode(true);
 102     }
 103 
 104     /**
 105      * Decodes image from an input stream passed into constructor.
 106      * @return a BufferedImage object
 107      * @param closeStream requests method to close the stream after the image is read
 108      * @throws IOException
 109      */
 110     public BufferedImage decode(boolean closeStream) throws IOException {
 111 
 112         byte[] id = read(12);
 113         checkEquality(id, new byte[] {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13});
 114 
 115         byte[] ihdr = read(4);
 116         checkEquality(ihdr, "IHDR".getBytes());
 117 
 118         int width = readInt();
 119         int height = readInt();
 120 
 121         BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 122 
 123         byte[] head = read(5);
 124         int mode;
 125         if(compare(head, new byte[]{1, 0, 0, 0, 0})) {
 126             mode = PNGEncoder.BW_MODE;
 127         } else if(compare(head, new byte[]{8, 0, 0, 0, 0})) {
 128             mode = PNGEncoder.GREYSCALE_MODE;
 129         } else if(compare(head, new byte[]{8, 2, 0, 0, 0})) {
 130             mode = PNGEncoder.COLOR_MODE;
 131         } else {
 132             throw(new JemmyException("Format error"));
 133         }
 134 
 135         readInt();//!!crc
 136 
 137         int size = readInt();
 138 
 139         byte[] idat = read(4);
 140         checkEquality(idat, "IDAT".getBytes());
 141 
 142         byte[] data = read(size);
 143 
 144 
 145         Inflater inflater = new Inflater();
 146         inflater.setInput(data, 0, size);
 147 
 148         int color;
 149 
 150         try {
 151             switch (mode) {
 152             case PNGEncoder.BW_MODE:
 153                 {
 154                     int bytes = (int)(width / 8);
 155                     if((width % 8) != 0) {
 156                         bytes++;
 157                     }
 158                     byte colorset;
 159                     byte[] row = new byte[bytes];
 160                     for (int y = 0; y < height; y++) {
 161                         inflater.inflate(new byte[1]);
 162                         inflater.inflate(row);
 163                         for (int x = 0; x < bytes; x++) {
 164                             colorset = row[x];
 165                             for (int sh = 0; sh < 8; sh++) {
 166                                 if(x * 8 + sh >= width) {
 167                                     break;
 168                                 }
 169                                 if((colorset & 0x80) == 0x80) {
 170                                     result.setRGB(x * 8 + sh, y, Color.white.getRGB());
 171                                 } else {
 172                                     result.setRGB(x * 8 + sh, y, Color.black.getRGB());
 173                                 }
 174                                 colorset <<= 1;
 175                             }
 176                         }
 177                     }
 178                 }
 179                 break;
 180             case PNGEncoder.GREYSCALE_MODE:
 181                 {
 182                     byte[] row = new byte[width];
 183                     for (int y = 0; y < height; y++) {
 184                         inflater.inflate(new byte[1]);
 185                         inflater.inflate(row);
 186                         for (int x = 0; x < width; x++) {
 187                             color = row[x];
 188                             result.setRGB(x, y, (color << 16) + (color << 8) + color);
 189                         }
 190                     }
 191                 }
 192                 break;
 193             case PNGEncoder.COLOR_MODE:
 194                 {
 195                     byte[] row = new byte[width * 3];
 196                     for (int y = 0; y < height; y++) {
 197                         inflater.inflate(new byte[1]);
 198                         inflater.inflate(row);
 199                         for (int x = 0; x < width; x++) {
 200                             result.setRGB(x, y,
 201                                           ((row[x * 3 + 0]&0xff) << 16) +
 202                                           ((row[x * 3 + 1]&0xff) << 8) +
 203                                           ((row[x * 3 + 2]&0xff)));
 204                         }
 205                     }
 206                 }
 207             }
 208         } catch(DataFormatException e) {
 209             throw(new JemmyException("ZIP error", e));
 210         }
 211 
 212         readInt();//!!crc
 213         readInt();//0
 214 
 215         byte[] iend = read(4);
 216         checkEquality(iend, "IEND".getBytes());
 217 
 218         readInt();//!!crc
 219         if (closeStream) {
 220             in.close();
 221         }
 222 
 223         return(result);
 224     }
 225 
 226     /**
 227      * Decodes image from file.
 228      * @param fileName a file to read image from
 229      * @return a BufferedImage instance.
 230      */
 231     public static BufferedImage decode(String fileName) {
 232         try {
 233             return(new PNGDecoder(new FileInputStream(fileName)).decode());
 234         } catch(IOException e) {
 235             throw(new JemmyException("IOException during image reading", e));
 236         }
 237     }
 238 
 239     /**
 240      * Decodes image from input stream
 241      * @param inputStream a file to read image from
 242      * @param closeStream requests method to close the stream after the image is read
 243      * @return a BufferedImage instance.
 244      */
 245     public static BufferedImage decode(InputStream inputStream, boolean closeStream) {
 246         try {
 247             return(new PNGDecoder(inputStream).decode(closeStream));
 248         } catch(IOException e) {
 249             throw(new JemmyException("IOException during image reading", e));
 250         }
 251     }
 252 
 253     /**
 254      * Decodes image from resource.
 255      * @param loader ClassLoader to use to get the resource
 256      * @param resource Image resource name
 257      * @return a BufferedImage instance.
 258      */
 259     public static BufferedImage decode(ClassLoader loader, String resource) {
 260         try {
 261             InputStream resourceStream = loader.getResourceAsStream(resource);
 262             if (resourceStream == null) {
 263                 throw new JemmyException("Resouce '" + resource + "' could not be found!");
 264             }
 265             return(new PNGDecoder(resourceStream).decode());
 266         } catch(IOException e) {
 267             throw(new JemmyException("IOException during image reading", e));
 268         }
 269     }
 270 
 271 }