1 |
769 |
jeremybenn |
/* Buffers.java --
|
2 |
|
|
Copyright (C) 2000, 2002, 2004 Free Software Foundation
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.java.awt;
|
40 |
|
|
|
41 |
|
|
import java.awt.image.DataBuffer;
|
42 |
|
|
import java.awt.image.DataBufferByte;
|
43 |
|
|
import java.awt.image.DataBufferDouble;
|
44 |
|
|
import java.awt.image.DataBufferFloat;
|
45 |
|
|
import java.awt.image.DataBufferInt;
|
46 |
|
|
import java.awt.image.DataBufferShort;
|
47 |
|
|
import java.awt.image.DataBufferUShort;
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* Utility class for creating and accessing data buffers of arbitrary
|
51 |
|
|
* data types.
|
52 |
|
|
*/
|
53 |
|
|
public final class Buffers
|
54 |
|
|
{
|
55 |
|
|
/**
|
56 |
|
|
* Create a data buffer of a particular type.
|
57 |
|
|
*
|
58 |
|
|
* @param dataType the desired data type of the buffer.
|
59 |
|
|
* @param data an array containing data, or null
|
60 |
|
|
* @param size the size of the data buffer bank
|
61 |
|
|
*/
|
62 |
|
|
public static DataBuffer createBuffer(int dataType, Object data,
|
63 |
|
|
int size)
|
64 |
|
|
{
|
65 |
|
|
if (data == null) return createBuffer(dataType, size, 1);
|
66 |
|
|
|
67 |
|
|
return createBufferFromData(dataType, data, size);
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
/**
|
72 |
|
|
* Create a data buffer of a particular type.
|
73 |
|
|
*
|
74 |
|
|
* @param dataType the desired data type of the buffer.
|
75 |
|
|
* @param size the size of the data buffer bank
|
76 |
|
|
*/
|
77 |
|
|
public static DataBuffer createBuffer(int dataType, int size) {
|
78 |
|
|
return createBuffer(dataType, size, 1);
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
|
|
* Create a data buffer of a particular type.
|
83 |
|
|
*
|
84 |
|
|
* @param dataType the desired data type of the buffer.
|
85 |
|
|
* @param size the size of the data buffer bank
|
86 |
|
|
* @param numBanks the number of banks the buffer should have
|
87 |
|
|
*/
|
88 |
|
|
public static DataBuffer createBuffer(int dataType, int size, int numBanks)
|
89 |
|
|
{
|
90 |
|
|
switch (dataType)
|
91 |
|
|
{
|
92 |
|
|
case DataBuffer.TYPE_BYTE:
|
93 |
|
|
return new DataBufferByte(size, numBanks);
|
94 |
|
|
case DataBuffer.TYPE_SHORT:
|
95 |
|
|
return new DataBufferShort(size, numBanks);
|
96 |
|
|
case DataBuffer.TYPE_USHORT:
|
97 |
|
|
return new DataBufferUShort(size, numBanks);
|
98 |
|
|
case DataBuffer.TYPE_INT:
|
99 |
|
|
return new DataBufferInt(size, numBanks);
|
100 |
|
|
case DataBuffer.TYPE_FLOAT:
|
101 |
|
|
return new DataBufferFloat(size, numBanks);
|
102 |
|
|
case DataBuffer.TYPE_DOUBLE:
|
103 |
|
|
return new DataBufferDouble(size, numBanks);
|
104 |
|
|
default:
|
105 |
|
|
throw new UnsupportedOperationException();
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* Create a data buffer of a particular type.
|
111 |
|
|
*
|
112 |
|
|
* @param dataType the desired data type of the buffer
|
113 |
|
|
* @param data an array containing the data
|
114 |
|
|
* @param size the size of the data buffer bank
|
115 |
|
|
*/
|
116 |
|
|
public static DataBuffer createBufferFromData(int dataType, Object data,
|
117 |
|
|
int size)
|
118 |
|
|
{
|
119 |
|
|
switch (dataType)
|
120 |
|
|
{
|
121 |
|
|
case DataBuffer.TYPE_BYTE:
|
122 |
|
|
return new DataBufferByte((byte[]) data, size);
|
123 |
|
|
case DataBuffer.TYPE_SHORT:
|
124 |
|
|
return new DataBufferShort((short[]) data, size);
|
125 |
|
|
case DataBuffer.TYPE_USHORT:
|
126 |
|
|
return new DataBufferUShort((short[]) data, size);
|
127 |
|
|
case DataBuffer.TYPE_INT:
|
128 |
|
|
return new DataBufferInt((int[]) data, size);
|
129 |
|
|
case DataBuffer.TYPE_FLOAT:
|
130 |
|
|
return new DataBufferFloat((float[]) data, size);
|
131 |
|
|
case DataBuffer.TYPE_DOUBLE:
|
132 |
|
|
return new DataBufferDouble((double[]) data, size);
|
133 |
|
|
default:
|
134 |
|
|
throw new UnsupportedOperationException();
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
/**
|
139 |
|
|
* Return the data array of a data buffer, regardless of the data
|
140 |
|
|
* type.
|
141 |
|
|
*
|
142 |
|
|
* @return an array of primitive values. The actual array type
|
143 |
|
|
* depends on the data type of the buffer.
|
144 |
|
|
*/
|
145 |
|
|
public static Object getData(DataBuffer buffer)
|
146 |
|
|
{
|
147 |
|
|
return getData(buffer, 0, null, 0, buffer.getSize());
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
/**
|
152 |
|
|
* Copy data from array contained in data buffer, much like
|
153 |
|
|
* System.arraycopy. Create a suitable destination array if the
|
154 |
|
|
* given destination array is null.
|
155 |
|
|
*/
|
156 |
|
|
public static Object getData(DataBuffer src, int srcOffset,
|
157 |
|
|
Object dest, int dstOffset,
|
158 |
|
|
int length)
|
159 |
|
|
{
|
160 |
|
|
Object from;
|
161 |
|
|
switch(src.getDataType())
|
162 |
|
|
{
|
163 |
|
|
case DataBuffer.TYPE_BYTE:
|
164 |
|
|
if (dest == null) dest = new byte[length+dstOffset];
|
165 |
|
|
for(int i = 0; i < length; i++)
|
166 |
|
|
((byte[])dest)[i + dstOffset] = (byte)src.getElem(i + srcOffset);
|
167 |
|
|
break;
|
168 |
|
|
|
169 |
|
|
case DataBuffer.TYPE_DOUBLE:
|
170 |
|
|
if (dest == null) dest = new double[length+dstOffset];
|
171 |
|
|
for(int i = 0; i < length; i++)
|
172 |
|
|
((double[])dest)[i + dstOffset] = src.getElemDouble(i + srcOffset);
|
173 |
|
|
break;
|
174 |
|
|
|
175 |
|
|
case DataBuffer.TYPE_FLOAT:
|
176 |
|
|
if (dest == null) dest = new float[length+dstOffset];
|
177 |
|
|
for(int i = 0; i < length; i++)
|
178 |
|
|
((float[])dest)[i + dstOffset] = src.getElemFloat(i + srcOffset);
|
179 |
|
|
break;
|
180 |
|
|
|
181 |
|
|
case DataBuffer.TYPE_INT:
|
182 |
|
|
if (dest == null) dest = new int[length+dstOffset];
|
183 |
|
|
for(int i = 0; i < length; i++)
|
184 |
|
|
((int[])dest)[i + dstOffset] = src.getElem(i + srcOffset);
|
185 |
|
|
break;
|
186 |
|
|
|
187 |
|
|
case DataBuffer.TYPE_SHORT:
|
188 |
|
|
case DataBuffer.TYPE_USHORT:
|
189 |
|
|
if (dest == null) dest = new short[length+dstOffset];
|
190 |
|
|
for(int i = 0; i < length; i++)
|
191 |
|
|
((short[])dest)[i + dstOffset] = (short)src.getElem(i + srcOffset);
|
192 |
|
|
break;
|
193 |
|
|
|
194 |
|
|
case DataBuffer.TYPE_UNDEFINED:
|
195 |
|
|
throw new ClassCastException("Unknown data buffer type");
|
196 |
|
|
}
|
197 |
|
|
return dest;
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
/**
|
201 |
|
|
* @param bits the width of a data element measured in bits
|
202 |
|
|
*
|
203 |
|
|
* @return the smallest data type that can store data elements of
|
204 |
|
|
* the given number of bits, without any truncation.
|
205 |
|
|
*/
|
206 |
|
|
public static int smallestAppropriateTransferType(int bits)
|
207 |
|
|
{
|
208 |
|
|
if (bits <= 8)
|
209 |
|
|
{
|
210 |
|
|
return DataBuffer.TYPE_BYTE;
|
211 |
|
|
}
|
212 |
|
|
else if (bits <= 16)
|
213 |
|
|
{
|
214 |
|
|
return DataBuffer.TYPE_USHORT;
|
215 |
|
|
}
|
216 |
|
|
else if (bits <= 32)
|
217 |
|
|
{
|
218 |
|
|
return DataBuffer.TYPE_INT;
|
219 |
|
|
}
|
220 |
|
|
else
|
221 |
|
|
{
|
222 |
|
|
return DataBuffer.TYPE_UNDEFINED;
|
223 |
|
|
}
|
224 |
|
|
}
|
225 |
|
|
}
|