001: /*
002: * Copyright 1995-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: /*-
026: * Reads JPEG images from an InputStream and reports the
027: * image data to an InputStreamImageSource object.
028: *
029: * The native implementation of the JPEG image decoder was adapted from
030: * release 6 of the free JPEG software from the Independent JPEG Group.
031: */
032: package sun.awt.image;
033:
034: import java.util.Vector;
035: import java.util.Hashtable;
036: import java.io.InputStream;
037: import java.io.IOException;
038: import java.awt.image.*;
039:
040: /**
041: * JPEG Image converter
042: *
043: * @version 1.29 05/05/07
044: * @author Jim Graham
045: */
046: public class JPEGImageDecoder extends ImageDecoder {
047: private static ColorModel RGBcolormodel;
048: private static ColorModel ARGBcolormodel;
049: private static ColorModel Graycolormodel;
050:
051: private static final Class InputStreamClass = InputStream.class;
052:
053: private static native void initIDs(Class InputStreamClass);
054:
055: private ColorModel colormodel;
056:
057: static {
058: java.security.AccessController
059: .doPrivileged(new sun.security.action.LoadLibraryAction(
060: "jpeg"));
061: initIDs(InputStreamClass);
062: RGBcolormodel = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
063: ARGBcolormodel = ColorModel.getRGBdefault();
064: byte g[] = new byte[256];
065: for (int i = 0; i < 256; i++) {
066: g[i] = (byte) i;
067: }
068: Graycolormodel = new IndexColorModel(8, 256, g, g, g);
069: }
070:
071: private native void readImage(InputStream is, byte buf[])
072: throws ImageFormatException, IOException;
073:
074: Hashtable props = new Hashtable();
075:
076: public JPEGImageDecoder(InputStreamImageSource src, InputStream is) {
077: super (src, is);
078: }
079:
080: /**
081: * An error has occurred. Throw an exception.
082: */
083: private static void error(String s1) throws ImageFormatException {
084: throw new ImageFormatException(s1);
085: }
086:
087: public boolean sendHeaderInfo(int width, int height, boolean gray,
088: boolean hasalpha, boolean multipass) {
089: setDimensions(width, height);
090:
091: setProperties(props);
092: if (gray) {
093: colormodel = Graycolormodel;
094: } else {
095: if (hasalpha) {
096: colormodel = ARGBcolormodel;
097: } else {
098: colormodel = RGBcolormodel;
099: }
100: }
101:
102: setColorModel(colormodel);
103:
104: int flags = hintflags;
105: if (!multipass) {
106: flags |= ImageConsumer.SINGLEPASS;
107: }
108: setHints(hintflags);
109: headerComplete();
110:
111: return true;
112: }
113:
114: public boolean sendPixels(int pixels[], int y) {
115: int count = setPixels(0, y, pixels.length, 1, colormodel,
116: pixels, 0, pixels.length);
117: if (count <= 0) {
118: aborted = true;
119: }
120: return !aborted;
121: }
122:
123: public boolean sendPixels(byte pixels[], int y) {
124: int count = setPixels(0, y, pixels.length, 1, colormodel,
125: pixels, 0, pixels.length);
126: if (count <= 0) {
127: aborted = true;
128: }
129: return !aborted;
130: }
131:
132: /**
133: * produce an image from the stream.
134: */
135: public void produceImage() throws IOException, ImageFormatException {
136: try {
137: readImage(input, new byte[1024]);
138: if (!aborted) {
139: imageComplete(ImageConsumer.STATICIMAGEDONE, true);
140: }
141: } catch (IOException e) {
142: if (!aborted) {
143: throw e;
144: }
145: } finally {
146: close();
147: }
148: }
149:
150: /**
151: * The ImageConsumer hints flag for a JPEG image.
152: */
153: private static final int hintflags = ImageConsumer.TOPDOWNLEFTRIGHT
154: | ImageConsumer.COMPLETESCANLINES
155: | ImageConsumer.SINGLEFRAME;
156: }
|