SampleModel.java 34.1 KB
Newer Older
1
/* Copyright (C) 2000, 2001, 2002, 2005, 2006,  Free Software Foundation
Tom Tromey committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package java.awt.image;

/**
40 41 42
 * A <code>SampleModel</code> is used to access pixel data from a 
 * {@link DataBuffer}.  This is used by the {@link Raster} class.
 * 
Tom Tromey committed
43 44 45 46 47 48 49 50 51 52
 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
 */
public abstract class SampleModel
{
  /** Width of image described. */
  protected int width;
  
  /** Height of image described. */
  protected int height;
  
53 54
  /** Number of bands in the image described.  Package-private here,
      shadowed by ComponentSampleModel. */
Tom Tromey committed
55 56 57 58 59 60 61 62
  protected int numBands;

  /** 
   * The DataBuffer type that is used to store the data of the image
   * described.
   */
  protected int dataType;

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  /**
   * Creates a new sample model with the specified attributes.
   * 
   * @param dataType  the data type (one of {@link DataBuffer#TYPE_BYTE},
   *   {@link DataBuffer#TYPE_USHORT}, {@link DataBuffer#TYPE_SHORT},
   *   {@link DataBuffer#TYPE_INT}, {@link DataBuffer#TYPE_FLOAT}, 
   *   {@link DataBuffer#TYPE_DOUBLE} or {@link DataBuffer#TYPE_UNDEFINED}).
   * @param w  the width in pixels (must be greater than zero).
   * @param h  the height in pixels (must be greater than zero).
   * @param numBands  the number of bands (must be greater than zero).
   * 
   * @throws IllegalArgumentException if <code>dataType</code> is not one of 
   *   the listed values.
   * @throws IllegalArgumentException if <code>w</code> is less than or equal
   *   to zero.
   * @throws IllegalArgumentException if <code>h</code> is less than or equal 
   *   to zero.
   * @throws IllegalArgumentException if <code>w * h</code> is greater than
   *   {@link Integer#MAX_VALUE}.
   */
Tom Tromey committed
83 84
  public SampleModel(int dataType, int w, int h, int numBands)
  {
85 86 87 88
    if (dataType != DataBuffer.TYPE_UNDEFINED)
      if (dataType < DataBuffer.TYPE_BYTE || dataType > DataBuffer.TYPE_DOUBLE)
        throw new IllegalArgumentException("Unrecognised 'dataType' argument.");
    
Tom Tromey committed
89 90
    if ((w <= 0) || (h <= 0)) 
      throw new IllegalArgumentException((w <= 0 ? " width<=0" : " width is ok")
91 92 93 94 95 96 97 98 99
          + (h <= 0 ? " height<=0" : " height is ok"));
        
    long area = (long) w * (long) h;
    if (area > Integer.MAX_VALUE)
      throw new IllegalArgumentException("w * h exceeds Integer.MAX_VALUE.");

    if (numBands <= 0)
      throw new IllegalArgumentException("Requires numBands > 0.");
      
Tom Tromey committed
100 101 102 103 104 105
    this.dataType = dataType;
    this.width = w;
    this.height = h;
    this.numBands = numBands;  
  }
  
106 107 108 109 110 111 112 113
  /**
   * Returns the width of the pixel data accessible via this 
   * <code>SampleModel</code>.
   * 
   * @return The width.
   * 
   * @see #getHeight()
   */
Tom Tromey committed
114 115 116 117 118
  public final int getWidth()
  {
    return width;
  }

119 120 121 122 123 124 125 126
  /**
   * Returns the height of the pixel data accessible via this 
   * <code>SampleModel</code>.
   * 
   * @return The height.
   * 
   * @see #getWidth()
   */
Tom Tromey committed
127 128 129 130 131
  public final int getHeight()
  {
    return height;
  }

132 133 134 135 136
  /**
   * Returns the number of bands for this <code>SampleModel</code>.
   * 
   * @return The number of bands.
   */
Tom Tromey committed
137 138 139 140 141 142 143
  public final int getNumBands()
  {
    return numBands;
  }
    
  public abstract int getNumDataElements();
  
144 145 146 147 148 149
  /**
   * Returns the type of the {@link DataBuffer} that this 
   * <code>SampleModel</code> accesses.
   * 
   * @return The data buffer type.
   */
Tom Tromey committed
150 151 152 153 154 155 156 157 158 159 160
  public final int getDataType()
  {
    return dataType;
  }

