PNGImageReaderSpi.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* PNGImageReaderSpi.java -- The ImageReader service provider 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 java.io.IOException;
  33. import java.io.InputStream;
  34. import java.util.Locale;
  35. import javax.imageio.ImageReader;
  36. import javax.imageio.spi.ImageReaderSpi;
  37. import javax.imageio.stream.ImageInputStream;
  38. /**
  39. * The ImageIO ImageReader service provider for PNG images.
  40. *
  41. * @author Roman Kennke (kennke@aicas.com)
  42. */
  43. public class PNGImageReaderSpi
  44. extends ImageReaderSpi
  45. {
  46. /**
  47. * The PNG file signature.
  48. */
  49. private static final byte[] SIGNATURE = new byte[]
  50. { (byte) 137, 80, 78, 71, 13, 10, 26, 10 };
  51. private static final String VENDOR_NAME = "GNU";
  52. static final String VERSION = "1.0";
  53. static final String READER_CLASSNAME =
  54. "gnu.javax.imageio.png.PNGImageReader";
  55. static final String[] NAMES = { "Portable Network Graphics" };
  56. static final String[] SUFFIXES = { ".png" , ".PNG" };
  57. static final String[] MIME_TYPES = { "image/png" };
  58. static final String[] WRITER_SPI_NAMES =
  59. new String[] { "gnu.javax.imageio.png.PNGWriterSpi" };
  60. static final Class[] INPUT_TYPES = new Class[]{ InputStream.class,
  61. ImageInputStream.class};
  62. public PNGImageReaderSpi()
  63. {
  64. super(VENDOR_NAME, VERSION, NAMES, SUFFIXES, MIME_TYPES, READER_CLASSNAME,
  65. INPUT_TYPES, WRITER_SPI_NAMES, false, null, null, null, null, false,
  66. null, null, null, null);
  67. }
  68. /**
  69. * Determines if the PNG ImageReader can decode the specified input.
  70. *
  71. * @param source the source to decode
  72. */
  73. public boolean canDecodeInput(Object source) throws IOException
  74. {
  75. boolean canDecode = false;
  76. if (source instanceof ImageInputStream)
  77. {
  78. ImageInputStream in = (ImageInputStream) source;
  79. in.mark();
  80. canDecode = true;
  81. for (int i = 0; i < SIGNATURE.length && canDecode; i++)
  82. {
  83. byte sig = (byte) in.read();
  84. if (sig != SIGNATURE[i]) {
  85. canDecode = false;
  86. }
  87. }
  88. in.reset();
  89. }
  90. return canDecode;
  91. }
  92. /**
  93. * Returns a new PNGImageReader instance.
  94. *
  95. * @param extension the extension, ignored
  96. */
  97. public ImageReader createReaderInstance(Object extension)
  98. throws IOException
  99. {
  100. return new PNGImageReader(this);
  101. }
  102. /**
  103. * Returns a description.
  104. *
  105. * @param locale the locale
  106. */
  107. public String getDescription(Locale locale)
  108. {
  109. return "Portable Network Graphics";
  110. }
  111. }