001: /*
002: * $RCSfile: DataBufferUtils.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:57:00 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.util;
013:
014: import java.awt.image.DataBuffer;
015: import java.lang.reflect.Constructor;
016: import java.lang.reflect.Method;
017:
018: /**
019: * Utility class to enable compatibility with Java 2 1.4 real-valued
020: * DataBuffer classes. Factory methods and data array accessors are
021: * defined to use reflection. The core Java classes are given precedence
022: * over their JAI equivalents.
023: */
024: public final class DataBufferUtils {
025:
026: /**
027: * Priority ordered array of DataBufferFloat class names.
028: */
029: private static final String[] FLOAT_CLASS_NAMES = {
030: "java.awt.image.DataBufferFloat",
031: "javax.media.jai.DataBufferFloat",
032: "com.sun.media.jai.codecimpl.util.DataBufferFloat" };
033:
034: /**
035: * Priority ordered array of DataBufferDouble class names.
036: */
037: private static final String[] DOUBLE_CLASS_NAMES = {
038: "java.awt.image.DataBufferDouble",
039: "javax.media.jai.DataBufferDouble",
040: "com.sun.media.jai.codecimpl.util.DataBufferDouble" };
041:
042: /**
043: * Classes to be used for DB float and double.
044: */
045: private static Class floatClass = null;
046: private static Class doubleClass = null;
047:
048: /**
049: * Initialize float and double DB classes.
050: */
051: static {
052: floatClass = getDataBufferClass(DataBuffer.TYPE_FLOAT);
053: doubleClass = getDataBufferClass(DataBuffer.TYPE_DOUBLE);
054: }
055:
056: /**
057: * Return the class for the specified data type.
058: *
059: * @param dataType The data type from among
060: * <code>DataBuffer.TYPE_*</code>.
061: */
062: private static final Class getDataBufferClass(int dataType) {
063: // Set the array of class names.
064: String[] classNames = null;
065: switch (dataType) {
066: case DataBuffer.TYPE_FLOAT:
067: classNames = FLOAT_CLASS_NAMES;
068: break;
069: case DataBuffer.TYPE_DOUBLE:
070: classNames = DOUBLE_CLASS_NAMES;
071: break;
072: default:
073: throw new IllegalArgumentException("dataType == "
074: + dataType + "!");
075: }
076:
077: // Initialize the return value.
078: Class dataBufferClass = null;
079:
080: // Loop over the class names array in priority order.
081: for (int i = 0; i < classNames.length; i++) {
082: try {
083: // Attempt to get the class.
084: dataBufferClass = Class.forName(classNames[i]);
085:
086: // Break if the class was found.
087: if (dataBufferClass != null) {
088: break;
089: }
090: } catch (ClassNotFoundException e) {
091: // Ignore the exception.
092: }
093: }
094:
095: // Throw an exception if no class was found.
096: if (dataBufferClass == null) {
097: throw new RuntimeException(
098: JaiI18N.getString("DataBufferUtils0")
099: + " "
100: + (String) (dataType == DataBuffer.TYPE_FLOAT ? "DataBufferFloat"
101: : "DataBufferDouble"));
102: }
103:
104: return dataBufferClass;
105: }
106:
107: /**
108: * Construct a <code>DataBuffer</code> of the requested type
109: * using the parameters with the specified types and values.
110: */
111: private static final DataBuffer constructDataBuffer(int dataType,
112: Class[] paramTypes, Object[] paramValues) {
113:
114: Class dbClass = null;
115: switch (dataType) {
116: case DataBuffer.TYPE_FLOAT:
117: dbClass = floatClass;
118: break;
119: case DataBuffer.TYPE_DOUBLE:
120: dbClass = doubleClass;
121: break;
122: default:
123: throw new IllegalArgumentException("dataType == "
124: + dataType + "!");
125: }
126:
127: DataBuffer dataBuffer = null;
128: try {
129: Constructor constructor = dbClass
130: .getConstructor(paramTypes);
131: dataBuffer = (DataBuffer) constructor
132: .newInstance(paramValues);
133: } catch (Exception e) {
134: throw new RuntimeException(JaiI18N
135: .getString("DataBufferUtils1"));
136: }
137:
138: return dataBuffer;
139: }
140:
141: /**
142: * Invoke the <code>DataBuffer</code> method of the specified name
143: * using the parameters with the specified types and values.
144: */
145: private static final Object invokeDataBufferMethod(
146: DataBuffer dataBuffer, String methodName,
147: Class[] paramTypes, Object[] paramValues) {
148: if (dataBuffer == null) {
149: throw new IllegalArgumentException("dataBuffer == null!");
150: }
151:
152: Class dbClass = dataBuffer.getClass();
153:
154: Object returnValue = null;
155: try {
156: Method method = dbClass.getMethod(methodName, paramTypes);
157: returnValue = method.invoke(dataBuffer, paramValues);
158: } catch (Exception e) {
159: throw new RuntimeException(JaiI18N
160: .getString("DataBufferUtils2")
161: + " \"" + methodName + "\".");
162: }
163:
164: return returnValue;
165: }
166:
167: public static final DataBuffer createDataBufferFloat(
168: float[][] dataArray, int size) {
169: return constructDataBuffer(DataBuffer.TYPE_FLOAT, new Class[] {
170: float[][].class, int.class }, new Object[] { dataArray,
171: new Integer(size) });
172: }
173:
174: public static final DataBuffer createDataBufferFloat(
175: float[][] dataArray, int size, int[] offsets) {
176: return constructDataBuffer(DataBuffer.TYPE_FLOAT, new Class[] {
177: float[][].class, int.class, int[].class },
178: new Object[] { dataArray, new Integer(size), offsets });
179: }
180:
181: public static final DataBuffer createDataBufferFloat(
182: float[] dataArray, int size) {
183: return constructDataBuffer(DataBuffer.TYPE_FLOAT, new Class[] {
184: float[].class, int.class }, new Object[] { dataArray,
185: new Integer(size) });
186: }
187:
188: public static final DataBuffer createDataBufferFloat(
189: float[] dataArray, int size, int offset) {
190: return constructDataBuffer(DataBuffer.TYPE_FLOAT, new Class[] {
191: float[].class, int.class, int.class }, new Object[] {
192: dataArray, new Integer(size), new Integer(offset) });
193: }
194:
195: public static final DataBuffer createDataBufferFloat(int size) {
196: return constructDataBuffer(DataBuffer.TYPE_FLOAT,
197: new Class[] { int.class }, new Object[] { new Integer(
198: size) });
199: }
200:
201: public static final DataBuffer createDataBufferFloat(int size,
202: int numBanks) {
203: return constructDataBuffer(DataBuffer.TYPE_FLOAT, new Class[] {
204: int.class, int.class }, new Object[] {
205: new Integer(size), new Integer(numBanks) });
206: }
207:
208: public static final float[][] getBankDataFloat(DataBuffer dataBuffer) {
209: return (float[][]) invokeDataBufferMethod(dataBuffer,
210: "getBankData", null, null);
211: }
212:
213: public static final float[] getDataFloat(DataBuffer dataBuffer) {
214: return (float[]) invokeDataBufferMethod(dataBuffer, "getData",
215: null, null);
216: }
217:
218: public static final float[] getDataFloat(DataBuffer dataBuffer,
219: int bank) {
220: return (float[]) invokeDataBufferMethod(dataBuffer, "getData",
221: new Class[] { int.class }, new Object[] { new Integer(
222: bank) });
223: }
224:
225: public static final DataBuffer createDataBufferDouble(
226: double[][] dataArray, int size) {
227: return constructDataBuffer(DataBuffer.TYPE_DOUBLE, new Class[] {
228: double[][].class, int.class }, new Object[] {
229: dataArray, new Integer(size) });
230: }
231:
232: public static final DataBuffer createDataBufferDouble(
233: double[][] dataArray, int size, int[] offsets) {
234: return constructDataBuffer(DataBuffer.TYPE_DOUBLE, new Class[] {
235: double[][].class, int.class, int[].class },
236: new Object[] { dataArray, new Integer(size), offsets });
237: }
238:
239: public static final DataBuffer createDataBufferDouble(
240: double[] dataArray, int size) {
241: return constructDataBuffer(DataBuffer.TYPE_DOUBLE, new Class[] {
242: double[].class, int.class }, new Object[] { dataArray,
243: new Integer(size) });
244: }
245:
246: public static final DataBuffer createDataBufferDouble(
247: double[] dataArray, int size, int offset) {
248: return constructDataBuffer(DataBuffer.TYPE_DOUBLE, new Class[] {
249: double[].class, int.class, int.class }, new Object[] {
250: dataArray, new Integer(size), new Integer(offset) });
251: }
252:
253: public static final DataBuffer createDataBufferDouble(int size) {
254: return constructDataBuffer(DataBuffer.TYPE_DOUBLE,
255: new Class[] { int.class }, new Object[] { new Integer(
256: size) });
257: }
258:
259: public static final DataBuffer createDataBufferDouble(int size,
260: int numBanks) {
261: return constructDataBuffer(DataBuffer.TYPE_DOUBLE, new Class[] {
262: int.class, int.class }, new Object[] {
263: new Integer(size), new Integer(numBanks) });
264: }
265:
266: public static final double[][] getBankDataDouble(
267: DataBuffer dataBuffer) {
268: return (double[][]) invokeDataBufferMethod(dataBuffer,
269: "getBankData", null, null);
270: }
271:
272: public static final double[] getDataDouble(DataBuffer dataBuffer) {
273: return (double[]) invokeDataBufferMethod(dataBuffer, "getData",
274: null, null);
275: }
276:
277: public static final double[] getDataDouble(DataBuffer dataBuffer,
278: int bank) {
279: return (double[]) invokeDataBufferMethod(dataBuffer, "getData",
280: new Class[] { int.class }, new Object[] { new Integer(
281: bank) });
282: }
283: }
|