  public int getTransferType()
  {
    // FIXME: Is this a reasonable default implementation?
    return dataType;
  }

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  /**
   * Returns an array containing the samples for the pixel at (x, y) in the
   * specified data buffer.  If <code>iArray</code> is not <code>null</code>,
   * it will be populated with the sample values and returned as the result of
   * this function (this avoids allocating a new array instance).
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param iArray  an array to populate with the sample values and return as 
   *     the result (if <code>null</code>, a new array will be allocated).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The pixel sample values.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
177 178
  public int[] getPixel(int x, int y, int[] iArray, DataBuffer data)
  {
179 180 181 182
    if (iArray == null) 
      iArray = new int[numBands];
    for (int b = 0; b < numBands; b++) 
      iArray[b] = getSample(x, y, b, data);
Tom Tromey committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    return iArray;
  }
  
  /**
   *
   * This method is provided as a faster alternative to getPixel(),
   * that can be used when there is no need to decode the pixel into
   * separate sample values.
   *
   * @param obj An array to return the pixel data in. If null, an
   * array of the right type and size will be created.
   *
   * @return A single pixel as an array object of a primitive type,
   * based on the transfer type. Eg. if transfer type is
   * DataBuffer.TYPE_USHORT, then a short[] object is returned.
   */
  public abstract Object getDataElements(int x, int y, Object obj,
200
                                         DataBuffer data);
Tom Tromey committed
201 202 203

    
  public Object getDataElements(int x, int y, int w, int h, Object obj,
204
                                DataBuffer data)
Tom Tromey committed
205
  {
206
    int size = w * h;
Tom Tromey committed
207
    int numDataElements = getNumDataElements();
208
    int dataSize = numDataElements * size;
Tom Tromey committed
209 210 211
    
    if (obj == null)
      {
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        switch (getTransferType())
          {
          case DataBuffer.TYPE_BYTE:
            obj = new byte[dataSize];
            break;
          case DataBuffer.TYPE_USHORT:
            obj = new short[dataSize];
            break;
          case DataBuffer.TYPE_INT:
            obj = new int[dataSize];
            break;
          default:
            // Seems like the only sensible thing to do.
            throw new ClassCastException();
          }
Tom Tromey committed
227 228 229
      }
    Object pixelData = null;
    int outOffset = 0;
230
    for (int yy = y; yy < (y + h); yy++)
Tom Tromey committed
231
      {
232 233 234 235 236 237 238
        for (int xx = x; xx < (x + w); xx++)
          {
            pixelData = getDataElements(xx, yy, pixelData, data);
            System.arraycopy(pixelData, 0, obj, outOffset,
                             numDataElements);
            outOffset += numDataElements;
          }
Tom Tromey committed
239 240 241 242 243
      }
    return obj;
  }

  public abstract void setDataElements(int x, int y, Object obj,
244
                                       DataBuffer data);
Tom Tromey committed
245 246

