home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / Notes.jar / Acme / JPM / Encoders / ImageEncoder.class (.txt) < prev   
Encoding:
Java Class File  |  1998-11-16  |  4.3 KB  |  178 lines

  1. package Acme.JPM.Encoders;
  2.  
  3. import java.awt.Image;
  4. import java.awt.image.ColorModel;
  5. import java.awt.image.ImageConsumer;
  6. import java.awt.image.ImageProducer;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.util.Hashtable;
  10.  
  11. public abstract class ImageEncoder implements ImageConsumer {
  12.    protected OutputStream out;
  13.    private ImageProducer producer;
  14.    private int width;
  15.    private int height;
  16.    private int hintflags;
  17.    private boolean started;
  18.    private boolean encoding;
  19.    private IOException iox;
  20.    private static final ColorModel rgbModel = ColorModel.getRGBdefault();
  21.    private Hashtable props;
  22.    private boolean accumulate;
  23.    private int[] accumulator;
  24.  
  25.    public ImageEncoder(Image img, OutputStream out) throws IOException {
  26.       this(img.getSource(), out);
  27.    }
  28.  
  29.    public ImageEncoder(ImageProducer producer, OutputStream out) throws IOException {
  30.       this.width = -1;
  31.       this.height = -1;
  32.       this.started = false;
  33.       this.accumulate = false;
  34.       this.producer = producer;
  35.       this.out = out;
  36.    }
  37.  
  38.    abstract void encodeStart(int var1, int var2) throws IOException;
  39.  
  40.    abstract void encodePixels(int var1, int var2, int var3, int var4, int[] var5, int var6, int var7) throws IOException;
  41.  
  42.    abstract void encodeDone() throws IOException;
  43.  
  44.    public synchronized void encode() throws IOException {
  45.       this.encoding = true;
  46.       this.iox = null;
  47.       this.producer.startProduction(this);
  48.  
  49.       while(this.encoding) {
  50.          try {
  51.             this.wait();
  52.          } catch (InterruptedException var1) {
  53.          }
  54.       }
  55.  
  56.       if (this.iox != null) {
  57.          throw this.iox;
  58.       }
  59.    }
  60.  
  61.    private void encodePixelsWrapper(int x, int y, int w, int h, int[] rgbPixels, int off, int scansize) throws IOException {
  62.       if (!this.started) {
  63.          this.started = true;
  64.          this.encodeStart(this.width, this.height);
  65.          if ((this.hintflags & 2) == 0) {
  66.             this.accumulate = true;
  67.             this.accumulator = new int[this.width * this.height];
  68.          }
  69.       }
  70.  
  71.       if (!this.accumulate) {
  72.          this.encodePixels(x, y, w, h, rgbPixels, off, scansize);
  73.       } else {
  74.          for(int row = 0; row < h; ++row) {
  75.             System.arraycopy(rgbPixels, row * scansize + off, this.accumulator, (y + row) * this.width + x, w);
  76.          }
  77.  
  78.       }
  79.    }
  80.  
  81.    private void encodeFinish() throws IOException {
  82.       if (this.accumulate) {
  83.          this.encodePixels(0, 0, this.width, this.height, this.accumulator, 0, this.width);
  84.          this.accumulator = null;
  85.          this.accumulate = false;
  86.       }
  87.  
  88.    }
  89.  
  90.    private synchronized void stop() {
  91.       this.encoding = false;
  92.       this.notifyAll();
  93.    }
  94.  
  95.    public void setDimensions(int width, int height) {
  96.       this.width = width;
  97.       this.height = height;
  98.    }
  99.  
  100.    public void setProperties(Hashtable props) {
  101.       this.props = props;
  102.    }
  103.  
  104.    public void setColorModel(ColorModel model) {
  105.    }
  106.  
  107.    public void setHints(int hintflags) {
  108.       this.hintflags = hintflags;
  109.    }
  110.  
  111.    public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) {
  112.       int[] rgbPixels = new int[w];
  113.  
  114.       for(int row = 0; row < h; ++row) {
  115.          int rowOff = off + row * scansize;
  116.  
  117.          for(int col = 0; col < w; ++col) {
  118.             rgbPixels[col] = model.getRGB(pixels[rowOff + col] & 255);
  119.          }
  120.  
  121.          try {
  122.             this.encodePixelsWrapper(x, y + row, w, 1, rgbPixels, 0, w);
  123.          } catch (IOException e) {
  124.             this.iox = e;
  125.             this.stop();
  126.             return;
  127.          }
  128.       }
  129.  
  130.    }
  131.  
  132.    public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) {
  133.       if (model == rgbModel) {
  134.          try {
  135.             this.encodePixelsWrapper(x, y, w, h, pixels, off, scansize);
  136.          } catch (IOException e) {
  137.             this.iox = e;
  138.             this.stop();
  139.          }
  140.       } else {
  141.          int[] rgbPixels = new int[w];
  142.  
  143.          for(int row = 0; row < h; ++row) {
  144.             int rowOff = off + row * scansize;
  145.  
  146.             for(int col = 0; col < w; ++col) {
  147.                rgbPixels[col] = model.getRGB(pixels[rowOff + col]);
  148.             }
  149.  
  150.             try {
  151.                this.encodePixelsWrapper(x, y + row, w, 1, rgbPixels, 0, w);
  152.             } catch (IOException e) {
  153.                this.iox = e;
  154.                this.stop();
  155.                return;
  156.             }
  157.          }
  158.  
  159.       }
  160.    }
  161.  
  162.    public void imageComplete(int status) {
  163.       this.producer.removeConsumer(this);
  164.       if (status == 4) {
  165.          this.iox = new IOException("image aborted");
  166.       } else {
  167.          try {
  168.             this.encodeFinish();
  169.             this.encodeDone();
  170.          } catch (IOException e) {
  171.             this.iox = e;
  172.          }
  173.       }
  174.  
  175.       this.stop();
  176.    }
  177. }
  178.