001: /*
002: * $RCSfile: SampleModelProxy.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:54 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.rmi;
013:
014: import java.awt.image.BandedSampleModel;
015: import java.awt.image.ComponentSampleModel;
016: import java.awt.image.DataBuffer;
017: import java.awt.image.MultiPixelPackedSampleModel;
018: import java.awt.image.PixelInterleavedSampleModel;
019: import java.awt.image.SampleModel;
020: import java.awt.image.SinglePixelPackedSampleModel;
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.io.Serializable;
025: import javax.media.jai.ComponentSampleModelJAI;
026: import javax.media.jai.RasterFactory;
027:
028: /**
029: * This class is a serializable proxy for a SampleModel from which the
030: * SampleModel may be reconstituted.
031: *
032: *
033: * @since EA3
034: */
035: public class SampleModelProxy implements Serializable {
036: /** Flag indicating a BandedSampleModel. */
037: private static final int TYPE_BANDED = 1;
038:
039: /** Flag indicating a PixelInterleavedSampleModel. */
040: private static final int TYPE_PIXEL_INTERLEAVED = 2;
041:
042: /** Flag indicating a SinglePixelPackedSampleModel. */
043: private static final int TYPE_SINGLE_PIXEL_PACKED = 3;
044:
045: /** Flag indicating a MultiPixelPackedSampleModel. */
046: private static final int TYPE_MULTI_PIXEL_PACKED = 4;
047:
048: /** Flag indicating a ComponentSampleModelJAI. */
049: private static final int TYPE_COMPONENT_JAI = 5;
050:
051: /** Flag indicating a generic ComponentSampleModel. */
052: private static final int TYPE_COMPONENT = 6;
053:
054: /** The SampleModel. */
055: private transient SampleModel sampleModel;
056:
057: /*
058: parameter banded ileaved packed1 packedN
059: --------- ------ ------- ------- -------
060: dataType * * * *
061: width * * * *
062: height * * * *
063: numBands *
064: bankIndices *
065: bandOffsets * *
066: pixelStride *
067: scanlineStride * * *
068: bitMasks *
069: numberOfBits *
070: dataBitOffset *
071: */
072:
073: /**
074: * Constructs a <code>SampleModelProxy</code> from a
075: * <code>SampleModel</code>.
076: *
077: * @param source The <code>SampleModel</code> to be serialized.
078: */
079: public SampleModelProxy(SampleModel source) {
080: sampleModel = source;
081: }
082:
083: /**
084: * Retrieves the associated <code>SampleModel</code>.
085: * @return The (perhaps reconstructed) <code>SampleModel</code>.
086: */
087: public SampleModel getSampleModel() {
088: return sampleModel;
089: }
090:
091: /**
092: * Serialize the <code>SampleModelProxy</code>.
093: *
094: * @param out The <code>ObjectOutputStream</code>.
095: */
096: private void writeObject(ObjectOutputStream out) throws IOException {
097: if (sampleModel instanceof ComponentSampleModel) {
098: ComponentSampleModel sm = (ComponentSampleModel) sampleModel;
099: int sampleModelType = TYPE_COMPONENT;
100: int transferType = sm.getTransferType();
101: if (sampleModel instanceof PixelInterleavedSampleModel) {
102: sampleModelType = TYPE_PIXEL_INTERLEAVED;
103: } else if (sampleModel instanceof BandedSampleModel) {
104: sampleModelType = TYPE_BANDED;
105: } else if (sampleModel instanceof ComponentSampleModelJAI
106: || transferType == DataBuffer.TYPE_FLOAT
107: || transferType == DataBuffer.TYPE_DOUBLE) {
108: sampleModelType = TYPE_COMPONENT_JAI;
109: }
110: out.writeInt(sampleModelType);
111: out.writeInt(transferType);
112: out.writeInt(sm.getWidth());
113: out.writeInt(sm.getHeight());
114: if (sampleModelType != TYPE_BANDED) {
115: out.writeInt(sm.getPixelStride());
116: }
117: out.writeInt(sm.getScanlineStride());
118: if (sampleModelType != TYPE_PIXEL_INTERLEAVED) {
119: out.writeObject(sm.getBankIndices());
120: }
121: out.writeObject(sm.getBandOffsets());
122: } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
123: SinglePixelPackedSampleModel sm = (SinglePixelPackedSampleModel) sampleModel;
124: out.writeInt(TYPE_SINGLE_PIXEL_PACKED);
125: out.writeInt(sm.getTransferType());
126: out.writeInt(sm.getWidth());
127: out.writeInt(sm.getHeight());
128: out.writeInt(sm.getScanlineStride());
129: out.writeObject(sm.getBitMasks());
130: } else if (sampleModel instanceof MultiPixelPackedSampleModel) {
131: MultiPixelPackedSampleModel sm = (MultiPixelPackedSampleModel) sampleModel;
132: out.writeInt(TYPE_MULTI_PIXEL_PACKED);
133: out.writeInt(sm.getTransferType());
134: out.writeInt(sm.getWidth());
135: out.writeInt(sm.getHeight());
136: out.writeInt(sm.getPixelBitStride());
137: out.writeInt(sm.getScanlineStride());
138: out.writeInt(sm.getDataBitOffset());
139: } else {
140: throw new RuntimeException(JaiI18N
141: .getString("SampleModelProxy0"));
142: }
143: }
144:
145: /**
146: * Deserialize the <code>SampleModelProxy</code>.
147: *
148: * @param out The <code>ObjectInputStream</code>.
149: */
150: private void readObject(ObjectInputStream in) throws IOException,
151: ClassNotFoundException {
152: int sampleModelType = (int) in.readInt();
153: switch (sampleModelType) {
154: case TYPE_PIXEL_INTERLEAVED:
155: sampleModel = RasterFactory
156: .createPixelInterleavedSampleModel(in.readInt(), in
157: .readInt(), in.readInt(), in.readInt(), in
158: .readInt(), (int[]) in.readObject());
159: break;
160: case TYPE_BANDED:
161: sampleModel = RasterFactory.createBandedSampleModel(in
162: .readInt(), in.readInt(), in.readInt(), in
163: .readInt(), (int[]) in.readObject(), (int[]) in
164: .readObject());
165: break;
166: case TYPE_COMPONENT_JAI:
167: sampleModel = new ComponentSampleModelJAI(in.readInt(), in
168: .readInt(), in.readInt(), in.readInt(), in
169: .readInt(), (int[]) in.readObject(), (int[]) in
170: .readObject());
171: break;
172: case TYPE_COMPONENT:
173: sampleModel = new ComponentSampleModel(in.readInt(), in
174: .readInt(), in.readInt(), in.readInt(), in
175: .readInt(), (int[]) in.readObject(), (int[]) in
176: .readObject());
177: break;
178: case TYPE_SINGLE_PIXEL_PACKED:
179: sampleModel = new SinglePixelPackedSampleModel(
180: in.readInt(), in.readInt(), in.readInt(), in
181: .readInt(), (int[]) in.readObject());
182: break;
183: case TYPE_MULTI_PIXEL_PACKED:
184: sampleModel = new MultiPixelPackedSampleModel(in.readInt(),
185: in.readInt(), in.readInt(), in.readInt(), in
186: .readInt(), in.readInt());
187: break;
188: default:
189: throw new RuntimeException(JaiI18N
190: .getString("SampleModelProxy0"));
191: }
192: }
193: }
|