View Javadoc
1   /*
2    * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  /* ****************************************************************
27   ******************************************************************
28   ******************************************************************
29   *** COPYRIGHT (c) Eastman Kodak Company, 1997
30   *** As  an unpublished  work pursuant to Title 17 of the United
31   *** States Code.  All rights reserved.
32   ******************************************************************
33   ******************************************************************
34   ******************************************************************/
35  
36  package java.awt.image;
37  
38  import static sun.java2d.StateTrackable.State.*;
39  
40  /**
41   * This class extends <CODE>DataBuffer</CODE> and stores data internally
42   * as integers.
43   * <p>
44   * <a name="optimizations">
45   * Note that some implementations may function more efficiently
46   * if they can maintain control over how the data for an image is
47   * stored.
48   * For example, optimizations such as caching an image in video
49   * memory require that the implementation track all modifications
50   * to that data.
51   * Other implementations may operate better if they can store the
52   * data in locations other than a Java array.
53   * To maintain optimum compatibility with various optimizations
54   * it is best to avoid constructors and methods which expose the
55   * underlying storage as a Java array as noted below in the
56   * documentation for those methods.
57   * </a>
58   */
59  public final class DataBufferInt extends DataBuffer
60  {
61      /** The default data bank. */
62      int data[];
63  
64      /** All data banks */
65      int bankdata[][];
66  
67      /**
68       * Constructs an integer-based <CODE>DataBuffer</CODE> with a single bank
69       * and the specified size.
70       *
71       * @param size The size of the <CODE>DataBuffer</CODE>.
72       */
73      public DataBufferInt(int size) {
74          super(STABLE, TYPE_INT, size);
75          data = new int[size];
76          bankdata = new int[1][];
77          bankdata[0] = data;
78      }
79  
80      /**
81       * Constructs an integer-based <CODE>DataBuffer</CODE> with the specified number of
82       * banks, all of which are the specified size.
83       *
84       * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
85       * @param numBanks The number of banks in the a<CODE>DataBuffer</CODE>.
86       */
87      public DataBufferInt(int size, int numBanks) {
88          super(STABLE, TYPE_INT, size, numBanks);
89          bankdata = new int[numBanks][];
90          for (int i= 0; i < numBanks; i++) {
91              bankdata[i] = new int[size];
92          }
93          data = bankdata[0];
94      }
95  
96      /**
97       * Constructs an integer-based <CODE>DataBuffer</CODE> with a single bank using the
98       * specified array.
99       * Only the first <CODE>size</CODE> elements should be used by accessors of
100      * this <CODE>DataBuffer</CODE>.  <CODE>dataArray</CODE> must be large enough to
101      * hold <CODE>size</CODE> elements.
102      * <p>
103      * Note that {@code DataBuffer} objects created by this constructor
104      * may be incompatible with <a href="#optimizations">performance
105      * optimizations</a> used by some implementations (such as caching
106      * an associated image in video memory).
107      *
108      * @param dataArray The integer array for the <CODE>DataBuffer</CODE>.
109      * @param size The size of the <CODE>DataBuffer</CODE> bank.
110      */
111     public DataBufferInt(int dataArray[], int size) {
112         super(UNTRACKABLE, TYPE_INT, size);
113         data = dataArray;
114         bankdata = new int[1][];
115         bankdata[0] = data;
116     }
117 
118     /**
119      * Constructs an integer-based <CODE>DataBuffer</CODE> with a single bank using the
120      * specified array, size, and offset.  <CODE>dataArray</CODE> must have at least
121      * <CODE>offset</CODE> + <CODE>size</CODE> elements.  Only elements <CODE>offset</CODE>
122      * through <CODE>offset</CODE> + <CODE>size</CODE> - 1
123      * should be used by accessors of this <CODE>DataBuffer</CODE>.
124      * <p>
125      * Note that {@code DataBuffer} objects created by this constructor
126      * may be incompatible with <a href="#optimizations">performance
127      * optimizations</a> used by some implementations (such as caching
128      * an associated image in video memory).
129      *
130      * @param dataArray The integer array for the <CODE>DataBuffer</CODE>.
131      * @param size The size of the <CODE>DataBuffer</CODE> bank.
132      * @param offset The offset into the <CODE>dataArray</CODE>.
133      */
134     public DataBufferInt(int dataArray[], int size, int offset) {
135         super(UNTRACKABLE, TYPE_INT, size, 1, offset);
136         data = dataArray;
137         bankdata = new int[1][];
138         bankdata[0] = data;
139     }
140 
141     /**
142      * Constructs an integer-based <CODE>DataBuffer</CODE> with the specified arrays.
143      * The number of banks will be equal to <CODE>dataArray.length</CODE>.
144      * Only the first <CODE>size</CODE> elements of each array should be used by
145      * accessors of this <CODE>DataBuffer</CODE>.
146      * <p>
147      * Note that {@code DataBuffer} objects created by this constructor
148      * may be incompatible with <a href="#optimizations">performance
149      * optimizations</a> used by some implementations (such as caching
150      * an associated image in video memory).
151      *
152      * @param dataArray The integer arrays for the <CODE>DataBuffer</CODE>.
153      * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
154      */
155     public DataBufferInt(int dataArray[][], int size) {
156         super(UNTRACKABLE, TYPE_INT, size, dataArray.length);
157         bankdata = (int [][]) dataArray.clone();
158         data = bankdata[0];
159     }
160 
161     /**
162      * Constructs an integer-based <CODE>DataBuffer</CODE> with the specified arrays, size,
163      * and offsets.
164      * The number of banks is equal to <CODE>dataArray.length</CODE>.  Each array must
165      * be at least as large as <CODE>size</CODE> + the corresponding offset.   There must
166      * be an entry in the offset array for each <CODE>dataArray</CODE> entry.  For each
167      * bank, only elements <CODE>offset</CODE> through
168      * <CODE>offset</CODE> + <CODE>size</CODE> - 1 should be
169      * used by accessors of this <CODE>DataBuffer</CODE>.
170      * <p>
171      * Note that {@code DataBuffer} objects created by this constructor
172      * may be incompatible with <a href="#optimizations">performance
173      * optimizations</a> used by some implementations (such as caching
174      * an associated image in video memory).
175      *
176      * @param dataArray The integer arrays for the <CODE>DataBuffer</CODE>.
177      * @param size The size of the banks in the <CODE>DataBuffer</CODE>.
178      * @param offsets The offsets into each array.
179      */
180     public DataBufferInt(int dataArray[][], int size, int offsets[]) {
181         super(UNTRACKABLE, TYPE_INT, size, dataArray.length, offsets);
182         bankdata = (int [][]) dataArray.clone();
183         data = bankdata[0];
184     }
185 
186     /**
187      * Returns the default (first) int data array in <CODE>DataBuffer</CODE>.
188      * <p>
189      * Note that calling this method may cause this {@code DataBuffer}
190      * object to be incompatible with <a href="#optimizations">performance
191      * optimizations</a> used by some implementations (such as caching
192      * an associated image in video memory).
193      *
194      * @return The first integer data array.
195      */
196     public int[] getData() {
197         theTrackable.setUntrackable();
198         return data;
199     }
200 
201     /**
202      * Returns the data array for the specified bank.
203      * <p>
204      * Note that calling this method may cause this {@code DataBuffer}
205      * object to be incompatible with <a href="#optimizations">performance
206      * optimizations</a> used by some implementations (such as caching
207      * an associated image in video memory).
208      *
209      * @param bank The bank whose data array you want to get.
210      * @return The data array for the specified bank.
211      */
212     public int[] getData(int bank) {
213         theTrackable.setUntrackable();
214         return bankdata[bank];
215     }
216 
217     /**
218      * Returns the data arrays for all banks.
219      * <p>
220      * Note that calling this method may cause this {@code DataBuffer}
221      * object to be incompatible with <a href="#optimizations">performance
222      * optimizations</a> used by some implementations (such as caching
223      * an associated image in video memory).
224      *
225      * @return All of the data arrays.
226      */
227     public int[][] getBankData() {
228         theTrackable.setUntrackable();
229         return (int [][]) bankdata.clone();
230     }
231 
232     /**
233      * Returns the requested data array element from the first (default) bank.
234      *
235      * @param i The data array element you want to get.
236      * @return The requested data array element as an integer.
237      * @see #setElem(int, int)
238      * @see #setElem(int, int, int)
239      */
240     public int getElem(int i) {
241         return data[i+offset];
242     }
243 
244     /**
245      * Returns the requested data array element from the specified bank.
246      *
247      * @param bank The bank from which you want to get a data array element.
248      * @param i The data array element you want to get.
249      * @return The requested data array element as an integer.
250      * @see #setElem(int, int)
251      * @see #setElem(int, int, int)
252      */
253     public int getElem(int bank, int i) {
254         return bankdata[bank][i+offsets[bank]];
255     }
256 
257     /**
258      * Sets the requested data array element in the first (default) bank
259      * to the specified value.
260      *
261      * @param i The data array element you want to set.
262      * @param val The integer value to which you want to set the data array element.
263      * @see #getElem(int)
264      * @see #getElem(int, int)
265      */
266     public void setElem(int i, int val) {
267         data[i+offset] = val;
268         theTrackable.markDirty();
269     }
270 
271     /**
272      * Sets the requested data array element in the specified bank
273      * to the integer value <CODE>i</CODE>.
274      * @param bank The bank in which you want to set the data array element.
275      * @param i The data array element you want to set.
276      * @param val The integer value to which you want to set the specified data array element.
277      * @see #getElem(int)
278      * @see #getElem(int, int)
279      */
280     public void setElem(int bank, int i, int val) {
281         bankdata[bank][i+offsets[bank]] = (int)val;
282         theTrackable.markDirty();
283     }
284 }