001/* Copyright (C) 2000, 2002 Free Software Foundation 002 003This file is part of GNU Classpath. 004 005GNU Classpath is free software; you can redistribute it and/or modify 006it under the terms of the GNU General Public License as published by 007the Free Software Foundation; either version 2, or (at your option) 008any later version. 009 010GNU Classpath is distributed in the hope that it will be useful, but 011WITHOUT ANY WARRANTY; without even the implied warranty of 012MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013General Public License for more details. 014 015You should have received a copy of the GNU General Public License 016along with GNU Classpath; see the file COPYING. If not, write to the 017Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01802110-1301 USA. 019 020Linking this library statically or dynamically with other modules is 021making a combined work based on this library. Thus, the terms and 022conditions of the GNU General Public License cover the whole 023combination. 024 025As a special exception, the copyright holders of this library give you 026permission to link this library with independent modules to produce an 027executable, regardless of the license terms of these independent 028modules, and to copy and distribute the resulting executable under 029terms of your choice, provided that you also meet, for each linked 030independent module, the terms and conditions of the license of that 031module. An independent module is a module which is not derived from 032or based on this library. If you modify this library, you may extend 033this exception to your version of the library, but you are not 034obligated to do so. If you do not wish to do so, delete this 035exception statement from your version. */ 036 037package java.awt.image; 038 039/* This is one of several classes that are nearly identical. Maybe we 040 should have a central template and generate all these files. This 041 is one of the cases where templates or macros would have been 042 useful to have in Java. 043 044 This file has been created using search-replace. My only fear is 045 that these classes will grow out-of-sync as of a result of changes 046 that are not propagated to the other files. As always, mirroring 047 code is a maintenance nightmare. */ 048 049/** 050 * A {@link DataBuffer} that uses an array of <code>byte</code> primitives 051 * to represent each of its banks. 052 * 053 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no) 054 */ 055public final class DataBufferByte extends DataBuffer 056{ 057 private byte[] data; 058 private byte[][] bankData; 059 060 /** 061 * Creates a new data buffer with a single data bank containing the 062 * specified number of <code>byte</code> elements. 063 * 064 * @param size the number of elements in the data bank. 065 */ 066 public DataBufferByte(int size) 067 { 068 super(TYPE_BYTE, size, 1, 0); 069 bankData = new byte[1][]; 070 data = new byte[size]; 071 bankData[0] = data; 072 } 073 074 /** 075 * Creates a new data buffer with the specified number of data banks, 076 * each containing the specified number of <code>byte</code> elements. 077 * 078 * @param size the number of elements in the data bank. 079 * @param numBanks the number of data banks. 080 */ 081 public DataBufferByte(int size, int numBanks) 082 { 083 super(TYPE_BYTE, size, numBanks); 084 bankData = new byte[numBanks][size]; 085 data = bankData[0]; 086 } 087 088 /** 089 * Creates a new data buffer backed by the specified data bank. 090 * <p> 091 * Note: there is no exception when <code>dataArray</code> is 092 * <code>null</code>, but in that case an exception will be thrown 093 * later if you attempt to access the data buffer. 094 * 095 * @param dataArray the data bank. 096 * @param size the number of elements in the data bank. 097 */ 098 public DataBufferByte(byte[] dataArray, int size) 099 { 100 super(TYPE_BYTE, size, 1, 0); 101 bankData = new byte[1][]; 102 data = dataArray; 103 bankData[0] = data; 104 } 105 106 /** 107 * Creates a new data buffer backed by the specified data bank, with 108 * the specified offset to the first element. 109 * <p> 110 * Note: there is no exception when <code>dataArray</code> is 111 * <code>null</code>, but in that case an exception will be thrown 112 * later if you attempt to access the data buffer. 113 * 114 * @param dataArray the data bank. 115 * @param size the number of elements in the data bank. 116 * @param offset the offset to the first element in the array. 117 */ 118 public DataBufferByte(byte[] dataArray, int size, int offset) 119 { 120 super(TYPE_BYTE, size, 1, offset); 121 bankData = new byte[1][]; 122 data = dataArray; 123 bankData[0] = data; 124 } 125 126 /** 127 * Creates a new data buffer backed by the specified data banks. 128 * 129 * @param dataArray the data banks. 130 * @param size the number of elements in the data bank. 131 * 132 * @throws NullPointerException if <code>dataArray</code> is 133 * <code>null</code>. 134 */ 135 public DataBufferByte(byte[][] dataArray, int size) 136 { 137 super(TYPE_BYTE, size, dataArray.length); 138 bankData = dataArray; 139 data = bankData[0]; 140 } 141 142 /** 143 * Creates a new data buffer backed by the specified data banks, with 144 * the specified offsets to the first element in each bank. 145 * 146 * @param dataArray the data banks. 147 * @param size the number of elements in the data bank. 148 * @param offsets the offsets to the first element in each data bank. 149 * 150 * @throws NullPointerException if <code>dataArray</code> is 151 * <code>null</code>. 152 */ 153 public DataBufferByte(byte[][] dataArray, int size, int[] offsets) 154 { 155 super(TYPE_BYTE, size, dataArray.length, offsets); 156 bankData = dataArray; 157 data = bankData[0]; 158 } 159 160 /** 161 * Returns the first data bank. 162 * 163 * @return The first data bank. 164 */ 165 public byte[] getData() 166 { 167 return data; 168 } 169 170 /** 171 * Returns a data bank. 172 * 173 * @param bank the bank index. 174 * @return A data bank. 175 */ 176 public byte[] getData(int bank) 177 { 178 return bankData[bank]; 179 } 180 181 /** 182 * Returns the array underlying this <code>DataBuffer</code>. 183 * 184 * @return The data banks. 185 */ 186 public byte[][] getBankData() 187 { 188 return bankData; 189 } 190 191 /** 192 * Returns an element from the first data bank. The offset (specified in 193 * the constructor) is added to <code>i</code> before accessing the 194 * underlying data array. 195 * 196 * @param i the element index. 197 * @return The element. 198 */ 199 public int getElem(int i) 200 { 201 return data[i+offset] & 0xff; // get unsigned byte as int 202 } 203 204 /** 205 * Returns an element from a particular data bank. The offset (specified in 206 * the constructor) is added to <code>i</code> before accessing the 207 * underlying data array. 208 * 209 * @param bank the bank index. 210 * @param i the element index. 211 * @return The element. 212 */ 213 public int getElem(int bank, int i) 214 { 215 // get unsigned byte as int 216 return bankData[bank][i+offsets[bank]] & 0xff; 217 } 218 219 /** 220 * Sets an element in the first data bank. The offset (specified in the 221 * constructor) is added to <code>i</code> before updating the underlying 222 * data array. 223 * 224 * @param i the element index. 225 * @param val the new element value. 226 */ 227 public void setElem(int i, int val) 228 { 229 data[i+offset] = (byte) val; 230 } 231 232 /** 233 * Sets an element in a particular data bank. The offset (specified in the 234 * constructor) is added to <code>i</code> before updating the underlying 235 * data array. 236 * 237 * @param bank the data bank index. 238 * @param i the element index. 239 * @param val the new element value. 240 */ 241 public void setElem(int bank, int i, int val) 242 { 243 bankData[bank][i+offsets[bank]] = (byte) val; 244 } 245}