001: /*
002: * $RCSfile: RasterProxy.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:56:52 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.rmi;
013:
014: import java.awt.Point;
015: import java.awt.Rectangle;
016: import java.awt.image.BandedSampleModel;
017: import java.awt.image.DataBuffer;
018: import java.awt.image.ComponentSampleModel;
019: import java.awt.image.MultiPixelPackedSampleModel;
020: import java.awt.image.PixelInterleavedSampleModel;
021: import java.awt.image.Raster;
022: import java.awt.image.SampleModel;
023: import java.awt.image.SinglePixelPackedSampleModel;
024: import java.awt.image.WritableRaster;
025: import java.io.IOException;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028: import java.io.Serializable;
029: import javax.media.jai.RasterFactory;
030: import javax.media.jai.remote.SerializableState;
031: import javax.media.jai.remote.SerializerFactory;
032:
033: /**
034: * This class is a serializable proxy for a Raster from which the
035: * Raster may be reconstituted.
036: *
037: *
038: * @since EA3
039: */
040: public class RasterProxy implements Serializable {
041: /** The Raster. */
042: private transient Raster raster;
043:
044: /**
045: * Constructs a <code>RasterProxy</code> from a
046: * <code>Raster</code>.
047: *
048: * @param source The <code>Raster</code> to be serialized.
049: */
050: public RasterProxy(Raster source) {
051: raster = source;
052: }
053:
054: /**
055: * Retrieves the associated <code>Raster</code>.
056: * @return The (perhaps reconstructed) <code>Raster</code>.
057: */
058: public Raster getRaster() {
059: return raster;
060: }
061:
062: /**
063: * Serialize the <code>RasterProxy</code>.
064: *
065: * @param out The <code>ObjectOutputStream</code>.
066: */
067: private void writeObject(ObjectOutputStream out) throws IOException {
068: Raster r;
069:
070: if (raster.getParent() != null) {
071: // Use the child ratser to create another Raster
072: // containing data for the requested bounds but which does
073: // not share the SampleModel and DataBuffer of the parent.
074: // Fix : 4631478
075: r = raster.createCompatibleWritableRaster(raster
076: .getBounds());
077: ((WritableRaster) r).setRect(raster);
078: } else {
079: r = raster;
080: }
081:
082: out.writeInt(r.getWidth());
083: out.writeInt(r.getHeight());
084: out.writeObject(SerializerFactory.getState(r.getSampleModel(),
085: null));
086: out.writeObject(SerializerFactory.getState(r.getDataBuffer(),
087: null));
088: out.writeObject(new Point(r.getMinX(), r.getMinY()));
089: }
090:
091: /**
092: * Deserialize the <code>RasterProxy</code>.
093: *
094: * @param out The <code>ObjectInputStream</code>.
095: */
096: private void readObject(ObjectInputStream in) throws IOException,
097: ClassNotFoundException {
098: int width;
099: int height;
100: SerializableState sampleModelState = null;
101: SerializableState dataBufferState = null;
102: Point location = null;
103:
104: width = in.readInt();
105: height = in.readInt();
106: sampleModelState = (SerializableState) in.readObject();
107: dataBufferState = (SerializableState) in.readObject();
108: location = (Point) in.readObject();
109:
110: // Restore the SampleModel from its serialized form.
111: SampleModel sampleModel = (SampleModel) sampleModelState
112: .getObject();
113: if (sampleModel == null) {
114: raster = null;
115: return;
116: }
117:
118: // Restore the DataBuffer from its serialized form.
119: DataBuffer dataBuffer = (DataBuffer) dataBufferState
120: .getObject();
121:
122: // Reconstruct the Raster.
123: raster = Raster.createRaster(sampleModel, dataBuffer, location);
124: }
125: }
|