Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / gnu / java / awt / image / XBMDecoder.java
blobda58dbbcfe39a809ea88a91550ec557cb72cbc6a
1 /* XBMDecoder -- Decodes X-bitmaps
2 Copyright (C) 1999, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.awt.image;
41 import java.awt.image.ColorModel;
42 import java.awt.image.ImageConsumer;
43 import java.io.BufferedReader;
44 import java.io.InputStream;
45 import java.io.InputStreamReader;
46 import java.io.IOException;
47 import java.io.Reader;
48 import java.net.URL;
49 import java.util.StringTokenizer;
50 import java.util.Vector;
52 public class XBMDecoder extends ImageDecoder
54 BufferedReader reader;
55 static final ColorModel cm = ColorModel.getRGBdefault ();
56 static final int black = 0xff000000;
57 static final int transparent = 0x00000000;
58 static final int masktable[] = { 0x01, 0x02, 0x04, 0x08,
59 0x10, 0x20, 0x40, 0x80 };
61 public XBMDecoder (String filename)
63 super (filename);
66 public XBMDecoder (URL url)
68 super (url);
71 public void produce (Vector v, InputStream is) throws IOException
73 reader = new BufferedReader (new InputStreamReader (is));
74 int width = -1, height = -1;
76 for (int i = 0; i < 2; i++)
78 String line = reader.readLine ();
79 StringTokenizer st = new StringTokenizer (line);
81 st.nextToken (); // #define
82 st.nextToken (); // name_[width|height]
83 if (i == 0)
84 width = Integer.parseInt (st.nextToken (), 10);
85 else
86 height = Integer.parseInt (st.nextToken (), 10);
89 for (int i = 0; i < v.size (); i++)
91 ImageConsumer ic = (ImageConsumer) v.elementAt (i);
93 ic.setDimensions (width, height);
94 ic.setColorModel (cm);
95 ic.setHints (ImageConsumer.COMPLETESCANLINES
96 | ImageConsumer.SINGLEFRAME
97 | ImageConsumer.SINGLEPASS
98 | ImageConsumer.TOPDOWNLEFTRIGHT);
101 /* skip to the byte array */
102 while (reader.read () != '{') { }
104 /* loop through each scanline */
105 for (int line = 0; line < height; line++)
107 int scanline[] = getScanline (reader, width);
109 for (int i = 0; i < v.size (); i++)
111 ImageConsumer ic = (ImageConsumer) v.elementAt (i);
112 ic.setPixels (0, 0 + line, width, 1, cm, scanline, 0, width);
116 /* tell each ImageConsumer that we're finished */
117 for (int i = 0; i < v.size (); i++)
119 ImageConsumer ic = (ImageConsumer) v.elementAt (i);
120 ic.imageComplete (ImageConsumer.STATICIMAGEDONE);
124 static public int[] getScanline (Reader in, int len) throws IOException
126 char byteStr[] = new char[2];
127 int scanline[] = new int[len];
128 int x = 0;
130 while (x < len)
132 int ch = in.read ();
133 if (ch == '0')
135 in.read (); // 'x'
137 byteStr[0] = (char) in.read ();
138 byteStr[1] = (char) in.read ();
140 int byteVal = Integer.parseInt (new String (byteStr), 16);
142 for (int i = 0; i < 8; i++, x++)
144 if (x == len) // condition occurs if bitmap is padded
145 return scanline;
147 scanline[x] = ((byteVal & masktable[i]) != 0) ?
148 black : transparent;
153 return scanline;