KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > image > DataBufferShort


1 /*
2  * @(#)DataBufferShort.java 1.19 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /* ****************************************************************
9  ******************************************************************
10  ******************************************************************
11  *** COPYRIGHT (c) Eastman Kodak Company, 1997
12  *** As an unpublished work pursuant to Title 17 of the United
13  *** States Code. All rights reserved.
14  ******************************************************************
15  ******************************************************************
16  ******************************************************************/

17
18 package java.awt.image;
19
20 /**
21  * This class extends <CODE>DataBuffer</CODE> and stores data internally as shorts.
22  */

23
24
25 public final class DataBufferShort extends DataBuffer JavaDoc
26 {
27     /** The default data bank. */
28     short data[];
29
30     /** All data banks */
31     short bankdata[][];
32
33     /**
34      * Constructs a short-based <CODE>DataBuffer</CODE> with a single bank and the
35      * specified size.
36      *
37      * @param size The size of the <CODE>DataBuffer</CODE>.
38      */

39     public DataBufferShort(int size) {
40         super(TYPE_SHORT,size);
41         data = new short[size];
42         bankdata = new short[1][];
43         bankdata[0] = data;
44     }
45
46     /**
47      * Constructs a short-based <CODE>DataBuffer</CODE> with the specified number of
48      * banks all of which are the specified size.
49      *
50      * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
51      * @param numBanks The number of banks in the a<CODE>DataBuffer</CODE>.
52      */

53     public DataBufferShort(int size, int numBanks) {
54         super(TYPE_SHORT,size,numBanks);
55         bankdata = new short[numBanks][];
56         for (int i= 0; i < numBanks; i++) {
57             bankdata[i] = new short[size];
58         }
59         data = bankdata[0];
60     }
61
62     /**
63      * Constructs a short-based <CODE>DataBuffer</CODE> with a single bank using the
64      * specified array.
65      * Only the first <CODE>size</CODE> elements should be used by accessors of
66      * this <CODE>DataBuffer</CODE>. <CODE>dataArray</CODE> must be large enough to
67      * hold <CODE>size</CODE> elements.
68      *
69      * @param dataArray The short array for the <CODE>DataBuffer</CODE>.
70      * @param size The size of the <CODE>DataBuffer</CODE> bank.
71      */

72     public DataBufferShort(short dataArray[], int size) {
73         super(TYPE_SHORT,size);
74         data = dataArray;
75         bankdata = new short[1][];
76         bankdata[0] = data;
77     }
78
79     /**
80      * Constructs a short-based <CODE>DataBuffer</CODE> with a single bank using the
81      * specified array, size, and offset. <CODE>dataArray</CODE> must have at least
82      * <CODE>offset</CODE> + <CODE>size</CODE> elements. Only elements <CODE>offset</CODE>
83      * through <CODE>offset</CODE> + <CODE>size</CODE> - 1
84      * should be used by accessors of this <CODE>DataBuffer</CODE>.
85      *
86      * @param dataArray The short array for the <CODE>DataBuffer</CODE>.
87      * @param size The size of the <CODE>DataBuffer</CODE> bank.
88      * @param offset The offset into the <CODE>dataArray</CODE>.
89      */

90     public DataBufferShort(short dataArray[], int size, int offset) {
91         super(TYPE_SHORT,size,1,offset);
92         data = dataArray;
93         bankdata = new short[1][];
94         bankdata[0] = data;
95     }
96
97     /**
98      * Constructs a short-based <CODE>DataBuffer</CODE> with the specified arrays.
99      * The number of banks will be equal to <CODE>dataArray.length</CODE>.
100      * Only the first <CODE>size</CODE> elements of each array should be used by
101      * accessors of this <CODE>DataBuffer</CODE>.
102      *
103      * @param dataArray The short arrays for the <CODE>DataBuffer</CODE>.
104      * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
105      */

106     public DataBufferShort(short dataArray[][], int size) {
107         super(TYPE_SHORT,size,dataArray.length);
108         bankdata = (short[][]) dataArray.clone();
109         data = bankdata[0];
110     }
111
112     /**
113      * Constructs a short-based <CODE>DataBuffer</CODE> with the specified arrays, size,
114      * and offsets.
115      * The number of banks is equal to <CODE>dataArray.length</CODE>. Each array must
116      * be at least as large as <CODE>size</CODE> + the corresponding offset. There must
117      * be an entry in the offset array for each <CODE>dataArray</CODE> entry. For each
118      * bank, only elements <CODE>offset</CODE> through
119      * <CODE>offset</CODE> + <CODE>size</CODE> - 1 should be
120      * used by accessors of this <CODE>DataBuffer</CODE>.
121      *
122      * @param dataArray The short arrays for the <CODE>DataBuffer</CODE>.
123      * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
124      * @param offsets The offsets into each array.
125      */

126     public DataBufferShort(short dataArray[][], int size, int offsets[]) {
127         super(TYPE_SHORT,size,dataArray.length,offsets);
128         bankdata = (short[][]) dataArray.clone();
129         data = bankdata[0];
130     }
131
132     /**
133      * Returns the default (first) byte data array.
134      *
135      * @return The first short data array.
136      */

137     public short[] getData() {
138         return data;
139     }
140
141     /**
142      * Returns the data array for the specified bank.
143      *
144      * @param bank The bank whose data array you want to get.
145      * @return The data array for the specified bank.
146      */

147     public short[] getData(int bank) {
148         return bankdata[bank];
149     }
150
151     /**
152      * Returns the data arrays for all banks.
153      * @return All of the data arrays.
154      */

155     public short[][] getBankData() {
156         return (short[][]) bankdata.clone();
157     }
158
159     /**
160      * Returns the requested data array element from the first (default) bank.
161      *
162      * @param i The data array element you want to get.
163      * @return The requested data array element as an integer.
164      * @see #setElem(int, int)
165      * @see #setElem(int, int, int)
166      */

167     public int getElem(int i) {
168         return (int)(data[i+offset]);
169     }
170
171     /**
172      * Returns the requested data array element from the specified bank.
173      *
174      * @param bank The bank from which you want to get a data array element.
175      * @param i The data array element you want to get.
176      * @return The requested data array element as an integer.
177      * @see #setElem(int, int)
178      * @see #setElem(int, int, int)
179      */

180     public int getElem(int bank, int i) {
181         return (int)(bankdata[bank][i+offsets[bank]]);
182     }
183
184     /**
185      * Sets the requested data array element in the first (default) bank
186      * to the specified value.
187      *
188      * @param i The data array element you want to set.
189      * @param val The integer value to which you want to set the data array element.
190      * @see #getElem(int)
191      * @see #getElem(int, int)
192      */

193     public void setElem(int i, int val) {
194         data[i+offset] = (short)val;
195     }
196
197     /**
198      * Sets the requested data array element in the specified bank
199      * from the given integer.
200      * @param bank The bank in which you want to set the data array element.
201      * @param i The data array element you want to set.
202      * @param val The integer value to which you want to set the specified data array element.
203      * @see #getElem(int)
204      * @see #getElem(int, int)
205      */

206     public void setElem(int bank, int i, int val) {
207         bankdata[bank][i+offsets[bank]] = (short)val;
208     }
209 }
210
Popular Tags