PNGChunk.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* PNGChunk.java -- Generic PNG chunk
  2. Copyright (C) 2006 Free Software Foundation
  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.InputStream;
  33. import java.io.OutputStream;
  34. import java.io.IOException;
  35. /**
  36. * Class to load and validate a generic PNG chunk.
  37. */
  38. public class PNGChunk
  39. {
  40. /**
  41. * CRC table and initialization code.
  42. */
  43. private static long[] crcTable;
  44. static
  45. {
  46. long c;
  47. crcTable = new long[256];
  48. for(int i = 0; i < 256; i++)
  49. {
  50. c = i;
  51. for(int j = 0; j < 8; j++)
  52. if( (c & 1) == 1 )
  53. c = 0xEDB88320L ^ (c >> 1);
  54. else
  55. c = c >> 1;
  56. crcTable[i] = c;
  57. }
  58. }
  59. /**
  60. * (recognized) PNG chunk types.
  61. */
  62. public static final int TYPE_HEADER = 0x49484452; // 'IHDR'
  63. public static final int TYPE_PALETTE = 0x504c5445;// 'PLTE'
  64. public static final int TYPE_DATA = 0x49444154; // 'IDAT'
  65. public static final int TYPE_TIME = 0x74494d45; // 'tIME'
  66. public static final int TYPE_END = 0x49454e44; // 'IEND'
  67. public static final int TYPE_PHYS = 0x70485973; // 'pHYS'
  68. public static final int TYPE_GAMMA = 0x67414d41; // 'gAMA'
  69. public static final int TYPE_PROFILE = 0x69434350; // 'iCCP'
  70. /**
  71. * The chunk type - Represented in the file as 4 ASCII bytes,
  72. */
  73. private int type;
  74. /**
  75. * The chunk data
  76. */
  77. protected byte[] data;
  78. /**
  79. * The chunk's crc
  80. */
  81. private int crc;
  82. /**
  83. * Constructor for reading a generic chunk.
  84. */
  85. protected PNGChunk( int type, byte[] data, int crc )
  86. {
  87. this.type = type;
  88. this.data = data;
  89. this.crc = crc;
  90. }
  91. /**
  92. * Constructor for creating new chunks.
  93. * (only used by subclasses - creating a generic chunk is rather useless)
  94. */
  95. protected PNGChunk( int type )
  96. {
  97. this.type = type;
  98. }
  99. /**
  100. * Loads a chunk from an InputStream. Does not perform validation,
  101. * but will throw an IOException if the read fails.
  102. * @param in - th einputstream to read from
  103. * @param strict - if true, a PNGException is thrown on all invalid chunks,
  104. * if false, only critical chunks will throw PNGExceptions.
  105. */
  106. public static PNGChunk readChunk(InputStream in, boolean strict)
  107. throws IOException, PNGException
  108. {
  109. byte data[] = new byte[4];
  110. if( in.read( data ) != 4 )
  111. throw new IOException("Could not read chunk length.");
  112. int length = ((data[0] & 0xFF) << 24) | ((data[1] & 0xFF) << 16 ) |
  113. ((data[2] & 0xFF) << 8) | (data[3] & 0xFF);
  114. if( in.read( data ) != 4 )
  115. throw new IOException("Could not read chunk type.");
  116. int type = ((data[0] & 0xFF) << 24) | ((data[1] & 0xFF) << 16 ) |
  117. ((data[2] & 0xFF) << 8) | (data[3] & 0xFF);
  118. byte[] chkdata = new byte[ length ];
  119. if( in.read( chkdata ) != length )
  120. throw new IOException("Could not read chunk data.");
  121. if( in.read( data ) != 4 )
  122. throw new IOException("Could not read chunk CRC.");
  123. int crc = ((data[0] & 0xFF) << 24) | ( (data[1] & 0xFF) << 16 ) |
  124. ((data[2] & 0xFF) << 8) | (data[3] & 0xFF);
  125. if( strict )
  126. return getChunk( type, chkdata, crc );
  127. else
  128. {
  129. try
  130. {
  131. return getChunk( type, chkdata, crc );
  132. }
  133. catch(PNGException pnge)
  134. {
  135. if( isEssentialChunk( type ) )
  136. throw pnge;
  137. return null;
  138. }
  139. }
  140. }
  141. /**
  142. * Returns a specialied object for a chunk, if we have one.
  143. */
  144. private static PNGChunk getChunk( int type, byte[] data, int crc )
  145. throws PNGException
  146. {
  147. switch( type )
  148. {
  149. case TYPE_HEADER:
  150. return new PNGHeader( type, data, crc );
  151. case TYPE_DATA:
  152. return new PNGData( type, data, crc );
  153. case TYPE_PALETTE:
  154. return new PNGPalette( type, data, crc );
  155. case TYPE_TIME:
  156. return new PNGTime( type, data, crc );
  157. case TYPE_PHYS:
  158. return new PNGPhys( type, data, crc );
  159. case TYPE_GAMMA:
  160. return new PNGGamma( type, data, crc );
  161. case TYPE_PROFILE:
  162. return new PNGICCProfile( type, data, crc );
  163. default:
  164. return new PNGChunk( type, data, crc );
  165. }
  166. }
  167. /**
  168. * Returns whether the chunk is essential or not
  169. */
  170. private static boolean isEssentialChunk( int type )
  171. {
  172. switch( type )
  173. {
  174. case TYPE_HEADER:
  175. case TYPE_DATA:
  176. case TYPE_PALETTE:
  177. case TYPE_END:
  178. return true;
  179. default:
  180. return false;
  181. }
  182. }
  183. /**
  184. * Validates the chunk
  185. */
  186. public boolean isValidChunk()
  187. {
  188. return (crc == calcCRC());
  189. }
  190. /**
  191. * Returns the chunk type.
  192. */
  193. public int getType()
  194. {
  195. return type;
  196. }
  197. /**
  198. * Writes a PNG chunk to an output stream,
  199. * performing the CRC calculation as well.
  200. */
  201. public void writeChunk(OutputStream out) throws IOException
  202. {
  203. out.write( getInt(data.length) );
  204. out.write( getInt(type) );
  205. out.write( data );
  206. out.write( getInt(calcCRC()) );
  207. }
  208. /**
  209. * Return whether the chunk contains any data.
  210. */
  211. public boolean isEmpty()
  212. {
  213. return ( data.length == 0 );
  214. }
  215. /**
  216. * Convenience method. Cast an int to four bytes (big endian).
  217. * (Now why doesn't java have a simple way of doing this?)
  218. */
  219. public static byte[] getInt(int intValue)
  220. {
  221. long i = (intValue & 0xFFFFFFFFL);
  222. byte[] b = new byte[4];
  223. b[0] = (byte)((i & 0xFF000000L) >> 24);
  224. b[1] = (byte)((i & 0x00FF0000L) >> 16);
  225. b[2] = (byte)((i & 0x0000FF00L) >> 8);
  226. b[3] = (byte)(i & 0x000000FFL);
  227. return b;
  228. }
  229. /**
  230. * Calculates this chunk's CRC value.
  231. */
  232. private int calcCRC()
  233. {
  234. long c = 0xFFFFFFFFL;
  235. byte[] t = getInt( type );
  236. for(int i = 0; i < 4; i++)
  237. c = crcTable[ (int)((c ^ t[i]) & 0xFF) ] ^ (c >> 8);
  238. for(int i = 0; i < data.length; i++)
  239. c = crcTable[ (int)((c ^ data[i]) & 0xFF) ] ^ (c >> 8);
  240. return (int)(c ^ 0xFFFFFFFFL);
  241. }
  242. public String toString()
  243. {
  244. return "PNG Chunk. Type: " + new String( getInt(type) ) + " , CRC: " +
  245. crc + " , calculated CRC: "+calcCRC();
  246. }
  247. }