source: trunk/gcc/libjava/gnu/java/awt/Buffers.java

Last change on this file was 2, checked in by bird, 22 years ago

Initial revision

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