01: /*
02: * $RCSfile: URLRIF.java,v $
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
05: *
06: * Use is subject to license terms.
07: *
08: * $Revision: 1.1 $
09: * $Date: 2005/02/11 04:56:46 $
10: * $State: Exp $
11: */
12: package com.sun.media.jai.opimage;
13:
14: import java.awt.RenderingHints;
15: import java.awt.image.RenderedImage;
16: import java.awt.image.renderable.ParameterBlock;
17: import java.awt.image.renderable.RenderedImageFactory;
18: import java.io.InputStream;
19: import java.io.IOException;
20: import java.net.URL;
21: import javax.media.jai.JAI;
22: import javax.media.jai.OperationRegistry;
23: import javax.media.jai.OpImage;
24: import javax.media.jai.registry.RIFRegistry;
25: import javax.media.jai.util.ImagingListener;
26: import com.sun.media.jai.codec.ImageDecodeParam;
27: import com.sun.media.jai.codec.SeekableStream;
28: import com.sun.media.jai.util.ImageUtil;
29:
30: /**
31: * @see javax.media.jai.operator.URLDescriptor
32: *
33: * @since EA4
34: *
35: */
36: public class URLRIF implements RenderedImageFactory {
37:
38: /** Constructor. */
39: public URLRIF() {
40: }
41:
42: /**
43: * Creates an image from a URL.
44: */
45: public RenderedImage create(ParameterBlock paramBlock,
46: RenderingHints renderHints) {
47: try {
48: // Create a SeekableStream from the URL (first parameter).
49: URL url = (URL) paramBlock.getObjectParameter(0);
50: InputStream stream = url.openStream();
51: SeekableStream src = SeekableStream.wrapInputStream(stream,
52: true);
53:
54: ImageDecodeParam param = null;
55: if (paramBlock.getNumParameters() > 1) {
56: param = (ImageDecodeParam) paramBlock
57: .getObjectParameter(1);
58: }
59:
60: ParameterBlock newParamBlock = new ParameterBlock();
61: newParamBlock.add(src);
62: newParamBlock.add(param);
63:
64: RenderingHints.Key key = JAI.KEY_OPERATION_BOUND;
65: int bound = OpImage.OP_NETWORK_BOUND;
66: if (renderHints == null) {
67: renderHints = new RenderingHints(key,
68: new Integer(bound));
69: } else if (!renderHints.containsKey(key)) {
70: renderHints.put(key, new Integer(bound));
71: }
72:
73: // Get the registry from the hints, if any.
74: // Don't check for null hints as it cannot be null here.
75: OperationRegistry registry = (OperationRegistry) renderHints
76: .get(JAI.KEY_OPERATION_REGISTRY);
77:
78: // Create the image using the most preferred RIF for "stream".
79: RenderedImage image = RIFRegistry.create(registry,
80: "stream", newParamBlock, renderHints);
81:
82: // NB: StreamImage is defined in FileLoadRIF.java.
83: return image == null ? null : new StreamImage(image, src);
84: } catch (IOException e) {
85: ImagingListener listener = ImageUtil
86: .getImagingListener(renderHints);
87: String message = JaiI18N.getString("URLRIF0");
88: listener.errorOccurred(message, e, this , false);
89: // e.printStackTrace();
90: return null;
91: }
92: }
93: }
|