Source for gnu.javax.imageio.png.PNGEncoder

   1: /* PNGEncoder.java --
   2:    Copyright (C) 2006 Free Software Foundation
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.javax.imageio.png;
  39: 
  40: import java.util.Vector;
  41: import java.util.zip.Deflater;
  42: import java.awt.color.ColorSpace;
  43: import java.awt.color.ICC_Profile;
  44: import java.awt.color.ICC_ColorSpace;
  45: import java.awt.image.BufferedImage;
  46: import java.awt.image.ColorModel;
  47: import java.awt.image.DataBuffer;
  48: import java.awt.image.DataBufferByte;
  49: import java.awt.image.DataBufferUShort;
  50: import java.awt.image.IndexColorModel;
  51: import java.awt.image.Raster;
  52: import java.awt.image.WritableRaster;
  53: 
  54: public class PNGEncoder 
  55: {
  56:   /**
  57:    * The default data chunk size. 8 kb.
  58:    */
  59:   private static final int defaultChunkSize = 8192;
  60: 
  61:   private PNGHeader header;
  62:   private PNGPalette palette;
  63:   private int stride, bpp;
  64:   private byte[] rawData;
  65:   private PNGICCProfile profile;
  66: 
  67:   public PNGEncoder( BufferedImage bi ) throws PNGException
  68:   {
  69:     ColorModel c = bi.getColorModel();
  70:     int width = bi.getWidth(); 
  71:     int height = bi.getHeight();
  72:     int depth = 0;
  73:     int colorType;
  74:     boolean interlace = false;
  75: 
  76:     if( c instanceof IndexColorModel )
  77:       {
  78:     colorType = PNGHeader.INDEXED;
  79:     int n = ((IndexColorModel)c).getMapSize();
  80:     if( n <= 2 )
  81:       depth = 1;
  82:     else if( n <= 4 )
  83:       depth = 2;
  84:     else if( n <= 16 )
  85:       depth = 4;
  86:     else if( n <= 256 )
  87:       depth = 8;
  88:     else
  89:       throw new PNGException("Depth must be <= 8 bits for indexed color.");
  90:     palette = new PNGPalette( ((IndexColorModel)c) );
  91:       }
  92:     else
  93:       { 
  94:     ColorSpace cs = c.getColorSpace();
  95:     ColorSpace grayCS = ColorSpace.getInstance( ColorSpace.CS_GRAY );
  96:     if( cs == grayCS || bi.getType() == BufferedImage.TYPE_BYTE_GRAY 
  97:         || bi.getType() == BufferedImage.TYPE_USHORT_GRAY )
  98:       colorType = c.hasAlpha() ? PNGHeader.GRAYSCALE_WITH_ALPHA : 
  99:         PNGHeader.GRAYSCALE;
 100:     else
 101:       colorType = c.hasAlpha() ? PNGHeader.RGB_WITH_ALPHA : PNGHeader.RGB;
 102:     // Figure out the depth
 103:     int[] bits = c.getComponentSize();
 104:     depth = bits[0];
 105:     for(int i = 1; i < bits.length; i++ )
 106:       if( bits[i] > depth ) depth = bits[i];
 107:     if( (cs != grayCS && !cs.isCS_sRGB()) && cs instanceof ICC_ColorSpace )
 108:       profile = new PNGICCProfile( ((ICC_ColorSpace)cs).getProfile() );
 109:       }
 110: 
 111:     header = new PNGHeader(width, height, depth, colorType, interlace);
 112: 
 113:     stride = header.getScanlineStride(); // scanline stride 
 114:     bpp = header.bytesPerPixel(); // bytes per pixel
 115:     getRawData( bi );
 116:   }
 117: 
 118:   /**
 119:    * Returns the generated header.
 120:    */ 
 121:   public PNGHeader getHeader()
 122:   {
 123:     return header;
 124:   }
 125: 
 126:   /**
 127:    * Returns the generated palette.
 128:    */ 
 129:   public PNGPalette getPalette()
 130:   {
 131:     return palette;
 132:   }
 133: 
 134:   /**
 135:    * Returns the associated ICC profile, if any.
 136:    */ 
 137:   public PNGICCProfile getProfile()
 138:   {
 139:     return profile;
 140:   }
 141: 
 142:   /**
 143:    * Encodes the raster and returns a Vector of PNGData chunks.
 144:    */
 145:   public Vector encodeImage()
 146:   {
 147:     Deflater deflater = new Deflater(); // The deflater
 148:     boolean useFilter = PNGFilter.useFilter( header );
 149:     byte[] lastScanline = new byte[ stride ];
 150: 
 151:     byte[] data = new byte[ rawData.length + header.getHeight() ];
 152: 
 153:     byte filterByte = PNGFilter.FILTER_NONE;
 154:     for( int i = 0; i < header.getHeight(); i++)
 155:       {
 156:     byte[] scanline = new byte[ stride ]; 
 157:     System.arraycopy(rawData, (i * stride), scanline, 0, stride);
 158:     if( useFilter && i > 0)
 159:       filterByte = PNGFilter.chooseFilter( scanline, lastScanline, bpp);
 160:     
 161:     byte[] filtered = PNGFilter.filterScanline( filterByte, scanline, 
 162:                             lastScanline, bpp );
 163:     data[i * (stride + 1)] = filterByte;
 164:     System.arraycopy(filtered, 0, data, 1 + (i * (stride + 1)), stride);
 165: 
 166:     lastScanline = scanline;
 167:       }
 168: 
 169:     deflater.setInput( data ); 
 170:     deflater.finish();
 171: 
 172:     PNGData chunk;
 173:     Vector chunks = new Vector();
 174:     do
 175:       {
 176:     chunk = new PNGData( defaultChunkSize );
 177:     chunk.deflateToChunk( deflater );
 178:     chunks.add( chunk );
 179:       }
 180:     while( chunk.chunkFull() );
 181:     chunk.shrink(); // Shrink the last chunk.
 182:     return chunks;
 183:   }
 184: 
 185:   /**
 186:    * Get the image's raw data.
 187:    * FIXME: This may need improving on.
 188:    */
 189:   private void getRawData( BufferedImage bi ) throws PNGException
 190:   {
 191:     WritableRaster raster = bi.getRaster();
 192:     rawData = new byte[ stride * header.getHeight() ];
 193:     if( header.isIndexed() )
 194:       {
 195:     DataBuffer db = raster.getDataBuffer();
 196:     if( !( db instanceof DataBufferByte ) )
 197:       throw new PNGException("Unexpected DataBuffer for an IndexColorModel.");
 198:     byte[] data = ((DataBufferByte)db).getData();
 199:     for(int i = 0; i < header.getHeight(); i++ )
 200:       System.arraycopy( data, i * stride, rawData, i * stride, stride );
 201:     return;
 202:       }
 203: 
 204:     if( header.getDepth() == 16 )
 205:       {
 206:     DataBuffer db = raster.getDataBuffer();
 207:     if( !( db instanceof DataBufferUShort ) )
 208:       throw new PNGException("Unexpected DataBuffer for 16-bit.");
 209:     short[] data = ((DataBufferUShort)db).getData();
 210:     for(int i = 0; i < header.getHeight(); i++ )
 211:       for(int j = 0; j < ( stride >> 1); j++)
 212:         {
 213:           rawData[ j * 2 + i * stride ] = (byte)((data[j + i * (stride >> 1 )] & 0xFF00) >> 8);
 214:           rawData[ j * 2 + i * stride + 1 ] = (byte)(data[j + i * (stride >> 1 )] & 0xFF);
 215:         }
 216:     return;
 217:       }
 218: 
 219:     int size = ( header.getColorType() == PNGHeader.RGB_WITH_ALPHA ) ? 4 : 3;
 220:     int width = header.getWidth();
 221:     int height = header.getHeight();
 222:     int[] pixels = bi.getRGB( 0, 0, width, height, null, 0, width );
 223: 
 224:     for( int i = 0; i < width * height; i++ )
 225:       {
 226:     rawData[ i * size ] = (byte)((pixels[i] & 0xFF0000) >> 16);
 227:     rawData[ i * size + 1 ] = (byte)((pixels[i] & 0xFF00) >> 8);
 228:     rawData[ i * size + 2 ] = (byte)(pixels[i] & 0xFF);
 229:       }
 230: 
 231:     if( size == 4 )
 232:       for( int i = 0; i < width * height; i++ )
 233:     rawData[ i * size + 3 ] = (byte)((pixels[i] & 0xFF000000) >> 24);
 234:   }
 235: }