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

  1. package sun.awt.image;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Rectangle;
  6. import java.awt.image.ColorModel;
  7. import java.awt.image.ImageConsumer;
  8. import java.awt.image.ImageObserver;
  9. import java.util.Hashtable;
  10.  
  11. public class ImageRepresentation extends ImageWatched implements ImageConsumer {
  12.    int pData;
  13.    InputStreamImageSource src;
  14.    Image image;
  15.    int tag;
  16.    int srcW;
  17.    int srcH;
  18.    int width;
  19.    int height;
  20.    int hints;
  21.    int availinfo;
  22.    boolean offscreen;
  23.    Rectangle newbits;
  24.    private boolean consuming = false;
  25.    private int numWaiters;
  26.  
  27.    ImageRepresentation(Image im, int w, int h, int t) {
  28.       this.image = im;
  29.       this.tag = t;
  30.       if (w != 0 && h != 0) {
  31.          if (w > 0) {
  32.             this.width = w;
  33.             this.availinfo |= 1;
  34.          }
  35.  
  36.          if (h > 0) {
  37.             this.height = h;
  38.             this.availinfo |= 2;
  39.          }
  40.  
  41.          if (this.image.getSource() instanceof InputStreamImageSource) {
  42.             this.src = (InputStreamImageSource)this.image.getSource();
  43.          }
  44.  
  45.       } else {
  46.          throw new IllegalArgumentException();
  47.       }
  48.    }
  49.  
  50.    native void offscreenInit();
  51.  
  52.    public synchronized void reconstruct(int flags) {
  53.       if (this.src != null) {
  54.          this.src.checkSecurity((Object)null, false);
  55.       }
  56.  
  57.       int missinginfo = flags & ~this.availinfo;
  58.       if (missinginfo != 0) {
  59.          ++this.numWaiters;
  60.  
  61.          try {
  62.             this.startProduction();
  63.  
  64.             for(int var9 = flags & ~this.availinfo; var9 != 0; var9 = flags & ~this.availinfo) {
  65.                try {
  66.                   this.wait();
  67.                } catch (InterruptedException var7) {
  68.                   Thread.currentThread().interrupt();
  69.                   return;
  70.                }
  71.             }
  72.  
  73.          } finally {
  74.             this.decrementWaiters();
  75.          }
  76.       }
  77.    }
  78.  
  79.    public synchronized void setDimensions(int w, int h) {
  80.       if (this.src != null) {
  81.          this.src.checkSecurity((Object)null, false);
  82.       }
  83.  
  84.       this.srcW = w;
  85.       this.srcH = h;
  86.       if ((this.availinfo & 1) == 0 && (this.availinfo & 2) == 0) {
  87.          this.width = w;
  88.          this.height = h;
  89.       } else if ((this.availinfo & 1) == 0) {
  90.          this.width = w * this.height / h;
  91.       } else {
  92.          if ((this.availinfo & 2) != 0) {
  93.             return;
  94.          }
  95.  
  96.          this.height = h * this.width / w;
  97.       }
  98.  
  99.       if (this.srcW > 0 && this.srcH > 0 && this.width > 0 && this.height > 0) {
  100.          this.availinfo |= 3;
  101.       } else {
  102.          this.imageComplete(1);
  103.       }
  104.    }
  105.  
  106.    public void setProperties(Hashtable props) {
  107.       if (this.src != null) {
  108.          this.src.checkSecurity((Object)null, false);
  109.       }
  110.  
  111.    }
  112.  
  113.    public void setColorModel(ColorModel model) {
  114.       if (this.src != null) {
  115.          this.src.checkSecurity((Object)null, false);
  116.       }
  117.  
  118.    }
  119.  
  120.    public void setHints(int h) {
  121.       if (this.src != null) {
  122.          this.src.checkSecurity((Object)null, false);
  123.       }
  124.  
  125.       this.hints = h;
  126.    }
  127.  
  128.    private native boolean setBytePixels(int var1, int var2, int var3, int var4, ColorModel var5, byte[] var6, int var7, int var8);
  129.  
  130.    public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pix, int off, int scansize) {
  131.       if (this.src != null) {
  132.          this.src.checkSecurity((Object)null, false);
  133.       }
  134.  
  135.       boolean ok = false;
  136.       synchronized(this){}
  137.  
  138.       int cx;
  139.       int cy;
  140.       int cw;
  141.       int ch;
  142.       try {
  143.          if (this.newbits == null) {
  144.             this.newbits = new Rectangle(0, 0, this.width, this.height);
  145.          }
  146.  
  147.          if (this.setBytePixels(x, y, w, h, model, pix, off, scansize)) {
  148.             this.availinfo |= 8;
  149.             ok = true;
  150.          }
  151.  
  152.          cx = this.newbits.x;
  153.          cy = this.newbits.y;
  154.          cw = this.newbits.width;
  155.          ch = this.newbits.height;
  156.       } catch (Throwable var16) {
  157.          throw var16;
  158.       }
  159.  
  160.       if (ok && !this.offscreen) {
  161.          ((ImageWatched)this).newInfo(this.image, 8, cx, cy, cw, ch);
  162.       }
  163.  
  164.    }
  165.  
  166.    private native boolean setIntPixels(int var1, int var2, int var3, int var4, ColorModel var5, int[] var6, int var7, int var8);
  167.  
  168.    public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pix, int off, int scansize) {
  169.       if (this.src != null) {
  170.          this.src.checkSecurity((Object)null, false);
  171.       }
  172.  
  173.       boolean ok = false;
  174.       synchronized(this){}
  175.  
  176.       int cx;
  177.       int cy;
  178.       int cw;
  179.       int ch;
  180.       try {
  181.          if (this.newbits == null) {
  182.             this.newbits = new Rectangle(0, 0, this.width, this.height);
  183.          }
  184.  
  185.          if (this.setIntPixels(x, y, w, h, model, pix, off, scansize)) {
  186.             this.availinfo |= 8;
  187.             ok = true;
  188.          }
  189.  
  190.          cx = this.newbits.x;
  191.          cy = this.newbits.y;
  192.          cw = this.newbits.width;
  193.          ch = this.newbits.height;
  194.       } catch (Throwable var16) {
  195.          throw var16;
  196.       }
  197.  
  198.       if (ok && !this.offscreen) {
  199.          ((ImageWatched)this).newInfo(this.image, 8, cx, cy, cw, ch);
  200.       }
  201.  
  202.    }
  203.  
  204.    private native boolean finish(boolean var1);
  205.  
  206.    public void imageComplete(int status) {
  207.       if (this.src != null) {
  208.          this.src.checkSecurity((Object)null, false);
  209.       }
  210.  
  211.       boolean done;
  212.       int info;
  213.       switch (status) {
  214.          case 1:
  215.             this.image.addInfo(64);
  216.             done = true;
  217.             info = 64;
  218.             this.dispose();
  219.             break;
  220.          case 2:
  221.             done = false;
  222.             info = 16;
  223.             break;
  224.          case 3:
  225.             done = true;
  226.             info = 32;
  227.             if (this.finish(false)) {
  228.                this.image.getSource().requestTopDownLeftRightResend(this);
  229.                this.finish(true);
  230.             }
  231.             break;
  232.          case 4:
  233.          default:
  234.             done = true;
  235.             info = 128;
  236.       }
  237.  
  238.       synchronized(this){}
  239.  
  240.       try {
  241.          if (done) {
  242.             this.image.getSource().removeConsumer(this);
  243.             this.consuming = false;
  244.             this.newbits = null;
  245.          }
  246.  
  247.          this.availinfo |= info;
  248.          this.notifyAll();
  249.       } catch (Throwable var6) {
  250.          throw var6;
  251.       }
  252.  
  253.       if (!this.offscreen) {
  254.          ((ImageWatched)this).newInfo(this.image, info, 0, 0, this.width, this.height);
  255.       }
  256.  
  257.    }
  258.  
  259.    void startProduction() {
  260.       if (!this.consuming) {
  261.          this.consuming = true;
  262.          this.image.getSource().startProduction(this);
  263.       }
  264.  
  265.    }
  266.  
  267.    private synchronized void checkConsumption() {
  268.       if (super.watchers == null && this.numWaiters == 0 && (this.availinfo & 32) == 0) {
  269.          this.dispose();
  270.       }
  271.  
  272.    }
  273.  
  274.    public synchronized void removeWatcher(ImageObserver iw) {
  275.       super.removeWatcher(iw);
  276.       this.checkConsumption();
  277.    }
  278.  
  279.    private synchronized void decrementWaiters() {
  280.       --this.numWaiters;
  281.       this.checkConsumption();
  282.    }
  283.  
  284.    public boolean prepare(ImageObserver iw) {
  285.       if (this.src != null) {
  286.          this.src.checkSecurity((Object)null, false);
  287.       }
  288.  
  289.       if ((this.availinfo & 64) != 0) {
  290.          if (iw != null) {
  291.             iw.imageUpdate(this.image, 192, -1, -1, -1, -1);
  292.          }
  293.  
  294.          return false;
  295.       } else {
  296.          boolean done = (this.availinfo & 32) != 0;
  297.          if (!done) {
  298.             ((ImageWatched)this).addWatcher(iw);
  299.             this.startProduction();
  300.          }
  301.  
  302.          return done;
  303.       }
  304.    }
  305.  
  306.    public int check(ImageObserver iw) {
  307.       if (this.src != null) {
  308.          this.src.checkSecurity((Object)null, false);
  309.       }
  310.  
  311.       if ((this.availinfo & 96) != 0) {
  312.          ((ImageWatched)this).addWatcher(iw);
  313.       }
  314.  
  315.       return this.availinfo;
  316.    }
  317.  
  318.    synchronized native void imageDraw(Graphics var1, int var2, int var3, Color var4);
  319.  
  320.    public boolean drawImage(Graphics g, int x, int y, Color c, ImageObserver iw) {
  321.       if (this.src != null) {
  322.          this.src.checkSecurity((Object)null, false);
  323.       }
  324.  
  325.       if ((this.availinfo & 64) != 0) {
  326.          if (iw != null) {
  327.             iw.imageUpdate(this.image, 192, -1, -1, -1, -1);
  328.          }
  329.  
  330.          return false;
  331.       } else {
  332.          boolean done = (this.availinfo & 32) != 0;
  333.          if (!done) {
  334.             ((ImageWatched)this).addWatcher(iw);
  335.             this.startProduction();
  336.          }
  337.  
  338.          this.imageDraw(g, x, y, c);
  339.          return done;
  340.       }
  341.    }
  342.  
  343.    private native void disposeImage();
  344.  
  345.    synchronized void abort() {
  346.       this.image.getSource().removeConsumer(this);
  347.       this.consuming = false;
  348.       this.newbits = null;
  349.       this.disposeImage();
  350.       ((ImageWatched)this).newInfo(this.image, 128, -1, -1, -1, -1);
  351.       this.availinfo &= -121;
  352.    }
  353.  
  354.    synchronized void dispose() {
  355.       this.image.getSource().removeConsumer(this);
  356.       this.consuming = false;
  357.       this.newbits = null;
  358.       this.disposeImage();
  359.       this.availinfo &= -57;
  360.    }
  361.  
  362.    public void finalize() {
  363.       this.disposeImage();
  364.    }
  365. }
  366.