home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / awt / image / PixelGrabber.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  4.3 KB  |  211 lines

  1. package java.awt.image;
  2.  
  3. import java.awt.Image;
  4. import java.util.Hashtable;
  5.  
  6. public class PixelGrabber implements ImageConsumer {
  7.    ImageProducer producer;
  8.    int dstX;
  9.    int dstY;
  10.    int dstW;
  11.    int dstH;
  12.    int[] pixelbuf;
  13.    int dstOff;
  14.    int dstScan;
  15.    private boolean grabbing;
  16.    private int flags;
  17.    private final int GRABBEDBITS;
  18.    private final int DONEBITS;
  19.  
  20.    public PixelGrabber(Image img, int x, int y, int w, int h, int[] pix, int off, int scansize) {
  21.       this(img.getSource(), x, y, w, h, pix, off, scansize);
  22.    }
  23.  
  24.    public PixelGrabber(ImageProducer ip, int x, int y, int w, int h, int[] pix, int off, int scansize) {
  25.       this.GRABBEDBITS = 48;
  26.       this.DONEBITS = 112;
  27.       this.producer = ip;
  28.       this.dstX = x;
  29.       this.dstY = y;
  30.       this.dstW = w;
  31.       this.dstH = h;
  32.       this.dstOff = off;
  33.       this.dstScan = scansize;
  34.       this.pixelbuf = pix;
  35.    }
  36.  
  37.    public boolean grabPixels() throws InterruptedException {
  38.       return this.grabPixels(0L);
  39.    }
  40.  
  41.    public synchronized boolean grabPixels(long ms) throws InterruptedException {
  42.       if ((this.flags & 112) != 0) {
  43.          return (this.flags & 48) != 0;
  44.       } else {
  45.          long end = ms + System.currentTimeMillis();
  46.          if (!this.grabbing) {
  47.             this.grabbing = true;
  48.             this.flags &= -129;
  49.             this.producer.startProduction(this);
  50.          }
  51.  
  52.          long timeout;
  53.          for(; this.grabbing; this.wait(timeout)) {
  54.             if (ms == 0L) {
  55.                timeout = 0L;
  56.             } else {
  57.                timeout = end - System.currentTimeMillis();
  58.                if (timeout <= 0L) {
  59.                   break;
  60.                }
  61.             }
  62.          }
  63.  
  64.          return (this.flags & 48) != 0;
  65.       }
  66.    }
  67.  
  68.    public synchronized int status() {
  69.       return this.flags;
  70.    }
  71.  
  72.    public void setDimensions(int width, int height) {
  73.    }
  74.  
  75.    public void setHints(int hints) {
  76.    }
  77.  
  78.    public void setProperties(Hashtable props) {
  79.    }
  80.  
  81.    public void setColorModel(ColorModel model) {
  82.    }
  83.  
  84.    public void setPixels(int srcX, int srcY, int srcW, int srcH, ColorModel model, byte[] pixels, int srcOff, int srcScan) {
  85.       if (srcY < this.dstY) {
  86.          int diff = this.dstY - srcY;
  87.          if (diff >= srcH) {
  88.             return;
  89.          }
  90.  
  91.          srcOff += srcScan * diff;
  92.          srcY += diff;
  93.          srcH -= diff;
  94.       }
  95.  
  96.       if (srcY + srcH > this.dstY + this.dstH) {
  97.          srcH = this.dstY + this.dstH - srcY;
  98.          if (srcH <= 0) {
  99.             return;
  100.          }
  101.       }
  102.  
  103.       if (srcX < this.dstX) {
  104.          int diff = this.dstX - srcX;
  105.          if (diff >= srcW) {
  106.             return;
  107.          }
  108.  
  109.          srcOff += diff;
  110.          srcX += diff;
  111.          srcW -= diff;
  112.       }
  113.  
  114.       if (srcX + srcW > this.dstX + this.dstW) {
  115.          srcW = this.dstX + this.dstW - srcX;
  116.          if (srcW <= 0) {
  117.             return;
  118.          }
  119.       }
  120.  
  121.       int dstPtr = this.dstOff + (srcY - this.dstY) * this.dstScan + (srcX - this.dstX);
  122.       int dstRem = this.dstScan - this.dstW;
  123.       int srcRem = srcScan - srcW;
  124.  
  125.       for(int h = srcH; h > 0; --h) {
  126.          for(int w = srcW; w > 0; --w) {
  127.             this.pixelbuf[dstPtr++] = model.getRGB(pixels[srcOff++] & 255);
  128.          }
  129.  
  130.          srcOff += srcRem;
  131.          dstPtr += dstRem;
  132.       }
  133.  
  134.       this.flags |= 8;
  135.    }
  136.  
  137.    public void setPixels(int srcX, int srcY, int srcW, int srcH, ColorModel model, int[] pixels, int srcOff, int srcScan) {
  138.       if (srcY < this.dstY) {
  139.          int diff = this.dstY - srcY;
  140.          if (diff >= srcH) {
  141.             return;
  142.          }
  143.  
  144.          srcOff += srcScan * diff;
  145.          srcY += diff;
  146.          srcH -= diff;
  147.       }
  148.  
  149.       if (srcY + srcH > this.dstY + this.dstH) {
  150.          srcH = this.dstY + this.dstH - srcY;
  151.          if (srcH <= 0) {
  152.             return;
  153.          }
  154.       }
  155.  
  156.       if (srcX < this.dstX) {
  157.          int diff = this.dstX - srcX;
  158.          if (diff >= srcW) {
  159.             return;
  160.          }
  161.  
  162.          srcOff += diff;
  163.          srcX += diff;
  164.          srcW -= diff;
  165.       }
  166.  
  167.       if (srcX + srcW > this.dstX + this.dstW) {
  168.          srcW = this.dstX + this.dstW - srcX;
  169.          if (srcW <= 0) {
  170.             return;
  171.          }
  172.       }
  173.  
  174.       int dstPtr = this.dstOff + (srcY - this.dstY) * this.dstScan + (srcX - this.dstX);
  175.       int dstRem = this.dstScan - this.dstW;
  176.       int srcRem = srcScan - srcW;
  177.  
  178.       for(int h = srcH; h > 0; --h) {
  179.          for(int w = srcW; w > 0; --w) {
  180.             this.pixelbuf[dstPtr++] = model.getRGB(pixels[srcOff++]);
  181.          }
  182.  
  183.          srcOff += srcRem;
  184.          dstPtr += dstRem;
  185.       }
  186.  
  187.       this.flags |= 8;
  188.    }
  189.  
  190.    public synchronized void imageComplete(int status) {
  191.       this.grabbing = false;
  192.       switch (status) {
  193.          case 1:
  194.          default:
  195.             this.flags |= 192;
  196.             break;
  197.          case 2:
  198.             this.flags |= 16;
  199.             break;
  200.          case 3:
  201.             this.flags |= 32;
  202.             break;
  203.          case 4:
  204.             this.flags |= 128;
  205.       }
  206.  
  207.       this.producer.removeConsumer(this);
  208.       this.notifyAll();
  209.    }
  210. }
  211.