001: /*
002: * $RCSfile: CLibPNGImageWriterSpi.java,v $
003: *
004: *
005: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * - Redistribution of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * - Redistribution in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * Neither the name of Sun Microsystems, Inc. or the names of
020: * contributors may be used to endorse or promote products derived
021: * from this software without specific prior written permission.
022: *
023: * This software is provided "AS IS," without a warranty of any
024: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
025: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
026: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027: * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
028: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
029: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
031: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035: * POSSIBILITY OF SUCH DAMAGES.
036: *
037: * You acknowledge that this software is not designed or intended for
038: * use in the design, construction, operation or maintenance of any
039: * nuclear facility.
040: *
041: * $Revision: 1.2 $
042: * $Date: 2006/03/31 19:43:40 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageioimpl.plugins.png;
046:
047: import java.awt.image.ColorModel;
048: import java.awt.image.IndexColorModel;
049: import java.awt.image.SampleModel;
050: import java.util.List;
051: import java.util.Locale;
052: import javax.imageio.ImageWriter;
053: import javax.imageio.ImageTypeSpecifier;
054: import javax.imageio.metadata.IIOMetadataFormat;
055: import javax.imageio.metadata.IIOMetadataFormatImpl;
056: import javax.imageio.spi.ImageWriterSpi;
057: import javax.imageio.spi.ServiceRegistry;
058: import com.sun.media.imageioimpl.common.PackageUtil;
059: import com.sun.media.imageioimpl.common.ImageUtil;
060:
061: /**
062: */
063: public class CLibPNGImageWriterSpi extends ImageWriterSpi {
064:
065: private static final String[] names = { "png", "PNG" };
066:
067: private static final String[] suffixes = { "png" };
068:
069: private static final String[] MIMETypes = { "image/png",
070: "image/x-png" };
071:
072: private static final String writerClassName = "com.sun.media.imageioimpl.plugins.png.CLibPNGImageWriter";
073:
074: private static final String[] readerSpiNames = { "com.sun.media.imageioimpl.plugins.png.CLibPNGImageReaderSpi" };
075:
076: private boolean registered = false;
077:
078: public CLibPNGImageWriterSpi() {
079: super (
080: PackageUtil.getVendor(),
081: PackageUtil.getVersion(),
082: names,
083: suffixes,
084: MIMETypes,
085: writerClassName,
086: STANDARD_OUTPUT_TYPE,
087: readerSpiNames,
088: false,
089: null,
090: null,
091: null,
092: null,
093: true,
094: CLibPNGMetadata.nativeMetadataFormatName,
095: "com.sun.media.imageioimpl.plugins.png.CLibPNGMetadataFormat",
096: null, null);
097: }
098:
099: public void onRegistration(ServiceRegistry registry, Class category) {
100: if (registered) {
101: return;
102: }
103:
104: registered = true;
105:
106: // Branch as a function of codecLib availability.
107: if (!PackageUtil.isCodecLibAvailable()) {
108: // Deregister provider.
109: registry.deregisterServiceProvider(this );
110: } else {
111:
112: List list = ImageUtil.getJDKImageReaderWriterSPI(registry,
113: "PNG", false);
114:
115: for (int i = 0; i < list.size(); i++) {
116: // Set pairwise ordering to give codecLib writer precedence
117: // over Sun core J2SE writer.
118: registry.setOrdering(category, this , list.get(i));
119: }
120: }
121: }
122:
123: public boolean canEncodeImage(ImageTypeSpecifier type) {
124: SampleModel sampleModel = type.getSampleModel();
125: ColorModel colorModel = type.getColorModel();
126:
127: // Ensure all channels have the same bit depth
128: int[] sampleSize = sampleModel.getSampleSize();
129: int bitDepth = sampleSize[0];
130: for (int i = 1; i < sampleSize.length; i++) {
131: if (sampleSize[i] != bitDepth) {
132: return false;
133: }
134: }
135:
136: // Ensure bitDepth is either 1, 8, or 16.
137: // XXX If codecLib writing support is improved this might
138: // be able to accept all depths between 1 and 16 inclusive.
139: if (!(bitDepth == 1 || bitDepth == 8 || bitDepth == 16)) {
140: return false;
141: }
142:
143: // Check number of bands, alpha
144: int numBands = sampleModel.getNumBands();
145: if (numBands < 1 || numBands > 4) {
146: return false;
147: }
148:
149: if (colorModel instanceof IndexColorModel) {
150: return true;
151: }
152:
153: boolean hasAlpha = colorModel != null && colorModel.hasAlpha();
154: if ((numBands == 1 || numBands == 3) && hasAlpha) {
155: return false;
156: }
157: if ((numBands == 2 || numBands == 4) && !hasAlpha) {
158: return false;
159: }
160:
161: return true;
162: }
163:
164: public String getDescription(Locale locale) {
165: String desc = PackageUtil.getSpecificationTitle()
166: + " natively-accelerated PNG Image Writer";
167: return desc;
168: }
169:
170: public ImageWriter createWriterInstance(Object extension) {
171: return new CLibPNGImageWriter(this);
172: }
173: }
|