home *** CD-ROM | disk | FTP | other *** search
- package java.awt.image;
-
- import java.awt.Image;
- import java.util.Hashtable;
-
- public class PixelGrabber implements ImageConsumer {
- ImageProducer producer;
- int dstX;
- int dstY;
- int dstW;
- int dstH;
- int[] pixelbuf;
- int dstOff;
- int dstScan;
- private boolean grabbing;
- private int flags;
- private final int GRABBEDBITS;
- private final int DONEBITS;
-
- public PixelGrabber(Image img, int x, int y, int w, int h, int[] pix, int off, int scansize) {
- this(img.getSource(), x, y, w, h, pix, off, scansize);
- }
-
- public PixelGrabber(ImageProducer ip, int x, int y, int w, int h, int[] pix, int off, int scansize) {
- this.GRABBEDBITS = 48;
- this.DONEBITS = 112;
- this.producer = ip;
- this.dstX = x;
- this.dstY = y;
- this.dstW = w;
- this.dstH = h;
- this.dstOff = off;
- this.dstScan = scansize;
- this.pixelbuf = pix;
- }
-
- public boolean grabPixels() throws InterruptedException {
- return this.grabPixels(0L);
- }
-
- public synchronized boolean grabPixels(long ms) throws InterruptedException {
- if ((this.flags & 112) != 0) {
- return (this.flags & 48) != 0;
- } else {
- long end = ms + System.currentTimeMillis();
- if (!this.grabbing) {
- this.grabbing = true;
- this.flags &= -129;
- this.producer.startProduction(this);
- }
-
- long timeout;
- for(; this.grabbing; this.wait(timeout)) {
- if (ms == 0L) {
- timeout = 0L;
- } else {
- timeout = end - System.currentTimeMillis();
- if (timeout <= 0L) {
- break;
- }
- }
- }
-
- return (this.flags & 48) != 0;
- }
- }
-
- public synchronized int status() {
- return this.flags;
- }
-
- public void setDimensions(int width, int height) {
- }
-
- public void setHints(int hints) {
- }
-
- public void setProperties(Hashtable props) {
- }
-
- public void setColorModel(ColorModel model) {
- }
-
- public void setPixels(int srcX, int srcY, int srcW, int srcH, ColorModel model, byte[] pixels, int srcOff, int srcScan) {
- if (srcY < this.dstY) {
- int diff = this.dstY - srcY;
- if (diff >= srcH) {
- return;
- }
-
- srcOff += srcScan * diff;
- srcY += diff;
- srcH -= diff;
- }
-
- if (srcY + srcH > this.dstY + this.dstH) {
- srcH = this.dstY + this.dstH - srcY;
- if (srcH <= 0) {
- return;
- }
- }
-
- if (srcX < this.dstX) {
- int diff = this.dstX - srcX;
- if (diff >= srcW) {
- return;
- }
-
- srcOff += diff;
- srcX += diff;
- srcW -= diff;
- }
-
- if (srcX + srcW > this.dstX + this.dstW) {
- srcW = this.dstX + this.dstW - srcX;
- if (srcW <= 0) {
- return;
- }
- }
-
- int dstPtr = this.dstOff + (srcY - this.dstY) * this.dstScan + (srcX - this.dstX);
- int dstRem = this.dstScan - this.dstW;
- int srcRem = srcScan - srcW;
-
- for(int h = srcH; h > 0; --h) {
- for(int w = srcW; w > 0; --w) {
- this.pixelbuf[dstPtr++] = model.getRGB(pixels[srcOff++] & 255);
- }
-
- srcOff += srcRem;
- dstPtr += dstRem;
- }
-
- this.flags |= 8;
- }
-
- public void setPixels(int srcX, int srcY, int srcW, int srcH, ColorModel model, int[] pixels, int srcOff, int srcScan) {
- if (srcY < this.dstY) {
- int diff = this.dstY - srcY;
- if (diff >= srcH) {
- return;
- }
-
- srcOff += srcScan * diff;
- srcY += diff;
- srcH -= diff;
- }
-
- if (srcY + srcH > this.dstY + this.dstH) {
- srcH = this.dstY + this.dstH - srcY;
- if (srcH <= 0) {
- return;
- }
- }
-
- if (srcX < this.dstX) {
- int diff = this.dstX - srcX;
- if (diff >= srcW) {
- return;
- }
-
- srcOff += diff;
- srcX += diff;
- srcW -= diff;
- }
-
- if (srcX + srcW > this.dstX + this.dstW) {
- srcW = this.dstX + this.dstW - srcX;
- if (srcW <= 0) {
- return;
- }
- }
-
- int dstPtr = this.dstOff + (srcY - this.dstY) * this.dstScan + (srcX - this.dstX);
- int dstRem = this.dstScan - this.dstW;
- int srcRem = srcScan - srcW;
-
- for(int h = srcH; h > 0; --h) {
- for(int w = srcW; w > 0; --w) {
- this.pixelbuf[dstPtr++] = model.getRGB(pixels[srcOff++]);
- }
-
- srcOff += srcRem;
- dstPtr += dstRem;
- }
-
- this.flags |= 8;
- }
-
- public synchronized void imageComplete(int status) {
- this.grabbing = false;
- switch (status) {
- case 1:
- default:
- this.flags |= 192;
- break;
- case 2:
- this.flags |= 16;
- break;
- case 3:
- this.flags |= 32;
- break;
- case 4:
- this.flags |= 128;
- }
-
- this.producer.removeConsumer(this);
- this.notifyAll();
- }
- }
-