FileDocCategorySizeDatePackage
PNGImageWriter.javaAPI DocAndroid 1.5 API9416Wed May 06 22:41:54 BST 2009org.apache.harmony.x.imageio.plugins.png

PNGImageWriter

public class PNGImageWriter extends ImageWriter
author
Viskov Nikolay
version
$Revision$

Fields Summary
private static final boolean
DEBUG
private static android.graphics.Bitmap
bm
private static int[]
BAND_OFFSETS
private static final int
PNG_COLOR_TYPE_GRAY
private static final int
PNG_COLOR_TYPE_RGB
private static final int
PNG_COLOR_TYPE_PLTE
private static final int
PNG_COLOR_TYPE_GRAY_ALPHA
private static final int
PNG_COLOR_TYPE_RGBA
Constructors Summary
protected PNGImageWriter(ImageWriterSpi iwSpi)

    
    //???AWT: private static native void initIDs(Class<ImageOutputStream> iosClass);

     
        //???AWT
        /*
        System.loadLibrary("pngencoder"); //$NON-NLS-1$
        initIDs(ImageOutputStream.class);
        */
    
        super(iwSpi);
    
Methods Summary
public javax.imageio.metadata.IIOMetadataconvertImageMetadata(javax.imageio.metadata.IIOMetadata arg0, javax.imageio.ImageTypeSpecifier arg1, javax.imageio.ImageWriteParam arg2)

        throw new NotImplementedException();
    
public javax.imageio.metadata.IIOMetadataconvertStreamMetadata(javax.imageio.metadata.IIOMetadata arg0, javax.imageio.ImageWriteParam arg1)

        throw new NotImplementedException();
    
public static android.graphics.BitmapgetBitmap()

        
        return bm;
    
public javax.imageio.metadata.IIOMetadatagetDefaultImageMetadata(javax.imageio.ImageTypeSpecifier arg0, javax.imageio.ImageWriteParam arg1)

        throw new NotImplementedException();
    
public javax.imageio.metadata.IIOMetadatagetDefaultStreamMetadata(javax.imageio.ImageWriteParam arg0)

        throw new NotImplementedException();
    
public javax.imageio.ImageWriteParamgetDefaultWriteParam()

        return new PNGImageWriterParam();
    
public voidwrite(javax.imageio.metadata.IIOMetadata streamMetadata, javax.imageio.IIOImage iioImage, javax.imageio.ImageWriteParam param)

        if (output == null) {
            throw new IllegalStateException("Output not been set");
        }
        if (iioImage == null) {
            throw new IllegalArgumentException("Image equals null");
        }
        // AWT???: I think this is not needed anymore
        // if (iioImage.hasRaster() && !canWriteRasters()) {
        //    throw new UnsupportedOperationException("Can't write raster");
        //}// ImageOutputStreamImpl
        
        Raster sourceRaster;
        RenderedImage img = null;
        if (!iioImage.hasRaster()) {
            img = iioImage.getRenderedImage();
            if (img instanceof BufferedImage) {
                sourceRaster = ((BufferedImage) img).getRaster();
            } else {
                sourceRaster = img.getData();
            }
        } else {
            sourceRaster = iioImage.getRaster();
        }

        SampleModel model = sourceRaster.getSampleModel();
        int srcWidth = sourceRaster.getWidth();
        int srcHeight = sourceRaster.getHeight();
        int numBands = model.getNumBands();
        
        ColorModel colorModel = img.getColorModel();
        int pixelSize = colorModel.getPixelSize();
        int bytePixelSize = pixelSize / 8;
        int bitDepth = pixelSize / numBands;
        
        // byte per band
        int bpb = bitDepth > 8 ? 2 : 1;
        
        boolean isInterlace = true;
        if (param instanceof PNGImageWriterParam) {
            isInterlace = ((PNGImageWriterParam) param).getInterlace();
        }
        
        int colorType = PNG_COLOR_TYPE_GRAY;
        int[] palette = null;
        
        if (colorModel instanceof IndexColorModel) {
            if (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8) {
//              Wrong bitDepth-numBands composition
                throw new IllegalArgumentException(Messages.getString("imageio.1"));//$NON-NLS-1$
            }
            if (numBands != 1) {
//              Wrong bitDepth-numBands composition
                throw new IllegalArgumentException(Messages.getString("imageio.1"));//$NON-NLS-1$
            }

            IndexColorModel icm = (IndexColorModel) colorModel;
            palette = new int[icm.getMapSize()];
            icm.getRGBs(palette);
            colorType = PNG_COLOR_TYPE_PLTE;
        }
        else if (numBands == 1) {
            if (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8 && bitDepth != 16) {
//              Wrong bitDepth-numBands composition
                throw new IllegalArgumentException(Messages.getString("imageio.1"));//$NON-NLS-1$
            }
            colorType = PNG_COLOR_TYPE_GRAY;
        }
        else if (numBands == 2) {
            if (bitDepth != 8 && bitDepth != 16) {
//              Wrong bitDepth-numBands composition
                throw new IllegalArgumentException(Messages.getString("imageio.1"));//$NON-NLS-1$
            }
            colorType = PNG_COLOR_TYPE_GRAY_ALPHA;
        }
        else if (numBands == 3) {
            if (bitDepth != 8 && bitDepth != 16) {
//              Wrong bitDepth-numBands composition
                throw new IllegalArgumentException(Messages.getString("imageio.1")); //$NON-NLS-1$
            }
            colorType = PNG_COLOR_TYPE_RGB;
        }
        else if (numBands == 4) {
            if (bitDepth != 8 && bitDepth != 16) {
                //Wrong bitDepth-numBands composition
                throw new IllegalArgumentException(Messages.getString("imageio.1")); //$NON-NLS-1$
            }
            colorType = PNG_COLOR_TYPE_RGBA;
        }
        
        /* ???AWT: I think this is not needed anymore
        int dbufferLenght = bytePixelSize * imageHeight * imageWidth;
        DataBufferByte dbuffer = new DataBufferByte(dbufferLenght);

        WritableRaster scanRaster = Raster.createInterleavedRaster(dbuffer, imageWidth, imageHeight, bpb * numBands
                * imageWidth, bpb * numBands, BAND_OFFSETS[numBands], null);

        scanRaster.setRect(((BufferedImage) image).getRaster()// image.getData()
                .createChild(0, 0, imageWidth, imageHeight, 0, 0, null));
        */

        if (DEBUG) {
            System.out.println("**** raster:" + sourceRaster);        
            System.out.println("**** model:" + model);
            System.out.println("**** type:" + colorType);
        }
        
        if (model instanceof SinglePixelPackedSampleModel) {
            DataBufferInt ibuf = (DataBufferInt)sourceRaster.getDataBuffer();
            int[] pixels = ibuf.getData();
            
            // Create a bitmap with the pixel
            bm = Bitmap.createBitmap(pixels, srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
            
            // Use Bitmap.compress() to write the image
            ImageOutputStream ios = (ImageOutputStream) getOutput();
            ImageOutputStreamWrapper iosw = new ImageOutputStreamWrapper(ios);
            bm.compress(CompressFormat.PNG, 100, iosw);
        } else {
            // ???AWT: Add support for other color models
            throw new RuntimeException("Color model not supported yet");
        }