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

  1. package sun.awt.image;
  2.  
  3. import java.awt.image.ColorModel;
  4. import java.awt.image.DirectColorModel;
  5. import java.awt.image.ImageConsumer;
  6. import java.awt.image.IndexColorModel;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.util.Hashtable;
  10.  
  11. public class JPEGImageDecoder extends ImageDecoder {
  12.    private static ColorModel RGBcolormodel;
  13.    private static ColorModel Graycolormodel;
  14.    PixelStore store;
  15.    Hashtable props = new Hashtable();
  16.    private static final int hintflags = 22;
  17.  
  18.    private native void readImage(InputStream var1, byte[] var2) throws ImageFormatException;
  19.  
  20.    public JPEGImageDecoder(InputStreamImageSource src, InputStream is) {
  21.       super(src, is);
  22.    }
  23.  
  24.    public synchronized boolean catchupConsumer(InputStreamImageSource src, ImageConsumer ic) {
  25.       return this.store == null || this.store.replay(src, ic);
  26.    }
  27.  
  28.    public synchronized void makeStore(int width, int height, boolean gray) {
  29.       if (gray) {
  30.          this.store = new PixelStore8(width, height);
  31.       } else {
  32.          this.store = new PixelStore32(width, height);
  33.       }
  34.    }
  35.  
  36.    private static void error(String s1) throws ImageFormatException {
  37.       throw new ImageFormatException(s1);
  38.    }
  39.  
  40.    public boolean sendHeaderInfo(int width, int height, boolean gray, boolean multipass) {
  41.       super.source.setDimensions(width, height);
  42.       this.makeStore(width, height, gray);
  43.       super.source.setProperties(this.props);
  44.       this.store.setProperties(this.props);
  45.       ColorModel colormodel = gray ? Graycolormodel : RGBcolormodel;
  46.       super.source.setColorModel(colormodel);
  47.       this.store.setColorModel(colormodel);
  48.       int flags = 22;
  49.       if (!multipass) {
  50.          flags |= 8;
  51.       }
  52.  
  53.       super.source.setHints(22);
  54.       this.store.setHints(22);
  55.       return true;
  56.    }
  57.  
  58.    public boolean sendPixels(int[] pixels, int y) {
  59.       int count = super.source.setPixels(0, y, pixels.length, 1, RGBcolormodel, pixels, 0, pixels.length);
  60.       if (this.store.setPixels(0, y, pixels.length, 1, pixels, 0, pixels.length)) {
  61.          ++count;
  62.       }
  63.  
  64.       return count > 0;
  65.    }
  66.  
  67.    public boolean sendPixels(byte[] pixels, int y) {
  68.       int count = super.source.setPixels(0, y, pixels.length, 1, Graycolormodel, pixels, 0, pixels.length);
  69.       if (this.store.setPixels(0, y, pixels.length, 1, pixels, 0, pixels.length)) {
  70.          ++count;
  71.       }
  72.  
  73.       return count > 0;
  74.    }
  75.  
  76.    public void produceImage() throws IOException, ImageFormatException {
  77.       try {
  78.          this.readImage(super.input, new byte[1024]);
  79.          this.store.imageComplete();
  80.          if (this.store.getBitState() != 2) {
  81.             super.source.setPixelStore(this.store);
  82.          }
  83.  
  84.          super.source.imageComplete(3);
  85.       } finally {
  86.          try {
  87.             super.input.close();
  88.          } catch (IOException var6) {
  89.          }
  90.  
  91.       }
  92.  
  93.    }
  94.  
  95.    static {
  96.       SecurityManager.setScopePermission();
  97.       System.loadLibrary("jpeg");
  98.       SecurityManager.resetScopePermission();
  99.       RGBcolormodel = new DirectColorModel(24, 16711680, 65280, 255);
  100.       byte[] g = new byte[256];
  101.  
  102.       for(int i = 0; i < 256; ++i) {
  103.          g[i] = (byte)i;
  104.       }
  105.  
  106.       Graycolormodel = new IndexColorModel(8, 256, g, g, g);
  107.    }
  108. }
  109.