1 7 8 17 18 package java.awt.image; 19 20 44 45 public abstract class DataBuffer 46 { 47 48 49 public static final int TYPE_BYTE = 0; 50 51 52 public static final int TYPE_USHORT = 1; 53 54 55 public static final int TYPE_SHORT = 2; 56 57 58 public static final int TYPE_INT = 3; 59 60 61 public static final int TYPE_FLOAT = 4; 62 63 64 public static final int TYPE_DOUBLE = 5; 65 66 67 public static final int TYPE_UNDEFINED = 32; 68 69 70 protected int dataType; 71 72 73 protected int banks; 74 75 76 protected int offset; 77 78 79 protected int size; 80 81 82 protected int offsets[]; 83 84 85 private static final int dataTypeSize[] = {8,16,16,32,32,64}; 86 87 93 public static int getDataTypeSize(int type) { 94 if (type < TYPE_BYTE || type > TYPE_DOUBLE) { 95 throw new IllegalArgumentException ("Unknown data type "+type); 96 } 97 return dataTypeSize[type]; 98 } 99 100 106 protected DataBuffer(int dataType, int size) { 107 this.dataType = dataType; 108 this.banks = 1; 109 this.size = size; 110 this.offset = 0; 111 this.offsets = new int[1]; } 113 114 122 protected DataBuffer(int dataType, int size, int numBanks) { 123 this.dataType = dataType; 124 this.banks = numBanks; 125 this.size = size; 126 this.offset = 0; 127 this.offsets = new int[banks]; } 129 130 139 protected DataBuffer(int dataType, int size, int numBanks, int offset) { 140 this.dataType = dataType; 141 this.banks = numBanks; 142 this.size = size; 143 this.offset = offset; 144 this.offsets = new int[numBanks]; 145 for (int i = 0; i < numBanks; i++) { 146 this.offsets[i] = offset; 147 } 148 } 149 150 163 protected DataBuffer(int dataType, int size, int numBanks, int offsets[]) { 164 if (numBanks != offsets.length) { 165 throw new ArrayIndexOutOfBoundsException ("Number of banks" + 166 " does not match number of bank offsets"); 167 } 168 this.dataType = dataType; 169 this.banks = numBanks; 170 this.size = size; 171 this.offset = offsets[0]; 172 this.offsets = (int[])offsets.clone(); 173 } 174 175 178 public int getDataType() { 179 return dataType; 180 } 181 182 185 public int getSize() { 186 return size; 187 } 188 189 192 public int getOffset() { 193 return offset; 194 } 195 196 199 public int[] getOffsets() { 200 return (int[])offsets.clone(); 201 } 202 203 206 public int getNumBanks() { 207 return banks; 208 } 209 210 218 public int getElem(int i) { 219 return getElem(0,i); 220 } 221 222 232 public abstract int getElem(int bank, int i); 233 234 243 public void setElem(int i, int val) { 244 setElem(0,i,val); 245 } 246 247 257 public abstract void setElem(int bank, int i, int val); 258 259 270 public float getElemFloat(int i) { 271 return (float)getElem(i); 272 } 273 274 287 public float getElemFloat(int bank, int i) { 288 return (float)getElem(bank,i); 289 } 290 291 302 public void setElemFloat(int i, float val) { 303 setElem(i,(int)val); 304 } 305 306 318 public void setElemFloat(int bank, int i, float val) { 319 setElem(bank,i,(int)val); 320 } 321 322 334 public double getElemDouble(int i) { 335 return (double)getElem(i); 336 } 337 338 350 public double getElemDouble(int bank, int i) { 351 return (double)getElem(bank,i); 352 } 353 354 365 public void setElemDouble(int i, double val) { 366 setElem(i,(int)val); 367 } 368 369 381 public void setElemDouble(int bank, int i, double val) { 382 setElem(bank,i,(int)val); 383 } 384 385 static int[] toIntArray(Object obj) { 386 if (obj instanceof int[]) { 387 return (int[])obj; 388 } else if (obj == null) { 389 return null; 390 } else if (obj instanceof short[]) { 391 short sdata[] = (short[])obj; 392 int idata[] = new int[sdata.length]; 393 for (int i = 0; i < sdata.length; i++) { 394 idata[i] = (int)sdata[i] & 0xffff; 395 } 396 return idata; 397 } else if (obj instanceof byte[]) { 398 byte bdata[] = (byte[])obj; 399 int idata[] = new int[bdata.length]; 400 for (int i = 0; i < bdata.length; i++) { 401 idata[i] = 0xff & (int)bdata[i]; 402 } 403 return idata; 404 } 405 return null; 406 } 407 } 408 | Popular Tags |