PNGEncoder.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* PNGEncoder.java --
  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.util.Vector;
  33. import java.util.zip.Deflater;
  34. import java.awt.color.ColorSpace;
  35. import java.awt.color.ICC_ColorSpace;
  36. import java.awt.image.BufferedImage;
  37. import java.awt.image.ColorModel;
  38. import java.awt.image.DataBuffer;
  39. import java.awt.image.DataBufferByte;
  40. import java.awt.image.DataBufferUShort;
  41. import java.awt.image.IndexColorModel;
  42. import java.awt.image.WritableRaster;
  43. public class PNGEncoder
  44. {
  45. /**
  46. * The default data chunk size. 8 kb.
  47. */
  48. private static final int defaultChunkSize = 8192;
  49. private PNGHeader header;
  50. private PNGPalette palette;
  51. private int stride, bpp;
  52. private byte[] rawData;
  53. private PNGICCProfile profile;
  54. public PNGEncoder( BufferedImage bi ) throws PNGException
  55. {
  56. ColorModel c = bi.getColorModel();
  57. int width = bi.getWidth();
  58. int height = bi.getHeight();
  59. int depth = 0;
  60. int colorType;
  61. boolean interlace = false;
  62. if( c instanceof IndexColorModel )
  63. {
  64. colorType = PNGHeader.INDEXED;
  65. int n = ((IndexColorModel)c).getMapSize();
  66. if( n <= 2 )
  67. depth = 1;
  68. else if( n <= 4 )
  69. depth = 2;
  70. else if( n <= 16 )
  71. depth = 4;
  72. else if( n <= 256 )
  73. depth = 8;
  74. else
  75. throw new PNGException("Depth must be <= 8 bits for indexed color.");
  76. palette = new PNGPalette( ((IndexColorModel)c) );
  77. }
  78. else
  79. {
  80. ColorSpace cs = c.getColorSpace();
  81. ColorSpace grayCS = ColorSpace.getInstance( ColorSpace.CS_GRAY );
  82. if( cs == grayCS || bi.getType() == BufferedImage.TYPE_BYTE_GRAY
  83. || bi.getType() == BufferedImage.TYPE_USHORT_GRAY )
  84. colorType = c.hasAlpha() ? PNGHeader.GRAYSCALE_WITH_ALPHA :
  85. PNGHeader.GRAYSCALE;
  86. else
  87. colorType = c.hasAlpha() ? PNGHeader.RGB_WITH_ALPHA : PNGHeader.RGB;
  88. // Figure out the depth
  89. int[] bits = c.getComponentSize();
  90. depth = bits[0];
  91. for(int i = 1; i < bits.length; i++ )
  92. if( bits[i] > depth ) depth = bits[i];
  93. if( (cs != grayCS && !cs.isCS_sRGB()) && cs instanceof ICC_ColorSpace )
  94. profile = new PNGICCProfile( ((ICC_ColorSpace)cs).getProfile() );
  95. }
  96. header = new PNGHeader(width, height, depth, colorType, interlace);
  97. stride = header.getScanlineStride(); // scanline stride
  98. bpp = header.bytesPerPixel(); // bytes per pixel
  99. getRawData( bi );
  100. }
  101. /**
  102. * Returns the generated header.
  103. */
  104. public PNGHeader getHeader()
  105. {
  106. return header;
  107. }
  108. /**
  109. * Returns the generated palette.
  110. */
  111. public PNGPalette getPalette()
  112. {
  113. return palette;
  114. }
  115. /**
  116. * Returns the associated ICC profile, if any.
  117. */
  118. public PNGICCProfile getProfile()
  119. {
  120. return profile;
  121. }
  122. /**
  123. * Encodes the raster and returns a Vector of PNGData chunks.
  124. */
  125. public Vector encodeImage()
  126. {
  127. Deflater deflater = new Deflater(); // The deflater
  128. boolean useFilter = PNGFilter.useFilter( header );
  129. byte[] lastScanline = new byte[ stride ];
  130. byte[] data = new byte[ rawData.length + header.getHeight() ];
  131. byte filterByte = PNGFilter.FILTER_NONE;
  132. for( int i = 0; i < header.getHeight(); i++)
  133. {
  134. byte[] scanline = new byte[ stride ];
  135. System.arraycopy(rawData, (i * stride), scanline, 0, stride);
  136. if( useFilter && i > 0)
  137. filterByte = PNGFilter.chooseFilter( scanline, lastScanline, bpp);
  138. byte[] filtered = PNGFilter.filterScanline( filterByte, scanline,
  139. lastScanline, bpp );
  140. data[i * (stride + 1)] = filterByte;
  141. System.arraycopy(filtered, 0, data, 1 + (i * (stride + 1)), stride);
  142. lastScanline = scanline;
  143. }
  144. deflater.setInput( data );
  145. deflater.finish();
  146. PNGData chunk;
  147. Vector chunks = new Vector();
  148. do
  149. {
  150. chunk = new PNGData( defaultChunkSize );
  151. chunk.deflateToChunk( deflater );
  152. chunks.add( chunk );
  153. }
  154. while( chunk.chunkFull() );
  155. chunk.shrink(); // Shrink the last chunk.
  156. return chunks;
  157. }
  158. /**
  159. * Get the image's raw data.
  160. * FIXME: This may need improving on.
  161. */
  162. private void getRawData( BufferedImage bi ) throws PNGException
  163. {
  164. WritableRaster raster = bi.getRaster();
  165. rawData = new byte[ stride * header.getHeight() ];
  166. if( header.isIndexed() )
  167. {
  168. DataBuffer db = raster.getDataBuffer();
  169. if( !( db instanceof DataBufferByte ) )
  170. throw new PNGException("Unexpected DataBuffer for an IndexColorModel.");
  171. byte[] data = ((DataBufferByte)db).getData();
  172. for(int i = 0; i < header.getHeight(); i++ )
  173. System.arraycopy( data, i * stride, rawData, i * stride, stride );
  174. return;
  175. }
  176. if( header.getDepth() == 16 )
  177. {
  178. DataBuffer db = raster.getDataBuffer();
  179. if( !( db instanceof DataBufferUShort ) )
  180. throw new PNGException("Unexpected DataBuffer for 16-bit.");
  181. short[] data = ((DataBufferUShort)db).getData();
  182. for(int i = 0; i < header.getHeight(); i++ )
  183. for(int j = 0; j < ( stride >> 1); j++)
  184. {
  185. rawData[ j * 2 + i * stride ] = (byte)((data[j + i * (stride >> 1 )] & 0xFF00) >> 8);
  186. rawData[ j * 2 + i * stride + 1 ] = (byte)(data[j + i * (stride >> 1 )] & 0xFF);
  187. }
  188. return;
  189. }
  190. int size = ( header.getColorType() == PNGHeader.RGB_WITH_ALPHA ) ? 4 : 3;
  191. int width = header.getWidth();
  192. int height = header.getHeight();
  193. int[] pixels = bi.getRGB( 0, 0, width, height, null, 0, width );
  194. for( int i = 0; i < width * height; i++ )
  195. {
  196. rawData[ i * size ] = (byte)((pixels[i] & 0xFF0000) >> 16);
  197. rawData[ i * size + 1 ] = (byte)((pixels[i] & 0xFF00) >> 8);
  198. rawData[ i * size + 2 ] = (byte)(pixels[i] & 0xFF);
  199. }
  200. if( size == 4 )
  201. for( int i = 0; i < width * height; i++ )
  202. rawData[ i * size + 3 ] = (byte)((pixels[i] & 0xFF000000) >> 24);
  203. }
  204. }