< prev index next >

src/java.desktop/share/classes/java/awt/image/SampleModel.java

Print this page




  94     /** Number of bands of the image data that this SampleModel describes. */
  95     protected int numBands;
  96 
  97     /** Data type of the DataBuffer storing the pixel data.
  98      *  @see java.awt.image.DataBuffer
  99      */
 100     protected int dataType;
 101 
 102     private static native void initIDs();
 103     static {
 104         ColorModel.loadLibraries();
 105         initIDs();
 106     }
 107 
 108     /**
 109      * Constructs a SampleModel with the specified parameters.
 110      * @param dataType  The data type of the DataBuffer storing the pixel data.
 111      * @param w         The width (in pixels) of the region of image data.
 112      * @param h         The height (in pixels) of the region of image data.
 113      * @param numBands  The number of bands of the image data.
 114      * @throws IllegalArgumentException if <code>w</code> or <code>h</code>
 115      *         is not greater than 0
 116      * @throws IllegalArgumentException if the product of <code>w</code>
 117      *         and <code>h</code> is greater than
 118      *         <code>Integer.MAX_VALUE</code>
 119      * @throws IllegalArgumentException if <code>dataType</code> is not
 120      *         one of the supported data types
 121      */
 122     public SampleModel(int dataType, int w, int h, int numBands)
 123     {
 124         long size = (long)w * h;
 125         if (w <= 0 || h <= 0) {
 126             throw new IllegalArgumentException("Width ("+w+") and height ("+
 127                                                h+") must be > 0");
 128         }
 129         if (size >= Integer.MAX_VALUE) {
 130             throw new IllegalArgumentException("Dimensions (width="+w+
 131                                                " height="+h+") are too large");
 132         }
 133 
 134         if (dataType < DataBuffer.TYPE_BYTE ||
 135             (dataType > DataBuffer.TYPE_DOUBLE &&
 136              dataType != DataBuffer.TYPE_UNDEFINED))
 137         {
 138             throw new IllegalArgumentException("Unsupported dataType: "+
 139                                                dataType);
 140         }
 141 
 142         if (numBands <= 0) {
 143             throw new IllegalArgumentException("Number of bands must be > 0");
 144         }
 145 
 146         this.dataType = dataType;
 147         this.width = w;
 148         this.height = h;
 149         this.numBands = numBands;
 150     }
 151 
 152     /** Returns the width in pixels.
 153      *  @return the width in pixels of the region of image data
 154      *          that this <code>SampleModel</code> describes.
 155      */
 156     public final int getWidth() {
 157          return width;
 158     }
 159 
 160     /** Returns the height in pixels.
 161      *  @return the height in pixels of the region of image data
 162      *          that this <code>SampleModel</code> describes.
 163      */
 164     public final int getHeight() {
 165          return height;
 166     }
 167 
 168     /** Returns the total number of bands of image data.
 169      *  @return the number of bands of image data that this
 170      *          <code>SampleModel</code> describes.
 171      */
 172     public final int getNumBands() {
 173          return numBands;
 174     }
 175 
 176     /** Returns the number of data elements needed to transfer a pixel
 177      *  via the getDataElements and setDataElements methods.  When pixels
 178      *  are transferred via these methods, they may be transferred in a
 179      *  packed or unpacked format, depending on the implementation of the
 180      *  SampleModel.  Using these methods, pixels are transferred as an
 181      *  array of getNumDataElements() elements of a primitive type given
 182      *  by getTransferType().  The TransferType may or may not be the same
 183      *  as the storage DataType.
 184      *  @return the number of data elements.
 185      *  @see #getDataElements(int, int, Object, DataBuffer)
 186      *  @see #getDataElements(int, int, int, int, Object, DataBuffer)
 187      *  @see #setDataElements(int, int, Object, DataBuffer)
 188      *  @see #setDataElements(int, int, int, int, Object, DataBuffer)
 189      *  @see #getTransferType
 190      */


 244             pixels = new int[numBands];
 245 
 246         for (int i=0; i<numBands; i++) {
 247             pixels[i] = getSample(x, y, i, data);
 248         }
 249 
 250         return pixels;
 251     }
 252 
 253     /**
 254      * Returns data for a single pixel in a primitive array of type
 255      * TransferType.  For image data supported by the Java 2D API, this
 256      * will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
 257      * DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT,
 258      * or DataBuffer.TYPE_DOUBLE.  Data may be returned in a packed format,
 259      * thus increasing efficiency for data transfers. Generally, obj
 260      * should be passed in as null, so that the Object will be created
 261      * automatically and will be of the right primitive data type.
 262      * <p>
 263      * The following code illustrates transferring data for one pixel from
 264      * DataBuffer <code>db1</code>, whose storage layout is described by
 265      * SampleModel <code>sm1</code>, to DataBuffer <code>db2</code>, whose
 266      * storage layout is described by SampleModel <code>sm2</code>.
 267      * The transfer will generally be more efficient than using
 268      * getPixel/setPixel.
 269      * <pre>
 270      *       SampleModel sm1, sm2;
 271      *       DataBuffer db1, db2;
 272      *       sm2.setDataElements(x, y, sm1.getDataElements(x, y, null, db1), db2);
 273      * </pre>
 274      * Using getDataElements/setDataElements to transfer between two
 275      * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
 276      * the same number of bands, corresponding bands have the same number of
 277      * bits per sample, and the TransferTypes are the same.
 278      * <p>
 279      * If obj is non-null, it should be a primitive array of type TransferType.
 280      * Otherwise, a ClassCastException is thrown.  An
 281      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 282      * not in bounds, or if obj is non-null and is not large enough to hold
 283      * the pixel data.
 284      * @param x         The X coordinate of the pixel location.
 285      * @param y         The Y coordinate of the pixel location.
 286      * @param obj       If non-null, a primitive array in which to return


 295      * @throws NullPointerException if data is null.
 296      * @throws ArrayIndexOutOfBoundsException if the coordinates are
 297      * not in bounds, or if obj is too small to hold the output.
 298      */
 299     public abstract Object getDataElements(int x, int y,
 300                                            Object obj, DataBuffer data);
 301 
 302     /**
 303      * Returns the pixel data for the specified rectangle of pixels in a
 304      * primitive array of type TransferType.
 305      * For image data supported by the Java 2D API, this
 306      * will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
 307      * DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT,
 308      * or DataBuffer.TYPE_DOUBLE.  Data may be returned in a packed format,
 309      * thus increasing efficiency for data transfers. Generally, obj
 310      * should be passed in as null, so that the Object will be created
 311      * automatically and will be of the right primitive data type.
 312      * <p>
 313      * The following code illustrates transferring data for a rectangular
 314      * region of pixels from
 315      * DataBuffer <code>db1</code>, whose storage layout is described by
 316      * SampleModel <code>sm1</code>, to DataBuffer <code>db2</code>, whose
 317      * storage layout is described by SampleModel <code>sm2</code>.
 318      * The transfer will generally be more efficient than using
 319      * getPixels/setPixels.
 320      * <pre>
 321      *       SampleModel sm1, sm2;
 322      *       DataBuffer db1, db2;
 323      *       sm2.setDataElements(x, y, w, h, sm1.getDataElements(x, y, w,
 324      *                           h, null, db1), db2);
 325      * </pre>
 326      * Using getDataElements/setDataElements to transfer between two
 327      * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
 328      * the same number of bands, corresponding bands have the same number of
 329      * bits per sample, and the TransferTypes are the same.
 330      * <p>
 331      * If obj is non-null, it should be a primitive array of type TransferType.
 332      * Otherwise, a ClassCastException is thrown.  An
 333      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 334      * not in bounds, or if obj is non-null and is not large enough to hold
 335      * the pixel data.
 336      * @param x         The minimum X coordinate of the pixel rectangle.
 337      * @param y         The minimum Y coordinate of the pixel rectangle.


 481                 }
 482             }
 483 
 484             obj = (Object)ddata;
 485             break;
 486         }
 487 
 488         return obj;
 489     }
 490 
 491     /**
 492      * Sets the data for a single pixel in the specified DataBuffer from a
 493      * primitive array of type TransferType.  For image data supported by
 494      * the Java 2D API, this will be one of DataBuffer.TYPE_BYTE,
 495      * DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT,
 496      * DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE.  Data in the array
 497      * may be in a packed format, thus increasing efficiency for data
 498      * transfers.
 499      * <p>
 500      * The following code illustrates transferring data for one pixel from
 501      * DataBuffer <code>db1</code>, whose storage layout is described by
 502      * SampleModel <code>sm1</code>, to DataBuffer <code>db2</code>, whose
 503      * storage layout is described by SampleModel <code>sm2</code>.
 504      * The transfer will generally be more efficient than using
 505      * getPixel/setPixel.
 506      * <pre>
 507      *       SampleModel sm1, sm2;
 508      *       DataBuffer db1, db2;
 509      *       sm2.setDataElements(x, y, sm1.getDataElements(x, y, null, db1),
 510      *                           db2);
 511      * </pre>
 512      * Using getDataElements/setDataElements to transfer between two
 513      * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
 514      * the same number of bands, corresponding bands have the same number of
 515      * bits per sample, and the TransferTypes are the same.
 516      * <p>
 517      * obj must be a primitive array of type TransferType.  Otherwise,
 518      * a ClassCastException is thrown.  An
 519      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 520      * not in bounds, or if obj is not large enough to hold the pixel data.
 521      * @param x         The X coordinate of the pixel location.
 522      * @param y         The Y coordinate of the pixel location.
 523      * @param obj       A primitive array containing pixel data.


 528      * @see java.awt.image.DataBuffer
 529      *
 530      * @throws NullPointerException if data is null.
 531      * @throws ArrayIndexOutOfBoundsException if the coordinates are
 532      * not in bounds, or if obj is too small to hold the input.
 533      */
 534     public abstract void setDataElements(int x, int y,
 535                                          Object obj, DataBuffer data);
 536 
 537     /**
 538      * Sets the data for a rectangle of pixels in the specified DataBuffer
 539      * from a primitive array of type TransferType.  For image data supported
 540      * by the Java 2D API, this will be one of DataBuffer.TYPE_BYTE,
 541      * DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT,
 542      * DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE.  Data in the array
 543      * may be in a packed format, thus increasing efficiency for data
 544      * transfers.
 545      * <p>
 546      * The following code illustrates transferring data for a rectangular
 547      * region of pixels from
 548      * DataBuffer <code>db1</code>, whose storage layout is described by
 549      * SampleModel <code>sm1</code>, to DataBuffer <code>db2</code>, whose
 550      * storage layout is described by SampleModel <code>sm2</code>.
 551      * The transfer will generally be more efficient than using
 552      * getPixels/setPixels.
 553      * <pre>
 554      *       SampleModel sm1, sm2;
 555      *       DataBuffer db1, db2;
 556      *       sm2.setDataElements(x, y, w, h, sm1.getDataElements(x, y, w, h,
 557      *                           null, db1), db2);
 558      * </pre>
 559      * Using getDataElements/setDataElements to transfer between two
 560      * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
 561      * the same number of bands, corresponding bands have the same number of
 562      * bits per sample, and the TransferTypes are the same.
 563      * <p>
 564      * obj must be a primitive array of type TransferType.  Otherwise,
 565      * a ClassCastException is thrown.  An
 566      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 567      * not in bounds, or if obj is not large enough to hold the pixel data.
 568      * @param x         The minimum X coordinate of the pixel rectangle.
 569      * @param y         The minimum Y coordinate of the pixel rectangle.
 570      * @param w         The width of the pixel rectangle.


1281      * @param x         The X coordinate of the pixel location.
1282      * @param y         The Y coordinate of the pixel location.
1283      * @param b         The band to set.
1284      * @param s         The input sample as an int.
1285      * @param data      The DataBuffer containing the image data.
1286      * @see #getSample(int, int, int,  DataBuffer)
1287      *
1288      * @throws NullPointerException if data is null.
1289      * @throws ArrayIndexOutOfBoundsException if the coordinates or
1290      * the band index are not in bounds.
1291      */
1292     public abstract void setSample(int x, int y, int b,
1293                                    int s,
1294                                    DataBuffer data);
1295 
1296     /**
1297      * Sets a sample in the specified band for the pixel located at (x,y)
1298      * in the DataBuffer using a float for input.
1299      * The default implementation of this method casts the input
1300      * float sample to an int and then calls the
1301      * <code>setSample(int, int, int, DataBuffer)</code> method using
1302      * that int value.
1303      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
1304      * not in bounds.
1305      * @param x         The X coordinate of the pixel location.
1306      * @param y         The Y coordinate of the pixel location.
1307      * @param b         The band to set.
1308      * @param s         The input sample as a float.
1309      * @param data      The DataBuffer containing the image data.
1310      * @see #getSample(int, int, int, DataBuffer)
1311      *
1312      * @throws NullPointerException if data is null.
1313      * @throws ArrayIndexOutOfBoundsException if the coordinates or
1314      * the band index are not in bounds.
1315      */
1316     public void setSample(int x, int y, int b,
1317                           float s ,
1318                           DataBuffer data) {
1319         int sample = (int)s;
1320 
1321         setSample(x, y, b, sample, data);
1322     }
1323 
1324     /**
1325      * Sets a sample in the specified band for the pixel located at (x,y)
1326      * in the DataBuffer using a double for input.
1327      * The default implementation of this method casts the input
1328      * double sample to an int and then calls the
1329      * <code>setSample(int, int, int, DataBuffer)</code> method using
1330      * that int value.
1331      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
1332      * not in bounds.
1333      * @param x         The X coordinate of the pixel location.
1334      * @param y         The Y coordinate of the pixel location.
1335      * @param b         The band to set.
1336      * @param s         The input sample as a double.
1337      * @param data      The DataBuffer containing the image data.
1338      * @see #getSample(int, int, int, DataBuffer)
1339      *
1340      * @throws NullPointerException if data is null.
1341      * @throws ArrayIndexOutOfBoundsException if the coordinates or
1342      * the band index are not in bounds.
1343      */
1344     public void setSample(int x, int y, int b,
1345                           double s,
1346                           DataBuffer data) {
1347         int sample = (int)s;
1348 
1349         setSample(x, y, b, sample, data);


1452 
1453 
1454         if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
1455             y < 0 || y >= height || h > height || y1 < 0 || y1 > height)
1456         {
1457             throw new ArrayIndexOutOfBoundsException("Invalid coordinates.");
1458         }
1459 
1460         for (int i=y; i<y1; i++) {
1461             for (int j=x; j<x1; j++) {
1462                 setSample(j, i, b, dArray[Offset++], data);
1463             }
1464         }
1465     }
1466 
1467     /**
1468      *  Creates a SampleModel which describes data in this SampleModel's
1469      *  format, but with a different width and height.
1470      *  @param w the width of the image data
1471      *  @param h the height of the image data
1472      *  @return a <code>SampleModel</code> describing the same image
1473      *          data as this <code>SampleModel</code>, but with a
1474      *          different size.
1475      */
1476     public abstract SampleModel createCompatibleSampleModel(int w, int h);
1477 
1478     /**
1479      * Creates a new SampleModel
1480      * with a subset of the bands of this
1481      * SampleModel.
1482      * @param bands the subset of bands of this <code>SampleModel</code>
1483      * @return a <code>SampleModel</code> with a subset of bands of this
1484      *         <code>SampleModel</code>.
1485      */
1486     public abstract SampleModel createSubsetSampleModel(int bands[]);
1487 
1488     /**
1489      * Creates a DataBuffer that corresponds to this SampleModel.
1490      * The DataBuffer's width and height will match this SampleModel's.
1491      * @return a <code>DataBuffer</code> corresponding to this
1492      *         <code>SampleModel</code>.
1493      */
1494     public abstract DataBuffer createDataBuffer();
1495 
1496     /** Returns the size in bits of samples for all bands.
1497      *  @return the size of samples for all bands.
1498      */
1499     public abstract int[] getSampleSize();
1500 
1501     /** Returns the size in bits of samples for the specified band.
1502      *  @param band the specified band
1503      *  @return the size of the samples of the specified band.
1504      */
1505     public abstract int getSampleSize(int band);
1506 
1507 }


  94     /** Number of bands of the image data that this SampleModel describes. */
  95     protected int numBands;
  96 
  97     /** Data type of the DataBuffer storing the pixel data.
  98      *  @see java.awt.image.DataBuffer
  99      */
 100     protected int dataType;
 101 
 102     private static native void initIDs();
 103     static {
 104         ColorModel.loadLibraries();
 105         initIDs();
 106     }
 107 
 108     /**
 109      * Constructs a SampleModel with the specified parameters.
 110      * @param dataType  The data type of the DataBuffer storing the pixel data.
 111      * @param w         The width (in pixels) of the region of image data.
 112      * @param h         The height (in pixels) of the region of image data.
 113      * @param numBands  The number of bands of the image data.
 114      * @throws IllegalArgumentException if {@code w} or {@code h}
 115      *         is not greater than 0
 116      * @throws IllegalArgumentException if the product of {@code w}
 117      *         and {@code h} is greater than
 118      *         {@code Integer.MAX_VALUE}
 119      * @throws IllegalArgumentException if {@code dataType} is not
 120      *         one of the supported data types
 121      */
 122     public SampleModel(int dataType, int w, int h, int numBands)
 123     {
 124         long size = (long)w * h;
 125         if (w <= 0 || h <= 0) {
 126             throw new IllegalArgumentException("Width ("+w+") and height ("+
 127                                                h+") must be > 0");
 128         }
 129         if (size >= Integer.MAX_VALUE) {
 130             throw new IllegalArgumentException("Dimensions (width="+w+
 131                                                " height="+h+") are too large");
 132         }
 133 
 134         if (dataType < DataBuffer.TYPE_BYTE ||
 135             (dataType > DataBuffer.TYPE_DOUBLE &&
 136              dataType != DataBuffer.TYPE_UNDEFINED))
 137         {
 138             throw new IllegalArgumentException("Unsupported dataType: "+
 139                                                dataType);
 140         }
 141 
 142         if (numBands <= 0) {
 143             throw new IllegalArgumentException("Number of bands must be > 0");
 144         }
 145 
 146         this.dataType = dataType;
 147         this.width = w;
 148         this.height = h;
 149         this.numBands = numBands;
 150     }
 151 
 152     /** Returns the width in pixels.
 153      *  @return the width in pixels of the region of image data
 154      *          that this {@code SampleModel} describes.
 155      */
 156     public final int getWidth() {
 157          return width;
 158     }
 159 
 160     /** Returns the height in pixels.
 161      *  @return the height in pixels of the region of image data
 162      *          that this {@code SampleModel} describes.
 163      */
 164     public final int getHeight() {
 165          return height;
 166     }
 167 
 168     /** Returns the total number of bands of image data.
 169      *  @return the number of bands of image data that this
 170      *          {@code SampleModel} describes.
 171      */
 172     public final int getNumBands() {
 173          return numBands;
 174     }
 175 
 176     /** Returns the number of data elements needed to transfer a pixel
 177      *  via the getDataElements and setDataElements methods.  When pixels
 178      *  are transferred via these methods, they may be transferred in a
 179      *  packed or unpacked format, depending on the implementation of the
 180      *  SampleModel.  Using these methods, pixels are transferred as an
 181      *  array of getNumDataElements() elements of a primitive type given
 182      *  by getTransferType().  The TransferType may or may not be the same
 183      *  as the storage DataType.
 184      *  @return the number of data elements.
 185      *  @see #getDataElements(int, int, Object, DataBuffer)
 186      *  @see #getDataElements(int, int, int, int, Object, DataBuffer)
 187      *  @see #setDataElements(int, int, Object, DataBuffer)
 188      *  @see #setDataElements(int, int, int, int, Object, DataBuffer)
 189      *  @see #getTransferType
 190      */


 244             pixels = new int[numBands];
 245 
 246         for (int i=0; i<numBands; i++) {
 247             pixels[i] = getSample(x, y, i, data);
 248         }
 249 
 250         return pixels;
 251     }
 252 
 253     /**
 254      * Returns data for a single pixel in a primitive array of type
 255      * TransferType.  For image data supported by the Java 2D API, this
 256      * will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
 257      * DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT,
 258      * or DataBuffer.TYPE_DOUBLE.  Data may be returned in a packed format,
 259      * thus increasing efficiency for data transfers. Generally, obj
 260      * should be passed in as null, so that the Object will be created
 261      * automatically and will be of the right primitive data type.
 262      * <p>
 263      * The following code illustrates transferring data for one pixel from
 264      * DataBuffer {@code db1}, whose storage layout is described by
 265      * SampleModel {@code sm1}, to DataBuffer {@code db2}, whose
 266      * storage layout is described by SampleModel {@code sm2}.
 267      * The transfer will generally be more efficient than using
 268      * getPixel/setPixel.
 269      * <pre>
 270      *       SampleModel sm1, sm2;
 271      *       DataBuffer db1, db2;
 272      *       sm2.setDataElements(x, y, sm1.getDataElements(x, y, null, db1), db2);
 273      * </pre>
 274      * Using getDataElements/setDataElements to transfer between two
 275      * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
 276      * the same number of bands, corresponding bands have the same number of
 277      * bits per sample, and the TransferTypes are the same.
 278      * <p>
 279      * If obj is non-null, it should be a primitive array of type TransferType.
 280      * Otherwise, a ClassCastException is thrown.  An
 281      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 282      * not in bounds, or if obj is non-null and is not large enough to hold
 283      * the pixel data.
 284      * @param x         The X coordinate of the pixel location.
 285      * @param y         The Y coordinate of the pixel location.
 286      * @param obj       If non-null, a primitive array in which to return


 295      * @throws NullPointerException if data is null.
 296      * @throws ArrayIndexOutOfBoundsException if the coordinates are
 297      * not in bounds, or if obj is too small to hold the output.
 298      */
 299     public abstract Object getDataElements(int x, int y,
 300                                            Object obj, DataBuffer data);
 301 
 302     /**
 303      * Returns the pixel data for the specified rectangle of pixels in a
 304      * primitive array of type TransferType.
 305      * For image data supported by the Java 2D API, this
 306      * will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
 307      * DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT,
 308      * or DataBuffer.TYPE_DOUBLE.  Data may be returned in a packed format,
 309      * thus increasing efficiency for data transfers. Generally, obj
 310      * should be passed in as null, so that the Object will be created
 311      * automatically and will be of the right primitive data type.
 312      * <p>
 313      * The following code illustrates transferring data for a rectangular
 314      * region of pixels from
 315      * DataBuffer {@code db1}, whose storage layout is described by
 316      * SampleModel {@code sm1}, to DataBuffer {@code db2}, whose
 317      * storage layout is described by SampleModel {@code sm2}.
 318      * The transfer will generally be more efficient than using
 319      * getPixels/setPixels.
 320      * <pre>
 321      *       SampleModel sm1, sm2;
 322      *       DataBuffer db1, db2;
 323      *       sm2.setDataElements(x, y, w, h, sm1.getDataElements(x, y, w,
 324      *                           h, null, db1), db2);
 325      * </pre>
 326      * Using getDataElements/setDataElements to transfer between two
 327      * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
 328      * the same number of bands, corresponding bands have the same number of
 329      * bits per sample, and the TransferTypes are the same.
 330      * <p>
 331      * If obj is non-null, it should be a primitive array of type TransferType.
 332      * Otherwise, a ClassCastException is thrown.  An
 333      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 334      * not in bounds, or if obj is non-null and is not large enough to hold
 335      * the pixel data.
 336      * @param x         The minimum X coordinate of the pixel rectangle.
 337      * @param y         The minimum Y coordinate of the pixel rectangle.


 481                 }
 482             }
 483 
 484             obj = (Object)ddata;
 485             break;
 486         }
 487 
 488         return obj;
 489     }
 490 
 491     /**
 492      * Sets the data for a single pixel in the specified DataBuffer from a
 493      * primitive array of type TransferType.  For image data supported by
 494      * the Java 2D API, this will be one of DataBuffer.TYPE_BYTE,
 495      * DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT,
 496      * DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE.  Data in the array
 497      * may be in a packed format, thus increasing efficiency for data
 498      * transfers.
 499      * <p>
 500      * The following code illustrates transferring data for one pixel from
 501      * DataBuffer {@code db1}, whose storage layout is described by
 502      * SampleModel {@code sm1}, to DataBuffer {@code db2}, whose
 503      * storage layout is described by SampleModel {@code sm2}.
 504      * The transfer will generally be more efficient than using
 505      * getPixel/setPixel.
 506      * <pre>
 507      *       SampleModel sm1, sm2;
 508      *       DataBuffer db1, db2;
 509      *       sm2.setDataElements(x, y, sm1.getDataElements(x, y, null, db1),
 510      *                           db2);
 511      * </pre>
 512      * Using getDataElements/setDataElements to transfer between two
 513      * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
 514      * the same number of bands, corresponding bands have the same number of
 515      * bits per sample, and the TransferTypes are the same.
 516      * <p>
 517      * obj must be a primitive array of type TransferType.  Otherwise,
 518      * a ClassCastException is thrown.  An
 519      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 520      * not in bounds, or if obj is not large enough to hold the pixel data.
 521      * @param x         The X coordinate of the pixel location.
 522      * @param y         The Y coordinate of the pixel location.
 523      * @param obj       A primitive array containing pixel data.


 528      * @see java.awt.image.DataBuffer
 529      *
 530      * @throws NullPointerException if data is null.
 531      * @throws ArrayIndexOutOfBoundsException if the coordinates are
 532      * not in bounds, or if obj is too small to hold the input.
 533      */
 534     public abstract void setDataElements(int x, int y,
 535                                          Object obj, DataBuffer data);
 536 
 537     /**
 538      * Sets the data for a rectangle of pixels in the specified DataBuffer
 539      * from a primitive array of type TransferType.  For image data supported
 540      * by the Java 2D API, this will be one of DataBuffer.TYPE_BYTE,
 541      * DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT,
 542      * DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE.  Data in the array
 543      * may be in a packed format, thus increasing efficiency for data
 544      * transfers.
 545      * <p>
 546      * The following code illustrates transferring data for a rectangular
 547      * region of pixels from
 548      * DataBuffer {@code db1}, whose storage layout is described by
 549      * SampleModel {@code sm1}, to DataBuffer {@code db2}, whose
 550      * storage layout is described by SampleModel {@code sm2}.
 551      * The transfer will generally be more efficient than using
 552      * getPixels/setPixels.
 553      * <pre>
 554      *       SampleModel sm1, sm2;
 555      *       DataBuffer db1, db2;
 556      *       sm2.setDataElements(x, y, w, h, sm1.getDataElements(x, y, w, h,
 557      *                           null, db1), db2);
 558      * </pre>
 559      * Using getDataElements/setDataElements to transfer between two
 560      * DataBuffer/SampleModel pairs is legitimate if the SampleModels have
 561      * the same number of bands, corresponding bands have the same number of
 562      * bits per sample, and the TransferTypes are the same.
 563      * <p>
 564      * obj must be a primitive array of type TransferType.  Otherwise,
 565      * a ClassCastException is thrown.  An
 566      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 567      * not in bounds, or if obj is not large enough to hold the pixel data.
 568      * @param x         The minimum X coordinate of the pixel rectangle.
 569      * @param y         The minimum Y coordinate of the pixel rectangle.
 570      * @param w         The width of the pixel rectangle.


1281      * @param x         The X coordinate of the pixel location.
1282      * @param y         The Y coordinate of the pixel location.
1283      * @param b         The band to set.
1284      * @param s         The input sample as an int.
1285      * @param data      The DataBuffer containing the image data.
1286      * @see #getSample(int, int, int,  DataBuffer)
1287      *
1288      * @throws NullPointerException if data is null.
1289      * @throws ArrayIndexOutOfBoundsException if the coordinates or
1290      * the band index are not in bounds.
1291      */
1292     public abstract void setSample(int x, int y, int b,
1293                                    int s,
1294                                    DataBuffer data);
1295 
1296     /**
1297      * Sets a sample in the specified band for the pixel located at (x,y)
1298      * in the DataBuffer using a float for input.
1299      * The default implementation of this method casts the input
1300      * float sample to an int and then calls the
1301      * {@code setSample(int, int, int, DataBuffer)} method using
1302      * that int value.
1303      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
1304      * not in bounds.
1305      * @param x         The X coordinate of the pixel location.
1306      * @param y         The Y coordinate of the pixel location.
1307      * @param b         The band to set.
1308      * @param s         The input sample as a float.
1309      * @param data      The DataBuffer containing the image data.
1310      * @see #getSample(int, int, int, DataBuffer)
1311      *
1312      * @throws NullPointerException if data is null.
1313      * @throws ArrayIndexOutOfBoundsException if the coordinates or
1314      * the band index are not in bounds.
1315      */
1316     public void setSample(int x, int y, int b,
1317                           float s ,
1318                           DataBuffer data) {
1319         int sample = (int)s;
1320 
1321         setSample(x, y, b, sample, data);
1322     }
1323 
1324     /**
1325      * Sets a sample in the specified band for the pixel located at (x,y)
1326      * in the DataBuffer using a double for input.
1327      * The default implementation of this method casts the input
1328      * double sample to an int and then calls the
1329      * {@code setSample(int, int, int, DataBuffer)} method using
1330      * that int value.
1331      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
1332      * not in bounds.
1333      * @param x         The X coordinate of the pixel location.
1334      * @param y         The Y coordinate of the pixel location.
1335      * @param b         The band to set.
1336      * @param s         The input sample as a double.
1337      * @param data      The DataBuffer containing the image data.
1338      * @see #getSample(int, int, int, DataBuffer)
1339      *
1340      * @throws NullPointerException if data is null.
1341      * @throws ArrayIndexOutOfBoundsException if the coordinates or
1342      * the band index are not in bounds.
1343      */
1344     public void setSample(int x, int y, int b,
1345                           double s,
1346                           DataBuffer data) {
1347         int sample = (int)s;
1348 
1349         setSample(x, y, b, sample, data);


1452 
1453 
1454         if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
1455             y < 0 || y >= height || h > height || y1 < 0 || y1 > height)
1456         {
1457             throw new ArrayIndexOutOfBoundsException("Invalid coordinates.");
1458         }
1459 
1460         for (int i=y; i<y1; i++) {
1461             for (int j=x; j<x1; j++) {
1462                 setSample(j, i, b, dArray[Offset++], data);
1463             }
1464         }
1465     }
1466 
1467     /**
1468      *  Creates a SampleModel which describes data in this SampleModel's
1469      *  format, but with a different width and height.
1470      *  @param w the width of the image data
1471      *  @param h the height of the image data
1472      *  @return a {@code SampleModel} describing the same image
1473      *          data as this {@code SampleModel}, but with a
1474      *          different size.
1475      */
1476     public abstract SampleModel createCompatibleSampleModel(int w, int h);
1477 
1478     /**
1479      * Creates a new SampleModel
1480      * with a subset of the bands of this
1481      * SampleModel.
1482      * @param bands the subset of bands of this {@code SampleModel}
1483      * @return a {@code SampleModel} with a subset of bands of this
1484      *         {@code SampleModel}.
1485      */
1486     public abstract SampleModel createSubsetSampleModel(int bands[]);
1487 
1488     /**
1489      * Creates a DataBuffer that corresponds to this SampleModel.
1490      * The DataBuffer's width and height will match this SampleModel's.
1491      * @return a {@code DataBuffer} corresponding to this
1492      *         {@code SampleModel}.
1493      */
1494     public abstract DataBuffer createDataBuffer();
1495 
1496     /** Returns the size in bits of samples for all bands.
1497      *  @return the size of samples for all bands.
1498      */
1499     public abstract int[] getSampleSize();
1500 
1501     /** Returns the size in bits of samples for the specified band.
1502      *  @param band the specified band
1503      *  @return the size of the samples of the specified band.
1504      */
1505     public abstract int getSampleSize(int band);
1506 
1507 }
< prev index next >