GIFImageReader.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* GIFImageReader.java --
  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.gif;
  32. import gnu.javax.imageio.IIOInputStream;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import javax.imageio.*;
  36. import javax.imageio.spi.*;
  37. import javax.imageio.metadata.*;
  38. import javax.imageio.stream.ImageInputStream;
  39. import java.util.Iterator;
  40. import java.awt.image.BufferedImage;
  41. import java.awt.image.IndexColorModel;
  42. import java.awt.image.SampleModel;
  43. import java.awt.image.MultiPixelPackedSampleModel;
  44. import java.awt.image.SinglePixelPackedSampleModel;
  45. import java.awt.image.DataBuffer;
  46. import java.awt.image.DataBufferByte;
  47. import java.awt.image.Raster;
  48. import java.awt.image.WritableRaster;
  49. public class GIFImageReader extends ImageReader
  50. {
  51. private GIFFile file;
  52. protected GIFImageReader(ImageReaderSpi originatingProvider)
  53. {
  54. super( originatingProvider );
  55. file = null;
  56. }
  57. private void readImage() throws IOException
  58. {
  59. if( file != null )
  60. return;
  61. try
  62. {
  63. if( input instanceof InputStream )
  64. file = new GIFFile( (InputStream)input );
  65. else
  66. file = new GIFFile( new IIOInputStream((ImageInputStream)input) );
  67. }
  68. catch(GIFFile.GIFException ge)
  69. {
  70. throw new IIOException(ge.getMessage());
  71. }
  72. }
  73. /**
  74. * Returns the Global/Local palette as an IndexColorModel
  75. */
  76. private IndexColorModel getPalette(int index)
  77. {
  78. GIFFile f = file.getImage( index );
  79. byte[] data = f.getRawPalette();
  80. int nc = f.getNColors();
  81. byte[] r = new byte[nc];
  82. byte[] g = new byte[nc];
  83. byte[] b = new byte[nc];
  84. for(int i = 0; i < nc; i ++ )
  85. {
  86. r[i] = data[ i * 3 ];
  87. g[i] = data[ i * 3 + 1 ];
  88. b[i] = data[ i * 3 + 2 ];
  89. }
  90. if( f.hasTransparency() )
  91. {
  92. byte[] a = new byte[nc];
  93. for(int i = 0; i < nc; i ++ )
  94. a[i] = (byte)0xFF;
  95. a[f.getTransparentIndex()] = 0;
  96. return new IndexColorModel(8, nc, r, g, b, a);
  97. }
  98. return new IndexColorModel(8, nc, r, g, b);
  99. }
  100. private void validateIndex(int imageIndex)
  101. throws IndexOutOfBoundsException
  102. {
  103. if( imageIndex < 0 || imageIndex >= getNumImages(false) )
  104. throw new IndexOutOfBoundsException("Invalid image index.");
  105. }
  106. public void setInput(Object input)
  107. {
  108. super.setInput(input);
  109. }
  110. public void setInput(Object input,
  111. boolean seekForwardOnly,
  112. boolean ignoreMetadata)
  113. {
  114. super.setInput(input, seekForwardOnly, ignoreMetadata);
  115. }
  116. public void setInput(Object input, boolean isStreamable)
  117. {
  118. super.setInput(input, isStreamable);
  119. if (!(input instanceof ImageInputStream) &&
  120. !(input instanceof InputStream))
  121. throw new IllegalArgumentException("Input not an ImageInputStream.");
  122. }
  123. private void checkStream() throws IOException
  124. {
  125. if (!(input instanceof ImageInputStream) &&
  126. !(input instanceof InputStream))
  127. throw new IllegalStateException("Input not an ImageInputStream or InputStream.");
  128. if(input == null)
  129. throw new IllegalStateException("No input stream.");
  130. }
  131. public int getWidth(int imageIndex) throws IOException
  132. {
  133. validateIndex( imageIndex );
  134. return file.getImage( imageIndex ).getWidth();
  135. }
  136. public int getHeight(int imageIndex) throws IOException
  137. {
  138. validateIndex( imageIndex );
  139. return file.getImage( imageIndex ).getHeight();
  140. }
  141. public Iterator getImageTypes(int imageIndex)
  142. {
  143. validateIndex( imageIndex );
  144. return null;
  145. }
  146. /**
  147. * Returns the number of images.
  148. */
  149. public int getNumImages(boolean allowSearch)
  150. {
  151. try // Image should be loaded here already. But just in case:
  152. {
  153. readImage();
  154. }
  155. catch(IOException ioe)
  156. {
  157. return 0; // Well, now we're in trouble. But return something anyway.
  158. }
  159. return file.nImages();
  160. }
  161. // FIXME: Support metadata
  162. public IIOMetadata getImageMetadata(int imageIndex)
  163. {
  164. validateIndex( imageIndex );
  165. return null;
  166. }
  167. // FIXME: Support metadata
  168. public IIOMetadata getStreamMetadata()
  169. {
  170. return null;
  171. }
  172. /**
  173. * Reads the image indexed by imageIndex and returns it as
  174. * a complete BufferedImage, using a supplied ImageReadParam.
  175. */
  176. public BufferedImage read(int imageIndex, ImageReadParam param)
  177. throws IOException, IIOException
  178. {
  179. validateIndex( imageIndex );
  180. GIFFile f = file.getImage( imageIndex );
  181. int width = f.getWidth();
  182. int height = f.getHeight();
  183. SampleModel sm;
  184. switch( f.getNColors() )
  185. {
  186. case 16:
  187. sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
  188. width, height, 4);
  189. break;
  190. case 4:
  191. sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
  192. width, height, 2);
  193. break;
  194. case 2:
  195. sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
  196. width, height, 1);
  197. break;
  198. default:
  199. sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE,
  200. width, height,
  201. new int[] {0xFF});
  202. break;
  203. }
  204. DataBuffer db = new DataBufferByte(f.getRawImage(), width * height, 0);
  205. WritableRaster raster = Raster.createWritableRaster(sm, db, null);
  206. return new BufferedImage(getPalette( imageIndex ), raster, false, null);
  207. }
  208. }