/* * Copyright (c) 1996 by Jan Andersson, Torpa Konsult AB. * * Permission to use, copy, and distribute this software for * NON-COMMERCIAL purposes and without fee is hereby granted * provided that this copyright notice appears in all copies. * */ import java.awt.Color; import java.awt.image.ImageConsumer; import java.awt.image.ImageProducer; import java.awt.image.ColorModel; import java.awt.image.IndexColorModel; import java.util.Hashtable; import java.io.InputStream; import java.io.StringBufferInputStream; /** * This class is an implementation of the ImageProducer interface that * uses a X11 bitmap (xbm) array, X11 pixmap (xpm) array or an input * stream to a xbm/xpm file to produce pixel values for an Image. * * Here is a few examples: which creates an image from a xbm-file: "foo.xbm". * The forground is set to blue and the Component background is used. *
 *     // create image from inline xbm data using transparent color. 
 *     // I.e no background specied.
 *     static private int[] arrow = {
 *        0x3f, 0x0f, 0x0f, 0x1f, 0x39, 0x71, 0xe0, 0xc0
 *     };
 *     Image image = createImage(
 *          new XImageSource(8, 8, arrow, Color.black, nil, true));
 *
 *     // create image from xpm-file "foo.xpm"
 *     FileInputStream is = new FileInputStream("foo.xpm");
 *     Image image = createImage(new XImageSource(is));
 * 
 * 
* * @see ImageProducer * * @version 1.1 96/02/20 * @author Jan Andersson, Torpa Konsult AB. (janne@torpa.se) */ public class XImageSource implements ImageProducer { int width; int height; byte pixels[]; ColorModel colorModel; private boolean error = false; private ImageConsumer theConsumer; /** * Constructs an ImageProducer object from an X11 style bitmap (xbm) * to produce data for an Image object. * @param w width * @param h height * @param bits bitmap in xbm format * @param fg foreground color * @param bg background color (if trans is false) * @param trans transparent if true */ public XImageSource(int w, int h, int[] bits, Color fg, Color bg, boolean trans) { xbmInitialize(w, h, bits, fg, bg, trans); } /** * Constructs an ImageProducer object from an X11 style bitmap (xbm) * input stream to produce data for an Image object. * @param is input stream to xbm data * @param fg foreground color * @param bg background color (if trans is false) * @param trans transparent if true */ public XImageSource(InputStream is, Color fg, Color bg, boolean trans) { // Use XbmParser to parse input stream XbmParser parser = new XbmParser(is); if (parser.parse()) xbmInitialize(parser.getWidth(), parser.getHeight(), parser.getBitmap(), fg, bg, trans); else error = true; } /** * Constructs an ImageProducer object from an X11 style pixmap (xpm) * consisting of an array of strings. * @param data string array in xpm format. */ public XImageSource(String lines[], int nblines) { // create string buffer from data array StringBuffer strBuf = new StringBuffer(1024); for (int i=0; i