001: /*
002: * $RCSfile: CLibJPEGImageReaderSpi.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/24 20:53:01 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageioimpl.plugins.jpeg;
046:
047: import java.util.Arrays;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Locale;
051: import javax.imageio.spi.ImageReaderSpi;
052: import javax.imageio.spi.ServiceRegistry;
053: import javax.imageio.stream.ImageInputStream;
054: import java.io.IOException;
055: import javax.imageio.ImageReader;
056: import javax.imageio.IIOException;
057: import com.sun.media.imageioimpl.common.PackageUtil;
058: import com.sun.media.imageioimpl.common.ImageUtil;
059:
060: public class CLibJPEGImageReaderSpi extends ImageReaderSpi {
061:
062: private static final String[] names = { "jpeg", "JPEG", "jpg",
063: "JPG", "jfif", "JFIF", "jpeg-lossless", "JPEG-LOSSLESS",
064: "jpeg-ls", "JPEG-LS" };
065:
066: private static final String[] suffixes = { "jpeg", "jpg", "jfif",
067: "jls" };
068:
069: private static final String[] MIMETypes = { "image/jpeg" };
070:
071: private static final String readerClassName = "com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader";
072:
073: private static final String[] writerSpiNames = { "com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi" };
074:
075: private boolean registered = false;
076:
077: public CLibJPEGImageReaderSpi() {
078: super (
079: PackageUtil.getVendor(),
080: PackageUtil.getVersion(),
081: names,
082: suffixes,
083: MIMETypes,
084: readerClassName,
085: STANDARD_INPUT_TYPE,
086: writerSpiNames,
087: false, // supportsStandardStreamMetadataFormat
088: null, // nativeStreamMetadataFormatName
089: null, // nativeStreamMetadataFormatClassName
090: null, // extraStreamMetadataFormatNames
091: null, // extraStreamMetadataFormatClassNames
092: true, // supportsStandardImageMetadataFormat
093: CLibJPEGMetadata.NATIVE_FORMAT,
094: CLibJPEGMetadata.NATIVE_FORMAT_CLASS,
095: new String[] { CLibJPEGMetadata.TIFF_FORMAT },
096: new String[] { CLibJPEGMetadata.TIFF_FORMAT_CLASS });
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: "JPEG", true);
114:
115: for (int i = 0; i < list.size(); i++) {
116: // Set pairwise ordering to give codecLib reader precedence
117: // over Sun core J2SE reader.
118: registry.setOrdering(category, this , list.get(i));
119: }
120: }
121: }
122:
123: public String getDescription(Locale locale) {
124: String desc = PackageUtil.getSpecificationTitle()
125: + " natively-accelerated JPEG Image Reader";
126: return desc;
127: }
128:
129: public boolean canDecodeInput(Object source) throws IOException {
130: if (!(source instanceof ImageInputStream)) {
131: return false;
132: }
133: ImageInputStream iis = (ImageInputStream) source;
134: iis.mark();
135: // If the first two bytes are a JPEG SOI marker, it's probably
136: // a JPEG file. If they aren't, it definitely isn't a JPEG file.
137: int byte1 = iis.read();
138: int byte2 = iis.read();
139: if ((byte1 != 0xFF) || (byte2 != 0xD8)) {
140: iis.reset();
141: return false;
142: }
143: do {
144: byte1 = iis.read();
145: byte2 = iis.read();
146: if (byte1 != 0xFF)
147: break; // something wrong, but probably readable
148: if (byte2 == 0xDA)
149: break; // Start of scan
150: if (byte2 == 0xC2) { // progressive mode, can't decode
151: iis.reset();
152: return false;
153: }
154: if ((byte2 >= 0xC0) && (byte2 <= 0xC3)) // not progressive, can decode
155: break;
156: int length = iis.read() << 8;
157: length += iis.read();
158: length -= 2;
159: while (length > 0)
160: length -= iis.skipBytes(length);
161: } while (true);
162: iis.reset();
163: return true;
164: }
165:
166: public ImageReader createReaderInstance(Object extension)
167: throws IIOException {
168: return new CLibJPEGImageReader(this);
169: }
170: }
|