< prev index next >

src/java.desktop/share/classes/java/awt/image/MultiPixelPackedSampleModel.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


 212         }
 213         return dataBuffer;
 214     }
 215 
 216     /**
 217      * Returns the number of data elements needed to transfer one pixel
 218      * via the {@link #getDataElements} and {@link #setDataElements}
 219      * methods.  For a {@code MultiPixelPackedSampleModel}, this is
 220      * one.
 221      * @return the number of data elements.
 222      */
 223     public int getNumDataElements() {
 224         return 1;
 225     }
 226 
 227     /**
 228      * Returns the number of bits per sample for all bands.
 229      * @return the number of bits per sample.
 230      */
 231     public int[] getSampleSize() {
 232         int sampleSize[] = {pixelBitStride};
 233         return sampleSize;
 234     }
 235 
 236     /**
 237      * Returns the number of bits per sample for the specified band.
 238      * @param band the specified band
 239      * @return the number of bits per sample for the specified band.
 240      */
 241     public int getSampleSize(int band) {
 242         return pixelBitStride;
 243     }
 244 
 245     /**
 246      * Returns the offset of pixel (x,&nbsp;y) in data array elements.
 247      * @param x the X coordinate of the specified pixel
 248      * @param y the Y coordinate of the specified pixel
 249      * @return the offset of the specified pixel.
 250      */
 251     public int getOffset(int x, int y) {
 252         int offset = y * scanlineStride;


 309             return DataBuffer.TYPE_USHORT;
 310         else
 311             return DataBuffer.TYPE_BYTE;
 312     }
 313 
 314     /**
 315      * Creates a new {@code MultiPixelPackedSampleModel} with a
 316      * subset of the bands of this
 317      * {@code MultiPixelPackedSampleModel}.  Since a
 318      * {@code MultiPixelPackedSampleModel} only has one band, the
 319      * bands argument must have a length of one and indicate the zeroth
 320      * band.
 321      * @param bands the specified bands
 322      * @return a new {@code SampleModel} with a subset of bands of
 323      * this {@code MultiPixelPackedSampleModel}.
 324      * @exception RasterFormatException if the number of bands requested
 325      * is not one.
 326      * @throws IllegalArgumentException if {@code w} or
 327      *         {@code h} is not greater than 0
 328      */
 329     public SampleModel createSubsetSampleModel(int bands[]) {
 330         if (bands != null) {
 331            if (bands.length != 1)
 332             throw new RasterFormatException("MultiPixelPackedSampleModel has "
 333                                             + "only one band.");
 334         }
 335         SampleModel sm = createCompatibleSampleModel(width, height);
 336         return sm;
 337     }
 338 
 339     /**
 340      * Returns as {@code int} the sample in a specified band for the
 341      * pixel located at (x,&nbsp;y).  An
 342      * {@code ArrayIndexOutOfBoundsException} is thrown if the
 343      * coordinates are not in bounds.
 344      * @param x         the X coordinate of the specified pixel
 345      * @param y         the Y coordinate of the specified pixel
 346      * @param b         the band to return, which is assumed to be 0
 347      * @param data      the {@code DataBuffer} containing the image
 348      *                  data
 349      * @return the specified band containing the sample of the specified


 513         }
 514 
 515         return obj;
 516     }
 517 
 518     /**
 519      * Returns the specified single band pixel in the first element
 520      * of an {@code int} array.
 521      * {@code ArrayIndexOutOfBoundsException} is thrown if the
 522      * coordinates are not in bounds.
 523      * @param x the X coordinate of the specified pixel
 524      * @param y the Y coordinate of the specified pixel
 525      * @param iArray the array containing the pixel to be returned or
 526      *  {@code null}
 527      * @param data the {@code DataBuffer} where image data is stored
 528      * @return an array containing the specified pixel.
 529      * @exception ArrayIndexOutOfBoundsException if the coordinates
 530      *  are not in bounds
 531      * @see #setPixel(int, int, int[], DataBuffer)
 532      */
 533     public int[] getPixel(int x, int y, int iArray[], DataBuffer data) {
 534         if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
 535             throw new ArrayIndexOutOfBoundsException
 536                 ("Coordinate out of bounds!");
 537         }
 538         int pixels[];
 539         if (iArray != null) {
 540            pixels = iArray;
 541         } else {
 542            pixels = new int [numBands];
 543         }
 544         int bitnum = dataBitOffset + x*pixelBitStride;
 545         int element = data.getElem(y*scanlineStride + bitnum/dataElementSize);
 546         int shift = dataElementSize - (bitnum & (dataElementSize-1))
 547                     - pixelBitStride;
 548         pixels[0] = (element >> shift) & bitMask;
 549         return pixels;
 550     }
 551 
 552     /**
 553      * Sets the data for a single pixel in the specified
 554      * {@code DataBuffer} from a primitive array of type
 555      * TransferType.  For a {@code MultiPixelPackedSampleModel},
 556      * only the first element of the array holds valid data,
 557      * and the type must be the smallest of
 558      * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT


   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


 212         }
 213         return dataBuffer;
 214     }
 215 
 216     /**
 217      * Returns the number of data elements needed to transfer one pixel
 218      * via the {@link #getDataElements} and {@link #setDataElements}
 219      * methods.  For a {@code MultiPixelPackedSampleModel}, this is
 220      * one.
 221      * @return the number of data elements.
 222      */
 223     public int getNumDataElements() {
 224         return 1;
 225     }
 226 
 227     /**
 228      * Returns the number of bits per sample for all bands.
 229      * @return the number of bits per sample.
 230      */
 231     public int[] getSampleSize() {
 232         int[] sampleSize = {pixelBitStride};
 233         return sampleSize;
 234     }
 235 
 236     /**
 237      * Returns the number of bits per sample for the specified band.
 238      * @param band the specified band
 239      * @return the number of bits per sample for the specified band.
 240      */
 241     public int getSampleSize(int band) {
 242         return pixelBitStride;
 243     }
 244 
 245     /**
 246      * Returns the offset of pixel (x,&nbsp;y) in data array elements.
 247      * @param x the X coordinate of the specified pixel
 248      * @param y the Y coordinate of the specified pixel
 249      * @return the offset of the specified pixel.
 250      */
 251     public int getOffset(int x, int y) {
 252         int offset = y * scanlineStride;


 309             return DataBuffer.TYPE_USHORT;
 310         else
 311             return DataBuffer.TYPE_BYTE;
 312     }
 313 
 314     /**
 315      * Creates a new {@code MultiPixelPackedSampleModel} with a
 316      * subset of the bands of this
 317      * {@code MultiPixelPackedSampleModel}.  Since a
 318      * {@code MultiPixelPackedSampleModel} only has one band, the
 319      * bands argument must have a length of one and indicate the zeroth
 320      * band.
 321      * @param bands the specified bands
 322      * @return a new {@code SampleModel} with a subset of bands of
 323      * this {@code MultiPixelPackedSampleModel}.
 324      * @exception RasterFormatException if the number of bands requested
 325      * is not one.
 326      * @throws IllegalArgumentException if {@code w} or
 327      *         {@code h} is not greater than 0
 328      */
 329     public SampleModel createSubsetSampleModel(int[] bands) {
 330         if (bands != null) {
 331            if (bands.length != 1)
 332             throw new RasterFormatException("MultiPixelPackedSampleModel has "
 333                                             + "only one band.");
 334         }
 335         SampleModel sm = createCompatibleSampleModel(width, height);
 336         return sm;
 337     }
 338 
 339     /**
 340      * Returns as {@code int} the sample in a specified band for the
 341      * pixel located at (x,&nbsp;y).  An
 342      * {@code ArrayIndexOutOfBoundsException} is thrown if the
 343      * coordinates are not in bounds.
 344      * @param x         the X coordinate of the specified pixel
 345      * @param y         the Y coordinate of the specified pixel
 346      * @param b         the band to return, which is assumed to be 0
 347      * @param data      the {@code DataBuffer} containing the image
 348      *                  data
 349      * @return the specified band containing the sample of the specified


 513         }
 514 
 515         return obj;
 516     }
 517 
 518     /**
 519      * Returns the specified single band pixel in the first element
 520      * of an {@code int} array.
 521      * {@code ArrayIndexOutOfBoundsException} is thrown if the
 522      * coordinates are not in bounds.
 523      * @param x the X coordinate of the specified pixel
 524      * @param y the Y coordinate of the specified pixel
 525      * @param iArray the array containing the pixel to be returned or
 526      *  {@code null}
 527      * @param data the {@code DataBuffer} where image data is stored
 528      * @return an array containing the specified pixel.
 529      * @exception ArrayIndexOutOfBoundsException if the coordinates
 530      *  are not in bounds
 531      * @see #setPixel(int, int, int[], DataBuffer)
 532      */
 533     public int[] getPixel(int x, int y, int[] iArray, DataBuffer data) {
 534         if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
 535             throw new ArrayIndexOutOfBoundsException
 536                 ("Coordinate out of bounds!");
 537         }
 538         int[] pixels;
 539         if (iArray != null) {
 540            pixels = iArray;
 541         } else {
 542            pixels = new int [numBands];
 543         }
 544         int bitnum = dataBitOffset + x*pixelBitStride;
 545         int element = data.getElem(y*scanlineStride + bitnum/dataElementSize);
 546         int shift = dataElementSize - (bitnum & (dataElementSize-1))
 547                     - pixelBitStride;
 548         pixels[0] = (element >> shift) & bitMask;
 549         return pixels;
 550     }
 551 
 552     /**
 553      * Sets the data for a single pixel in the specified
 554      * {@code DataBuffer} from a primitive array of type
 555      * TransferType.  For a {@code MultiPixelPackedSampleModel},
 556      * only the first element of the array holds valid data,
 557      * and the type must be the smallest of
 558      * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT


< prev index next >