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