PNGHeader.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* PNGHeader.java -- PNG Header
  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. /**
  33. * A PNG Header chunk.
  34. */
  35. public class PNGHeader extends PNGChunk
  36. {
  37. private int width, height, depth;
  38. private int colorType, compression, filter, interlace;
  39. /**
  40. * The valid interlace types.
  41. */
  42. public static final int INTERLACE_NONE = 0;
  43. public static final int INTERLACE_ADAM7 = 1;
  44. /**
  45. * The valid color types.
  46. */
  47. public static final int GRAYSCALE = 0;
  48. public static final int RGB = 2;
  49. public static final int INDEXED = 3;
  50. public static final int GRAYSCALE_WITH_ALPHA = 4;
  51. public static final int RGB_WITH_ALPHA = 6;
  52. /**
  53. * Parses a PNG Header chunk.
  54. */
  55. protected PNGHeader( int type, byte[] data, int crc ) throws PNGException
  56. {
  57. super( type, data, crc );
  58. if( data.length < 13 )
  59. throw new PNGException("Unexpectedly short header chunk. (" + data.length
  60. + " bytes)");
  61. width = ((data[0] & 0xFF) << 24) | ( (data[1] & 0xFF) << 16 ) |
  62. ((data[2] & 0xFF) << 8) | (data[3] & 0xFF);
  63. height = ((data[4] & 0xFF) << 24) | ( (data[5] & 0xFF) << 16 ) |
  64. ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
  65. depth = (data[8] & 0xFF);
  66. colorType = (data[9] & 0xFF);
  67. compression = (data[10] & 0xFF);
  68. filter = (data[11] & 0xFF);
  69. interlace = (data[12] & 0xFF);
  70. }
  71. /**
  72. * Create a PNG header chunk.
  73. * Warning: This trusts that the parameters are valid.
  74. */
  75. public PNGHeader(int width, int height, int depth,
  76. int colorType, boolean interlace)
  77. {
  78. super( TYPE_HEADER );
  79. data = new byte[ 13 ];
  80. this.width = width;
  81. this.height = height;
  82. this.depth = depth;
  83. compression = filter = 0;
  84. this.colorType = colorType;
  85. this.interlace = interlace ? 1 : 0;
  86. // Build the data chunk.
  87. byte[] a = getInt( width );
  88. byte[] b = getInt( height );
  89. data[0] = a[0]; data[1] = a[1]; data[2] = a[2]; data[3] = a[3];
  90. data[4] = b[0]; data[5] = b[1]; data[6] = b[2]; data[7] = b[3];
  91. data[8] = (byte)depth;
  92. data[9] = (byte)colorType;
  93. data[10] = (byte)compression;
  94. data[11] = (byte)filter;
  95. data[12] = (byte)this.interlace;
  96. }
  97. /**
  98. * Validates the header fields
  99. */
  100. public boolean isValidChunk()
  101. {
  102. if( !super.isValidChunk() )
  103. return false;
  104. // width and height must be nonzero
  105. if( width == 0 || height == 0 )
  106. return false;
  107. // colorType can be 0,2,3,4,6
  108. if( (colorType & 0xFFFFFFF8) != 0 || colorType == 5 || colorType == 1)
  109. return false;
  110. // Possible valid depths are 1,2,4,8,16
  111. if( !((depth == 1) || (depth == 2) || (depth == 4) ||
  112. (depth == 8) || (depth == 16)) )
  113. return false;
  114. if( colorType == INDEXED && depth == 16 )
  115. return false;
  116. if( ( colorType == RGB || colorType == GRAYSCALE_WITH_ALPHA ||
  117. colorType == RGB_WITH_ALPHA ) &&
  118. depth < 8 )
  119. return false;
  120. // Only compression and filter methods zero are defined
  121. if( compression != 0 || filter != 0 )
  122. return false;
  123. // Interlace methods, 0 and 1 are valid values.
  124. if( (interlace & 0xFFFFFFFE) != 0 )
  125. return false;
  126. return true;
  127. }
  128. /**
  129. * Returns <code>true</code> if this PNG is indexed-color
  130. */
  131. public boolean isIndexed()
  132. {
  133. return (colorType == INDEXED);
  134. }
  135. /**
  136. * Returns <code>true</code> if this PNG is grayscale
  137. */
  138. public boolean isGrayscale()
  139. {
  140. return ((colorType == GRAYSCALE) || (colorType == GRAYSCALE_WITH_ALPHA));
  141. }
  142. /**
  143. * Returns the color type of the image.
  144. */
  145. public int getColorType()
  146. {
  147. return colorType;
  148. }
  149. /**
  150. * Returns whether the image is interlaced or not.
  151. */
  152. public boolean isInterlaced()
  153. {
  154. return (interlace != 0);
  155. }
  156. /**
  157. * Returns the number of bytes per pixel.
  158. */
  159. public int bytesPerPixel()
  160. {
  161. switch( colorType )
  162. {
  163. case GRAYSCALE_WITH_ALPHA:
  164. return ((depth * 2) >> 3);
  165. case RGB:
  166. return ((depth * 3) >> 3);
  167. case RGB_WITH_ALPHA:
  168. return ((depth * 4) >> 3);
  169. default:
  170. case GRAYSCALE:
  171. case INDEXED:
  172. int i = (depth >> 3);
  173. if( i > 0 ) return i;
  174. return 1; // if bytes per pixel < 1, return 1 anyway.
  175. }
  176. }
  177. /**
  178. * Returns the stride of one scanline, in bytes.
  179. */
  180. public int getScanlineStride()
  181. {
  182. long nBits = 0; // bits per scanline - scanlines are on byte offsets.
  183. switch( colorType )
  184. {
  185. case GRAYSCALE:
  186. nBits = width * depth;
  187. break;
  188. case RGB:
  189. nBits = width * depth * 3;
  190. break;
  191. case INDEXED:
  192. nBits = depth * width;
  193. break;
  194. case GRAYSCALE_WITH_ALPHA:
  195. nBits = depth * width * 2;
  196. break;
  197. case RGB_WITH_ALPHA:
  198. nBits = depth * width * 4;
  199. break;
  200. }
  201. // Round up number of bits to the nearest byte
  202. if( (nBits & 0x07) != 0 )
  203. nBits += (8 - (nBits & 0x07));
  204. return (int)(nBits >> 3); // return # of bytes.
  205. }
  206. public int getWidth()
  207. {
  208. return width;
  209. }
  210. public int getHeight()
  211. {
  212. return height;
  213. }
  214. public int getDepth()
  215. {
  216. return depth;
  217. }
  218. /**
  219. * Debugging string.
  220. */
  221. public String toString()
  222. {
  223. return "Header Chunk. Image width:"+width+" height:"+height+
  224. " depth:"+depth+" color type:"+colorType+" compression type:"+
  225. compression+" filter type:"+ filter+" interlace:"+interlace;
  226. }
  227. }