< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2014, 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


  49  *  in the first bank of a DataBuffer.  Accessor methods are provided so
  50  *  that the image data can be manipulated directly. Scanline stride is the
  51  *  number of data array elements between a given sample and the corresponding
  52  *  sample in the same column of the next scanline. Bit masks are the masks
  53  *  required to extract the samples representing the bands of the pixel.
  54  *  Bit offsets are the offsets in bits into the data array
  55  *  element of the samples representing the bands of the pixel.
  56  * <p>
  57  * The following code illustrates extracting the bits of the sample
  58  * representing band {@code b} for pixel {@code x,y}
  59  * from DataBuffer {@code data}:
  60  * <pre>{@code
  61  *      int sample = data.getElem(y * scanlineStride + x);
  62  *      sample = (sample & bitMasks[b]) >>> bitOffsets[b];
  63  * }</pre>
  64  */
  65 
  66 public class SinglePixelPackedSampleModel extends SampleModel
  67 {
  68     /** Bit masks for all bands of the image data. */
  69     private int bitMasks[];
  70 
  71     /** Bit Offsets for all bands of the image data. */
  72     private int bitOffsets[];
  73 
  74     /** Bit sizes for all the bands of the image data. */
  75     private int bitSizes[];
  76 
  77     /** Maximum bit size. */
  78     private int maxBitSize;
  79 
  80     /** Line stride of the region of image data described by this
  81      *  SinglePixelPackedSampleModel.
  82      */
  83     private int scanlineStride;
  84 
  85     private static native void initIDs();
  86     static {
  87         ColorModel.loadLibraries();
  88         initIDs();
  89     }
  90 
  91     /**
  92      * Constructs a SinglePixelPackedSampleModel with bitMasks.length bands.
  93      * Each sample is stored in a data array element in the position of
  94      * its corresponding bit mask.  Each bit mask must be contiguous and
  95      * masks must not overlap. Bit masks exceeding data type capacity are
  96      * truncated.
  97      * @param dataType  The data type for storing samples.
  98      * @param w         The width (in pixels) of the region of the
  99      *                  image data described.
 100      * @param h         The height (in pixels) of the region of the
 101      *                  image data described.
 102      * @param bitMasks  The bit masks for all bands.
 103      * @throws IllegalArgumentException if {@code dataType} is not
 104      *         either {@code DataBuffer.TYPE_BYTE},
 105      *         {@code DataBuffer.TYPE_USHORT}, or
 106      *         {@code DataBuffer.TYPE_INT}
 107      */
 108     public SinglePixelPackedSampleModel(int dataType, int w, int h,
 109                                    int bitMasks[]) {
 110         this(dataType, w, h, w, bitMasks);
 111         if (dataType != DataBuffer.TYPE_BYTE &&
 112             dataType != DataBuffer.TYPE_USHORT &&
 113             dataType != DataBuffer.TYPE_INT) {
 114             throw new IllegalArgumentException("Unsupported data type "+
 115                                                dataType);
 116         }
 117     }
 118 
 119     /**
 120      * Constructs a SinglePixelPackedSampleModel with bitMasks.length bands
 121      * and a scanline stride equal to scanlineStride data array elements.
 122      * Each sample is stored in a data array element in the position of
 123      * its corresponding bit mask.  Each bit mask must be contiguous and
 124      * masks must not overlap. Bit masks exceeding data type capacity are
 125      * truncated.
 126      * @param dataType  The data type for storing samples.
 127      * @param w         The width (in pixels) of the region of
 128      *                  image data described.
 129      * @param h         The height (in pixels) of the region of
 130      *                  image data described.
 131      * @param scanlineStride The line stride of the image data.
 132      * @param bitMasks The bit masks for all bands.
 133      * @throws IllegalArgumentException if {@code w} or
 134      *         {@code h} is not greater than 0
 135      * @throws IllegalArgumentException if any mask in
 136      *         {@code bitMask} is not contiguous
 137      * @throws IllegalArgumentException if {@code dataType} is not
 138      *         either {@code DataBuffer.TYPE_BYTE},
 139      *         {@code DataBuffer.TYPE_USHORT}, or
 140      *         {@code DataBuffer.TYPE_INT}
 141      */
 142     public SinglePixelPackedSampleModel(int dataType, int w, int h,
 143                                    int scanlineStride, int bitMasks[]) {
 144         super(dataType, w, h, bitMasks.length);
 145         if (dataType != DataBuffer.TYPE_BYTE &&
 146             dataType != DataBuffer.TYPE_USHORT &&
 147             dataType != DataBuffer.TYPE_INT) {
 148             throw new IllegalArgumentException("Unsupported data type "+
 149                                                dataType);
 150         }
 151         this.dataType = dataType;
 152         this.bitMasks = bitMasks.clone();
 153         this.scanlineStride = scanlineStride;
 154 
 155         this.bitOffsets = new int[numBands];
 156         this.bitSizes = new int[numBands];
 157 
 158         int maxMask = (int)((1L << DataBuffer.getDataTypeSize(dataType)) - 1);
 159 
 160         this.maxBitSize = 0;
 161         for (int i=0; i<numBands; i++) {
 162             int bitOffset = 0, bitSize = 0, mask;
 163             this.bitMasks[i] &= maxMask;


 289     /** Returns the scanline stride of this SinglePixelPackedSampleModel.
 290      *  @return the scanline stride of this
 291      *          {@code SinglePixelPackedSampleModel}.
 292      */
 293     public int getScanlineStride() {
 294       return scanlineStride;
 295     }
 296 
 297     /**
 298      * This creates a new SinglePixelPackedSampleModel with a subset of the
 299      * bands of this SinglePixelPackedSampleModel.  The new
 300      * SinglePixelPackedSampleModel can be used with any DataBuffer that the
 301      * existing SinglePixelPackedSampleModel can be used with.  The new
 302      * SinglePixelPackedSampleModel/DataBuffer combination will represent
 303      * an image with a subset of the bands of the original
 304      * SinglePixelPackedSampleModel/DataBuffer combination.
 305      * @exception RasterFormatException if the length of the bands argument is
 306      *                                  greater than the number of bands in
 307      *                                  the sample model.
 308      */
 309     public SampleModel createSubsetSampleModel(int bands[]) {
 310         if (bands.length > numBands)
 311             throw new RasterFormatException("There are only " +
 312                                             numBands +
 313                                             " bands");
 314         int newBitMasks[] = new int[bands.length];
 315         for (int i=0; i<bands.length; i++)
 316             newBitMasks[i] = bitMasks[bands[i]];
 317 
 318         return new SinglePixelPackedSampleModel(this.dataType, width, height,
 319                                            this.scanlineStride, newBitMasks);
 320     }
 321 
 322     /**
 323      * Returns data for a single pixel in a primitive array of type
 324      * TransferType.  For a SinglePixelPackedSampleModel, the array will
 325      * have one element, and the type will be the same as the storage
 326      * data type.  Generally, obj
 327      * should be passed in as null, so that the Object will be created
 328      * automatically and will be of the right primitive data type.
 329      * <p>
 330      * The following code illustrates transferring data for one pixel from
 331      * DataBuffer {@code db1}, whose storage layout is described by
 332      * SinglePixelPackedSampleModel {@code sppsm1}, to
 333      * DataBuffer {@code db2}, whose storage layout is described by
 334      * SinglePixelPackedSampleModel {@code sppsm2}.


 409             idata[0] = data.getElem(y * scanlineStride + x);
 410 
 411             obj = (Object)idata;
 412             break;
 413         }
 414 
 415         return obj;
 416     }
 417 
 418     /**
 419      * Returns all samples in for the specified pixel in an int array.
 420      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 421      * not in bounds.
 422      * @param x         The X coordinate of the pixel location.
 423      * @param y         The Y coordinate of the pixel location.
 424      * @param iArray    If non-null, returns the samples in this array
 425      * @param data      The DataBuffer containing the image data.
 426      * @return all samples for the specified pixel.
 427      * @see #setPixel(int, int, int[], DataBuffer)
 428      */
 429     public int [] getPixel(int x, int y, int iArray[], DataBuffer data) {
 430         if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
 431             throw new ArrayIndexOutOfBoundsException
 432                 ("Coordinate out of bounds!");
 433         }
 434         int pixels[];
 435         if (iArray == null) {
 436             pixels = new int [numBands];
 437         } else {
 438             pixels = iArray;
 439         }
 440 
 441         int value = data.getElem(y * scanlineStride + x);
 442         for (int i=0; i<numBands; i++) {
 443             pixels[i] = (value & bitMasks[i]) >>> bitOffsets[i];
 444         }
 445         return pixels;
 446     }
 447 
 448     /**
 449      * Returns all samples for the specified rectangle of pixels in
 450      * an int array, one sample per array element.
 451      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 452      * not in bounds.
 453      * @param x         The X coordinate of the upper left pixel location.
 454      * @param y         The Y coordinate of the upper left pixel location.
 455      * @param w         The width of the pixel rectangle.
 456      * @param h         The height of the pixel rectangle.
 457      * @param iArray    If non-null, returns the samples in this array.
 458      * @param data      The DataBuffer containing the image data.
 459      * @return all samples for the specified region of pixels.
 460      * @see #setPixels(int, int, int, int, int[], DataBuffer)
 461      */
 462     public int[] getPixels(int x, int y, int w, int h,
 463                            int iArray[], DataBuffer data) {
 464         int x1 = x + w;
 465         int y1 = y + h;
 466 
 467         if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
 468             y < 0 || y >= height || h > height || y1 < 0 || y1 >  height)
 469         {
 470             throw new ArrayIndexOutOfBoundsException
 471                 ("Coordinate out of bounds!");
 472         }
 473         int pixels[];
 474         if (iArray != null) {
 475            pixels = iArray;
 476         } else {
 477            pixels = new int [w*h*numBands];
 478         }
 479         int lineOffset = y*scanlineStride + x;
 480         int dstOffset = 0;
 481 
 482         for (int i = 0; i < h; i++) {
 483            for (int j = 0; j < w; j++) {
 484               int value = data.getElem(lineOffset+j);
 485               for (int k=0; k < numBands; k++) {
 486                   pixels[dstOffset++] =
 487                      ((value & bitMasks[k]) >>> bitOffsets[k]);
 488               }
 489            }
 490            lineOffset += scanlineStride;
 491         }
 492         return pixels;
 493     }


 515         return ((sample & bitMasks[b]) >>> bitOffsets[b]);
 516     }
 517 
 518     /**
 519      * Returns the samples for a specified band for the specified rectangle
 520      * of pixels in an int array, one sample per array element.
 521      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 522      * not in bounds.
 523      * @param x         The X coordinate of the upper left pixel location.
 524      * @param y         The Y coordinate of the upper left pixel location.
 525      * @param w         The width of the pixel rectangle.
 526      * @param h         The height of the pixel rectangle.
 527      * @param b         The band to return.
 528      * @param iArray    If non-null, returns the samples in this array.
 529      * @param data      The DataBuffer containing the image data.
 530      * @return the samples for the specified band for the specified
 531      *         region of pixels.
 532      * @see #setSamples(int, int, int, int, int, int[], DataBuffer)
 533      */
 534     public int[] getSamples(int x, int y, int w, int h, int b,
 535                            int iArray[], DataBuffer data) {
 536         // Bounds check for 'b' will be performed automatically
 537         if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
 538             throw new ArrayIndexOutOfBoundsException
 539                 ("Coordinate out of bounds!");
 540         }
 541         int samples[];
 542         if (iArray != null) {
 543            samples = iArray;
 544         } else {
 545            samples = new int [w*h];
 546         }
 547         int lineOffset = y*scanlineStride + x;
 548         int dstOffset = 0;
 549 
 550         for (int i = 0; i < h; i++) {
 551            for (int j = 0; j < w; j++) {
 552               int value = data.getElem(lineOffset+j);
 553               samples[dstOffset++] =
 554                  ((value & bitMasks[b]) >>> bitOffsets[b]);
 555            }
 556            lineOffset += scanlineStride;
 557         }
 558         return samples;
 559     }
 560 
 561     /**


 617 
 618         case DataBuffer.TYPE_INT:
 619 
 620             int[] iarray = (int[])obj;
 621             data.setElem(y*scanlineStride+x, iarray[0]);
 622             break;
 623         }
 624     }
 625 
 626     /**
 627      * Sets a pixel in the DataBuffer using an int array of samples for input.
 628      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 629      * not in bounds.
 630      * @param x         The X coordinate of the pixel location.
 631      * @param y         The Y coordinate of the pixel location.
 632      * @param iArray    The input samples in an int array.
 633      * @param data      The DataBuffer containing the image data.
 634      * @see #getPixel(int, int, int[], DataBuffer)
 635      */
 636     public void setPixel(int x, int y,
 637                          int iArray[],
 638                          DataBuffer data) {
 639         if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
 640             throw new ArrayIndexOutOfBoundsException
 641                 ("Coordinate out of bounds!");
 642         }
 643         int lineOffset = y * scanlineStride + x;
 644         int value = data.getElem(lineOffset);
 645         for (int i=0; i < numBands; i++) {
 646             value &= ~bitMasks[i];
 647             value |= ((iArray[i] << bitOffsets[i]) & bitMasks[i]);
 648         }
 649         data.setElem(lineOffset, value);
 650     }
 651 
 652     /**
 653      * Sets all samples for a rectangle of pixels from an int array containing
 654      * one sample per array element.
 655      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 656      * not in bounds.
 657      * @param x         The X coordinate of the upper left pixel location.
 658      * @param y         The Y coordinate of the upper left pixel location.
 659      * @param w         The width of the pixel rectangle.
 660      * @param h         The height of the pixel rectangle.
 661      * @param iArray    The input samples in an int array.
 662      * @param data      The DataBuffer containing the image data.
 663      * @see #getPixels(int, int, int, int, int[], DataBuffer)
 664      */
 665     public void setPixels(int x, int y, int w, int h,
 666                           int iArray[], DataBuffer data) {
 667         int x1 = x + w;
 668         int y1 = y + h;
 669 
 670         if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
 671             y < 0 || y >= height || h > height || y1 < 0 || y1 >  height)
 672         {
 673             throw new ArrayIndexOutOfBoundsException
 674                 ("Coordinate out of bounds!");
 675         }
 676 
 677         int lineOffset = y*scanlineStride + x;
 678         int srcOffset = 0;
 679 
 680         for (int i = 0; i < h; i++) {
 681            for (int j = 0; j < w; j++) {
 682                int value = data.getElem(lineOffset+j);
 683                for (int k=0; k < numBands; k++) {
 684                    value &= ~bitMasks[k];
 685                    int srcValue = iArray[srcOffset++];
 686                    value |= ((srcValue << bitOffsets[k])


 715         value &= ~bitMasks[b];
 716         value |= (s << bitOffsets[b]) & bitMasks[b];
 717         data.setElem(y*scanlineStride + x,value);
 718     }
 719 
 720     /**
 721      * Sets the samples in the specified band for the specified rectangle
 722      * of pixels from an int array containing one sample per array element.
 723      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 724      * not in bounds.
 725      * @param x         The X coordinate of the upper left pixel location.
 726      * @param y         The Y coordinate of the upper left pixel location.
 727      * @param w         The width of the pixel rectangle.
 728      * @param h         The height of the pixel rectangle.
 729      * @param b         The band to set.
 730      * @param iArray    The input samples in an int array.
 731      * @param data      The DataBuffer containing the image data.
 732      * @see #getSamples(int, int, int, int, int, int[], DataBuffer)
 733      */
 734     public void setSamples(int x, int y, int w, int h, int b,
 735                           int iArray[], DataBuffer data) {
 736         // Bounds check for 'b' will be performed automatically
 737         if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
 738             throw new ArrayIndexOutOfBoundsException
 739                 ("Coordinate out of bounds!");
 740         }
 741         int lineOffset = y*scanlineStride + x;
 742         int srcOffset = 0;
 743 
 744         for (int i = 0; i < h; i++) {
 745            for (int j = 0; j < w; j++) {
 746               int value = data.getElem(lineOffset+j);
 747               value &= ~bitMasks[b];
 748               int sample = iArray[srcOffset++];
 749               value |= (sample << bitOffsets[b]) & bitMasks[b];
 750               data.setElem(lineOffset+j,value);
 751            }
 752            lineOffset += scanlineStride;
 753         }
 754     }
 755 


   1 /*
   2  * Copyright (c) 1997, 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


  49  *  in the first bank of a DataBuffer.  Accessor methods are provided so
  50  *  that the image data can be manipulated directly. Scanline stride is the
  51  *  number of data array elements between a given sample and the corresponding
  52  *  sample in the same column of the next scanline. Bit masks are the masks
  53  *  required to extract the samples representing the bands of the pixel.
  54  *  Bit offsets are the offsets in bits into the data array
  55  *  element of the samples representing the bands of the pixel.
  56  * <p>
  57  * The following code illustrates extracting the bits of the sample
  58  * representing band {@code b} for pixel {@code x,y}
  59  * from DataBuffer {@code data}:
  60  * <pre>{@code
  61  *      int sample = data.getElem(y * scanlineStride + x);
  62  *      sample = (sample & bitMasks[b]) >>> bitOffsets[b];
  63  * }</pre>
  64  */
  65 
  66 public class SinglePixelPackedSampleModel extends SampleModel
  67 {
  68     /** Bit masks for all bands of the image data. */
  69     private int[] bitMasks;
  70 
  71     /** Bit Offsets for all bands of the image data. */
  72     private int[] bitOffsets;
  73 
  74     /** Bit sizes for all the bands of the image data. */
  75     private int[] bitSizes;
  76 
  77     /** Maximum bit size. */
  78     private int maxBitSize;
  79 
  80     /** Line stride of the region of image data described by this
  81      *  SinglePixelPackedSampleModel.
  82      */
  83     private int scanlineStride;
  84 
  85     private static native void initIDs();
  86     static {
  87         ColorModel.loadLibraries();
  88         initIDs();
  89     }
  90 
  91     /**
  92      * Constructs a SinglePixelPackedSampleModel with bitMasks.length bands.
  93      * Each sample is stored in a data array element in the position of
  94      * its corresponding bit mask.  Each bit mask must be contiguous and
  95      * masks must not overlap. Bit masks exceeding data type capacity are
  96      * truncated.
  97      * @param dataType  The data type for storing samples.
  98      * @param w         The width (in pixels) of the region of the
  99      *                  image data described.
 100      * @param h         The height (in pixels) of the region of the
 101      *                  image data described.
 102      * @param bitMasks  The bit masks for all bands.
 103      * @throws IllegalArgumentException if {@code dataType} is not
 104      *         either {@code DataBuffer.TYPE_BYTE},
 105      *         {@code DataBuffer.TYPE_USHORT}, or
 106      *         {@code DataBuffer.TYPE_INT}
 107      */
 108     public SinglePixelPackedSampleModel(int dataType, int w, int h,
 109                                    int[] bitMasks) {
 110         this(dataType, w, h, w, bitMasks);
 111         if (dataType != DataBuffer.TYPE_BYTE &&
 112             dataType != DataBuffer.TYPE_USHORT &&
 113             dataType != DataBuffer.TYPE_INT) {
 114             throw new IllegalArgumentException("Unsupported data type "+
 115                                                dataType);
 116         }
 117     }
 118 
 119     /**
 120      * Constructs a SinglePixelPackedSampleModel with bitMasks.length bands
 121      * and a scanline stride equal to scanlineStride data array elements.
 122      * Each sample is stored in a data array element in the position of
 123      * its corresponding bit mask.  Each bit mask must be contiguous and
 124      * masks must not overlap. Bit masks exceeding data type capacity are
 125      * truncated.
 126      * @param dataType  The data type for storing samples.
 127      * @param w         The width (in pixels) of the region of
 128      *                  image data described.
 129      * @param h         The height (in pixels) of the region of
 130      *                  image data described.
 131      * @param scanlineStride The line stride of the image data.
 132      * @param bitMasks The bit masks for all bands.
 133      * @throws IllegalArgumentException if {@code w} or
 134      *         {@code h} is not greater than 0
 135      * @throws IllegalArgumentException if any mask in
 136      *         {@code bitMask} is not contiguous
 137      * @throws IllegalArgumentException if {@code dataType} is not
 138      *         either {@code DataBuffer.TYPE_BYTE},
 139      *         {@code DataBuffer.TYPE_USHORT}, or
 140      *         {@code DataBuffer.TYPE_INT}
 141      */
 142     public SinglePixelPackedSampleModel(int dataType, int w, int h,
 143                                    int scanlineStride, int[] bitMasks) {
 144         super(dataType, w, h, bitMasks.length);
 145         if (dataType != DataBuffer.TYPE_BYTE &&
 146             dataType != DataBuffer.TYPE_USHORT &&
 147             dataType != DataBuffer.TYPE_INT) {
 148             throw new IllegalArgumentException("Unsupported data type "+
 149                                                dataType);
 150         }
 151         this.dataType = dataType;
 152         this.bitMasks = bitMasks.clone();
 153         this.scanlineStride = scanlineStride;
 154 
 155         this.bitOffsets = new int[numBands];
 156         this.bitSizes = new int[numBands];
 157 
 158         int maxMask = (int)((1L << DataBuffer.getDataTypeSize(dataType)) - 1);
 159 
 160         this.maxBitSize = 0;
 161         for (int i=0; i<numBands; i++) {
 162             int bitOffset = 0, bitSize = 0, mask;
 163             this.bitMasks[i] &= maxMask;


 289     /** Returns the scanline stride of this SinglePixelPackedSampleModel.
 290      *  @return the scanline stride of this
 291      *          {@code SinglePixelPackedSampleModel}.
 292      */
 293     public int getScanlineStride() {
 294       return scanlineStride;
 295     }
 296 
 297     /**
 298      * This creates a new SinglePixelPackedSampleModel with a subset of the
 299      * bands of this SinglePixelPackedSampleModel.  The new
 300      * SinglePixelPackedSampleModel can be used with any DataBuffer that the
 301      * existing SinglePixelPackedSampleModel can be used with.  The new
 302      * SinglePixelPackedSampleModel/DataBuffer combination will represent
 303      * an image with a subset of the bands of the original
 304      * SinglePixelPackedSampleModel/DataBuffer combination.
 305      * @exception RasterFormatException if the length of the bands argument is
 306      *                                  greater than the number of bands in
 307      *                                  the sample model.
 308      */
 309     public SampleModel createSubsetSampleModel(int[] bands) {
 310         if (bands.length > numBands)
 311             throw new RasterFormatException("There are only " +
 312                                             numBands +
 313                                             " bands");
 314         int[] newBitMasks = new int[bands.length];
 315         for (int i=0; i<bands.length; i++)
 316             newBitMasks[i] = bitMasks[bands[i]];
 317 
 318         return new SinglePixelPackedSampleModel(this.dataType, width, height,
 319                                            this.scanlineStride, newBitMasks);
 320     }
 321 
 322     /**
 323      * Returns data for a single pixel in a primitive array of type
 324      * TransferType.  For a SinglePixelPackedSampleModel, the array will
 325      * have one element, and the type will be the same as the storage
 326      * data type.  Generally, obj
 327      * should be passed in as null, so that the Object will be created
 328      * automatically and will be of the right primitive data type.
 329      * <p>
 330      * The following code illustrates transferring data for one pixel from
 331      * DataBuffer {@code db1}, whose storage layout is described by
 332      * SinglePixelPackedSampleModel {@code sppsm1}, to
 333      * DataBuffer {@code db2}, whose storage layout is described by
 334      * SinglePixelPackedSampleModel {@code sppsm2}.


 409             idata[0] = data.getElem(y * scanlineStride + x);
 410 
 411             obj = (Object)idata;
 412             break;
 413         }
 414 
 415         return obj;
 416     }
 417 
 418     /**
 419      * Returns all samples in for the specified pixel in an int array.
 420      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 421      * not in bounds.
 422      * @param x         The X coordinate of the pixel location.
 423      * @param y         The Y coordinate of the pixel location.
 424      * @param iArray    If non-null, returns the samples in this array
 425      * @param data      The DataBuffer containing the image data.
 426      * @return all samples for the specified pixel.
 427      * @see #setPixel(int, int, int[], DataBuffer)
 428      */
 429     public int [] getPixel(int x, int y, int[] iArray, DataBuffer data) {
 430         if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
 431             throw new ArrayIndexOutOfBoundsException
 432                 ("Coordinate out of bounds!");
 433         }
 434         int[] pixels;
 435         if (iArray == null) {
 436             pixels = new int [numBands];
 437         } else {
 438             pixels = iArray;
 439         }
 440 
 441         int value = data.getElem(y * scanlineStride + x);
 442         for (int i=0; i<numBands; i++) {
 443             pixels[i] = (value & bitMasks[i]) >>> bitOffsets[i];
 444         }
 445         return pixels;
 446     }
 447 
 448     /**
 449      * Returns all samples for the specified rectangle of pixels in
 450      * an int array, one sample per array element.
 451      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 452      * not in bounds.
 453      * @param x         The X coordinate of the upper left pixel location.
 454      * @param y         The Y coordinate of the upper left pixel location.
 455      * @param w         The width of the pixel rectangle.
 456      * @param h         The height of the pixel rectangle.
 457      * @param iArray    If non-null, returns the samples in this array.
 458      * @param data      The DataBuffer containing the image data.
 459      * @return all samples for the specified region of pixels.
 460      * @see #setPixels(int, int, int, int, int[], DataBuffer)
 461      */
 462     public int[] getPixels(int x, int y, int w, int h,
 463                            int[] iArray, DataBuffer data) {
 464         int x1 = x + w;
 465         int y1 = y + h;
 466 
 467         if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
 468             y < 0 || y >= height || h > height || y1 < 0 || y1 >  height)
 469         {
 470             throw new ArrayIndexOutOfBoundsException
 471                 ("Coordinate out of bounds!");
 472         }
 473         int[] pixels;
 474         if (iArray != null) {
 475            pixels = iArray;
 476         } else {
 477            pixels = new int [w*h*numBands];
 478         }
 479         int lineOffset = y*scanlineStride + x;
 480         int dstOffset = 0;
 481 
 482         for (int i = 0; i < h; i++) {
 483            for (int j = 0; j < w; j++) {
 484               int value = data.getElem(lineOffset+j);
 485               for (int k=0; k < numBands; k++) {
 486                   pixels[dstOffset++] =
 487                      ((value & bitMasks[k]) >>> bitOffsets[k]);
 488               }
 489            }
 490            lineOffset += scanlineStride;
 491         }
 492         return pixels;
 493     }


 515         return ((sample & bitMasks[b]) >>> bitOffsets[b]);
 516     }
 517 
 518     /**
 519      * Returns the samples for a specified band for the specified rectangle
 520      * of pixels in an int array, one sample per array element.
 521      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 522      * not in bounds.
 523      * @param x         The X coordinate of the upper left pixel location.
 524      * @param y         The Y coordinate of the upper left pixel location.
 525      * @param w         The width of the pixel rectangle.
 526      * @param h         The height of the pixel rectangle.
 527      * @param b         The band to return.
 528      * @param iArray    If non-null, returns the samples in this array.
 529      * @param data      The DataBuffer containing the image data.
 530      * @return the samples for the specified band for the specified
 531      *         region of pixels.
 532      * @see #setSamples(int, int, int, int, int, int[], DataBuffer)
 533      */
 534     public int[] getSamples(int x, int y, int w, int h, int b,
 535                            int[] iArray, DataBuffer data) {
 536         // Bounds check for 'b' will be performed automatically
 537         if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
 538             throw new ArrayIndexOutOfBoundsException
 539                 ("Coordinate out of bounds!");
 540         }
 541         int[] samples;
 542         if (iArray != null) {
 543            samples = iArray;
 544         } else {
 545            samples = new int [w*h];
 546         }
 547         int lineOffset = y*scanlineStride + x;
 548         int dstOffset = 0;
 549 
 550         for (int i = 0; i < h; i++) {
 551            for (int j = 0; j < w; j++) {
 552               int value = data.getElem(lineOffset+j);
 553               samples[dstOffset++] =
 554                  ((value & bitMasks[b]) >>> bitOffsets[b]);
 555            }
 556            lineOffset += scanlineStride;
 557         }
 558         return samples;
 559     }
 560 
 561     /**


 617 
 618         case DataBuffer.TYPE_INT:
 619 
 620             int[] iarray = (int[])obj;
 621             data.setElem(y*scanlineStride+x, iarray[0]);
 622             break;
 623         }
 624     }
 625 
 626     /**
 627      * Sets a pixel in the DataBuffer using an int array of samples for input.
 628      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 629      * not in bounds.
 630      * @param x         The X coordinate of the pixel location.
 631      * @param y         The Y coordinate of the pixel location.
 632      * @param iArray    The input samples in an int array.
 633      * @param data      The DataBuffer containing the image data.
 634      * @see #getPixel(int, int, int[], DataBuffer)
 635      */
 636     public void setPixel(int x, int y,
 637                          int[] iArray,
 638                          DataBuffer data) {
 639         if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
 640             throw new ArrayIndexOutOfBoundsException
 641                 ("Coordinate out of bounds!");
 642         }
 643         int lineOffset = y * scanlineStride + x;
 644         int value = data.getElem(lineOffset);
 645         for (int i=0; i < numBands; i++) {
 646             value &= ~bitMasks[i];
 647             value |= ((iArray[i] << bitOffsets[i]) & bitMasks[i]);
 648         }
 649         data.setElem(lineOffset, value);
 650     }
 651 
 652     /**
 653      * Sets all samples for a rectangle of pixels from an int array containing
 654      * one sample per array element.
 655      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 656      * not in bounds.
 657      * @param x         The X coordinate of the upper left pixel location.
 658      * @param y         The Y coordinate of the upper left pixel location.
 659      * @param w         The width of the pixel rectangle.
 660      * @param h         The height of the pixel rectangle.
 661      * @param iArray    The input samples in an int array.
 662      * @param data      The DataBuffer containing the image data.
 663      * @see #getPixels(int, int, int, int, int[], DataBuffer)
 664      */
 665     public void setPixels(int x, int y, int w, int h,
 666                           int[] iArray, DataBuffer data) {
 667         int x1 = x + w;
 668         int y1 = y + h;
 669 
 670         if (x < 0 || x >= width || w > width || x1 < 0 || x1 > width ||
 671             y < 0 || y >= height || h > height || y1 < 0 || y1 >  height)
 672         {
 673             throw new ArrayIndexOutOfBoundsException
 674                 ("Coordinate out of bounds!");
 675         }
 676 
 677         int lineOffset = y*scanlineStride + x;
 678         int srcOffset = 0;
 679 
 680         for (int i = 0; i < h; i++) {
 681            for (int j = 0; j < w; j++) {
 682                int value = data.getElem(lineOffset+j);
 683                for (int k=0; k < numBands; k++) {
 684                    value &= ~bitMasks[k];
 685                    int srcValue = iArray[srcOffset++];
 686                    value |= ((srcValue << bitOffsets[k])


 715         value &= ~bitMasks[b];
 716         value |= (s << bitOffsets[b]) & bitMasks[b];
 717         data.setElem(y*scanlineStride + x,value);
 718     }
 719 
 720     /**
 721      * Sets the samples in the specified band for the specified rectangle
 722      * of pixels from an int array containing one sample per array element.
 723      * ArrayIndexOutOfBoundsException may be thrown if the coordinates are
 724      * not in bounds.
 725      * @param x         The X coordinate of the upper left pixel location.
 726      * @param y         The Y coordinate of the upper left pixel location.
 727      * @param w         The width of the pixel rectangle.
 728      * @param h         The height of the pixel rectangle.
 729      * @param b         The band to set.
 730      * @param iArray    The input samples in an int array.
 731      * @param data      The DataBuffer containing the image data.
 732      * @see #getSamples(int, int, int, int, int, int[], DataBuffer)
 733      */
 734     public void setSamples(int x, int y, int w, int h, int b,
 735                           int[] iArray, DataBuffer data) {
 736         // Bounds check for 'b' will be performed automatically
 737         if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) {
 738             throw new ArrayIndexOutOfBoundsException
 739                 ("Coordinate out of bounds!");
 740         }
 741         int lineOffset = y*scanlineStride + x;
 742         int srcOffset = 0;
 743 
 744         for (int i = 0; i < h; i++) {
 745            for (int j = 0; j < w; j++) {
 746               int value = data.getElem(lineOffset+j);
 747               value &= ~bitMasks[b];
 748               int sample = iArray[srcOffset++];
 749               value |= (sample << bitOffsets[b]) & bitMasks[b];
 750               data.setElem(lineOffset+j,value);
 751            }
 752            lineOffset += scanlineStride;
 753         }
 754     }
 755 


< prev index next >