下面列出了java.awt.image.PixelInterleavedSampleModel#java.awt.image.Raster 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Stores the Raster data at the specified location.
* An ArrayIndexOutOfBounds exception will be thrown at runtime
* if the pixel coordinates are out of bounds.
* @param x The X coordinate of the pixel location.
* @param y The Y coordinate of the pixel location.
* @param inRaster Raster of data to place at x,y location.
*/
public void setDataElements(int x, int y, Raster inRaster) {
// Check if we can use fast code
if (!(inRaster instanceof BytePackedRaster) ||
((BytePackedRaster)inRaster).pixelBitStride != pixelBitStride) {
super.setDataElements(x, y, inRaster);
return;
}
int srcOffX = inRaster.getMinX();
int srcOffY = inRaster.getMinY();
int dstOffX = srcOffX + x;
int dstOffY = srcOffY + y;
int width = inRaster.getWidth();
int height = inRaster.getHeight();
if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
(dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
throw new ArrayIndexOutOfBoundsException
("Coordinate out of bounds!");
}
setDataElements(dstOffX, dstOffY,
srcOffX, srcOffY,
width, height,
(BytePackedRaster)inRaster);
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
/**
* Stores the Raster data at the specified location.
* @param dstX The absolute X coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param dstY The absolute Y coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param width The number of pixels to store horizontally
* @param height The number of pixels to store vertically
* @param inRaster Raster of data to place at x,y location.
*/
private void setDataElements(int dstX, int dstY,
int width, int height,
Raster inRaster) {
// Assume bounds checking has been performed previously
if (width <= 0 || height <= 0) {
return;
}
// Write inRaster (minX, minY) to (dstX, dstY)
int srcOffX = inRaster.getMinX();
int srcOffY = inRaster.getMinY();
Object tdata = null;
// // REMIND: Do something faster!
// if (inRaster instanceof ShortBandedRaster) {
// }
for (int startY=0; startY < height; startY++) {
// Grab one scanline at a time
tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
width, 1, tdata);
setDataElements(dstX, dstY + startY, width, 1, tdata);
}
}
/**
* Stores the Raster data at the specified location.
* @param dstX The absolute X coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param dstY The absolute Y coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param width The number of pixels to store horizontally
* @param height The number of pixels to store vertically
* @param inRaster Raster of data to place at x,y location.
*/
private void setDataElements(int dstX, int dstY,
int width, int height,
Raster inRaster) {
// Assume bounds checking has been performed previously
if (width <= 0 || height <= 0) {
return;
}
// Write inRaster (minX, minY) to (dstX, dstY)
int srcOffX = inRaster.getMinX();
int srcOffY = inRaster.getMinY();
Object tdata = null;
// // REMIND: Do something faster!
// if (inRaster instanceof ShortBandedRaster) {
// }
for (int startY=0; startY < height; startY++) {
// Grab one scanline at a time
tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
width, 1, tdata);
setDataElements(dstX, dstY + startY, width, 1, tdata);
}
}
public synchronized Raster getRaster(int x, int y, int w, int h) {
WritableRaster t = savedTile;
if (t == null || w > t.getWidth() || h > t.getHeight()) {
t = getColorModel().createCompatibleWritableRaster(w, h);
IntegerComponentRaster icr = (IntegerComponentRaster) t;
Arrays.fill(icr.getDataStorage(), color);
// Note - markDirty is probably unnecessary since icr is brand new
icr.markDirty();
if (w <= 64 && h <= 64) {
savedTile = t;
}
}
return t;
}
public double getSlope(BufferedImage bi1, BufferedImage bi2, int u, int v, int s, int n) throws IOException {
Raster r1 = bi1.getRaster().createTranslatedChild(0,0);
Raster r2 = bi2.getRaster().createTranslatedChild(0,0);
if (r1.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
if (r2.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
SimpleRegression reg = new SimpleRegression(true);
int minX = u<0?u*-1:0;
int minY = v<0?v*-1:0;
int maxX = u>0?bi1.getWidth()-u: bi1.getWidth();
int maxY = v>0?bi1.getHeight()-v: bi1.getHeight();
for (int x=minX; x<maxX; x++) {
for (int y=minY; y<maxY; y++) {
double d1 = r1.getSampleDouble(x+u,y+v,0);
if (d1> intensityThreshold) {
double d2 = r2.getSampleDouble(x, y, 0);
reg.addData(d2, d1);
}
}
}
double slope = reg.getSlope();
double intercept = reg.getIntercept();
logger.info("i,j: "+s+","+n+": "+ "slope: "+slope+" ; intercept: "+intercept);
return slope;
}
public static void main(String[] args) {
byte[][] data = new byte[1][10];
ByteLookupTable lut = new ByteLookupTable(0, data);
RasterOp op = new LookupOp(lut, null);
int[] bandOffsets = {0};
Point location = new Point(0, 0);
DataBuffer db = new DataBufferByte(10 * 10);
SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE,
10, 10, 1, 10,
bandOffsets);
Raster src = Raster.createRaster(sm, db, location);
op.filter(src, null); // this used to result in NullPointerException
}
@Override
public Raster getRaster(int x, int y, int w, int h) {
WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
int[] data = new int[w * h * 4];
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
double distance = mPoint.distance(x + i, y + j);
double radius = mRadius.distance(0, 0);
double ratio = distance / radius;
if (ratio > 1.0) {
ratio = 1.0;
}
int base = (j * w + i) * 4;
data[base] = (int) (color1.getRed() + ratio * (color2.getRed() - color1.getRed()));
data[base + 1] = (int) (color1.getGreen() + ratio * (color2.getGreen() - color1.getGreen()));
data[base + 2] = (int) (color1.getBlue() + ratio * (color2.getBlue() - color1.getBlue()));
data[base + 3] = (int) (color1.getAlpha() + ratio * (color2.getAlpha() - color1.getAlpha()));
}
}
raster.setPixels(0, 0, w, h, data);
return raster;
}
public BufferedImage render(int width, int height) {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
render(image);
Raster raster = image.getData();
lattice = new int[width][height];
int[] pixel = new int[4];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
raster.getPixel(i, j, pixel);
if (colorDistanceSquared(pixel, shapeColor) < colorDistanceSquared(pixel, background)) {
lattice[i][j] = 1;
} else {
lattice[i][j] = 0;
}
}
}
return image;
}
@Override
public WritableRaster createCompatibleWritableRaster(final int w,
final int h)
{
if (pixelBits == 16) {
final int[] bandOffsets = new int[nChannels];
for (int i = 0; i < nChannels; i++)
bandOffsets[i] = i;
final SampleModel m = new ComponentSampleModel(DataBuffer.TYPE_SHORT, w,
h, nChannels, w * nChannels, bandOffsets);
final DataBuffer db = new DataBufferShort(w * h, nChannels);
return Raster.createWritableRaster(m, db, null);
}
return helper.createCompatibleWritableRaster(w, h);
}
/**
* <p>Returns an array of pixels, stored as integers, from a <code>BufferedImage</code>. The pixels are grabbed from
* a rectangular area defined by a location and two dimensions. Calling this method on an image of type different
* from <code>BufferedImage.TYPE_INT_ARGB</code> and <code>BufferedImage.TYPE_INT_RGB</code> will unmanage the
* image.</p>
*
* @param img the source image
* @param x the x location at which to start grabbing pixels
* @param y the y location at which to start grabbing pixels
* @param w the width of the rectangle of pixels to grab
* @param h the height of the rectangle of pixels to grab
* @param pixels a pre-allocated array of pixels of size w*h; can be null
* @return <code>pixels</code> if non-null, a new array of integers otherwise
* @throws IllegalArgumentException is <code>pixels</code> is non-null and of length < w*h
*/
static byte[] getPixels(BufferedImage img,
int x, int y, int w, int h, byte[] pixels) {
if (w == 0 || h == 0) {
return new byte[0];
}
if (pixels == null) {
pixels = new byte[w * h];
} else if (pixels.length < w * h) {
throw new IllegalArgumentException("pixels array must have a length >= w*h");
}
int imageType = img.getType();
if (imageType == BufferedImage.TYPE_BYTE_GRAY) {
Raster raster = img.getRaster();
return (byte[]) raster.getDataElements(x, y, w, h, pixels);
} else {
throw new IllegalArgumentException("Only type BYTE_GRAY is supported");
}
}
/**
* Stores the Raster data at the specified location.
* @param dstX The absolute X coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param dstY The absolute Y coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param width The number of pixels to store horizontally
* @param height The number of pixels to store vertically
* @param inRaster Raster of data to place at x,y location.
*/
private void setDataElements(int dstX, int dstY,
int width, int height,
Raster inRaster) {
// Assume bounds checking has been performed previously
if (width <= 0 || height <= 0) {
return;
}
// Write inRaster (minX, minY) to (dstX, dstY)
int srcOffX = inRaster.getMinX();
int srcOffY = inRaster.getMinY();
Object tdata = null;
// // REMIND: Do something faster!
// if (inRaster instanceof ShortBandedRaster) {
// }
for (int startY=0; startY < height; startY++) {
// Grab one scanline at a time
tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
width, 1, tdata);
setDataElements(dstX, dstY + startY, width, 1, tdata);
}
}
/**
* Stores the Raster data at the specified location.
* @param dstX The absolute X coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param dstY The absolute Y coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param width The number of pixels to store horizontally
* @param height The number of pixels to store vertically
* @param inRaster Raster of data to place at x,y location.
*/
private void setDataElements(int dstX, int dstY,
int width, int height,
Raster inRaster) {
// Assume bounds checking has been performed previously
if (width <= 0 || height <= 0) {
return;
}
// Write inRaster (minX, minY) to (dstX, dstY)
int srcOffX = inRaster.getMinX();
int srcOffY = inRaster.getMinY();
Object tdata = null;
// // REMIND: Do something faster!
// if (inRaster instanceof ShortComponentRaster) {
// }
for (int startY=0; startY < height; startY++) {
// Grab one scanline at a time
tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
width, 1, tdata);
setDataElements(dstX, dstY + startY, width, 1, tdata);
}
}
/**
* Stores the Raster data at the specified location.
* @param dstX The absolute X coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param dstY The absolute Y coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param width The number of pixels to store horizontally
* @param height The number of pixels to store vertically
* @param inRaster Raster of data to place at x,y location.
*/
private void setDataElements(int dstX, int dstY,
int width, int height,
Raster inRaster) {
// Assume bounds checking has been performed previously
if (width <= 0 || height <= 0) {
return;
}
// Write inRaster (minX, minY) to (dstX, dstY)
int srcOffX = inRaster.getMinX();
int srcOffY = inRaster.getMinY();
Object tdata = null;
// REMIND: Do something faster!
// if (inRaster instanceof ShortInterleavedRaster) {
// }
for (int startY=0; startY < height; startY++) {
// Grab one scanline at a time
tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
width, 1, tdata);
setDataElements(dstX, dstY + startY, width, 1, tdata);
}
}
/**
* Get datasets from an image
* @param image
* @param keepBitWidth if true, then use signed primitives of same bit width for possibly unsigned data
* @return array of datasets
*/
static public Dataset[] makeDatasets(final BufferedImage image, boolean keepBitWidth) {
// make raster from buffered image
final Raster ras = image.getData();
final SampleModel sm = ras.getSampleModel();
int[] dtype = getDTypeFromImage(sm, keepBitWidth);
final int bands = ras.getNumBands();
Dataset[] data = new Dataset[bands];
createDatasets(ras, data, dtype[0]);
if (dtype[1] == 1) {
for (int i = 0; i < bands; i++) {
tagIntForShortDataset(data[i]);
}
}
return data;
}
/**
* Constructs IntegerRaster with the given size.
* @param _nrow the number of rows
* @param _ncol the number of columns
*/
public IntegerRaster(int _nrow, int _ncol) {
nrow = _nrow;
ncol = _ncol;
dimension = new Dimension(ncol, nrow);
int size = nrow*ncol;
ComponentColorModel ccm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] {8, 8, 8}, false, // hasAlpha
false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
BandedSampleModel csm = new BandedSampleModel(DataBuffer.TYPE_BYTE, ncol, nrow, ncol, new int[] {0, 1, 2}, new int[] {0, 0, 0});
rgbData = new byte[3][size];
DataBuffer databuffer = new DataBufferByte(rgbData, size);
WritableRaster raster = Raster.createWritableRaster(csm, databuffer, new Point(0, 0));
image = new BufferedImage(ccm, raster, false, null);
// col in x direction, row in y direction
xmin = 0;
xmax = ncol;
ymin = nrow;
ymax = 0; // zero is on top
}
/**
* Stores the Raster data at the specified location.
* @param dstX The absolute X coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param dstY The absolute Y coordinate of the destination pixel
* that will receive a copy of the upper-left pixel of the
* inRaster
* @param width The number of pixels to store horizontally
* @param height The number of pixels to store vertically
* @param inRaster Raster of data to place at x,y location.
*/
private void setDataElements(int dstX, int dstY,
int width, int height,
Raster inRaster) {
// Assume bounds checking has been performed previously
if (width <= 0 || height <= 0) {
return;
}
// Write inRaster (minX, minY) to (dstX, dstY)
int srcOffX = inRaster.getMinX();
int srcOffY = inRaster.getMinY();
Object tdata = null;
// REMIND: Do something faster!
// if (inRaster instanceof ShortInterleavedRaster) {
// }
for (int startY=0; startY < height; startY++) {
// Grab one scanline at a time
tdata = inRaster.getDataElements(srcOffX, srcOffY+startY,
width, 1, tdata);
setDataElements(dstX, dstY + startY, width, 1, tdata);
}
}
public void runTest(Object ctx, int numReps) {
ImageOpTests.Context ictx = (ImageOpTests.Context)ctx;
RasterOp op = ictx.rasterOp;
Raster src = ictx.rasSrc;
WritableRaster dst = ictx.rasDst;
if (ictx.touchSrc) {
Graphics gSrc = ictx.bufSrc.getGraphics();
do {
gSrc.fillRect(0, 0, 1, 1);
op.filter(src, dst);
} while (--numReps > 0);
} else {
do {
op.filter(src, dst);
} while (--numReps > 0);
}
}
synchronized static WritableRaster makeByteRaster(Raster srcRas,
int w, int h)
{
if (byteRasRef != null) {
WritableRaster wr = (WritableRaster) byteRasRef.get();
if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
byteRasRef = null;
return wr;
}
}
// If we are going to cache this Raster, make it non-tiny
if (w <= 32 && h <= 32) {
w = h = 32;
}
return srcRas.createCompatibleWritableRaster(w, h);
}
/**
* Took this cacheRaster code from GradientPaint. It appears to recycle
* rasters for use by any other instance, as long as they are sufficiently
* large.
*/
private static synchronized void putCachedRaster(ColorModel cm,
Raster ras)
{
if (cached != null) {
Raster cras = (Raster) cached.get();
if (cras != null) {
int cw = cras.getWidth();
int ch = cras.getHeight();
int iw = ras.getWidth();
int ih = ras.getHeight();
if (cw >= iw && ch >= ih) {
return;
}
if (cw * ch >= iw * ih) {
return;
}
}
}
cachedModel = cm;
cached = new WeakReference<Raster>(ras);
}
private static Raster flipRaster(Raster r) {
int w = r.getWidth();
int h = r.getHeight();
WritableRaster rf = r.createCompatibleWritableRaster(r.getMinX(),r.getMinY(), w,h);
int[] p = new int[w*3];
for (int y=r.getMinY(); y<r.getMinY()+h; y++) {
p = r.getPixels(r.getMinX(),y,w,1,p);
rf.setPixels(r.getMinX(),r.getMinY()+h-(y-r.getMinY())-1,w,1,p);
}
return rf;
}
public int[][] fromFile(File file) throws IOException {
BufferedImage image = ImageIO.read(file);
if (height > 0 && width > 0)
image = toBufferedImage(image.getScaledInstance(height, width, Image.SCALE_SMOOTH));
Raster raster = image.getData();
int w = raster.getWidth(), h = raster.getHeight();
int[][] ret = new int[w][h];
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++)
ret[i][j] = raster.getSample(i, j, 0);
return ret;
}
public void doTest(Raster src, WritableRaster dst) {
System.out.println("Test for raster:" + src);
try {
dst = op.filter(src, dst);
} catch (Exception e) {
throw new RuntimeException("Test failed.", e);
}
}
public void doTest(Raster src, WritableRaster dst) {
System.out.println("Test for raster:" + src);
try {
dst = op.filter(src, dst);
} catch (Exception e) {
throw new RuntimeException("Test failed.", e);
}
}
public Raster readRaster(int imageIndex,
ImageReadParam param) throws IOException {
checkIndex(imageIndex);
processImageStarted(imageIndex);
if (param == null) {
param = getDefaultReadParam();
}
param = new J2KImageReadParamJava(param);
if (!ignoreMetadata) {
imageMetadata = new J2KMetadata();
iis.seek(streamPosition0);
readState = new J2KReadState(iis,
(J2KImageReadParamJava)param,
imageMetadata,
this);
} else {
iis.seek(streamPosition0);
readState = new J2KReadState(iis,
(J2KImageReadParamJava)param,
this);
}
Raster ras = readState.readAsRaster();
if (abortRequested())
processReadAborted();
else
processImageComplete();
return ras;
}
/**
* Creates a WritableRaster with the specified width and height, that
* has a data layout (SampleModel) compatible with this ColorModel.
* @see WritableRaster
* @see SampleModel
*/
public WritableRaster createCompatibleWritableRaster (int w, int h) {
int[] bOffs = {2, 1, 0};
return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
w, h, w*3, 3,
bOffs, null);
}
private BufferedImage decodeSingleBitGrayscaleImage(DataBuffer buffer) {
WritableRaster raster = Raster.createPackedRaster(buffer, width, height, 1, new Point(0, 0));
return new BufferedImage(
new IndexColorModel(
1,
2,
new byte[] {(byte) 0xff, 0},
new byte[] {(byte) 0xff, 0},
new byte[] {(byte) 0xff, 0}),
raster,
false,
null);
}
/**
* Creates a WritableRaster with the specified width and height, that
* has a data layout (SampleModel) compatible with this ColorModel.
* @see WritableRaster
* @see SampleModel
*/
public WritableRaster createCompatibleWritableRaster (int w, int h) {
int[] bOffs = {2, 1, 0};
return Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
w, h, w*3, 3,
bOffs, null);
}
public void runTest(Object context, int numReps) {
Raster ras = ((Context) context).ras;
Object elemdata = ((Context) context).elemdata;
do {
ras.getDataElements(numReps&7, 0, elemdata);
} while (--numReps > 0);
}
synchronized static void dropRaster(ColorModel cm, Raster outRas) {
if (outRas == null) {
return;
}
if (xrgbmodel == cm) {
xrgbRasRef = new WeakReference<>(outRas);
} else if (argbmodel == cm) {
argbRasRef = new WeakReference<>(outRas);
}
}