  public void setDataElements(int x, int y, int w, int h,
247
                              Object obj, DataBuffer data)
Tom Tromey committed
248 249 250 251 252 253 254
  {
    int numDataElements = getNumDataElements();
    
    Object pixelData;
    switch (getTransferType())
      {
      case DataBuffer.TYPE_BYTE:
255 256
        pixelData = new byte[numDataElements];
        break;
Tom Tromey committed
257
      case DataBuffer.TYPE_USHORT:
258
      case DataBuffer.TYPE_SHORT:
259 260
        pixelData = new short[numDataElements];
        break;
Tom Tromey committed
261
      case DataBuffer.TYPE_INT:
262 263
        pixelData = new int[numDataElements];
        break;
264 265 266 267 268 269
      case DataBuffer.TYPE_FLOAT:
        pixelData = new float[numDataElements];
        break;
      case DataBuffer.TYPE_DOUBLE:
        pixelData = new double[numDataElements];
        break;
Tom Tromey committed
270
      default:
271 272
        // The RI silently igores invalid types.
        pixelData = null;
Tom Tromey committed
273 274
      }

275 276
    int inOffset = 0;
    if (pixelData != null)
Tom Tromey committed
277
      {
278
        for (int yy=y; yy<(y+h); yy++)
279
          {
280 281 282 283 284 285
            for (int xx=x; xx<(x+w); xx++)
              {
                System.arraycopy(obj, inOffset, pixelData, 0, numDataElements);
                setDataElements(xx, yy, pixelData, data);
                inOffset += numDataElements;
              }
286
          }
Tom Tromey committed
287 288 289
      }
  }

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  /**
   * Returns an array containing the samples for the pixel at (x, y) in the
   * specified data buffer.  If <code>fArray</code> is not <code>null</code>,
   * it will be populated with the sample values and returned as the result of
   * this function (this avoids allocating a new array instance).
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param fArray  an array to populate with the sample values and return as 
   *     the result (if <code>null</code>, a new array will be allocated).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The pixel sample values.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
306 307
  public float[] getPixel(int x, int y, float[] fArray, DataBuffer data)
  {
308 309
    if (fArray == null) 
      fArray = new float[numBands];
Tom Tromey committed
310
    
311
    for (int b = 0; b < numBands; b++)
Tom Tromey committed
312 313 314 315 316 317
      {
        fArray[b] = getSampleFloat(x, y, b, data);
      }
    return fArray;
  }

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  /**
   * Returns an array containing the samples for the pixel at (x, y) in the
   * specified data buffer.  If <code>dArray</code> is not <code>null</code>,
   * it will be populated with the sample values and returned as the result of
   * this function (this avoids allocating a new array instance).
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param dArray  an array to populate with the sample values and return as 
   *     the result (if <code>null</code>, a new array will be allocated).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The pixel sample values.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
334
  public double[] getPixel(int x, int y, double[] dArray, DataBuffer data) {
335 336 337
    if (dArray == null) 
      dArray = new double[numBands];
    for (int b = 0; b < numBands; b++)
Tom Tromey committed
338
      {
339
        dArray[b] = getSampleDouble(x, y, b, data);
Tom Tromey committed
340 341 342 343
      }
    return dArray;
  }

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  /**
   * Returns an array containing the samples for the pixels in the region 
   * specified by (x, y, w, h) in the specified data buffer.  The array is
   * ordered by pixels (that is, all the samples for the first pixel are 
   * grouped together, followed by all the samples for the second pixel, and so
   * on).  If <code>iArray</code> is not <code>null</code>, it will be 
   * populated with the sample values and returned as the result of this 
   * function (this avoids allocating a new array instance).
   * 
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param iArray  an array to populate with the sample values and return as 
   *     the result (if <code>null</code>, a new array will be allocated).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The pixel sample values.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
365
  public int[] getPixels(int x, int y, int w, int h, int[] iArray,
366
                         DataBuffer data)
Tom Tromey committed
367
  {
368
    int size = w * h;
Tom Tromey committed
369 370
    int outOffset = 0;
    int[] pixel = null;
371 372 373
    if (iArray == null) 
      iArray = new int[w * h * numBands];
    for (int yy = y; yy < (y + h); yy++)
Tom Tromey committed
374
      {
375 376 377 378 379 380
        for (int xx = x; xx < (x + w); xx++)
          {
            pixel = getPixel(xx, yy, pixel, data);
            System.arraycopy(pixel, 0, iArray, outOffset, numBands);
            outOffset += numBands;
          }
Tom Tromey committed
381 382 383 384
      }
    return iArray;
  }

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
  /**
   * Returns an array containing the samples for the pixels in the region 
   * specified by (x, y, w, h) in the specified data buffer.  The array is
   * ordered by pixels (that is, all the samples for the first pixel are 
   * grouped together, followed by all the samples for the second pixel, and so
   * on).  If <code>fArray</code> is not <code>null</code>, it will be 
   * populated with the sample values and returned as the result of this 
   * function (this avoids allocating a new array instance).
   * 
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param fArray  an array to populate with the sample values and return as 
   *     the result (if <code>null</code>, a new array will be allocated).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The pixel sample values.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
406
  public float[] getPixels(int x, int y, int w, int h, float[] fArray,
407
                           DataBuffer data)
Tom Tromey committed
408
  {
409
    int size = w * h;
Tom Tromey committed
410 411
    int outOffset = 0;
    float[] pixel = null;
412 413
    if (fArray == null) fArray = new float[w * h * numBands];
    for (int yy = y; yy < (y + h); yy++)
Tom Tromey committed
414
      {
415 416 417 418 419 420
        for (int xx = x; xx < (x + w); xx++)
          {
            pixel = getPixel(xx, yy, pixel, data);
            System.arraycopy(pixel, 0, fArray, outOffset, numBands);
            outOffset += numBands;
          }
Tom Tromey committed
421 422 423 424
      }
    return fArray;
  }
    
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
  /**
   * Returns an array containing the samples for the pixels in the region 
   * specified by (x, y, w, h) in the specified data buffer.  The array is
   * ordered by pixels (that is, all the samples for the first pixel are 
   * grouped together, followed by all the samples for the second pixel, and so
   * on).  If <code>dArray</code> is not <code>null</code>, it will be 
   * populated with the sample values and returned as the result of this 
   * function (this avoids allocating a new array instance).
   * 
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param dArray  an array to populate with the sample values and return as 
   *     the result (if <code>null</code>, a new array will be allocated).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The pixel sample values.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
446
  public double[] getPixels(int x, int y, int w, int h, double[] dArray,
447
                            DataBuffer data)
Tom Tromey committed
448
  {
449
    int size = w * h;
Tom Tromey committed
450 451
    int outOffset = 0;
    double[] pixel = null;
452 453 454
    if (dArray == null) 
      dArray = new double[w * h * numBands];
    for (int yy = y; yy < (y + h); yy++)
Tom Tromey committed
455
      {
456 457 458 459 460 461
        for (int xx = x; xx < (x + w); xx++)
          {
            pixel = getPixel(xx, yy, pixel, data);
            System.arraycopy(pixel, 0, dArray, outOffset, numBands);
            outOffset += numBands;
          }
Tom Tromey committed
462 463 464 465
      }
    return dArray;
  }

466 467 468 469 470 471 472 473 474 475 476 477 478 479
  /**
   * Returns the sample value for the pixel at (x, y) in the specified data 
   * buffer.
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param b  the band (in the range <code>0</code> to 
   *     <code>getNumBands() - 1</code>).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The sample value.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
480 481
  public abstract int getSample(int x, int y, int b, DataBuffer data);

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  /**
   * Returns the sample value for the pixel at (x, y) in the specified data 
   * buffer.
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param b  the band (in the range <code>0</code> to 
   *     <code>getNumBands() - 1</code>).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The sample value.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   * 
   * @see #getSample(int, int, int, DataBuffer)
   */
Tom Tromey committed
498 499 500 501 502
  public float getSampleFloat(int x, int y, int b, DataBuffer data)
  {
    return getSample(x, y, b, data);
  }

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
  /**
   * Returns the sample value for the pixel at (x, y) in the specified data 
   * buffer.
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param b  the band (in the range <code>0</code> to 
   *     <code>getNumBands() - 1</code>).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The sample value.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   * 
   * @see #getSample(int, int, int, DataBuffer)
   */
Tom Tromey committed
519 520 521 522 523
  public double getSampleDouble(int x, int y, int b, DataBuffer data)
  {
    return getSampleFloat(x, y, b, data);
  }

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
  /**
   * Returns an array containing the samples from one band for the pixels in 
   * the region specified by (x, y, w, h) in the specified data buffer.  If 
   * <code>iArray</code> is not <code>null</code>, it will be 
   * populated with the sample values and returned as the result of this 
   * function (this avoids allocating a new array instance).
   * 
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param b  the band (in the range <code>0</code> to 
   *     </code>getNumBands() - 1</code>).
   * @param iArray  an array to populate with the sample values and return as 
   *     the result (if <code>null</code>, a new array will be allocated).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The sample values.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
545
  public int[] getSamples(int x, int y, int w, int h, int b,
546
                          int[] iArray, DataBuffer data)
Tom Tromey committed
547
  {
548
    int size = w * h;
Tom Tromey committed
549
    int outOffset = 0;
550 551 552
    if (iArray == null) 
      iArray = new int[size];
    for (int yy = y; yy < (y + h); yy++)
Tom Tromey committed
553
      {
554 555 556 557
        for (int xx = x; xx < (x + w); xx++)
          {
            iArray[outOffset++] = getSample(xx, yy, b, data);
          }
Tom Tromey committed
558 559 560 561
      }
    return iArray;
  }

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
  /**
   * Returns an array containing the samples from one band for the pixels in 
   * the region specified by (x, y, w, h) in the specified data buffer.  If 
   * <code>fArray</code> is not <code>null</code>, it will be 
   * populated with the sample values and returned as the result of this 
   * function (this avoids allocating a new array instance).
   * 
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param b  the band (in the range <code>0</code> to 
   *     </code>getNumBands() - 1</code>).
   * @param fArray  an array to populate with the sample values and return as 
   *     the result (if <code>null</code>, a new array will be allocated).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The sample values.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
583
  public float[] getSamples(int x, int y, int w, int h, int b,
584
                            float[] fArray, DataBuffer data)
Tom Tromey committed
585
  {
586
    int size = w * h;
Tom Tromey committed
587
    int outOffset = 0;
588 589 590
    if (fArray == null) 
      fArray = new float[size];
    for (int yy = y; yy < (y + h); yy++)
Tom Tromey committed
591
      {
592 593 594 595
        for (int xx = x; xx < (x + w); xx++)
          {
            fArray[outOffset++] = getSampleFloat(xx, yy, b, data);
          }
Tom Tromey committed
596 597 598 599
      }
    return fArray;
  }

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
  /**
   * Returns an array containing the samples from one band for the pixels in 
   * the region specified by (x, y, w, h) in the specified data buffer.  If 
   * <code>dArray</code> is not <code>null</code>, it will be 
   * populated with the sample values and returned as the result of this 
   * function (this avoids allocating a new array instance).
   * 
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param b  the band (in the range <code>0</code> to 
   *     </code>getNumBands() - 1</code>).
   * @param dArray  an array to populate with the sample values and return as 
   *     the result (if <code>null</code>, a new array will be allocated).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @return The sample values.
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
621
  public double[] getSamples(int x, int y, int w, int h, int b,
622
                             double[] dArray, DataBuffer data)
Tom Tromey committed
623
  {
624
    int size = w * h;
Tom Tromey committed
625
    int outOffset = 0;
626 627 628
    if (dArray == null) 
      dArray = new double[size];
    for (int yy = y; yy < (y + h); yy++)
Tom Tromey committed
629
      {
630 631 632 633
        for (int xx = x; xx < (x + w); xx++)
          {
            dArray[outOffset++] = getSampleDouble(xx, yy, b, data);
          }
Tom Tromey committed
634 635 636 637
      }
    return dArray;
  }
  
638 639 640 641 642 643 644 645 646 647 648 649
  /**
   * Sets the samples for the pixel at (x, y) in the specified data buffer to
   * the specified values. 
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param iArray  the sample values (<code>null</code> not permitted).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if either <code>iArray</code> or 
   *     <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
650 651
  public void setPixel(int x, int y, int[] iArray, DataBuffer data)
  {
652 653
    for (int b = 0; b < numBands; b++) 
      setSample(x, y, b, iArray[b], data);
Tom Tromey committed
654 655
  }

656 657 658 659 660 661 662 663 664 665 666 667
  /**
   * Sets the samples for the pixel at (x, y) in the specified data buffer to
   * the specified values. 
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param fArray  the sample values (<code>null</code> not permitted).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if either <code>fArray</code> or 
   *     <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
668 669
  public void setPixel(int x, int y, float[] fArray, DataBuffer data)
  {
670 671
    for (int b = 0; b < numBands; b++) 
      setSample(x, y, b, fArray[b], data);
Tom Tromey committed
672 673
  }

674 675 676 677 678 679 680 681 682 683 684 685
  /**
   * Sets the samples for the pixel at (x, y) in the specified data buffer to
   * the specified values. 
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param dArray  the sample values (<code>null</code> not permitted).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if either <code>dArray</code> or 
   *     <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
686 687
  public void setPixel(int x, int y, double[] dArray, DataBuffer data)
  {
688 689
    for (int b = 0; b < numBands; b++) 
      setSample(x, y, b, dArray[b], data);
Tom Tromey committed
690 691
  }

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
  /**
   * Sets the sample values for the pixels in the region specified by 
   * (x, y, w, h) in the specified data buffer.  The array is
   * ordered by pixels (that is, all the samples for the first pixel are 
   * grouped together, followed by all the samples for the second pixel, and so
   * on). 
   *  
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param iArray  the pixel sample values (<code>null</code> not permitted).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if either <code>iArray</code> or 
   *     <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
709
  public void setPixels(int x, int y, int w, int h, int[] iArray,
710
                        DataBuffer data)
Tom Tromey committed
711 712 713
  {
    int inOffset = 0;
    int[] pixel = new int[numBands];
714
    for (int yy = y; yy < (y + h); yy++)
Tom Tromey committed
715
      {
716 717 718 719 720 721
        for (int xx = x; xx < (x + w); xx++)
          {
            System.arraycopy(iArray, inOffset, pixel, 0, numBands);
            setPixel(xx, yy, pixel, data);
            inOffset += numBands;
          }
Tom Tromey committed
722 723 724
      }
  }

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
  /**
   * Sets the sample values for the pixels in the region specified by 
   * (x, y, w, h) in the specified data buffer.  The array is
   * ordered by pixels (that is, all the samples for the first pixel are 
   * grouped together, followed by all the samples for the second pixel, and so
   * on). 
   *  
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param fArray  the pixel sample values (<code>null</code> not permitted).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if either <code>fArray</code> or 
   *     <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
742
  public void setPixels(int x, int y, int w, int h, float[] fArray,
743
                        DataBuffer data)
Tom Tromey committed
744 745 746
  {
    int inOffset = 0;
    float[] pixel = new float[numBands];
747
    for (int yy = y; yy < (y + h); yy++)
Tom Tromey committed
748
      {
749 750 751 752 753 754
        for (int xx = x; xx < (x + w); xx++)
          {
            System.arraycopy(fArray, inOffset, pixel, 0, numBands);
            setPixel(xx, yy, pixel, data);
            inOffset += numBands;
          }
Tom Tromey committed
755 756 757
      }
  }

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
  /**
   * Sets the sample values for the pixels in the region specified by 
   * (x, y, w, h) in the specified data buffer.  The array is
   * ordered by pixels (that is, all the samples for the first pixel are 
   * grouped together, followed by all the samples for the second pixel, and so
   * on). 
   *  
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param dArray  the pixel sample values (<code>null</code> not permitted).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if either <code>dArray</code> or 
   *     <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
775
  public void setPixels(int x, int y, int w, int h, double[] dArray,
776
                        DataBuffer data)
Tom Tromey committed
777 778 779
  {
    int inOffset = 0;
    double[] pixel = new double[numBands];
780
    for (int yy = y; yy < (y + h); yy++)
Tom Tromey committed
781
      {
782 783 784 785 786 787
        for (int xx = x; xx < (x + w); xx++)
          {
            System.arraycopy(dArray, inOffset, pixel, 0, numBands);
            setPixel(xx, yy, pixel, data);
            inOffset += numBands;
          }
Tom Tromey committed
788 789 790
      }
  }

791 792 793 794 795 796 797 798 799 800 801 802 803
  /**
   * Sets the sample value for a band for the pixel at (x, y) in the 
   * specified data buffer. 
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param b  the band (in the range <code>0</code> to 
   *     <code>getNumBands() - 1</code>).
   * @param s  the sample value.
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
804
  public abstract void setSample(int x, int y, int b, int s,
805
                                 DataBuffer data);
Tom Tromey committed
806

807 808 809 810 811 812 813 814 815 816 817 818 819
  /**
   * Sets the sample value for a band for the pixel at (x, y) in the 
   * specified data buffer. 
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param b  the band (in the range <code>0</code> to 
   *     <code>getNumBands() - 1</code>).
   * @param s  the sample value.
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
820
  public void setSample(int x, int y, int b, float s,
821
                        DataBuffer data)
Tom Tromey committed
822 823 824 825
  {
    setSample(x, y, b, (int) s, data);
  }

826 827 828 829 830 831 832 833 834 835 836 837 838
  /**
   * Sets the sample value for a band for the pixel at (x, y) in the 
   * specified data buffer. 
   * 
   * @param x  the x-coordinate of the pixel.
   * @param y  the y-coordinate of the pixel.
   * @param b  the band (in the range <code>0</code> to 
   *     <code>getNumBands() - 1</code>).
   * @param s  the sample value.
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
839
  public void setSample(int x, int y, int b, double s,
840
                        DataBuffer data)
Tom Tromey committed
841 842 843 844
  {
    setSample(x, y, b, (float) s, data);
  }

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
  /**
   * Sets the sample values for one band for the pixels in the region 
   * specified by (x, y, w, h) in the specified data buffer. 
   * 
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param b  the band (in the range <code>0</code> to 
   *     </code>getNumBands() - 1</code>).
   * @param iArray  the sample values (<code>null</code> not permitted).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if either <code>iArray</code> or 
   *     <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
861
  public void setSamples(int x, int y, int w, int h, int b,
862
                         int[] iArray, DataBuffer data)
Tom Tromey committed
863
  {
864
    int size = w * h;
Tom Tromey committed
865
    int inOffset = 0;
866 867 868
    for (int yy = y; yy < (y + h); yy++)
      for (int xx = x; xx < (x + w); xx++)
        setSample(xx, yy, b, iArray[inOffset++], data);
Tom Tromey committed
869 870
  }

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
  /**
   * Sets the sample values for one band for the pixels in the region 
   * specified by (x, y, w, h) in the specified data buffer. 
   * 
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param b  the band (in the range <code>0</code> to 
   *     </code>getNumBands() - 1</code>).
   * @param fArray  the sample values (<code>null</code> not permitted).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if either <code>iArray</code> or 
   *     <code>data</code> is <code>null</code>.
   */
Tom Tromey committed
887
  public void setSamples(int x, int y, int w, int h, int b,
888
                         float[] fArray, DataBuffer data)
Tom Tromey committed
889
  {
890 891 892 893 894 895 896 897
    int size = w * h;
    int inOffset = 0;
    for (int yy = y; yy < (y + h); yy++)
      for (int xx = x; xx < (x + w); xx++)
        setSample(xx, yy, b, fArray[inOffset++], data);

  }

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
  /**
   * Sets the sample values for one band for the pixels in the region 
   * specified by (x, y, w, h) in the specified data buffer. 
   * 
   * @param x  the x-coordinate of the top-left pixel.
   * @param y  the y-coordinate of the top-left pixel.
   * @param w  the width of the region of pixels.
   * @param h  the height of the region of pixels.
   * @param b  the band (in the range <code>0</code> to 
   *     </code>getNumBands() - 1</code>).
   * @param dArray  the sample values (<code>null</code> not permitted).
   * @param data  the data buffer (<code>null</code> not permitted).
   * 
   * @throws NullPointerException if either <code>iArray</code> or 
   *     <code>data</code> is <code>null</code>.
   */
914 915 916
  public void setSamples(int x, int y, int w, int h, int b,
                         double[] dArray, DataBuffer data) {
    int size = w * h;
Tom Tromey committed
917
    int inOffset = 0;
918 919 920 921 922
    for (int yy = y; yy < (y + h); yy++)
      for (int xx = x; xx < (x + w); xx++)
        setSample(xx, yy, b, dArray[inOffset++], data);
  }

923 924 925 926 927 928 929 930 931
  /**
   * Creates a new <code>SampleModel</code> that is compatible with this
   * model and has the specified width and height.
   * 
   * @param w  the width (in pixels).
   * @param h  the height (in pixels).
   * 
   * @return The new sample model.
   */
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
  public abstract SampleModel createCompatibleSampleModel(int w, int h);

