home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / awt / image / XbmImageDecoder.java < prev   
Encoding:
Java Source  |  1997-01-27  |  5.0 KB  |  190 lines

  1. /*
  2.  * @(#)XbmImageDecoder.java    1.8 96/03/13 James Gosling
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. /*-
  21.  *    Reads xbitmap format images into a DIBitmap structure.
  22.  */
  23. package sun.awt.image;
  24.  
  25. import java.io.*;
  26. import java.awt.image.*;
  27.  
  28. /**
  29.  * Parse files of the form:
  30.  * 
  31.  * #define foo_width w
  32.  * #define foo_height h
  33.  * static char foo_bits[] = {
  34.  * 0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,
  35.  * 0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,
  36.  * 0xnn,0xnn,0xnn,0xnn};
  37.  * 
  38.  * @version 1.8 03/13/96
  39.  * @author James Gosling
  40.  */
  41. public class XbmImageDecoder extends ImageDecoder {
  42.     private static byte XbmColormap[] = {(byte) 255, (byte) 255, (byte) 255,
  43.                      0, 0, 0};
  44.     private static int XbmHints = (ImageConsumer.TOPDOWNLEFTRIGHT |
  45.                    ImageConsumer.COMPLETESCANLINES |
  46.                    ImageConsumer.SINGLEPASS |
  47.                    ImageConsumer.SINGLEFRAME);
  48.  
  49.     public XbmImageDecoder(InputStreamImageSource src, InputStream is) {
  50.     super(src, is);
  51.     if (!(input instanceof BufferedInputStream)) {
  52.         // If the topmost stream is a metered stream,
  53.         // we take forever to decode the image...
  54.         input = new BufferedInputStream(input, 80);
  55.     }
  56.     }
  57.  
  58.     PixelStore8 store;
  59.  
  60.     public synchronized boolean catchupConsumer(InputStreamImageSource src,
  61.                         ImageConsumer ic)
  62.     {
  63.     return ((store == null) || store.replay(src, ic));
  64.     }
  65.  
  66.     public synchronized void makeStore(int width, int height) {
  67.     store = new PixelStore8(width, height);
  68.     }
  69.  
  70.     /**
  71.      * An error has occurred. Throw an exception.
  72.      */
  73.     private static void error(String s1) throws ImageFormatException {
  74.     throw new ImageFormatException(s1);
  75.     }
  76.  
  77.     /**
  78.      * produce an image from the stream.
  79.      */
  80.     public void produceImage() throws IOException, ImageFormatException {
  81.     char nm[] = new char[80];
  82.     int c;
  83.     int i = 0;
  84.     int state = 0;
  85.     int H = 0;
  86.     int W = 0;
  87.     int x = 0;
  88.     int y = 0;
  89.     boolean start = true;
  90.     byte raster[] = null;
  91.     IndexColorModel model = null;
  92.     while ((c = input.read()) != -1) {
  93.         if ('a' <= c && c <= 'z' ||
  94.             'A' <= c && c <= 'Z' ||
  95.             '0' <= c && c <= '9' || c == '#' || c == '_') {
  96.         if (i < 78)
  97.             nm[i++] = (char) c;
  98.         } else if (i > 0) {
  99.         int nc = i;
  100.         i = 0;
  101.         if (start) {
  102.             if (nc != 7 ||
  103.             nm[0] != '#' ||
  104.             nm[1] != 'd' ||
  105.             nm[2] != 'e' ||
  106.             nm[3] != 'f' ||
  107.             nm[4] != 'i' ||
  108.             nm[5] != 'n' ||
  109.             nm[6] != 'e')
  110.             {
  111.             error("Not an XBM file");
  112.             }
  113.             start = false;
  114.         }
  115.         if (nm[nc - 1] == 'h')
  116.             state = 1;    /* expecting width */
  117.         else if (nm[nc - 1] == 't' && nc > 1 && nm[nc - 2] == 'h')
  118.             state = 2;    /* expecting height */
  119.         else if (nc > 2 && state < 0 && nm[0] == '0' && nm[1] == 'x') {
  120.             int n = 0;
  121.             for (int p = 2; p < nc; p++) {
  122.             c = nm[p];
  123.             if ('0' <= c && c <= '9')
  124.                 c = c - '0';
  125.             else if ('A' <= c && c <= 'Z')
  126.                 c = c - 'A' + 10;
  127.             else if ('a' <= c && c <= 'z')
  128.                 c = c - 'a' + 10;
  129.             else
  130.                 c = 0;
  131.             n = n * 16 + c;
  132.             }
  133.             for (int mask = 1; mask <= 0x80; mask <<= 1) {
  134.             if (x < W) {
  135.                 if ((n & mask) != 0)
  136.                 raster[x] = 1;
  137.                 else
  138.                 raster[x] = 0;
  139.             }
  140.             x++;
  141.             }
  142.             if (x >= W) {
  143.             source.setPixels(0, y, W, 1, model, raster, 0, W);
  144.             store.setPixels(0, y, W, 1, raster, 0, W);
  145.             x = 0;
  146.             if (y++ >= H) {
  147.                 break;
  148.             }
  149.             }
  150.         } else {
  151.             int n = 0;
  152.             for (int p = 0; p < nc; p++)
  153.             if ('0' <= (c = nm[p]) && c <= '9')
  154.                 n = n * 10 + c - '0';
  155.             else {
  156.                 n = -1;
  157.                 break;
  158.             }
  159.             if (n > 0 && state > 0) {
  160.             if (state == 1)
  161.                 W = n;
  162.             else
  163.                 H = n;
  164.             if (W == 0 || H == 0)
  165.                 state = 0;
  166.             else {
  167.                 model = new IndexColorModel(8, 2, XbmColormap,
  168.                             0, false, 0);
  169.                 source.setDimensions(W, H);
  170.                 makeStore(W, H);
  171.                 source.setColorModel(model);
  172.                 store.setColorModel(model);
  173.                 source.setHints(XbmHints);
  174.                 store.setHints(XbmHints);
  175.                 raster = new byte[W];
  176.                 state = -1;
  177.             }
  178.             }
  179.         }
  180.         }
  181.     }
  182.     input.close();
  183.     store.imageComplete();
  184.     if (store.getBitState() != PixelStore.BITS_LOST) {
  185.         source.setPixelStore(store);
  186.     }
  187.     source.imageComplete(ImageConsumer.STATICIMAGEDONE);
  188.     }
  189. }
  190.