KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DataBufferByte.java 1.17 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 bytes.
22  * Values stored in the byte array(s) of this <CODE>DataBuffer</CODE> are treated as
23  * unsigned values.
24  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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