home *** CD-ROM | disk | FTP | other *** search
- package sun.awt.image;
-
- import java.awt.image.ImageConsumer;
- import java.awt.image.IndexColorModel;
- import java.io.IOException;
- import java.io.InputStream;
-
- public class XbmImageDecoder extends ImageDecoder {
- private static byte[] XbmColormap = new byte[]{-1, -1, -1, 0, 0, 0};
- private static int XbmHints = 30;
- PixelStore8 store;
-
- public XbmImageDecoder(InputStreamImageSource src, InputStream is) {
- super(src, is);
- }
-
- public synchronized boolean catchupConsumer(InputStreamImageSource src, ImageConsumer ic) {
- return this.store == null || this.store.replay(src, ic);
- }
-
- public synchronized void makeStore(int width, int height) {
- this.store = new PixelStore8(width, height);
- }
-
- private static void error(String s1) throws ImageFormatException {
- throw new ImageFormatException(s1);
- }
-
- public void produceImage() throws IOException, ImageFormatException {
- char[] nm = new char[80];
- int i = 0;
- int state = 0;
- int H = 0;
- int W = 0;
- int x = 0;
- int y = 0;
- boolean start = true;
- byte[] raster = null;
- IndexColorModel model = null;
-
- int c;
- while((c = super.input.read()) != -1) {
- if ((c < 97 || c > 122) && (c < 65 || c > 90) && (c < 48 || c > 57) && c != 35 && c != 95) {
- if (i > 0) {
- int nc = i;
- i = 0;
- if (start) {
- if (nc != 7 || nm[0] != '#' || nm[1] != 'd' || nm[2] != 'e' || nm[3] != 'f' || nm[4] != 'i' || nm[5] != 'n' || nm[6] != 'e') {
- error("Not an XBM file");
- }
-
- start = false;
- }
-
- if (nm[nc - 1] == 'h') {
- state = 1;
- } else if (nm[nc - 1] == 't' && nc > 1 && nm[nc - 2] == 'h') {
- state = 2;
- } else if (nc > 2 && state < 0 && nm[0] == '0' && nm[1] == 'x') {
- int n = 0;
-
- for(int p = 2; p < nc; ++p) {
- c = nm[p];
- if (c >= 48 && c <= 57) {
- c -= 48;
- } else if (c >= 65 && c <= 90) {
- c = c - 65 + 10;
- } else if (c >= 97 && c <= 122) {
- c = c - 97 + 10;
- } else {
- c = 0;
- }
-
- n = n * 16 + c;
- }
-
- for(int mask = 1; mask <= 128; mask <<= 1) {
- if (x < W) {
- if ((n & mask) != 0) {
- raster[x] = 1;
- } else {
- raster[x] = 0;
- }
- }
-
- ++x;
- }
-
- if (x >= W) {
- super.source.setPixels(0, y, W, 1, model, raster, 0, W);
- this.store.setPixels(0, y, W, 1, raster, 0, W);
- x = 0;
- if (y++ >= H) {
- break;
- }
- }
- } else {
- int n = 0;
-
- for(int p = 0; p < nc; ++p) {
- if ((c = nm[p]) < 48 || c > 57) {
- n = -1;
- break;
- }
-
- n = n * 10 + c - 48;
- }
-
- if (n > 0 && state > 0) {
- if (state == 1) {
- W = n;
- } else {
- H = n;
- }
-
- if (W != 0 && H != 0) {
- model = new IndexColorModel(8, 2, XbmColormap, 0, false, 0);
- super.source.setDimensions(W, H);
- this.makeStore(W, H);
- super.source.setColorModel(model);
- this.store.setColorModel(model);
- super.source.setHints(XbmHints);
- this.store.setHints(XbmHints);
- raster = new byte[W];
- state = -1;
- } else {
- state = 0;
- }
- }
- }
- }
- } else if (i < 78) {
- nm[i++] = (char)c;
- }
- }
-
- super.input.close();
- this.store.imageComplete();
- if (this.store.getBitState() != 2) {
- super.source.setPixelStore(this.store);
- }
-
- super.source.imageComplete(3);
- }
- }
-