📄 pixelgrabber.java
字号:
* If no width was specified for the rectangle of pixels to grab then
* then this information will only be available after the image has
* delivered the dimensions.
* @return the final height used for the pixel buffer or -1 if the height
* is not yet known
* @see #getStatus
*/
public synchronized int getHeight() {
return (dstH < 0) ? -1 : dstH;
}
/**
* Get the pixel buffer. If the PixelGrabber was not constructed
* with an explicit pixel buffer to hold the pixels then this method
* will return null until the size and format of the image data is
* known.
* Since the PixelGrabber may fall back on accumulating the data
* in the default RGB ColorModel at any time if the source image
* uses more than one ColorModel to deliver the data, the array
* object returned by this method may change over time until the
* image grab is complete.
* @return either a byte array or an int array
* @see #getStatus
*/
public synchronized Object getPixels() {
return (bytePixels == null)
? ((Object) intPixels)
: ((Object) bytePixels);
}
/**
* Get the ColorModel for the pixels stored in the array. If the
* PixelGrabber was constructed with an explicit pixel buffer then
* this method will always return the default RGB ColorModel,
* otherwise it may return null until the ColorModel used by the
* ImageProducer is known.
* Since the PixelGrabber may fall back on accumulating the data
* in the default RGB ColorModel at any time if the source image
* uses more than one ColorModel to deliver the data, the ColorModel
* object returned by this method may change over time until the
* image grab is complete.
* @return the ColorModel object used for storing the pixels
* @see #getStatus
* @see ColorModel#getRGBdefault
*/
public synchronized ColorModel getColorModel() {
return imageModel;
}
/**
* The setDimensions method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
*/
public void setDimensions(int width, int height) {
if (dstW < 0) {
dstW = width - dstX;
}
if (dstH < 0) {
dstH = height - dstY;
}
if (dstW <= 0 || dstH <= 0) {
imageComplete(STATICIMAGEDONE);
} else if (intPixels == null &&
imageModel == ColorModel.getRGBdefault()) {
intPixels = new int[dstW * dstH];
dstScan = dstW;
dstOff = 0;
}
flags |= (ImageObserver.WIDTH | ImageObserver.HEIGHT);
}
/**
* The setHints method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
*/
public void setHints(int hints) {
return;
}
/**
* The setProperties method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
*/
public void setProperties(Hashtable props) {
return;
}
/**
* The setColorModel method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
*/
public void setColorModel(ColorModel model) {
return;
}
private void convertToRGB() {
int size = dstW * dstH;
int newpixels[] = new int[size];
if (bytePixels != null) {
for (int i = 0; i < size; i++) {
newpixels[i] = imageModel.getRGB(bytePixels[i] & 0xff);
}
} else if (intPixels != null) {
for (int i = 0; i < size; i++) {
newpixels[i] = imageModel.getRGB(intPixels[i]);
}
}
bytePixels = null;
intPixels = newpixels;
dstScan = dstW;
dstOff = 0;
imageModel = ColorModel.getRGBdefault();
}
/**
* The setPixels method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
*/
public void setPixels(int srcX, int srcY, int srcW, int srcH,
ColorModel model,
byte pixels[], int srcOff, int srcScan) {
if (srcY < dstY) {
int diff = dstY - srcY;
if (diff >= srcH) {
return;
}
srcOff += srcScan * diff;
srcY += diff;
srcH -= diff;
}
if (srcY + srcH > dstY + dstH) {
srcH = (dstY + dstH) - srcY;
if (srcH <= 0) {
return;
}
}
if (srcX < dstX) {
int diff = dstX - srcX;
if (diff >= srcW) {
return;
}
srcOff += diff;
srcX += diff;
srcW -= diff;
}
if (srcX + srcW > dstX + dstW) {
srcW = (dstX + dstW) - srcX;
if (srcW <= 0) {
return;
}
}
int dstPtr = dstOff + (srcY - dstY) * dstScan + (srcX - dstX);
if (intPixels == null) {
if (bytePixels == null) {
bytePixels = new byte[dstW * dstH];
dstScan = dstW;
dstOff = 0;
imageModel = model;
} else if (imageModel != model) {
convertToRGB();
}
if (bytePixels != null) {
for (int h = srcH; h > 0; h--) {
System.arraycopy(pixels, srcOff, bytePixels, dstPtr, srcW);
srcOff += srcScan;
dstPtr += dstScan;
}
}
}
if (intPixels != null) {
int dstRem = dstScan - srcW;
int srcRem = srcScan - srcW;
for (int h = srcH; h > 0; h--) {
for (int w = srcW; w > 0; w--) {
intPixels[dstPtr++] = model.getRGB(pixels[srcOff++]&0xff);
}
srcOff += srcRem;
dstPtr += dstRem;
}
}
flags |= ImageObserver.SOMEBITS;
}
/**
* The setPixels method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
*/
public void setPixels(int srcX, int srcY, int srcW, int srcH,
ColorModel model,
int pixels[], int srcOff, int srcScan) {
if (srcY < dstY) {
int diff = dstY - srcY;
if (diff >= srcH) {
return;
}
srcOff += srcScan * diff;
srcY += diff;
srcH -= diff;
}
if (srcY + srcH > dstY + dstH) {
srcH = (dstY + dstH) - srcY;
if (srcH <= 0) {
return;
}
}
if (srcX < dstX) {
int diff = dstX - srcX;
if (diff >= srcW) {
return;
}
srcOff += diff;
srcX += diff;
srcW -= diff;
}
if (srcX + srcW > dstX + dstW) {
srcW = (dstX + dstW) - srcX;
if (srcW <= 0) {
return;
}
}
if (intPixels == null) {
if (bytePixels == null) {
intPixels = new int[dstW * dstH];
dstScan = dstW;
dstOff = 0;
imageModel = model;
} else {
convertToRGB();
}
}
int dstPtr = dstOff + (srcY - dstY) * dstScan + (srcX - dstX);
if (imageModel == model) {
for (int h = srcH; h > 0; h--) {
System.arraycopy(pixels, srcOff, intPixels, dstPtr, srcW);
srcOff += srcScan;
dstPtr += dstScan;
}
} else {
if (imageModel != ColorModel.getRGBdefault()) {
convertToRGB();
}
int dstRem = dstScan - srcW;
int srcRem = srcScan - srcW;
for (int h = srcH; h > 0; h--) {
for (int w = srcW; w > 0; w--) {
intPixels[dstPtr++] = model.getRGB(pixels[srcOff++]);
}
srcOff += srcRem;
dstPtr += dstRem;
}
}
flags |= ImageObserver.SOMEBITS;
}
/**
* The imageComplete method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
*/
public synchronized void imageComplete(int status) {
grabbing = false;
switch (status) {
default:
case IMAGEERROR:
flags |= ImageObserver.ERROR | ImageObserver.ABORT;
break;
case IMAGEABORTED:
flags |= ImageObserver.ABORT;
break;
case STATICIMAGEDONE:
flags |= ImageObserver.ALLBITS;
break;
case SINGLEFRAMEDONE:
flags |= ImageObserver.FRAMEBITS;
break;
}
producer.removeConsumer(this);
notifyAll();
}
/**
* DEPRECATED: Replaced by getStatus().
*/
public synchronized int status() {
return flags;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -