001: /*
002: * $RCSfile: RasterState.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.RenderingHints;
017: import java.awt.image.DataBuffer;
018: import java.awt.image.Raster;
019: import java.awt.image.SampleModel;
020: import java.awt.image.WritableRaster;
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import javax.media.jai.remote.SerializableState;
025: import javax.media.jai.remote.SerializerFactory;
026:
027: /**
028: * This class is a serializable proxy for a Raster from which the
029: * Raster may be reconstituted.
030: *
031: * - modified from RasterProxy.
032: *
033: * @since 1.1
034: */
035: public class RasterState extends SerializableStateImpl {
036:
037: /** The supported classes */
038: public static Class[] getSupportedClasses() {
039: return new Class[] { Raster.class, WritableRaster.class };
040: }
041:
042: /** Support subclasses as Raster is a factory class. */
043: public static boolean permitsSubclasses() {
044: return true;
045: }
046:
047: /**
048: * Constructs a <code>RasterState</code> from a
049: * <code>Raster</code>.
050: *
051: * @param c The <code>Raster</code> subclass.
052: * @param o The <code>Raster</code> object to be serialized.
053: * @param h The <code>RenderingHints</code> (ignored).
054: */
055: public RasterState(Class c, Object o, RenderingHints h) {
056: super (c, o, h);
057: }
058:
059: /**
060: * Serialize the <code>RasterState</code>.
061: *
062: * @param out The <code>ObjectOutputStream</code>.
063: */
064: private void writeObject(ObjectOutputStream out) throws IOException {
065: Raster raster = (Raster) theObject;
066: Raster r;
067:
068: if (raster.getParent() != null) {
069: // Use the child ratser to create another Raster
070: // containing data for the requested bounds but which does
071: // not share the SampleModel and DataBuffer of the parent.
072: // Fix : 4631478
073: r = raster.createCompatibleWritableRaster(raster
074: .getBounds());
075: ((WritableRaster) r).setRect(raster);
076: } else {
077: r = raster;
078: }
079:
080: out.writeInt(r.getWidth());
081: out.writeInt(r.getHeight());
082: out.writeObject(SerializerFactory.getState(r.getSampleModel(),
083: null));
084: out.writeObject(SerializerFactory.getState(r.getDataBuffer(),
085: null));
086: out.writeObject(new Point(r.getMinX(), r.getMinY()));
087: }
088:
089: /**
090: * Deserialize the <code>RasterState</code>.
091: *
092: * @param out The <code>ObjectInputStream</code>.
093: */
094: private void readObject(ObjectInputStream in) throws IOException,
095: ClassNotFoundException {
096: int width;
097: int height;
098: SerializableState sampleModelState = null;
099: SerializableState dataBufferState = null;
100: Point location = null;
101:
102: width = in.readInt();
103: height = in.readInt();
104: sampleModelState = (SerializableState) in.readObject();
105: dataBufferState = (SerializableState) in.readObject();
106: location = (Point) in.readObject();
107:
108: // Restore the SampleModel from its serialized form.
109: SampleModel sampleModel = (SampleModel) sampleModelState
110: .getObject();
111: if (sampleModel == null) {
112: theObject = null;
113: return;
114: }
115:
116: // Restore the DataBuffer from its serialized form.
117: DataBuffer dataBuffer = (DataBuffer) dataBufferState
118: .getObject();
119:
120: // Reconstruct the Raster.
121: theObject = Raster.createRaster(sampleModel, dataBuffer,
122: location);
123: }
124: }
|