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

  1. package sun.awt.image;
  2.  
  3. import java.awt.image.ImageConsumer;
  4. import java.awt.image.IndexColorModel;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7.  
  8. public class XbmImageDecoder extends ImageDecoder {
  9.    private static byte[] XbmColormap = new byte[]{-1, -1, -1, 0, 0, 0};
  10.    private static int XbmHints = 30;
  11.    PixelStore8 store;
  12.  
  13.    public XbmImageDecoder(InputStreamImageSource src, InputStream is) {
  14.       super(src, is);
  15.    }
  16.  
  17.    public synchronized boolean catchupConsumer(InputStreamImageSource src, ImageConsumer ic) {
  18.       return this.store == null || this.store.replay(src, ic);
  19.    }
  20.  
  21.    public synchronized void makeStore(int width, int height) {
  22.       this.store = new PixelStore8(width, height);
  23.    }
  24.  
  25.    private static void error(String s1) throws ImageFormatException {
  26.       throw new ImageFormatException(s1);
  27.    }
  28.  
  29.    public void produceImage() throws IOException, ImageFormatException {
  30.       char[] nm = new char[80];
  31.       int i = 0;
  32.       int state = 0;
  33.       int H = 0;
  34.       int W = 0;
  35.       int x = 0;
  36.       int y = 0;
  37.       boolean start = true;
  38.       byte[] raster = null;
  39.       IndexColorModel model = null;
  40.  
  41.       int c;
  42.       while((c = super.input.read()) != -1) {
  43.          if ((c < 97 || c > 122) && (c < 65 || c > 90) && (c < 48 || c > 57) && c != 35 && c != 95) {
  44.             if (i > 0) {
  45.                int nc = i;
  46.                i = 0;
  47.                if (start) {
  48.                   if (nc != 7 || nm[0] != '#' || nm[1] != 'd' || nm[2] != 'e' || nm[3] != 'f' || nm[4] != 'i' || nm[5] != 'n' || nm[6] != 'e') {
  49.                      error("Not an XBM file");
  50.                   }
  51.  
  52.                   start = false;
  53.                }
  54.  
  55.                if (nm[nc - 1] == 'h') {
  56.                   state = 1;
  57.                } else if (nm[nc - 1] == 't' && nc > 1 && nm[nc - 2] == 'h') {
  58.                   state = 2;
  59.                } else if (nc > 2 && state < 0 && nm[0] == '0' && nm[1] == 'x') {
  60.                   int n = 0;
  61.  
  62.                   for(int p = 2; p < nc; ++p) {
  63.                      c = nm[p];
  64.                      if (c >= 48 && c <= 57) {
  65.                         c -= 48;
  66.                      } else if (c >= 65 && c <= 90) {
  67.                         c = c - 65 + 10;
  68.                      } else if (c >= 97 && c <= 122) {
  69.                         c = c - 97 + 10;
  70.                      } else {
  71.                         c = 0;
  72.                      }
  73.  
  74.                      n = n * 16 + c;
  75.                   }
  76.  
  77.                   for(int mask = 1; mask <= 128; mask <<= 1) {
  78.                      if (x < W) {
  79.                         if ((n & mask) != 0) {
  80.                            raster[x] = 1;
  81.                         } else {
  82.                            raster[x] = 0;
  83.                         }
  84.                      }
  85.  
  86.                      ++x;
  87.                   }
  88.  
  89.                   if (x >= W) {
  90.                      super.source.setPixels(0, y, W, 1, model, raster, 0, W);
  91.                      this.store.setPixels(0, y, W, 1, raster, 0, W);
  92.                      x = 0;
  93.                      if (y++ >= H) {
  94.                         break;
  95.                      }
  96.                   }
  97.                } else {
  98.                   int n = 0;
  99.  
  100.                   for(int p = 0; p < nc; ++p) {
  101.                      if ((c = nm[p]) < 48 || c > 57) {
  102.                         n = -1;
  103.                         break;
  104.                      }
  105.  
  106.                      n = n * 10 + c - 48;
  107.                   }
  108.  
  109.                   if (n > 0 && state > 0) {
  110.                      if (state == 1) {
  111.                         W = n;
  112.                      } else {
  113.                         H = n;
  114.                      }
  115.  
  116.                      if (W != 0 && H != 0) {
  117.                         model = new IndexColorModel(8, 2, XbmColormap, 0, false, 0);
  118.                         super.source.setDimensions(W, H);
  119.                         this.makeStore(W, H);
  120.                         super.source.setColorModel(model);
  121.                         this.store.setColorModel(model);
  122.                         super.source.setHints(XbmHints);
  123.                         this.store.setHints(XbmHints);
  124.                         raster = new byte[W];
  125.                         state = -1;
  126.                      } else {
  127.                         state = 0;
  128.                      }
  129.                   }
  130.                }
  131.             }
  132.          } else if (i < 78) {
  133.             nm[i++] = (char)c;
  134.          }
  135.       }
  136.  
  137.       super.input.close();
  138.       this.store.imageComplete();
  139.       if (this.store.getBitState() != 2) {
  140.          super.source.setPixelStore(this.store);
  141.       }
  142.  
  143.       super.source.imageComplete(3);
  144.    }
  145. }
  146.