1 package com.insanityengine.ghia.pixels;
2
3 import java.awt.*;
4 import java.awt.image.*;
5 import java.awt.event.*;
6 import java.applet.*;
7
8 /***
9 *
10 * <P>
11 * Pretty much like MemoryImageSource, but without the lame constructor
12 * signature...
13 * <img src="http://www.dlogic.org/~vic/images/smileys/yahoo_tongue.gif"/>
14 * </P>
15 *
16 * @author BrianHammond
17 *
18 * $Header: /usr/local/cvsroot/ghia/src/java/com/insanityengine/ghia/pixels/PixelImage.java,v 1.4 2005/03/27 00:25:14 brian Exp $
19 *
20 */
21
22 public class PixelImage implements ImageProducer, PixelBufferInterface {
23
24 /***
25 *
26 * Constructor
27 *
28 */
29 public PixelImage() {}
30
31 /***
32 *
33 * Constructor
34 *
35 * @param width of the image
36 * @param height of the image
37 *
38 */
39 public PixelImage( int width, int height ) {
40 init( width, height );
41 }
42
43 /***
44 *
45 * Initialize this PixelImage
46 *
47 * @param width of the image
48 * @param height of the image
49 *
50 */
51 public void init( int width, int height ) {
52 this.w = width;
53 this.h = height;
54
55 xm = w / 2;
56 ym = h / 2;
57
58 max = w * h;
59 pixels = new int[ w * h ];
60 memimg = Toolkit.getDefaultToolkit().createImage( this );
61 }
62
63 /***
64 *
65 * Set the ImageObserver
66 *
67 * @param observer to use
68 *
69 */
70 public void setObserver( ImageObserver observer ) {
71 observ = observer;
72 }
73
74 /***
75 *
76 * Determines the width of the buffer.
77 *
78 * @return the width of this buffer
79 *
80 */
81 public int getWidth() {
82 return w;
83 }
84
85 /***
86 *
87 * Determines the height of the buffer.
88 *
89 * @return the height of this buffer
90 *
91 */
92 public int getHeight() {
93 return h;
94 }
95
96 /***
97 *
98 * Get the pixels
99 *
100 * @return the pixels
101 *
102 */
103 public final int[] getPixels() {
104 return pixels;
105 }
106
107 /***
108 *
109 * Set the pixel at idx to color
110 *
111 * @param idx pixel to set
112 * @param color use
113 *
114 */
115 public final void setColor( int idx, int color ) {
116 pixels[ idx ] = color;
117 }
118
119 /***
120 *
121 * Set the pixel at (x,y) to color
122 *
123 * @param x coordinate
124 * @param y coordinate
125 * @param color use
126 *
127 */
128 public final void setColor( int x, int y, int color ) {
129 setColor( x + y * w, color );
130 }
131
132 /***
133 *
134 * Set the pixel at (x,y) to color
135 *
136 * @param x coordinate
137 * @param y coordinate
138 * @param alpha component of the color
139 * @param red component of the color
140 * @param green component of the color
141 * @param blue component of the color
142 *
143 */
144 public final void setColor( int x, int y, int alpha, int red, int green, int blue ) {
145 setColor(
146 x + y * w,
147 alpha << 24 | red << 16 | green << 8 | blue
148 );
149 }
150
151 /***
152 *
153 * Clear the buffer
154 *
155 */
156 public void clear() {
157 int cleared = 1;
158 pixels[ 0 ] = clearColor;
159 for ( cleared = 1 ; cleared < max / 2 ; cleared *= 2 ) {
160 System.arraycopy( pixels, 0, pixels, cleared, cleared );
161 }
162 System.arraycopy( pixels, 0, pixels, cleared, max - cleared );
163 }
164
165 /***
166 *
167 * Copy the contents of the pixel to the graphic g
168 *
169 * @param graphic to copy to
170 *
171 */
172 public void copyTo( Graphics graphic ) {
173 update();
174 graphic.drawImage( memimg, 0, 0, observ );
175 }
176
177 /***
178 *
179 * Set the clear color
180 *
181 * @return color to clear with
182 *
183 */
184 public void setClearColor( int color ) {
185 clearColor = color;
186 }
187
188 /***
189 *
190 * Get the clear color
191 *
192 * @return the clear color
193 *
194 */
195 public int getClearColor() {
196 return clearColor;
197 }
198
199
200
201
202
203 private ImageConsumer con = null;
204
205 public synchronized void addConsumer( ImageConsumer c ) { con = c; }
206
207 public final void startProduction( ImageConsumer c ) {
208 if ( con != c ) {
209 con = c;
210 con.setDimensions( w ,h );
211 con.setProperties( null );
212 con.setColorModel( cm );
213 con.setHints( ip_hints );
214 }
215 con.setPixels( 0, 0, w, h, cm, pixels, 0, w );
216 con.imageComplete( ip_sfd );
217 }
218
219 public final void update() {
220 if ( null != con ) startProduction( con );
221 }
222
223 public final boolean isConsumer( ImageConsumer c ) { return ( con == c ); }
224
225 public final void requestTopDownLeftRightResend( ImageConsumer c ) {}
226
227 public final void removeConsumer( ImageConsumer c ) {}
228
229
230
231
232
233 private static ColorModel cm = new DirectColorModel( 32, 0xFF0000, 0x00FF00, 0x0000FF );
234 private static int ip_hints = (
235 ImageConsumer.TOPDOWNLEFTRIGHT |
236 ImageConsumer.COMPLETESCANLINES |
237 ImageConsumer.SINGLEPASS |
238 ImageConsumer.SINGLEFRAME
239 );
240 private static int ip_sfd = ImageConsumer.SINGLEFRAMEDONE;
241
242
243
244 protected int w = 0;
245 protected int h = 0;
246 protected int pixels[] = null;
247
248 int xm = 0;
249 int ym = 0;
250 int max = 0;
251
252 private ImageObserver observ = null;
253
254 Image memimg = null;
255
256 int clearColor = 0;
257
258 };
259
260 /***
261 *
262 * $Log: PixelImage.java,v $
263 * Revision 1.4 2005/03/27 00:25:14 brian
264 * www not elvis
265 *
266 * Revision 1.3 2005/03/27 00:14:11 brian
267 * fix image
268 *
269 * Revision 1.2 2005/03/19 17:50:02 brian
270 * repackaging
271 *
272 * Revision 1.1 2004/09/02 13:17:16 brian
273 * the big reorg
274 *
275 * Revision 1.8 2004/09/01 14:53:53 brian
276 * more javadoc
277 *
278 * Revision 1.7 2004/09/01 02:18:56 brian
279 * javadoc
280 *
281 * Revision 1.6 2004/09/01 01:10:42 brian
282 * fix class level javadoc placement
283 *
284 * Revision 1.5 2004/09/01 00:11:06 brian
285 * author, log and header stuff
286 *
287 *
288 */