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: package sun.awt.image;
027:
028: import java.util.Hashtable;
029: import java.io.InputStream;
030: import java.io.IOException;
031: import java.awt.image.*;
032:
033: public abstract class ImageDecoder {
034: InputStreamImageSource source;
035: InputStream input;
036: Thread feeder;
037:
038: protected boolean aborted;
039: protected boolean finished;
040: ImageConsumerQueue queue;
041: ImageDecoder next;
042:
043: public ImageDecoder(InputStreamImageSource src, InputStream is) {
044: source = src;
045: input = is;
046: feeder = Thread.currentThread();
047: }
048:
049: public boolean isConsumer(ImageConsumer ic) {
050: return ImageConsumerQueue.isConsumer(queue, ic);
051: }
052:
053: public void removeConsumer(ImageConsumer ic) {
054: queue = ImageConsumerQueue.removeConsumer(queue, ic, false);
055: if (!finished && queue == null) {
056: abort();
057: }
058: }
059:
060: protected ImageConsumerQueue nextConsumer(ImageConsumerQueue cq) {
061: synchronized (source) {
062: if (aborted) {
063: return null;
064: }
065: cq = ((cq == null) ? queue : cq.next);
066: while (cq != null) {
067: if (cq.interested) {
068: return cq;
069: }
070: cq = cq.next;
071: }
072: }
073: return null;
074: }
075:
076: protected int setDimensions(int w, int h) {
077: ImageConsumerQueue cq = null;
078: int count = 0;
079: while ((cq = nextConsumer(cq)) != null) {
080: cq.consumer.setDimensions(w, h);
081: count++;
082: }
083: return count;
084: }
085:
086: protected int setProperties(Hashtable props) {
087: ImageConsumerQueue cq = null;
088: int count = 0;
089: while ((cq = nextConsumer(cq)) != null) {
090: cq.consumer.setProperties(props);
091: count++;
092: }
093: return count;
094: }
095:
096: protected int setColorModel(ColorModel model) {
097: ImageConsumerQueue cq = null;
098: int count = 0;
099: while ((cq = nextConsumer(cq)) != null) {
100: cq.consumer.setColorModel(model);
101: count++;
102: }
103: return count;
104: }
105:
106: protected int setHints(int hints) {
107: ImageConsumerQueue cq = null;
108: int count = 0;
109: while ((cq = nextConsumer(cq)) != null) {
110: cq.consumer.setHints(hints);
111: count++;
112: }
113: return count;
114: }
115:
116: protected void headerComplete() {
117: feeder.setPriority(ImageFetcher.LOW_PRIORITY);
118: }
119:
120: protected int setPixels(int x, int y, int w, int h,
121: ColorModel model, byte pix[], int off, int scansize) {
122: source.latchConsumers(this );
123: ImageConsumerQueue cq = null;
124: int count = 0;
125: while ((cq = nextConsumer(cq)) != null) {
126: cq.consumer
127: .setPixels(x, y, w, h, model, pix, off, scansize);
128: count++;
129: }
130: return count;
131: }
132:
133: protected int setPixels(int x, int y, int w, int h,
134: ColorModel model, int pix[], int off, int scansize) {
135: source.latchConsumers(this );
136: ImageConsumerQueue cq = null;
137: int count = 0;
138: while ((cq = nextConsumer(cq)) != null) {
139: cq.consumer
140: .setPixels(x, y, w, h, model, pix, off, scansize);
141: count++;
142: }
143: return count;
144: }
145:
146: protected int imageComplete(int status, boolean done) {
147: source.latchConsumers(this );
148: if (done) {
149: finished = true;
150: source.doneDecoding(this );
151: }
152: ImageConsumerQueue cq = null;
153: int count = 0;
154: while ((cq = nextConsumer(cq)) != null) {
155: cq.consumer.imageComplete(status);
156: count++;
157: }
158: return count;
159: }
160:
161: public abstract void produceImage() throws IOException,
162: ImageFormatException;
163:
164: public void abort() {
165: aborted = true;
166: source.doneDecoding(this );
167: close();
168: java.security.AccessController
169: .doPrivileged(new java.security.PrivilegedAction() {
170: public Object run() {
171: feeder.interrupt();
172: return null;
173: }
174: });
175: }
176:
177: public synchronized void close() {
178: if (input != null) {
179: try {
180: input.close();
181: } catch (IOException e) {
182: }
183: }
184: }
185: }
|