  /**
   * Return a SampleModel with a subset of the bands in this model.
   * 
   * Selects bands.length bands from this sample model.  The bands chosen
   * are specified in the indices of bands[].  This also permits permuting
   * the bands as well as taking a subset.  Thus, giving an array with
   * 1, 2, 3, ..., numbands, will give an identical sample model.
   * 
   * @param bands Array with band indices to include.
   * @return A new sample model
   */
  public abstract SampleModel createSubsetSampleModel(int[] bands);

947 948 949 950 951 952
  /**
   * Creates a new {@link DataBuffer} of the correct type and size for this 
   * <code>SampleModel</code>.
   * 
   * @return The data buffer.
   */
953 954
  public abstract DataBuffer createDataBuffer();

955 956 957 958 959 960 961 962
  /**
   * Returns an array containing the size (in bits) for each band accessed by
   * the <code>SampleModel</code>.
   * 
   * @return An array.
   * 
   * @see #getSampleSize(int)
   */
963 964
  public abstract int[] getSampleSize();

965 966 967 968 969 970 971 972
  /**
   * Returns the size (in bits) of the samples for the specified band.
   * 
   * @param band  the band (in the range <code>0</code> to 
   *     <code>getNumBands() - 1</code>).
   *     
   * @return The sample size (in bits).
   */
973
  public abstract int getSampleSize(int band);
Tom Tromey committed
974
}