Source for gnu.javax.imageio.png.PNGImageReader

   1: /* PNGImageReader.java -- The ImageIO ImageReader for PNG
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.javax.imageio.png;
  40: 
  41: import gnu.javax.imageio.IIOInputStream;
  42: 
  43: import java.awt.image.BufferedImage;
  44: import java.io.IOException;
  45: import java.io.InputStream;
  46: import java.util.ArrayList;
  47: import java.util.Iterator;
  48: 
  49: import javax.imageio.ImageReadParam;
  50: import javax.imageio.ImageReader;
  51: import javax.imageio.ImageTypeSpecifier;
  52: import javax.imageio.metadata.IIOMetadata;
  53: import javax.imageio.stream.ImageInputStream;
  54: 
  55: /**
  56:  * The ImageIO ImageReader for PNG images.
  57:  *
  58:  * @author Roman Kennke (kennke@aicas.com)
  59:  */
  60: public class PNGImageReader
  61:   extends ImageReader
  62: {
  63: 
  64:   /**
  65:    * The PNG file.
  66:    */
  67:   private PNGFile pngFile;
  68: 
  69:   /**
  70:    * The decoded image.
  71:    */
  72:   private BufferedImage image;
  73: 
  74:   /**
  75:    * The supported image types for PNG.
  76:    */
  77:   private ArrayList imageTypes;
  78: 
  79:   /**
  80:    * Creates a new instance.
  81:    *
  82:    * @param spi the corresponding ImageReaderSpi
  83:    */
  84:   public PNGImageReader(PNGImageReaderSpi spi)
  85:   {
  86:     super(spi);
  87:   }
  88: 
  89:   /**
  90:    * Returns the height of the image.
  91:    */
  92:   public int getHeight(int imageIndex)
  93:     throws IOException
  94:   {
  95:     checkIndex(imageIndex);
  96:     readImage();
  97:     return image.getHeight();
  98:   }
  99: 
 100:   /**
 101:    * Returns the width of the image.
 102:    *
 103:    * @param imageIndex the index of the image
 104:    *
 105:    * @return the width of the image
 106:    */
 107:   public int getWidth(int imageIndex) throws IOException
 108:   {
 109:     checkIndex(imageIndex);
 110:     readImage();
 111:     return image.getWidth();
 112:   }
 113: 
 114:   /**
 115:    * Returns the image types for the image.
 116:    *
 117:    * @see ImageReader#getImageTypes(int)
 118:    */
 119:   public Iterator getImageTypes(int imageIndex)
 120:     throws IOException
 121:   {
 122:     checkIndex(imageIndex);
 123:     readImage();
 124:     if (imageTypes == null)
 125:       {
 126:         imageTypes = new ArrayList();
 127:         imageTypes.add(new ImageTypeSpecifier(image.getColorModel(),
 128:                                               image.getSampleModel()));
 129:       }
 130:     return imageTypes.iterator();
 131:   }
 132: 
 133:   /**
 134:    * Returns the number of images in the stream.
 135:    *
 136:    * @return the number of images in the stream
 137:    *
 138:    * @see ImageReader#getNumImages(boolean)
 139:    */
 140:   public int getNumImages(boolean allowSearch)
 141:     throws IOException
 142:   {
 143:     return 1;
 144:   }
 145: 
 146:   /**
 147:    * Reads the image.
 148:    *
 149:    * @param imageIndex the index of the image to read
 150:    * @param param additional parameters
 151:    */
 152:   public BufferedImage read(int imageIndex, ImageReadParam param)
 153:     throws IOException
 154:   {
 155:     checkIndex(imageIndex);
 156:     readImage();
 157:     return image;
 158:   }
 159: 
 160:   /**
 161:    * Sets the input and checks the input parameter.
 162:    *
 163:    * @see ImageReader#setInput(Object, boolean, boolean)
 164:    */
 165:   public void setInput(Object input,
 166:                        boolean seekForwardOnly,
 167:                        boolean ignoreMetadata)
 168:   {
 169:     super.setInput(input, seekForwardOnly, ignoreMetadata);
 170:     if (! (input instanceof InputStream || input instanceof ImageInputStream))
 171:       throw new IllegalArgumentException("Input not an ImageInputStream");
 172:   }
 173: 
 174:   public IIOMetadata getImageMetadata(int imageIndex)
 175:     throws IOException
 176:   {
 177:     // TODO: Not (yet) supported.
 178:     checkIndex(imageIndex);
 179:     return null;
 180:   }
 181: 
 182:   public IIOMetadata getStreamMetadata()
 183:     throws IOException
 184:   {
 185:     // TODO: Not (yet) supported.
 186:     return null;
 187:   }
 188: 
 189:   /**
 190:    * Checks the image indexa and throws and IndexOutOfBoundsException if
 191:    * appropriate.
 192:    *
 193:    * @param index the index to check
 194:    */
 195:   private void checkIndex(int index)
 196:   {
 197:     if (index > 0)
 198:       throw new IndexOutOfBoundsException("Image index out of bounds");
 199:   }
 200: 
 201:   /**
 202:    * Makes sure that the image is read.
 203:    *
 204:    * @throws IOException if something goes wrong
 205:    */
 206:   private void readImage()
 207:     throws IOException
 208:   {
 209:     if (pngFile == null)
 210:       {
 211:         if (input instanceof InputStream)
 212:           pngFile = new PNGFile((InputStream) input);
 213:         else if (input instanceof ImageInputStream)
 214:           pngFile = new PNGFile(new IIOInputStream((ImageInputStream) input));
 215:         else
 216:           assert false : "Must not happen";
 217:       }
 218: 
 219:     if (pngFile != null && image == null)
 220:       {
 221:         image = pngFile.getBufferedImage();
 222:       }
 223:   }
 224: }