01: /*
02: * $RCSfile: EncodeRIF.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:24 $
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.OutputStream;
19: import java.io.IOException;
20: import javax.media.jai.RenderedOp;
21: import javax.media.jai.util.ImagingListener;
22: import com.sun.media.jai.codec.ImageCodec;
23: import com.sun.media.jai.codec.ImageEncodeParam;
24: import com.sun.media.jai.codec.ImageEncoder;
25: import com.sun.media.jai.util.ImageUtil;
26:
27: /**
28: * @see javax.media.jai.operator.FileDescriptor
29: *
30: * @since EA4
31: *
32: */
33: public class EncodeRIF implements RenderedImageFactory {
34:
35: /** Constructor. */
36: public EncodeRIF() {
37: }
38:
39: /**
40: * Stores an image to a stream.
41: */
42: public RenderedImage create(ParameterBlock paramBlock,
43: RenderingHints renderHints) {
44:
45: ImagingListener listener = ImageUtil
46: .getImagingListener(renderHints);
47:
48: // Retrieve the OutputStream.
49: OutputStream stream = (OutputStream) paramBlock
50: .getObjectParameter(0);
51:
52: // Retrieve the format.
53: String format = (String) paramBlock.getObjectParameter(1);
54:
55: // Retrieve the ImageEncodeParam (which may be null).
56: ImageEncodeParam param = null;
57: if (paramBlock.getNumParameters() > 2) {
58: param = (ImageEncodeParam) paramBlock.getObjectParameter(2);
59: }
60:
61: // Create an ImageEncoder.
62: ImageEncoder encoder = ImageCodec.createImageEncoder(format,
63: stream, param);
64:
65: // Check the ImageEncoder.
66: if (encoder == null) {
67: throw new RuntimeException(JaiI18N.getString("EncodeRIF0"));
68: }
69:
70: // Store the data.
71: RenderedImage im = (RenderedImage) paramBlock.getSource(0);
72: try {
73: encoder.encode(im);
74: stream.flush();
75: // Fix 4665208: EncodeRIF closed the stream after flush
76: // User may put more into the stream
77: //stream.close();
78: } catch (IOException e) {
79: String message = JaiI18N.getString("EncodeRIF1") + " "
80: + format;
81: listener.errorOccurred(message, e, this , false);
82: // e.printStackTrace();
83: return null;
84: }
85:
86: return im;
87: }
88: }
|