1 /*
   2  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.imageio.plugins.png;
  27 
  28 import java.awt.image.ColorModel;
  29 import java.awt.image.IndexColorModel;
  30 import java.awt.image.SampleModel;
  31 import java.util.Locale;
  32 import javax.imageio.ImageWriter;
  33 import javax.imageio.ImageTypeSpecifier;
  34 import javax.imageio.metadata.IIOMetadataFormat;
  35 import javax.imageio.metadata.IIOMetadataFormatImpl;
  36 import javax.imageio.spi.ImageWriterSpi;
  37 import javax.imageio.stream.ImageOutputStream;
  38 
  39 public class PNGImageWriterSpi extends ImageWriterSpi {
  40 
  41     private static final String vendorName = "Oracle Corporation";
  42 
  43     private static final String version = "1.0";
  44 
  45     private static final String[] names = { "png", "PNG" };
  46 
  47     private static final String[] suffixes = { "png" };
  48 
  49     private static final String[] MIMETypes = { "image/png", "image/x-png" };
  50 
  51     private static final String writerClassName =
  52         "com.sun.imageio.plugins.png.PNGImageWriter";
  53 
  54     private static final String[] readerSpiNames = {
  55         "com.sun.imageio.plugins.png.PNGImageReaderSpi"
  56     };
  57 
  58     public PNGImageWriterSpi() {
  59           super(vendorName,
  60                 version,
  61                 names,
  62                 suffixes,
  63                 MIMETypes,
  64                 writerClassName,
  65                 new Class[] { ImageOutputStream.class },
  66                 readerSpiNames,
  67                 false,
  68                 null, null,
  69                 null, null,
  70                 true,
  71                 PNGMetadata.nativeMetadataFormatName,
  72                 "com.sun.imageio.plugins.png.PNGMetadataFormat",
  73                 null, null
  74                 );
  75     }
  76 
  77     public boolean canEncodeImage(ImageTypeSpecifier type) {
  78         SampleModel sampleModel = type.getSampleModel();
  79         ColorModel colorModel = type.getColorModel();
  80 
  81         // Find the maximum bit depth across all channels
  82         int[] sampleSize = sampleModel.getSampleSize();
  83         int bitDepth = sampleSize[0];
  84         for (int i = 1; i < sampleSize.length; i++) {
  85             if (sampleSize[i] > bitDepth) {
  86                 bitDepth = sampleSize[i];
  87             }
  88         }
  89 
  90         // Ensure bitDepth is between 1 and 16
  91         if (bitDepth < 1 || bitDepth > 16) {
  92             return false;
  93         }
  94 
  95         // Check number of bands, alpha
  96         int numBands = sampleModel.getNumBands();
  97         if (numBands < 1 || numBands > 4) {
  98             return false;
  99         }
 100 
 101         boolean hasAlpha = colorModel.hasAlpha();
 102         // Fix 4464413: PNGTransparency reg-test was failing
 103         // because for IndexColorModels that have alpha,
 104         // numBands == 1 && hasAlpha == true, thus causing
 105         // the check below to fail and return false.
 106         if (colorModel instanceof IndexColorModel) {
 107             return true;
 108         }
 109         if ((numBands == 1 || numBands == 3) && hasAlpha) {
 110             return false;
 111         }
 112         if ((numBands == 2 || numBands == 4) && !hasAlpha) {
 113             return false;
 114         }
 115 
 116         return true;
 117     }
 118 
 119     public String getDescription(Locale locale) {
 120         return "Standard PNG image writer";
 121     }
 122 
 123     public ImageWriter createWriterInstance(Object extension) {
 124         return new PNGImageWriter(this);
 125     }
 126 }