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