001: /*
002: * $RCSfile: CLibJPEGImageWriterSpi.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.4 $
042: * $Date: 2006/04/26 00:45:06 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageioimpl.plugins.jpeg;
046:
047: import java.awt.image.ColorModel;
048: import java.awt.image.IndexColorModel;
049: import java.awt.image.SampleModel;
050: import java.io.IOException;
051: import java.util.List;
052: import java.util.Locale;
053: import javax.imageio.ImageWriter;
054: import javax.imageio.ImageTypeSpecifier;
055: import javax.imageio.metadata.IIOMetadataFormat;
056: import javax.imageio.metadata.IIOMetadataFormatImpl;
057: import javax.imageio.spi.ImageWriterSpi;
058: import javax.imageio.spi.ServiceRegistry;
059: import com.sun.media.imageioimpl.common.PackageUtil;
060: import com.sun.media.imageioimpl.common.ImageUtil;
061:
062: /**
063: */
064: public class CLibJPEGImageWriterSpi extends ImageWriterSpi {
065:
066: private static final String[] names = { "jpeg", "JPEG", "jpg",
067: "JPG", "jfif", "JFIF", "jpeg-lossless", "JPEG-LOSSLESS",
068: "jpeg-ls", "JPEG-LS" };
069:
070: private static final String[] suffixes = { "jpeg", "jpg", "jfif",
071: "jls" };
072:
073: private static final String[] MIMETypes = { "image/jpeg" };
074:
075: private static final String writerClassName = "com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriter";
076:
077: private static final String[] readerSpiNames = { "com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReaderSpi" };
078:
079: private boolean registered = false;
080:
081: public CLibJPEGImageWriterSpi() {
082: super (PackageUtil.getVendor(), PackageUtil.getVersion(), names,
083: suffixes, MIMETypes, writerClassName,
084: STANDARD_OUTPUT_TYPE, readerSpiNames, false, null,
085: null, null, null, false, null, null, null, null);
086: }
087:
088: public void onRegistration(ServiceRegistry registry, Class category) {
089: if (registered) {
090: return;
091: }
092:
093: registered = true;
094:
095: // Branch as a function of codecLib availability.
096: if (!PackageUtil.isCodecLibAvailable()) {
097: // Deregister provider.
098: registry.deregisterServiceProvider(this );
099: } else {
100:
101: List list = ImageUtil.getJDKImageReaderWriterSPI(registry,
102: "JPEG", false);
103:
104: for (int i = 0; i < list.size(); i++) {
105: // Set pairwise ordering to give codecLib writer precedence
106: // over Sun core J2SE writer.
107: registry.setOrdering(category, this , list.get(i));
108: }
109: }
110: }
111:
112: public boolean canEncodeImage(ImageTypeSpecifier type) {
113: ColorModel colorModel = type.getColorModel();
114:
115: if (colorModel instanceof IndexColorModel) {
116: // No need to check further: writer converts to 8-8-8 RGB.
117: return true;
118: }
119:
120: SampleModel sampleModel = type.getSampleModel();
121:
122: // Ensure all channels have the same bit depth
123: int bitDepth;
124: if (colorModel != null) {
125: int[] componentSize = colorModel.getComponentSize();
126: bitDepth = componentSize[0];
127: for (int i = 1; i < componentSize.length; i++) {
128: if (componentSize[i] != bitDepth) {
129: return false;
130: }
131: }
132: } else {
133: int[] sampleSize = sampleModel.getSampleSize();
134: bitDepth = sampleSize[0];
135: for (int i = 1; i < sampleSize.length; i++) {
136: if (sampleSize[i] != bitDepth) {
137: return false;
138: }
139: }
140: }
141:
142: // Ensure bitDepth is no more than 16
143: if (bitDepth > 16) {
144: return false;
145: }
146:
147: // Check number of bands.
148: int numBands = sampleModel.getNumBands();
149: if (numBands < 1 || numBands > 4) {
150: return false;
151: }
152:
153: return true;
154: }
155:
156: public String getDescription(Locale locale) {
157: String desc = PackageUtil.getSpecificationTitle()
158: + " natively-accelerated JPEG Image Writer";
159: return desc;
160: }
161:
162: public ImageWriter createWriterInstance(Object extension)
163: throws IOException {
164: return new CLibJPEGImageWriter(this);
165: }
166: }
|