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

  1. package sun.awt.image;
  2.  
  3. import java.awt.image.ColorModel;
  4. import java.awt.image.ImageConsumer;
  5. import java.awt.image.ImageProducer;
  6. import java.util.Hashtable;
  7. import sun.misc.Ref;
  8.  
  9. public abstract class PixelStore extends Ref {
  10.    int width;
  11.    int height;
  12.    ColorModel colormodel;
  13.    Hashtable properties;
  14.    boolean[] seen;
  15.    int numlines;
  16.    int availinfo;
  17.    int hints;
  18.    static final int NO_BITS_YET = 0;
  19.    static final int BITS_ALLOCATED = 1;
  20.    static final int BITS_LOST = 2;
  21.    int bit_state;
  22.    int[] offsets;
  23.  
  24.    public PixelStore() {
  25.    }
  26.  
  27.    public PixelStore(int w, int h) {
  28.       this.setDimensions(w, h);
  29.    }
  30.  
  31.    public PixelStore(int w, int h, ColorModel cm) {
  32.       this.setDimensions(w, h);
  33.       this.setColorModel(cm);
  34.    }
  35.  
  36.    public synchronized void setDimensions(int w, int h) {
  37.       this.width = w;
  38.       this.height = h;
  39.       this.availinfo |= 3;
  40.    }
  41.  
  42.    public synchronized void setProperties(Hashtable props) {
  43.       this.properties = props;
  44.       this.availinfo |= 4;
  45.    }
  46.  
  47.    public synchronized void setColorModel(ColorModel cm) {
  48.       this.colormodel = cm;
  49.    }
  50.  
  51.    public synchronized void setHints(int h) {
  52.       this.hints = h;
  53.    }
  54.  
  55.    protected void recordPixels(int x, int y, int w, int h) {
  56.       this.numlines = Math.max(this.numlines, y + h);
  57.       if ((this.hints & 2) == 0) {
  58.          if (this.seen == null) {
  59.             this.seen = new boolean[this.height];
  60.          }
  61.  
  62.          for(int i = 0; i < h; ++i) {
  63.             this.seen[y + i] = true;
  64.          }
  65.       }
  66.  
  67.    }
  68.  
  69.    public synchronized boolean setPixels(int x, int y, int w, int h, byte[] pix, int srcoff, int scansize) {
  70.       this.recordPixels(x, y, w, h);
  71.       Object[] lines = ((Ref)this).get();
  72.       if (lines != null) {
  73.          int i = srcoff;
  74.  
  75.          for(int Y = y; Y < y + h; ++Y) {
  76.             byte[] ras = (byte[])lines[Y];
  77.             int dstoff = this.offsets[Y] + x;
  78.             System.arraycopy(pix, i, ras, dstoff, w);
  79.             i += scansize;
  80.          }
  81.  
  82.          this.availinfo |= 8;
  83.       }
  84.  
  85.       return lines != null;
  86.    }
  87.  
  88.    public synchronized boolean setPixels(int x, int y, int w, int h, int[] pix, int srcoff, int scansize) {
  89.       this.recordPixels(x, y, w, h);
  90.       Object[] lines = ((Ref)this).get();
  91.       if (lines != null) {
  92.          int i = srcoff;
  93.  
  94.          for(int Y = y; Y < y + h; ++Y) {
  95.             int[] ras = (int[])lines[Y];
  96.             int dstoff = this.offsets[Y] + x;
  97.             System.arraycopy(pix, i, ras, dstoff, w);
  98.             i += scansize;
  99.          }
  100.  
  101.          this.availinfo |= 8;
  102.       }
  103.  
  104.       return lines != null;
  105.    }
  106.  
  107.    public synchronized void imageComplete() {
  108.       if (((Ref)this).get() != null && this.bit_state == 1) {
  109.          this.numlines = this.height;
  110.          this.hints = 30;
  111.          this.availinfo |= 32;
  112.       }
  113.  
  114.    }
  115.  
  116.    public synchronized int getWidth() {
  117.       return this.width;
  118.    }
  119.  
  120.    public synchronized int getHeight() {
  121.       return this.height;
  122.    }
  123.  
  124.    public synchronized ColorModel getColorModel() {
  125.       return this.colormodel;
  126.    }
  127.  
  128.    public synchronized int getBitState() {
  129.       if (this.bit_state == 1 && this.numlines > 0) {
  130.          ((Ref)this).get();
  131.       }
  132.  
  133.       return this.bit_state;
  134.    }
  135.  
  136.    abstract void replayLines(ImageConsumer var1, int var2, int var3, Object var4);
  137.  
  138.    public synchronized boolean replay(ImageProducer ip, ImageConsumer ic) {
  139.       return this.replay(ip, ic, true);
  140.    }
  141.  
  142.    public synchronized boolean replay(ImageProducer ip, ImageConsumer ic, boolean full) {
  143.       if (full && (this.availinfo & 1) != 0 && (this.availinfo & 2) != 0) {
  144.          ic.setDimensions(this.width, this.height);
  145.          if (!ip.isConsumer(ic)) {
  146.             return true;
  147.          }
  148.       }
  149.  
  150.       if (full && (this.availinfo & 4) != 0) {
  151.          ic.setProperties(this.properties);
  152.       }
  153.  
  154.       if (full && this.colormodel != null) {
  155.          ic.setColorModel(this.colormodel);
  156.          if (!ip.isConsumer(ic)) {
  157.             return true;
  158.          }
  159.       }
  160.  
  161.       if (this.hints != 0) {
  162.          ic.setHints(this.hints & 30);
  163.          if (!ip.isConsumer(ic)) {
  164.             return true;
  165.          }
  166.       }
  167.  
  168.       Object[] lines = null;
  169.       if (this.bit_state == 1 && this.numlines > 0) {
  170.          lines = ((Ref)this).get();
  171.       }
  172.  
  173.       if (this.bit_state == 2) {
  174.          return false;
  175.       } else {
  176.          Thread me = Thread.currentThread();
  177.          if (ImageFetcher.isFetcher(me) && me.getPriority() > 3) {
  178.             SecurityManager.setScopePermission();
  179.             me.setPriority(3);
  180.             SecurityManager.resetScopePermission();
  181.          }
  182.  
  183.          if (lines != null) {
  184.             Object prevline = null;
  185.             int prevcount = 0;
  186.  
  187.             for(int i = 0; i < this.numlines; ++i) {
  188.                if (this.seen != null && !this.seen[i]) {
  189.                   if (prevline != null) {
  190.                      this.replayLines(ic, i - prevcount, prevcount, prevline);
  191.                      if (!ip.isConsumer(ic)) {
  192.                         return true;
  193.                      }
  194.  
  195.                      prevline = null;
  196.                      prevcount = 0;
  197.                   }
  198.                } else {
  199.                   Object line = lines[i];
  200.                   if (prevline != line && prevline != null) {
  201.                      this.replayLines(ic, i - prevcount, prevcount, prevline);
  202.                      if (!ip.isConsumer(ic)) {
  203.                         return true;
  204.                      }
  205.  
  206.                      prevcount = 0;
  207.                   }
  208.  
  209.                   prevline = line;
  210.                   ++prevcount;
  211.                }
  212.             }
  213.  
  214.             if (prevline != null) {
  215.                this.replayLines(ic, this.numlines - prevcount, prevcount, prevline);
  216.                if (!ip.isConsumer(ic)) {
  217.                   return true;
  218.                }
  219.             }
  220.          }
  221.  
  222.          if (full && this.bit_state == 1 && (this.availinfo & 32) != 0) {
  223.             ic.imageComplete(3);
  224.          }
  225.  
  226.          return true;
  227.       }
  228.    }
  229.  
  230.    abstract Object allocateLines(int var1);
  231.  
  232.    public Object reconstitute() {
  233.       Object[] lines = null;
  234.       if (this.bit_state == 0) {
  235.          if ((this.availinfo & 2) == 0 || (this.availinfo & 1) == 0 || this.colormodel == null) {
  236.             return null;
  237.          }
  238.  
  239.          this.bit_state = 1;
  240.          lines = new Object[this.height];
  241.          this.offsets = new int[this.height];
  242.          int i = 0;
  243.  
  244.          while(i < this.height) {
  245.             int trylines = this.height - i;
  246.             Object chunk = null;
  247.  
  248.             while(chunk == null && trylines > 0) {
  249.                try {
  250.                   chunk = this.allocateLines(trylines);
  251.                   if (chunk == null) {
  252.                      break;
  253.                   }
  254.                } catch (OutOfMemoryError var6) {
  255.                   chunk = null;
  256.                   trylines /= 2;
  257.                }
  258.             }
  259.  
  260.             if (chunk == null) {
  261.                this.bit_state = 2;
  262.                return null;
  263.             }
  264.  
  265.             for(int off = 0; trylines > 0; --trylines) {
  266.                lines[i] = chunk;
  267.                this.offsets[i] = off;
  268.                off += this.width;
  269.                ++i;
  270.             }
  271.          }
  272.       } else if (this.bit_state == 1) {
  273.          this.bit_state = 2;
  274.       }
  275.  
  276.       return lines;
  277.    }
  278. }
  279.