diff options
Diffstat (limited to 'gnu/javax/imageio/png/PNGEncoder.java')
-rw-r--r-- | gnu/javax/imageio/png/PNGEncoder.java | 144 |
1 files changed, 72 insertions, 72 deletions
diff --git a/gnu/javax/imageio/png/PNGEncoder.java b/gnu/javax/imageio/png/PNGEncoder.java index b8441c4e3..b31787635 100644 --- a/gnu/javax/imageio/png/PNGEncoder.java +++ b/gnu/javax/imageio/png/PNGEncoder.java @@ -49,7 +49,7 @@ import java.awt.image.DataBufferUShort; import java.awt.image.IndexColorModel; import java.awt.image.WritableRaster; -public class PNGEncoder +public class PNGEncoder { /** * The default data chunk size. 8 kb. @@ -65,7 +65,7 @@ public class PNGEncoder public PNGEncoder( BufferedImage bi ) throws PNGException { ColorModel c = bi.getColorModel(); - int width = bi.getWidth(); + int width = bi.getWidth(); int height = bi.getHeight(); int depth = 0; int colorType; @@ -73,49 +73,49 @@ public class PNGEncoder if( c instanceof IndexColorModel ) { - colorType = PNGHeader.INDEXED; - int n = ((IndexColorModel)c).getMapSize(); - if( n <= 2 ) - depth = 1; - else if( n <= 4 ) - depth = 2; - else if( n <= 16 ) - depth = 4; - else if( n <= 256 ) - depth = 8; - else - throw new PNGException("Depth must be <= 8 bits for indexed color."); - palette = new PNGPalette( ((IndexColorModel)c) ); + colorType = PNGHeader.INDEXED; + int n = ((IndexColorModel)c).getMapSize(); + if( n <= 2 ) + depth = 1; + else if( n <= 4 ) + depth = 2; + else if( n <= 16 ) + depth = 4; + else if( n <= 256 ) + depth = 8; + else + throw new PNGException("Depth must be <= 8 bits for indexed color."); + palette = new PNGPalette( ((IndexColorModel)c) ); } else - { - ColorSpace cs = c.getColorSpace(); - ColorSpace grayCS = ColorSpace.getInstance( ColorSpace.CS_GRAY ); - if( cs == grayCS || bi.getType() == BufferedImage.TYPE_BYTE_GRAY - || bi.getType() == BufferedImage.TYPE_USHORT_GRAY ) - colorType = c.hasAlpha() ? PNGHeader.GRAYSCALE_WITH_ALPHA : - PNGHeader.GRAYSCALE; - else - colorType = c.hasAlpha() ? PNGHeader.RGB_WITH_ALPHA : PNGHeader.RGB; - // Figure out the depth - int[] bits = c.getComponentSize(); - depth = bits[0]; - for(int i = 1; i < bits.length; i++ ) - if( bits[i] > depth ) depth = bits[i]; - if( (cs != grayCS && !cs.isCS_sRGB()) && cs instanceof ICC_ColorSpace ) - profile = new PNGICCProfile( ((ICC_ColorSpace)cs).getProfile() ); + { + ColorSpace cs = c.getColorSpace(); + ColorSpace grayCS = ColorSpace.getInstance( ColorSpace.CS_GRAY ); + if( cs == grayCS || bi.getType() == BufferedImage.TYPE_BYTE_GRAY + || bi.getType() == BufferedImage.TYPE_USHORT_GRAY ) + colorType = c.hasAlpha() ? PNGHeader.GRAYSCALE_WITH_ALPHA : + PNGHeader.GRAYSCALE; + else + colorType = c.hasAlpha() ? PNGHeader.RGB_WITH_ALPHA : PNGHeader.RGB; + // Figure out the depth + int[] bits = c.getComponentSize(); + depth = bits[0]; + for(int i = 1; i < bits.length; i++ ) + if( bits[i] > depth ) depth = bits[i]; + if( (cs != grayCS && !cs.isCS_sRGB()) && cs instanceof ICC_ColorSpace ) + profile = new PNGICCProfile( ((ICC_ColorSpace)cs).getProfile() ); } header = new PNGHeader(width, height, depth, colorType, interlace); - stride = header.getScanlineStride(); // scanline stride + stride = header.getScanlineStride(); // scanline stride bpp = header.bytesPerPixel(); // bytes per pixel getRawData( bi ); } /** * Returns the generated header. - */ + */ public PNGHeader getHeader() { return header; @@ -123,7 +123,7 @@ public class PNGEncoder /** * Returns the generated palette. - */ + */ public PNGPalette getPalette() { return palette; @@ -131,7 +131,7 @@ public class PNGEncoder /** * Returns the associated ICC profile, if any. - */ + */ public PNGICCProfile getProfile() { return profile; @@ -151,29 +151,29 @@ public class PNGEncoder byte filterByte = PNGFilter.FILTER_NONE; for( int i = 0; i < header.getHeight(); i++) { - byte[] scanline = new byte[ stride ]; - System.arraycopy(rawData, (i * stride), scanline, 0, stride); - if( useFilter && i > 0) - filterByte = PNGFilter.chooseFilter( scanline, lastScanline, bpp); - - byte[] filtered = PNGFilter.filterScanline( filterByte, scanline, - lastScanline, bpp ); - data[i * (stride + 1)] = filterByte; - System.arraycopy(filtered, 0, data, 1 + (i * (stride + 1)), stride); - - lastScanline = scanline; + byte[] scanline = new byte[ stride ]; + System.arraycopy(rawData, (i * stride), scanline, 0, stride); + if( useFilter && i > 0) + filterByte = PNGFilter.chooseFilter( scanline, lastScanline, bpp); + + byte[] filtered = PNGFilter.filterScanline( filterByte, scanline, + lastScanline, bpp ); + data[i * (stride + 1)] = filterByte; + System.arraycopy(filtered, 0, data, 1 + (i * (stride + 1)), stride); + + lastScanline = scanline; } - deflater.setInput( data ); + deflater.setInput( data ); deflater.finish(); PNGData chunk; Vector chunks = new Vector(); do { - chunk = new PNGData( defaultChunkSize ); - chunk.deflateToChunk( deflater ); - chunks.add( chunk ); + chunk = new PNGData( defaultChunkSize ); + chunk.deflateToChunk( deflater ); + chunks.add( chunk ); } while( chunk.chunkFull() ); chunk.shrink(); // Shrink the last chunk. @@ -190,28 +190,28 @@ public class PNGEncoder rawData = new byte[ stride * header.getHeight() ]; if( header.isIndexed() ) { - DataBuffer db = raster.getDataBuffer(); - if( !( db instanceof DataBufferByte ) ) - throw new PNGException("Unexpected DataBuffer for an IndexColorModel."); - byte[] data = ((DataBufferByte)db).getData(); - for(int i = 0; i < header.getHeight(); i++ ) - System.arraycopy( data, i * stride, rawData, i * stride, stride ); - return; + DataBuffer db = raster.getDataBuffer(); + if( !( db instanceof DataBufferByte ) ) + throw new PNGException("Unexpected DataBuffer for an IndexColorModel."); + byte[] data = ((DataBufferByte)db).getData(); + for(int i = 0; i < header.getHeight(); i++ ) + System.arraycopy( data, i * stride, rawData, i * stride, stride ); + return; } if( header.getDepth() == 16 ) { - DataBuffer db = raster.getDataBuffer(); - if( !( db instanceof DataBufferUShort ) ) - throw new PNGException("Unexpected DataBuffer for 16-bit."); - short[] data = ((DataBufferUShort)db).getData(); - for(int i = 0; i < header.getHeight(); i++ ) - for(int j = 0; j < ( stride >> 1); j++) - { - rawData[ j * 2 + i * stride ] = (byte)((data[j + i * (stride >> 1 )] & 0xFF00) >> 8); - rawData[ j * 2 + i * stride + 1 ] = (byte)(data[j + i * (stride >> 1 )] & 0xFF); - } - return; + DataBuffer db = raster.getDataBuffer(); + if( !( db instanceof DataBufferUShort ) ) + throw new PNGException("Unexpected DataBuffer for 16-bit."); + short[] data = ((DataBufferUShort)db).getData(); + for(int i = 0; i < header.getHeight(); i++ ) + for(int j = 0; j < ( stride >> 1); j++) + { + rawData[ j * 2 + i * stride ] = (byte)((data[j + i * (stride >> 1 )] & 0xFF00) >> 8); + rawData[ j * 2 + i * stride + 1 ] = (byte)(data[j + i * (stride >> 1 )] & 0xFF); + } + return; } int size = ( header.getColorType() == PNGHeader.RGB_WITH_ALPHA ) ? 4 : 3; @@ -221,13 +221,13 @@ public class PNGEncoder for( int i = 0; i < width * height; i++ ) { - rawData[ i * size ] = (byte)((pixels[i] & 0xFF0000) >> 16); - rawData[ i * size + 1 ] = (byte)((pixels[i] & 0xFF00) >> 8); - rawData[ i * size + 2 ] = (byte)(pixels[i] & 0xFF); + rawData[ i * size ] = (byte)((pixels[i] & 0xFF0000) >> 16); + rawData[ i * size + 1 ] = (byte)((pixels[i] & 0xFF00) >> 8); + rawData[ i * size + 2 ] = (byte)(pixels[i] & 0xFF); } if( size == 4 ) for( int i = 0; i < width * height; i++ ) - rawData[ i * size + 3 ] = (byte)((pixels[i] & 0xFF000000) >> 24); + rawData[ i * size + 3 ] = (byte)((pixels[i] & 0xFF000000) >> 24); } } |