下面列出了java.awt.image.Raster#getHeight() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Create datasets from a Raster
* @param r raster
* @param data array to output datasets
* @param dtype dataset type
*/
static public void createDatasets(Raster r, Dataset[] data, final int dtype) {
final int bands = data.length;
final int height = r.getHeight();
final int width = r.getWidth();
Dataset tmp;
for (int i = 0; i < bands; i++) {
if (dtype == Dataset.FLOAT32) {
tmp = DatasetFactory.createFromObject(r.getSamples(0, 0, width, height, i, (float[]) null), height, width);
} else if (dtype == Dataset.FLOAT64) {
tmp = DatasetFactory.createFromObject(r.getSamples(0, 0, width, height, i, (double[]) null), height, width);
} else if (dtype == Dataset.INT32) {
tmp = DatasetFactory.createFromObject(r.getSamples(0, 0, width, height, i, (int[]) null), height, width);
} else {
tmp = DatasetFactory.createFromObject(dtype, r.getSamples(0, 0, width, height, i, (int[]) null), height, width);
}
data[i] = tmp;
}
}
/**
* 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);
}
/**
* 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) {
int srcOffX = inRaster.getMinX();
int srcOffY = inRaster.getMinY();
int dstOffX = x + srcOffX;
int dstOffY = y + srcOffY;
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, inRaster);
}
/**
* 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 Raster getCachedRaster(ColorModel cm,
int w, int h)
{
if (cm == cachedModel) {
if (cached != null) {
Raster ras = (Raster) cached.get();
if (ras != null &&
ras.getWidth() >= w &&
ras.getHeight() >= h)
{
cached = null;
return ras;
}
}
}
return cm.createCompatibleWritableRaster(w, h);
}
/**
* Encodes and writes raster data, together with alpha (transparency) data, as a 32-bit bitmap.
* @param raster the source raster data
* @param alpha the source alpha data
* @param out the output to which the bitmap will be written
* @throws java.io.IOException if an error occurs
*/
public static void write32(Raster raster, Raster alpha, net.sf.image4j.io.LittleEndianOutputStream out) throws IOException {
int width = raster.getWidth();
int height = raster.getHeight();
// write lines
for (int y = height - 1; y >= 0; y--) {
// write pixel data for each line
for (int x = 0; x < width; x++) {
// get RGBA values
int r = raster.getSample(x, y, 0);
int g = raster.getSample(x, y, 1);
int b = raster.getSample(x, y, 2);
int a = alpha.getSample(x, y, 0);
// write RGBA values
out.writeByte(b);
out.writeByte(g);
out.writeByte(r);
out.writeByte(a);
}
}
}
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<>(ras);
}
/** Set source */
private void setDefaults(Raster raster) {
// override the params in the super class
setSuperProperties();
if (raster != null) {
this.raster = raster;
tileGridXOffset = raster.getMinX();
tileGridYOffset = raster.getMinY();
tileWidth = raster.getWidth();
tileHeight = raster.getHeight();
tilingSet = true;
numTiles = 1;
numComponents = raster.getSampleModel().getNumBands();
}
setDefaults();
}
/**
* 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) {
int dstOffX = x + inRaster.getMinX();
int dstOffY = y + inRaster.getMinY();
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, width, height, inRaster);
}
private BufferedImage toGrayScale (Raster in, PixelCorrection c,
boolean invertColors, double lowerBound, double upperBound)
{
double offset = - lowerBound;
double scaleFactor = 256. / (upperBound - lowerBound);
int width = in.getWidth();
int height = in.getHeight();
// generate
BufferedImage out = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for(int j = 0; j < height; j++)
{
for(int i = 0; i < width; i++)
{
int pixel = checkAndApplyCorrection(in.getSample(i, j, 0), c);
if(pixel == c.nodata)
{
if(invertColors)
out.setRGB(i, j, new Color(255, 255, 255).getRGB());
else
out.setRGB(i, j, new Color(0, 0, 0).getRGB());
continue;
}
double normalized = (pixel + offset)*scaleFactor;
int gray = (int)(Math.max(0, Math.min(255, normalized)));
if(invertColors)
gray = 255 - gray;
out.setRGB(i, j, new Color(gray, gray, gray).getRGB());
}
}
return out;
}
/**
* 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) {
int dstOffX = x + inRaster.getMinX();
int dstOffY = y + inRaster.getMinY();
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, width, height, inRaster);
}
public void setRect(int dx, int dy, Raster srcRaster) {
if (!(srcRaster instanceof ByteInterleavedRaster)) {
super.setRect(dx, dy, srcRaster);
return;
}
int width = srcRaster.getWidth();
int height = srcRaster.getHeight();
int srcOffX = srcRaster.getMinX();
int srcOffY = srcRaster.getMinY();
int dstOffX = dx+srcOffX;
int dstOffY = dy+srcOffY;
// Clip to this raster
if (dstOffX < this.minX) {
int skipX = minX - dstOffX;
width -= skipX;
srcOffX += skipX;
dstOffX = this.minX;
}
if (dstOffY < this.minY) {
int skipY = this.minY - dstOffY;
height -= skipY;
srcOffY += skipY;
dstOffY = this.minY;
}
if (dstOffX+width > this.maxX) {
width = this.maxX - dstOffX;
}
if (dstOffY+height > this.maxY) {
height = this.maxY - dstOffY;
}
setDataElements(dstOffX, dstOffY,
srcOffX, srcOffY,
width, height, srcRaster);
}
static synchronized Raster getCachedRaster(ColorModel cm, int w, int h) {
if (cm == cachedModel) {
if (cached != null) {
Raster ras = (Raster) cached.get();
if (ras != null &&
ras.getWidth() >= w &&
ras.getHeight() >= h)
{
cached = null;
return ras;
}
}
}
return cm.createCompatibleWritableRaster(w, h);
}
/**
* 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) {
int dstOffX = x + inRaster.getMinX();
int dstOffY = y + inRaster.getMinY();
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, width, height, inRaster);
}
/**
* 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) {
int dstOffX = x + inRaster.getMinX();
int dstOffY = y + inRaster.getMinY();
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, width, height, inRaster);
}
static synchronized Raster getCachedRaster(ColorModel cm, int w, int h) {
if (cm == cachedModel) {
if (cached != null) {
Raster ras = (Raster) cached.get();
if (ras != null &&
ras.getWidth() >= w &&
ras.getHeight() >= h)
{
cached = null;
return ras;
}
}
}
return cm.createCompatibleWritableRaster(w, h);
}
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
int w = src.getWidth();
int h = src.getHeight();
DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();
DataBufferInt dstOutDB = (DataBufferInt) dstOut.getDataBuffer();
int srcRGB[] = srcDB.getBankData()[0];
int dstOutRGB[] = dstOutDB.getBankData()[0];
int srcOffset = srcDB.getOffset();
int dstOutOffset = dstOutDB.getOffset();
int srcScanStride = ((SinglePixelPackedSampleModel) src.getSampleModel()).getScanlineStride();
int dstOutScanStride = ((SinglePixelPackedSampleModel) dstOut.getSampleModel()).getScanlineStride();
int srcAdjust = srcScanStride - w;
int dstOutAdjust = dstOutScanStride - w;
int si = srcOffset;
int doi = dstOutOffset;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
dstOutRGB[doi] = srcRGB[si] ^ 0x00ffffff;
si++;
doi++;
}
si += srcAdjust;
doi += dstOutAdjust;
}
}
/**
* Copies pixels from Raster srcRaster to this WritableRaster.
* For each (x, y) address in srcRaster, the corresponding pixel
* is copied to address (x+dx, y+dy) in this WritableRaster,
* unless (x+dx, y+dy) falls outside the bounds of this raster.
* srcRaster must have the same number of bands as this WritableRaster.
* The copy is a simple copy of source samples to the corresponding
* destination samples. For details, see
* {@link WritableRaster#setRect(Raster)}.
*
* @param dx The X translation factor from src space to dst space
* of the copy.
* @param dy The Y translation factor from src space to dst space
* of the copy.
* @param srcRaster The Raster from which to copy pixels.
*/
public void setRect(int dx, int dy, Raster srcRaster) {
// Check if we can use fast code
if (!(srcRaster instanceof BytePackedRaster) ||
((BytePackedRaster)srcRaster).pixelBitStride != pixelBitStride) {
super.setRect(dx, dy, srcRaster);
return;
}
int width = srcRaster.getWidth();
int height = srcRaster.getHeight();
int srcOffX = srcRaster.getMinX();
int srcOffY = srcRaster.getMinY();
int dstOffX = dx+srcOffX;
int dstOffY = dy+srcOffY;
// Clip to this raster
if (dstOffX < this.minX) {
int skipX = this.minX - dstOffX;
width -= skipX;
srcOffX += skipX;
dstOffX = this.minX;
}
if (dstOffY < this.minY) {
int skipY = this.minY - dstOffY;
height -= skipY;
srcOffY += skipY;
dstOffY = this.minY;
}
if (dstOffX+width > this.maxX) {
width = this.maxX - dstOffX;
}
if (dstOffY+height > this.maxY) {
height = this.maxY - dstOffY;
}
setDataElements(dstOffX, dstOffY,
srcOffX, srcOffY,
width, height,
(BytePackedRaster)srcRaster);
}
/**
* Compose src onto dst using the alpha of sel to interpolate between the two.
* I can't think of a way to do this using AlphaComposite.
* @param src the source raster
* @param dst the destination raster
* @param sel the mask raster
*/
public static void composeThroughMask(Raster src, WritableRaster dst, Raster sel) {
int x = src.getMinX();
int y = src.getMinY();
int w = src.getWidth();
int h = src.getHeight();
int srcRGB[] = null;
int selRGB[] = null;
int dstRGB[] = null;
for ( int i = 0; i < h; i++ ) {
srcRGB = src.getPixels(x, y, w, 1, srcRGB);
selRGB = sel.getPixels(x, y, w, 1, selRGB);
dstRGB = dst.getPixels(x, y, w, 1, dstRGB);
int k = x;
for ( int j = 0; j < w; j++ ) {
int sr = srcRGB[k];
int dir = dstRGB[k];
int sg = srcRGB[k+1];
int dig = dstRGB[k+1];
int sb = srcRGB[k+2];
int dib = dstRGB[k+2];
int sa = srcRGB[k+3];
int dia = dstRGB[k+3];
float a = selRGB[k+3]/255f;
float ac = 1-a;
dstRGB[k] = (int)(a*sr + ac*dir);
dstRGB[k+1] = (int)(a*sg + ac*dig);
dstRGB[k+2] = (int)(a*sb + ac*dib);
dstRGB[k+3] = (int)(a*sa + ac*dia);
k += 4;
}
dst.setPixels(x, y, w, 1, dstRGB);
y++;
}
}
/**
* The runnable method for this class. This will produce an image using
* the current RenderableImage and RenderContext and send it to all the
* ImageConsumer currently registered with this class.
*/
public void run() {
// First get the rendered image
RenderedImage rdrdImage;
if (rc != null) {
rdrdImage = rdblImage.createRendering(rc);
} else {
rdrdImage = rdblImage.createDefaultRendering();
}
// And its ColorModel
ColorModel colorModel = rdrdImage.getColorModel();
Raster raster = rdrdImage.getData();
SampleModel sampleModel = raster.getSampleModel();
DataBuffer dataBuffer = raster.getDataBuffer();
if (colorModel == null) {
colorModel = ColorModel.getRGBdefault();
}
int minX = raster.getMinX();
int minY = raster.getMinY();
int width = raster.getWidth();
int height = raster.getHeight();
Enumeration<ImageConsumer> icList;
ImageConsumer ic;
// Set up the ImageConsumers
icList = ics.elements();
while (icList.hasMoreElements()) {
ic = icList.nextElement();
ic.setDimensions(width,height);
ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT |
ImageConsumer.COMPLETESCANLINES |
ImageConsumer.SINGLEPASS |
ImageConsumer.SINGLEFRAME);
}
// Get RGB pixels from the raster scanline by scanline and
// send to consumers.
int pix[] = new int[width];
int i,j;
int numBands = sampleModel.getNumBands();
int tmpPixel[] = new int[numBands];
for (j = 0; j < height; j++) {
for(i = 0; i < width; i++) {
sampleModel.getPixel(i, j, tmpPixel, dataBuffer);
pix[i] = colorModel.getDataElement(tmpPixel, 0);
}
// Now send the scanline to the Consumers
icList = ics.elements();
while (icList.hasMoreElements()) {
ic = icList.nextElement();
ic.setPixels(0, j, width, 1, colorModel, pix, 0, width);
}
}
// Now tell the consumers we're done.
icList = ics.elements();
while (icList.hasMoreElements()) {
ic = icList.nextElement();
ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
}
}
/**
* Copies pixels from Raster srcRaster to this WritableRaster.
* For each (x, y) address in srcRaster, the corresponding pixel
* is copied to address (x+dx, y+dy) in this WritableRaster,
* unless (x+dx, y+dy) falls outside the bounds of this raster.
* srcRaster must have the same number of bands as this WritableRaster.
* The copy is a simple copy of source samples to the corresponding
* destination samples. For details, see
* {@link WritableRaster#setRect(Raster)}.
*
* @param dx The X translation factor from src space to dst space
* of the copy.
* @param dy The Y translation factor from src space to dst space
* of the copy.
* @param srcRaster The Raster from which to copy pixels.
*/
public void setRect(int dx, int dy, Raster srcRaster) {
// Check if we can use fast code
if (!(srcRaster instanceof BytePackedRaster) ||
((BytePackedRaster)srcRaster).pixelBitStride != pixelBitStride) {
super.setRect(dx, dy, srcRaster);
return;
}
int width = srcRaster.getWidth();
int height = srcRaster.getHeight();
int srcOffX = srcRaster.getMinX();
int srcOffY = srcRaster.getMinY();
int dstOffX = dx+srcOffX;
int dstOffY = dy+srcOffY;
// Clip to this raster
if (dstOffX < this.minX) {
int skipX = this.minX - dstOffX;
width -= skipX;
srcOffX += skipX;
dstOffX = this.minX;
}
if (dstOffY < this.minY) {
int skipY = this.minY - dstOffY;
height -= skipY;
srcOffY += skipY;
dstOffY = this.minY;
}
if (dstOffX+width > this.maxX) {
width = this.maxX - dstOffX;
}
if (dstOffY+height > this.maxY) {
height = this.maxY - dstOffY;
}
setDataElements(dstOffX, dstOffY,
srcOffX, srcOffY,
width, height,
(BytePackedRaster)srcRaster);
}