PNGImageReader.java 5.7 KB

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