001: /*
002: * Copyright 1995-2004 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.util.Enumeration;
030:
031: import java.awt.Component;
032: import java.awt.Color;
033: import java.awt.Graphics;
034: import java.awt.Image;
035: import java.awt.image.BufferedImage;
036: import java.awt.image.ColorModel;
037: import java.awt.image.ImageProducer;
038: import java.awt.image.ImageConsumer;
039: import java.awt.image.ImageObserver;
040: import sun.awt.image.ImageRepresentation;
041: import sun.awt.image.FileImageSource;
042:
043: public class ToolkitImage extends Image {
044:
045: /**
046: * The object which is used to reconstruct the original image data
047: * as needed.
048: */
049: ImageProducer source;
050:
051: InputStreamImageSource src;
052:
053: ImageRepresentation imagerep;
054:
055: static {
056: /* ensure that the necessary native libraries are loaded */
057: NativeLibLoader.loadLibraries();
058: }
059:
060: protected ToolkitImage() {
061: }
062:
063: /**
064: * Construct an image from an ImageProducer object.
065: */
066: public ToolkitImage(ImageProducer is) {
067: source = is;
068: if (is instanceof InputStreamImageSource) {
069: src = (InputStreamImageSource) is;
070: }
071: }
072:
073: public ImageProducer getSource() {
074: if (src != null) {
075: src.checkSecurity(null, false);
076: }
077: return source;
078: }
079:
080: private int width = -1;
081: private int height = -1;
082: private Hashtable properties;
083:
084: private int availinfo;
085:
086: /**
087: * Return the width of the original image source.
088: * If the width isn't known, then the image is reconstructed.
089: */
090: public int getWidth() {
091: if (src != null) {
092: src.checkSecurity(null, false);
093: }
094: if ((availinfo & ImageObserver.WIDTH) == 0) {
095: reconstruct(ImageObserver.WIDTH);
096: }
097: return width;
098: }
099:
100: /**
101: * Return the width of the original image source.
102: * If the width isn't known, then the ImageObserver object will be
103: * notified when the data is available.
104: */
105: public synchronized int getWidth(ImageObserver iw) {
106: if (src != null) {
107: src.checkSecurity(null, false);
108: }
109: if ((availinfo & ImageObserver.WIDTH) == 0) {
110: addWatcher(iw, true);
111: if ((availinfo & ImageObserver.WIDTH) == 0) {
112: return -1;
113: }
114: }
115: return width;
116: }
117:
118: /**
119: * Return the height of the original image source.
120: * If the height isn't known, then the image is reconstructed.
121: */
122: public int getHeight() {
123: if (src != null) {
124: src.checkSecurity(null, false);
125: }
126: if ((availinfo & ImageObserver.HEIGHT) == 0) {
127: reconstruct(ImageObserver.HEIGHT);
128: }
129: return height;
130: }
131:
132: /**
133: * Return the height of the original image source.
134: * If the height isn't known, then the ImageObserver object will be
135: * notified when the data is available.
136: */
137: public synchronized int getHeight(ImageObserver iw) {
138: if (src != null) {
139: src.checkSecurity(null, false);
140: }
141: if ((availinfo & ImageObserver.HEIGHT) == 0) {
142: addWatcher(iw, true);
143: if ((availinfo & ImageObserver.HEIGHT) == 0) {
144: return -1;
145: }
146: }
147: return height;
148: }
149:
150: /**
151: * Return a property of the image by name. Individual property names
152: * are defined by the various image formats. If a property is not
153: * defined for a particular image, then this method will return the
154: * UndefinedProperty object. If the properties for this image are
155: * not yet known, then this method will return null and the ImageObserver
156: * object will be notified later. The property name "comment" should
157: * be used to store an optional comment which can be presented to
158: * the user as a description of the image, its source, or its author.
159: */
160: public Object getProperty(String name, ImageObserver observer) {
161: if (name == null) {
162: throw new NullPointerException(
163: "null property name is not allowed");
164: }
165:
166: if (src != null) {
167: src.checkSecurity(null, false);
168: }
169: if (properties == null) {
170: addWatcher(observer, true);
171: if (properties == null) {
172: return null;
173: }
174: }
175: Object o = properties.get(name);
176: if (o == null) {
177: o = Image.UndefinedProperty;
178: }
179: return o;
180: }
181:
182: public boolean hasError() {
183: if (src != null) {
184: src.checkSecurity(null, false);
185: }
186: return (availinfo & ImageObserver.ERROR) != 0;
187: }
188:
189: public int check(ImageObserver iw) {
190: if (src != null) {
191: src.checkSecurity(null, false);
192: }
193: if ((availinfo & ImageObserver.ERROR) == 0
194: && ((~availinfo) & (ImageObserver.WIDTH
195: | ImageObserver.HEIGHT | ImageObserver.PROPERTIES)) != 0) {
196: addWatcher(iw, false);
197: }
198: return availinfo;
199: }
200:
201: public void preload(ImageObserver iw) {
202: if (src != null) {
203: src.checkSecurity(null, false);
204: }
205: if ((availinfo & ImageObserver.ALLBITS) == 0) {
206: addWatcher(iw, true);
207: }
208: }
209:
210: private synchronized void addWatcher(ImageObserver iw, boolean load) {
211: if ((availinfo & ImageObserver.ERROR) != 0) {
212: if (iw != null) {
213: iw.imageUpdate(this , ImageObserver.ERROR
214: | ImageObserver.ABORT, -1, -1, -1, -1);
215: }
216: return;
217: }
218: ImageRepresentation ir = getImageRep();
219: ir.addWatcher(iw);
220: if (load) {
221: ir.startProduction();
222: }
223: }
224:
225: private synchronized void reconstruct(int flags) {
226: if ((flags & ~availinfo) != 0) {
227: if ((availinfo & ImageObserver.ERROR) != 0) {
228: return;
229: }
230: ImageRepresentation ir = getImageRep();
231: ir.startProduction();
232: while ((flags & ~availinfo) != 0) {
233: try {
234: wait();
235: } catch (InterruptedException e) {
236: Thread.currentThread().interrupt();
237: return;
238: }
239: if ((availinfo & ImageObserver.ERROR) != 0) {
240: return;
241: }
242: }
243: }
244: }
245:
246: synchronized void addInfo(int newinfo) {
247: availinfo |= newinfo;
248: notifyAll();
249: }
250:
251: void setDimensions(int w, int h) {
252: width = w;
253: height = h;
254: addInfo(ImageObserver.WIDTH | ImageObserver.HEIGHT);
255: }
256:
257: void setProperties(Hashtable props) {
258: if (props == null) {
259: props = new Hashtable();
260: }
261: properties = props;
262: addInfo(ImageObserver.PROPERTIES);
263: }
264:
265: synchronized void infoDone(int status) {
266: if (status == ImageConsumer.IMAGEERROR
267: || ((~availinfo) & (ImageObserver.WIDTH | ImageObserver.HEIGHT)) != 0) {
268: addInfo(ImageObserver.ERROR);
269: } else if ((availinfo & ImageObserver.PROPERTIES) == 0) {
270: setProperties(null);
271: }
272: }
273:
274: public void flush() {
275: if (src != null) {
276: src.checkSecurity(null, false);
277: }
278:
279: ImageRepresentation ir;
280: synchronized (this ) {
281: availinfo &= ~ImageObserver.ERROR;
282: ir = imagerep;
283: imagerep = null;
284: }
285: if (ir != null) {
286: ir.abort();
287: }
288: if (src != null) {
289: src.flush();
290: }
291: }
292:
293: protected ImageRepresentation makeImageRep() {
294: return new ImageRepresentation(this ,
295: ColorModel.getRGBdefault(), false);
296: }
297:
298: public synchronized ImageRepresentation getImageRep() {
299: if (src != null) {
300: src.checkSecurity(null, false);
301: }
302: if (imagerep == null) {
303: imagerep = makeImageRep();
304: }
305: return imagerep;
306: }
307:
308: public Graphics getGraphics() {
309: throw new UnsupportedOperationException(
310: "getGraphics() not valid for images "
311: + "created with createImage(producer)");
312: }
313:
314: /* this method is needed by printing code */
315: public ColorModel getColorModel() {
316: ImageRepresentation imageRep = getImageRep();
317: return imageRep.getColorModel();
318: }
319:
320: /* this method is needed by printing code */
321: public BufferedImage getBufferedImage() {
322: ImageRepresentation imageRep = getImageRep();
323: return imageRep.getBufferedImage();
324: }
325:
326: public void setAccelerationPriority(float priority) {
327: super .setAccelerationPriority(priority);
328: ImageRepresentation imageRep = getImageRep();
329: imageRep.setAccelerationPriority(accelerationPriority);
330: }
331: }
|