1 /*
   2  * Copyright (c) 2000, 2017, 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 package java.awt.image;
  27 
  28 import static sun.java2d.StateTrackable.State.*;
  29 
  30 /**
  31  * This class extends {@code DataBuffer} and stores data internally
  32  * in {@code float} form.
  33  * <p>
  34  * <a id="optimizations">
  35  * Note that some implementations may function more efficiently
  36  * if they can maintain control over how the data for an image is
  37  * stored.
  38  * For example, optimizations such as caching an image in video
  39  * memory require that the implementation track all modifications
  40  * to that data.
  41  * Other implementations may operate better if they can store the
  42  * data in locations other than a Java array.
  43  * To maintain optimum compatibility with various optimizations
  44  * it is best to avoid constructors and methods which expose the
  45  * underlying storage as a Java array as noted below in the
  46  * documentation for those methods.
  47  * </a>
  48  *
  49  * @since 1.4
  50  */
  51 
  52 public final class DataBufferFloat extends DataBuffer {
  53 
  54     /** The array of data banks. */
  55     float bankdata[][];
  56 
  57     /** A reference to the default data bank. */
  58     float data[];
  59 
  60     /**
  61      * Constructs a {@code float}-based {@code DataBuffer}
  62      * with a specified size.
  63      *
  64      * @param size The number of elements in the DataBuffer.
  65      */
  66     public DataBufferFloat(int size) {
  67         super(STABLE, TYPE_FLOAT, size);
  68         data = new float[size];
  69         bankdata = new float[1][];
  70         bankdata[0] = data;
  71     }
  72 
  73     /**
  74      * Constructs a {@code float}-based {@code DataBuffer}
  75      * with a specified number of banks, all of which are of a
  76      * specified size.
  77      *
  78      * @param size The number of elements in each bank of the
  79      * {@code DataBuffer}.
  80      * @param numBanks The number of banks in the
  81      *        {@code DataBuffer}.
  82      */
  83     public DataBufferFloat(int size, int numBanks) {
  84         super(STABLE, TYPE_FLOAT, size, numBanks);
  85         bankdata = new float[numBanks][];
  86         for (int i= 0; i < numBanks; i++) {
  87             bankdata[i] = new float[size];
  88         }
  89         data = bankdata[0];
  90     }
  91 
  92     /**
  93      * Constructs a {@code float}-based {@code DataBuffer}
  94      * with the specified data array.  Only the first
  95      * {@code size} elements are available for use by this
  96      * {@code DataBuffer}.  The array must be large enough to
  97      * hold {@code size} elements.
  98      * <p>
  99      * Note that {@code DataBuffer} objects created by this constructor
 100      * may be incompatible with <a href="#optimizations">performance
 101      * optimizations</a> used by some implementations (such as caching
 102      * an associated image in video memory).
 103      *
 104      * @param dataArray An array of {@code float}s to be used as the
 105      *                  first and only bank of this {@code DataBuffer}.
 106      * @param size The number of elements of the array to be used.
 107      */
 108     public DataBufferFloat(float dataArray[], int size) {
 109         super(UNTRACKABLE, TYPE_FLOAT, size);
 110         data = dataArray;
 111         bankdata = new float[1][];
 112         bankdata[0] = data;
 113     }
 114 
 115     /**
 116      * Constructs a {@code float}-based {@code DataBuffer}
 117      * with the specified data array.  Only the elements between
 118      * {@code offset} and {@code offset + size - 1} are
 119      * available for use by this {@code DataBuffer}.  The array
 120      * must be large enough to hold {@code offset + size}
 121      * elements.
 122      * <p>
 123      * Note that {@code DataBuffer} objects created by this constructor
 124      * may be incompatible with <a href="#optimizations">performance
 125      * optimizations</a> used by some implementations (such as caching
 126      * an associated image in video memory).
 127      *
 128      * @param dataArray An array of {@code float}s to be used as the
 129      *                  first and only bank of this {@code DataBuffer}.
 130      * @param size The number of elements of the array to be used.
 131      * @param offset The offset of the first element of the array
 132      *               that will be used.
 133      */
 134     public DataBufferFloat(float dataArray[], int size, int offset) {
 135         super(UNTRACKABLE, TYPE_FLOAT, size, 1, offset);
 136         data = dataArray;
 137         bankdata = new float[1][];
 138         bankdata[0] = data;
 139     }
 140 
 141     /**
 142      * Constructs a {@code float}-based {@code DataBuffer}
 143      * with the specified data arrays.  Only the first
 144      * {@code size} elements of each array are available for use
 145      * by this {@code DataBuffer}.  The number of banks will be
 146      * equal to {@code dataArray.length}.
 147      * <p>
 148      * Note that {@code DataBuffer} objects created by this constructor
 149      * may be incompatible with <a href="#optimizations">performance
 150      * optimizations</a> used by some implementations (such as caching
 151      * an associated image in video memory).
 152      *
 153      * @param dataArray An array of arrays of {@code float}s to be
 154      *                  used as the banks of this {@code DataBuffer}.
 155      * @param size The number of elements of each array to be used.
 156      */
 157     public DataBufferFloat(float dataArray[][], int size) {
 158         super(UNTRACKABLE, TYPE_FLOAT, size, dataArray.length);
 159         bankdata = dataArray.clone();
 160         data = bankdata[0];
 161     }
 162 
 163     /**
 164      * Constructs a {@code float}-based {@code DataBuffer}
 165      * with the specified data arrays, size, and per-bank offsets.
 166      * The number of banks is equal to {@code dataArray.length}.
 167      * Each array must be at least as large as {@code size} plus the
 168      * corresponding offset.  There must be an entry in the offsets
 169      * array for each data array.
 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 An array of arrays of {@code float}s to be
 177      *                  used as the banks of this {@code DataBuffer}.
 178      * @param size The number of elements of each array to be used.
 179      * @param offsets An array of integer offsets, one for each bank.
 180      */
 181     public DataBufferFloat(float dataArray[][], int size, int offsets[]) {
 182         super(UNTRACKABLE, TYPE_FLOAT, size,dataArray.length, offsets);
 183         bankdata = dataArray.clone();
 184         data = bankdata[0];
 185     }
 186 
 187     /**
 188      * Returns the default (first) {@code float} data array.
 189      * <p>
 190      * Note that calling this method may cause this {@code DataBuffer}
 191      * object to be incompatible with <a href="#optimizations">performance
 192      * optimizations</a> used by some implementations (such as caching
 193      * an associated image in video memory).
 194      *
 195      * @return the first float data array.
 196      */
 197     public float[] getData() {
 198         theTrackable.setUntrackable();
 199         return data;
 200     }
 201 
 202     /**
 203      * Returns the data array for the specified bank.
 204      * <p>
 205      * Note that calling this method may cause this {@code DataBuffer}
 206      * object to be incompatible with <a href="#optimizations">performance
 207      * optimizations</a> used by some implementations (such as caching
 208      * an associated image in video memory).
 209      *
 210      * @param bank the data array
 211      * @return the data array specified by {@code bank}.
 212      */
 213     public float[] getData(int bank) {
 214         theTrackable.setUntrackable();
 215         return bankdata[bank];
 216     }
 217 
 218     /**
 219      * Returns the data array for all banks.
 220      * <p>
 221      * Note that calling this method may cause this {@code DataBuffer}
 222      * object to be incompatible with <a href="#optimizations">performance
 223      * optimizations</a> used by some implementations (such as caching
 224      * an associated image in video memory).
 225      *
 226      * @return all data arrays for this data buffer.
 227      */
 228     public float[][] getBankData() {
 229         theTrackable.setUntrackable();
 230         return bankdata.clone();
 231     }
 232 
 233     /**
 234      * Returns the requested data array element from the first
 235      * (default) bank as an {@code int}.
 236      *
 237      * @param i The desired data array element.
 238      *
 239      * @return The data entry as an {@code int}.
 240      * @see #setElem(int, int)
 241      * @see #setElem(int, int, int)
 242      */
 243     public int getElem(int i) {
 244         return (int)(data[i+offset]);
 245     }
 246 
 247     /**
 248      * Returns the requested data array element from the specified
 249      * bank as an {@code int}.
 250      *
 251      * @param bank The bank number.
 252      * @param i The desired data array element.
 253      *
 254      * @return The data entry as an {@code int}.
 255      * @see #setElem(int, int)
 256      * @see #setElem(int, int, int)
 257      */
 258     public int getElem(int bank, int i) {
 259         return (int)(bankdata[bank][i+offsets[bank]]);
 260     }
 261 
 262     /**
 263      * Sets the requested data array element in the first (default)
 264      * bank to the given {@code int}.
 265      *
 266      * @param i The desired data array element.
 267      * @param val The value to be set.
 268      * @see #getElem(int)
 269      * @see #getElem(int, int)
 270      */
 271     public void setElem(int i, int val) {
 272         data[i+offset] = (float)val;
 273         theTrackable.markDirty();
 274     }
 275 
 276     /**
 277      * Sets the requested data array element in the specified bank to
 278      * the given {@code int}.
 279      *
 280      * @param bank The bank number.
 281      * @param i The desired data array element.
 282      * @param val The value to be set.
 283      * @see #getElem(int)
 284      * @see #getElem(int, int)
 285      */
 286     public void setElem(int bank, int i, int val) {
 287         bankdata[bank][i+offsets[bank]] = (float)val;
 288         theTrackable.markDirty();
 289     }
 290 
 291     /**
 292      * Returns the requested data array element from the first
 293      * (default) bank as a {@code float}.
 294      *
 295      * @param i The desired data array element.
 296      *
 297      * @return The data entry as a {@code float}.
 298      * @see #setElemFloat(int, float)
 299      * @see #setElemFloat(int, int, float)
 300      */
 301     public float getElemFloat(int i) {
 302         return data[i+offset];
 303     }
 304 
 305     /**
 306      * Returns the requested data array element from the specified
 307      * bank as a {@code float}.
 308      *
 309      * @param bank The bank number.
 310      * @param i The desired data array element.
 311      *
 312      * @return The data entry as a {@code float}.
 313      * @see #setElemFloat(int, float)
 314      * @see #setElemFloat(int, int, float)
 315      */
 316     public float getElemFloat(int bank, int i) {
 317         return bankdata[bank][i+offsets[bank]];
 318     }
 319 
 320     /**
 321      * Sets the requested data array element in the first (default)
 322      * bank to the given {@code float}.
 323      *
 324      * @param i The desired data array element.
 325      * @param val The value to be set.
 326      * @see #getElemFloat(int)
 327      * @see #getElemFloat(int, int)
 328      */
 329     public void setElemFloat(int i, float val) {
 330         data[i+offset] = val;
 331         theTrackable.markDirty();
 332     }
 333 
 334     /**
 335      * Sets the requested data array element in the specified bank to
 336      * the given {@code float}.
 337      *
 338      * @param bank The bank number.
 339      * @param i The desired data array element.
 340      * @param val The value to be set.
 341      * @see #getElemFloat(int)
 342      * @see #getElemFloat(int, int)
 343      */
 344     public void setElemFloat(int bank, int i, float val) {
 345         bankdata[bank][i+offsets[bank]] = val;
 346         theTrackable.markDirty();
 347     }
 348 
 349     /**
 350      * Returns the requested data array element from the first
 351      * (default) bank as a {@code double}.
 352      *
 353      * @param i The desired data array element.
 354      *
 355      * @return The data entry as a {@code double}.
 356      * @see #setElemDouble(int, double)
 357      * @see #setElemDouble(int, int, double)
 358      */
 359     public double getElemDouble(int i) {
 360         return (double)data[i+offset];
 361     }
 362 
 363     /**
 364      * Returns the requested data array element from the specified
 365      * bank as a {@code double}.
 366      *
 367      * @param bank The bank number.
 368      * @param i The desired data array element.
 369      *
 370      * @return The data entry as a {@code double}.
 371      * @see #setElemDouble(int, double)
 372      * @see #setElemDouble(int, int, double)
 373      */
 374     public double getElemDouble(int bank, int i) {
 375         return (double)bankdata[bank][i+offsets[bank]];
 376     }
 377 
 378     /**
 379      * Sets the requested data array element in the first (default)
 380      * bank to the given {@code double}.
 381      *
 382      * @param i The desired data array element.
 383      * @param val The value to be set.
 384      * @see #getElemDouble(int)
 385      * @see #getElemDouble(int, int)
 386      */
 387     public void setElemDouble(int i, double val) {
 388         data[i+offset] = (float)val;
 389         theTrackable.markDirty();
 390     }
 391 
 392     /**
 393      * Sets the requested data array element in the specified bank to
 394      * the given {@code double}.
 395      *
 396      * @param bank The bank number.
 397      * @param i The desired data array element.
 398      * @param val The value to be set.
 399      * @see #getElemDouble(int)
 400      * @see #getElemDouble(int, int)
 401      */
 402     public void setElemDouble(int bank, int i, double val) {
 403         bankdata[bank][i+offsets[bank]] = (float)val;
 404         theTrackable.markDirty();
 405     }
 406 }