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

Print this page


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


 132      * @param bitMasks The bit masks for all bands.
 133      * @throws IllegalArgumentException if <code>w</code> or
 134      *         <code>h</code> is not greater than 0
 135      * @throws IllegalArgumentException if any mask in
 136      *         <code>bitMask</code> is not contiguous
 137      * @throws IllegalArgumentException if <code>dataType</code> is not
 138      *         either <code>DataBuffer.TYPE_BYTE</code>,
 139      *         <code>DataBuffer.TYPE_USHORT</code>, or
 140      *         <code>DataBuffer.TYPE_INT</code>
 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 = (int[]) 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;
 164             mask = this.bitMasks[i];
 165             if (mask != 0) {
 166                 while ((mask & 1) == 0) {
 167                     mask = mask >>> 1;
 168                     bitOffset++;
 169                 }
 170                 while ((mask & 1) == 1) {
 171                     mask = mask >>> 1;
 172                     bitSize++;


 259      *  The data element containing pixel <code>x,y</code>
 260      *  can be retrieved from a DataBuffer <code>data</code> with a
 261      *  SinglePixelPackedSampleModel <code>sppsm</code> as:
 262      * <pre>
 263      *        data.getElem(sppsm.getOffset(x, y));
 264      * </pre>
 265      * @param x the X coordinate of the specified pixel
 266      * @param y the Y coordinate of the specified pixel
 267      * @return the offset of the specified pixel.
 268      */
 269     public int getOffset(int x, int y) {
 270         int offset = y * scanlineStride + x;
 271         return offset;
 272     }
 273 
 274     /** Returns the bit offsets into the data array element representing
 275      *  a pixel for all bands.
 276      *  @return the bit offsets representing a pixel for all bands.
 277      */
 278     public int [] getBitOffsets() {
 279       return (int[])bitOffsets.clone();
 280     }
 281 
 282     /** Returns the bit masks for all bands.
 283      *  @return the bit masks for all bands.
 284      */
 285     public int [] getBitMasks() {
 286       return (int[])bitMasks.clone();
 287     }
 288 
 289     /** Returns the scanline stride of this SinglePixelPackedSampleModel.
 290      *  @return the scanline stride of this
 291      *          <code>SinglePixelPackedSampleModel</code>.
 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


 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 |= ((int)sample << bitOffsets[b]) & bitMasks[b];
 750               data.setElem(lineOffset+j,value);
 751            }
 752            lineOffset += scanlineStride;
 753         }
 754     }
 755 
 756     public boolean equals(Object o) {
 757         if ((o == null) || !(o instanceof SinglePixelPackedSampleModel)) {
 758             return false;
 759         }
 760 
 761         SinglePixelPackedSampleModel that = (SinglePixelPackedSampleModel)o;
 762         return this.width == that.width &&
 763             this.height == that.height &&
 764             this.numBands == that.numBands &&
 765             this.dataType == that.dataType &&
 766             Arrays.equals(this.bitMasks, that.bitMasks) &&
 767             Arrays.equals(this.bitOffsets, that.bitOffsets) &&
 768             Arrays.equals(this.bitSizes, that.bitSizes) &&
 769             this.maxBitSize == that.maxBitSize &&


   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


 132      * @param bitMasks The bit masks for all bands.
 133      * @throws IllegalArgumentException if <code>w</code> or
 134      *         <code>h</code> is not greater than 0
 135      * @throws IllegalArgumentException if any mask in
 136      *         <code>bitMask</code> is not contiguous
 137      * @throws IllegalArgumentException if <code>dataType</code> is not
 138      *         either <code>DataBuffer.TYPE_BYTE</code>,
 139      *         <code>DataBuffer.TYPE_USHORT</code>, or
 140      *         <code>DataBuffer.TYPE_INT</code>
 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;
 164             mask = this.bitMasks[i];
 165             if (mask != 0) {
 166                 while ((mask & 1) == 0) {
 167                     mask = mask >>> 1;
 168                     bitOffset++;
 169                 }
 170                 while ((mask & 1) == 1) {
 171                     mask = mask >>> 1;
 172                     bitSize++;


 259      *  The data element containing pixel <code>x,y</code>
 260      *  can be retrieved from a DataBuffer <code>data</code> with a
 261      *  SinglePixelPackedSampleModel <code>sppsm</code> as:
 262      * <pre>
 263      *        data.getElem(sppsm.getOffset(x, y));
 264      * </pre>
 265      * @param x the X coordinate of the specified pixel
 266      * @param y the Y coordinate of the specified pixel
 267      * @return the offset of the specified pixel.
 268      */
 269     public int getOffset(int x, int y) {
 270         int offset = y * scanlineStride + x;
 271         return offset;
 272     }
 273 
 274     /** Returns the bit offsets into the data array element representing
 275      *  a pixel for all bands.
 276      *  @return the bit offsets representing a pixel for all bands.
 277      */
 278     public int [] getBitOffsets() {
 279       return bitOffsets.clone();
 280     }
 281 
 282     /** Returns the bit masks for all bands.
 283      *  @return the bit masks for all bands.
 284      */
 285     public int [] getBitMasks() {
 286       return bitMasks.clone();
 287     }
 288 
 289     /** Returns the scanline stride of this SinglePixelPackedSampleModel.
 290      *  @return the scanline stride of this
 291      *          <code>SinglePixelPackedSampleModel</code>.
 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


 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 
 756     public boolean equals(Object o) {
 757         if ((o == null) || !(o instanceof SinglePixelPackedSampleModel)) {
 758             return false;
 759         }
 760 
 761         SinglePixelPackedSampleModel that = (SinglePixelPackedSampleModel)o;
 762         return this.width == that.width &&
 763             this.height == that.height &&
 764             this.numBands == that.numBands &&
 765             this.dataType == that.dataType &&
 766             Arrays.equals(this.bitMasks, that.bitMasks) &&
 767             Arrays.equals(this.bitOffsets, that.bitOffsets) &&
 768             Arrays.equals(this.bitSizes, that.bitSizes) &&
 769             this.maxBitSize == that.maxBitSize &&