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