
META-INF.modules.java.desktop.classes.java.awt.image.ComponentSampleModel Maven / Gradle / Ivy
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* ****************************************************************
******************************************************************
******************************************************************
*** COPYRIGHT (c) Eastman Kodak Company, 1997
*** As an unpublished work pursuant to Title 17 of the United
*** States Code. All rights reserved.
******************************************************************
******************************************************************
******************************************************************/
package java.awt.image;
import java.util.Arrays;
/**
* This class represents image data which is stored such that each sample
* of a pixel occupies one data element of the DataBuffer. It stores the
* N samples which make up a pixel in N separate data array elements.
* Different bands may be in different banks of the DataBuffer.
* Accessor methods are provided so that image data can be manipulated
* directly. This class can support different kinds of interleaving, e.g.
* band interleaving, scanline interleaving, and pixel interleaving.
* Pixel stride is the number of data array elements between two samples
* for the same band on the same scanline. Scanline stride is the number
* of data array elements between a given sample and the corresponding sample
* in the same column of the next scanline. Band offsets denote the number
* of data array elements from the first data array element of the bank
* of the DataBuffer holding each band to the first sample of the band.
* The bands are numbered from 0 to N-1. This class can represent image
* data for which each sample is an unsigned integral number which can be
* stored in 8, 16, or 32 bits (using {@code DataBuffer.TYPE_BYTE},
* {@code DataBuffer.TYPE_USHORT}, or {@code DataBuffer.TYPE_INT},
* respectively), data for which each sample is a signed integral number
* which can be stored in 16 bits (using {@code DataBuffer.TYPE_SHORT}),
* or data for which each sample is a signed float or double quantity
* (using {@code DataBuffer.TYPE_FLOAT} or
* {@code DataBuffer.TYPE_DOUBLE}, respectively).
* All samples of a given ComponentSampleModel
* are stored with the same precision. All strides and offsets must be
* non-negative. This class supports
* {@link DataBuffer#TYPE_BYTE TYPE_BYTE},
* {@link DataBuffer#TYPE_USHORT TYPE_USHORT},
* {@link DataBuffer#TYPE_SHORT TYPE_SHORT},
* {@link DataBuffer#TYPE_INT TYPE_INT},
* {@link DataBuffer#TYPE_FLOAT TYPE_FLOAT},
* {@link DataBuffer#TYPE_DOUBLE TYPE_DOUBLE},
* @see java.awt.image.PixelInterleavedSampleModel
* @see java.awt.image.BandedSampleModel
*/
public class ComponentSampleModel extends SampleModel
{
/** Offsets for all bands in data array elements. */
protected int[] bandOffsets;
/** Index for each bank storing a band of image data. */
protected int[] bankIndices;
/**
* The number of bands in this
* {@code ComponentSampleModel}.
*/
protected int numBands = 1;
/**
* The number of banks in this
* {@code ComponentSampleModel}.
*/
protected int numBanks = 1;
/**
* Line stride (in data array elements) of the region of image
* data described by this ComponentSampleModel.
*/
protected int scanlineStride;
/** Pixel stride (in data array elements) of the region of image
* data described by this ComponentSampleModel.
*/
protected int pixelStride;
/**
* Constructs a ComponentSampleModel with the specified parameters.
* The number of bands will be given by the length of the bandOffsets array.
* All bands will be stored in the first bank of the DataBuffer.
* @param dataType the data type for storing samples
* @param w the width (in pixels) of the region of
* image data described
* @param h the height (in pixels) of the region of
* image data described
* @param pixelStride the pixel stride of the region of image
* data described
* @param scanlineStride the line stride of the region of image
* data described
* @param bandOffsets the offsets of all bands
* @throws IllegalArgumentException if {@code w} or
* {@code h} is not greater than 0
* @throws IllegalArgumentException if {@code pixelStride}
* is less than 0
* @throws IllegalArgumentException if {@code scanlineStride}
* is less than 0
* @throws IllegalArgumentException if {@code numBands}
* is less than 1
* @throws IllegalArgumentException if the product of {@code w}
* and {@code h} is greater than
* {@code Integer.MAX_VALUE}
* @throws IllegalArgumentException if {@code dataType} is not
* one of the supported data types
*/
public ComponentSampleModel(int dataType,
int w, int h,
int pixelStride,
int scanlineStride,
int[] bandOffsets) {
super(dataType, w, h, bandOffsets.length);
this.dataType = dataType;
this.pixelStride = pixelStride;
this.scanlineStride = scanlineStride;
this.bandOffsets = bandOffsets.clone();
numBands = this.bandOffsets.length;
if (pixelStride < 0) {
throw new IllegalArgumentException("Pixel stride must be >= 0");
}
// TODO - bug 4296691 - remove this check
if (scanlineStride < 0) {
throw new IllegalArgumentException("Scanline stride must be >= 0");
}
if (numBands < 1) {
throw new IllegalArgumentException("Must have at least one band.");
}
if ((dataType < DataBuffer.TYPE_BYTE) ||
(dataType > DataBuffer.TYPE_DOUBLE)) {
throw new IllegalArgumentException("Unsupported dataType.");
}
bankIndices = new int[numBands];
for (int i=0; i= 0");
}
// TODO - bug 4296691 - remove this check
if (scanlineStride < 0) {
throw new IllegalArgumentException("Scanline stride must be >= 0");
}
if ((dataType < DataBuffer.TYPE_BYTE) ||
(dataType > DataBuffer.TYPE_DOUBLE)) {
throw new IllegalArgumentException("Unsupported dataType.");
}
int maxBank = this.bankIndices[0];
if (maxBank < 0) {
throw new IllegalArgumentException("Index of bank 0 is less than "+
"0 ("+maxBank+")");
}
for (int i=1; i < this.bankIndices.length; i++) {
if (this.bankIndices[i] > maxBank) {
maxBank = this.bankIndices[i];
}
else if (this.bankIndices[i] < 0) {
throw new IllegalArgumentException("Index of bank "+i+
" is less than 0 ("+
maxBank+")");
}
}
numBanks = maxBank+1;
numBands = this.bandOffsets.length;
if (this.bandOffsets.length != this.bankIndices.length) {
throw new IllegalArgumentException("Length of bandOffsets must "+
"equal length of bankIndices.");
}
verify();
}
private void verify() {
int requiredSize = getBufferSize();
}
/**
* Returns the size of the data buffer (in data elements) needed
* for a data buffer that matches this ComponentSampleModel.
*/
private int getBufferSize() {
int maxBandOff=bandOffsets[0];
for (int i=1; i (Integer.MAX_VALUE - 1)) {
throw new IllegalArgumentException("Invalid band offset");
}
if (pixelStride < 0 || pixelStride > (Integer.MAX_VALUE / width)) {
throw new IllegalArgumentException("Invalid pixel stride");
}
if (scanlineStride < 0 || scanlineStride > (Integer.MAX_VALUE / height)) {
throw new IllegalArgumentException("Invalid scanline stride");
}
int size = maxBandOff + 1;
int val = pixelStride * (width - 1);
if (val > (Integer.MAX_VALUE - size)) {
throw new IllegalArgumentException("Invalid pixel stride");
}
size += val;
val = scanlineStride * (height - 1);
if (val > (Integer.MAX_VALUE - size)) {
throw new IllegalArgumentException("Invalid scan stride");
}
size += val;
return size;
}
/**
* Preserves band ordering with new step factor...
*/
int []orderBands(int[] orig, int step) {
int[] map = new int[orig.length];
int[] ret = new int[orig.length];
for (int i=0; i orig[map[j]]) {
index = j;
}
}
ret[map[index]] = i*step;
map[index] = map[i];
}
return ret;
}
/**
* Creates a new {@code ComponentSampleModel} with the specified
* width and height. The new {@code SampleModel} will have the same
* number of bands, storage data type, interleaving scheme, and
* pixel stride as this {@code SampleModel}.
* @param w the width of the resulting {@code SampleModel}
* @param h the height of the resulting {@code SampleModel}
* @return a new {@code ComponentSampleModel} with the specified size
* @throws IllegalArgumentException if {@code w} or
* {@code h} is not greater than 0
*/
public SampleModel createCompatibleSampleModel(int w, int h) {
SampleModel ret=null;
long size;
int minBandOff=bandOffsets[0];
int maxBandOff=bandOffsets[0];
for (int i=1; i lStride) {
if (pStride > bStride) {
if (lStride > bStride) { // pix > line > band
bandOff = new int[bandOffsets.length];
for (int i=0; i band > line
bandOff = orderBands(bandOffsets,lStride*h);
pStride = bands*lStride*h;
}
} else { // band > pix > line
pStride = lStride*h;
bandOff = orderBands(bandOffsets,pStride*w);
}
} else {
if (pStride > bStride) { // line > pix > band
bandOff = new int[bandOffsets.length];
for (int i=0; i bStride) { // line > band > pix
bandOff = orderBands(bandOffsets,pStride*w);
lStride = bands*pStride*w;
} else { // band > line > pix
lStride = pStride*w;
bandOff = orderBands(bandOffsets,lStride*h);
}
}
}
// make sure we make room for negative offsets...
int base = 0;
if (scanlineStride < 0) {
base += lStride*h;
lStride *= -1;
}
if (pixelStride < 0) {
base += pStride*w;
pStride *= -1;
}
for (int i=0; i bankIndices.length)
throw new RasterFormatException("There are only " +
bankIndices.length +
" bands");
int[] newBankIndices = new int[bands.length];
int[] newBandOffsets = new int[bands.length];
for (int i=0; i
* data.getElem(csm.getOffset(x, y));
*
* @param x the X location of the pixel
* @param y the Y location of the pixel
* @return the offset for the first band of the specified pixel.
*/
public int getOffset(int x, int y) {
int offset = y*scanlineStride + x*pixelStride + bandOffsets[0];
return offset;
}
/** Gets the offset for band b of pixel (x,y).
* A sample of band {@code b} can be retrieved from a
* {@code DataBuffer data}
* with a {@code ComponentSampleModel csm} as
* * data.getElem(csm.getOffset(x, y, b)); ** @param x the X location of the specified pixel * @param y the Y location of the specified pixel * @param b the specified band * @return the offset for the specified band of the specified pixel. */ public int getOffset(int x, int y, int b) { int offset = y*scanlineStride + x*pixelStride + bandOffsets[b]; return offset; } /** Returns the number of bits per sample for all bands. * @return an array containing the number of bits per sample * for all bands, where each element in the array * represents a band. */ public final int[] getSampleSize() { int[] sampleSize = new int [numBands]; int sizeInBits = getSampleSize(0); for (int i=0; i
* ComponentSampleModel csm1, csm2; * DataBufferInt db1, db2; * csm2.setDataElements(x, y, * csm1.getDataElements(x, y, null, db1), db2); ** * Using {@code getDataElements} and {@code setDataElements} * to transfer between two {@code DataBuffer/SampleModel} * pairs is legitimate if the {@code SampleModel} objects have * the same number of bands, corresponding bands have the same number of * bits per sample, and the {@code TransferType}s are the same. *
* If {@code obj} is not {@code null}, it should be a
* primitive array of type {@code TransferType}.
* Otherwise, a {@code ClassCastException} is thrown. An
* {@code ArrayIndexOutOfBoundsException} might be thrown if the
* coordinates are not in bounds, or if {@code obj} is not
* {@code null} and is not large enough to hold
* the pixel data.
*
* @param x the X coordinate of the pixel location
* @param y the Y coordinate of the pixel location
* @param obj if non-{@code null}, a primitive array
* in which to return the pixel data
* @param data the {@code DataBuffer} containing the image data
* @return the data of the specified pixel
* @see #setDataElements(int, int, Object, DataBuffer)
*
* @throws NullPointerException if data is null.
* @throws ArrayIndexOutOfBoundsException if the coordinates are
* not in bounds, or if obj is too small to hold the output.
*/
public Object getDataElements(int x, int y, Object obj, DataBuffer data) {
if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
int type = getTransferType();
int numDataElems = getNumDataElements();
int pixelOffset = y*scanlineStride + x*pixelStride;
switch(type) {
case DataBuffer.TYPE_BYTE:
byte[] bdata;
if (obj == null)
bdata = new byte[numDataElems];
else
bdata = (byte[])obj;
for (int i=0; i
* The following code illustrates transferring data for one pixel from
* {@code DataBuffer db1}, whose storage layout is
* described by {@code ComponentSampleModel csm1},
* to {@code DataBuffer db2}, whose storage layout
* is described by {@code ComponentSampleModel csm2}.
* The transfer is usually more efficient than using
* {@code getPixel} and {@code setPixel}.
*
* A {@code ClassCastException} is thrown if {@code obj} is not
* a primitive array of type {@code TransferType}.
* An {@code ArrayIndexOutOfBoundsException} might be thrown if
* the coordinates are not in bounds, or if {@code obj} is not large
* enough to hold the pixel data.
* @param x the X coordinate of the pixel location
* @param y the Y coordinate of the pixel location
* @param obj a primitive array containing pixel data
* @param data the DataBuffer containing the image data
* @see #getDataElements(int, int, Object, DataBuffer)
*/
public void setDataElements(int x, int y, Object obj, DataBuffer data) {
if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) {
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
int type = getTransferType();
int numDataElems = getNumDataElements();
int pixelOffset = y*scanlineStride + x*pixelStride;
switch(type) {
case DataBuffer.TYPE_BYTE:
byte[] barray = (byte[])obj;
for (int i=0; i
* ComponentSampleModel csm1, csm2;
* DataBufferInt db1, db2;
* csm2.setDataElements(x, y, csm1.getDataElements(x, y, null, db1),
* db2);
*
* Using {@code getDataElements} and {@code setDataElements}
* to transfer between two {@code DataBuffer/SampleModel} pairs
* is legitimate if the {@code SampleModel} objects have
* the same number of bands, corresponding bands have the same number of
* bits per sample, and the {@code TransferType}s are the same.
*