View Javadoc

1   package com.insanityengine.ghia.pixels;
2   
3   import java.awt.Image;
4   import java.awt.image.*;
5   
6   import com.insanityengine.ghia.m3.*;
7   
8   /***
9    *
10   * <P>
11   * Think of the Image as a tasty treat with an object wrapper.
12   * An ImageSkin strips off the skin and (re)encapsulate the meat.
13   * </P>
14   *
15   * <P>
16   * Pretty sick, huh? It is meant to make it easier to muck around 
17   * with the image bits of the Image
18   * </P>
19   *
20   * @author BrianHammond
21   *
22   * $Header: /usr/local/cvsroot/ghia/src/java/com/insanityengine/ghia/pixels/ImageSkin.java,v 1.7 2005/03/26 04:50:12 brian Exp $
23   *
24   */
25  
26  public class ImageSkin implements PixelBufferReadInterface {
27  
28  	/***
29  	 *
30  	 * Constructor
31  	 *
32  	 * @param width of the ImageSkin
33  	 * @param height of the ImageSkin
34  	 * @param pixels to use
35  	 *
36  	 */
37  	public ImageSkin( int width, int height, int[] pixels ) {
38  		init( width, height, pixels );
39  	}
40  
41  	/***
42  	 *
43  	 * Constructor
44  	 *
45  	 * @param image to skin
46  	 * @param observer to us
47  	 *
48  	 */
49  	public ImageSkin( Image image, ImageObserver observer ) {
50  		init( image, observer );
51  	}
52  
53  	/***
54  	 *
55  	 * Constructor
56  	 *
57  	 * @param pixelBuffer to skin
58  	 *
59  	 */
60  	public ImageSkin( PixelBufferReadInterface pixelBuffer ) {
61  		init( pixelBuffer );
62  	}
63  
64  	/***
65  	 *
66  	 * Use these new goodies for your guts
67  	 *
68  	 * @param width of the ImageSkin
69  	 * @param height of the ImageSkin
70  	 * @param pixels to use
71  	 *
72  	 */
73  	public void init( int w, int h, int[] pixels ) {
74  		this.w = w;
75  		this.h = h;
76  		this.pixels = pixels;
77  		clamp.setPixels( w, h, pixels );
78  		wrap.setPixels( w, h, pixels );
79  	}
80  
81  	/***
82  	 *
83  	 * Use a PixelGrabber to pull out the guts of the image
84  	 *
85  	 * @param image to skin
86  	 * @param observer to us
87  	 *
88  	 */
89  	public void init( Image image, ImageObserver observer ) {
90  		if ( null == image ) {
91  			System.out.println( "ImageSkin: null image" );
92  			return;
93  		}
94  
95  		w = image.getWidth( observer );
96  		h = image.getHeight( observer );
97  
98  		if ( w < 0 || h < 0 ) {
99  			System.out.println( "ImageSkin: bad image " + observer );
100 			return;
101 		}
102 		
103 		max = w * h;
104 		pixels = new int[ max ];
105 		PixelGrabber pg = new PixelGrabber( image, 0, 0, w, h, pixels, 0, w );
106 		try { 
107 			pg.grabPixels(); 
108 		} catch ( Exception e ) {
109 			e.printStackTrace();
110 			return;
111 		} 
112 
113 		init( w, h, pixels );
114 	}
115 
116 	/***
117 	 *
118 	 * 
119 	 *
120 	 * @param pixelBuffer to skin
121 	 *
122 	 */
123 	public void init( PixelBufferReadInterface pixelBuffer ) {
124 		init( 
125 			pixelBuffer.getWidth()
126 			,
127 			pixelBuffer.getHeight()
128 			, 
129 			pixelBuffer.getPixels()
130 		);
131 	}
132 
133 	/***
134 	 * 
135 	 * Convert a coordinate (x,y) to an index with bounds checking
136 	 * ala clamp
137 	 * 
138 	 * @param x coordinate
139 	 * @param y coordinate
140 	 * 
141 	 * @return the equivalent index
142 	 * 
143 	 */
144 	int getIdx( int x, int y ) {
145 		if ( x < 0 ) x = 0;
146 		if ( y < 0 ) y = 0;
147 
148 		if ( x >= w ) x = w - 1;
149 		if ( y >= h ) y = h - 1;
150 
151 		return x + y * w;
152 	}
153 
154 	/***
155 	 * 
156 	 * Convert a coordinate (tx,ty) where tx and ty reside on 
157 	 * the interval betwen 0 and 1 and convert to a corresponding 
158 	 * (x,y). Performs bounds checking
159 	 * 
160 	 * @param x coordinate
161 	 * @param y coordinate
162 	 * 
163 	 * @return the equivalent index
164 	 * 
165 	 */
166 	public final int getScaledIdx( float tx, float ty ) {
167 		return getIdx( ( int ) ( w * tx ), ( int ) ( h * ty ) );
168 	}
169 
170 	/***
171 	 * 
172 	 * Get the pixel value at (x,y) using the current PixelGetter
173 	 * 
174 	 * @param x coordinate
175 	 * @param y coordinate
176 	 * 
177 	 * @return the appropriate pixel value
178 	 * 
179 	 */
180 	public final int getPixelAt( int x, int y ) {
181 		return type.getPixelAt( x, y );
182 	}
183 
184 	/*** 
185 	 * 
186 	 * getPixelAtCoord
187 	 * 
188 	 * @param tx
189 	 * @param ty
190 	 * 
191 	 * @return a int
192 	 * 
193 	 */
194 	public final int getPixelAtCoord( float tx, float ty ) {
195 		return getPixelAt(
196 			( int ) ( w * tx )
197 			,
198 			( int ) ( h * ty )
199 		);
200 	}
201 
202 	/***
203 	 * 
204 	 * Set the current PixelGetter via funtion index
205 	 * 
206 	 * @param funk tion to use
207 	 * 
208 	 */
209 	public final void setFunction( int funk ) {
210 		switch ( funk ) {
211 			case M3_Constants.TEXT_WRAP: type = wrap; break;
212 			default: type = clamp;
213 		}
214 	}
215 
216 	/***
217 	 * 
218 	 * Determines the width of the buffer.
219 	 * 
220 	 * @return the width of this buffer
221 	 * 
222 	 */
223 	public final int getWidth() { 
224 		return w; 
225 	}
226 
227 	/***
228 	 *
229 	 * Determines the height of the buffer.
230 	 *
231 	 * @return the height of this buffer
232 	 *
233 	 */
234 	public final int getHeight() { 
235 		return h; 
236 	}
237 
238 	/***
239 	 *
240 	 * Get the pixels
241 	 *
242 	 * @return the pixels
243 	 *
244 	 */
245 	public int[] getPixels() {
246 		return pixels;
247 	}
248 	
249 	/***
250 	 *
251 	 * Set the PixelGetter class for subsequent calls to get
252 	 * getPixelAt
253 	 *
254 	 * @param pgetter to use 
255 	 *
256 	 */
257 	public final void setPixelGetter( PixelGetter pgetter ) {
258 		type = pgetter;
259 		type.setPixels( w, h, pixels );
260 	}
261 
262 	/***
263 	 * 
264 	 * Needed to implement PixelBufferReadInterface
265 	 *
266 	 */
267 	public int getClearColor() {
268 		return 0;
269 	}
270 
271 	/*** 
272 	 * 
273 	 * getMax
274 	 * 
275 	 * @return a int
276 	 * 
277 	 */
278 	public int getMax() { 
279 		return max;
280 	}
281 
282 	/*** 
283 	 * 
284 	 * toString
285 	 * 
286 	 * @return a String
287 	 * 
288 	 */
289 	public String toString() {
290 		return (
291 			super.toString() 
292 			+ "[" 
293 			+ "size=" + w + "x" + h
294 			+ "]"
295 		);
296 	}
297 
298 	////////////////////////////
299 	
300 	private int w = 0, h = 0, max = 0;
301 	private int[] pixels = null;
302 
303 	private ClampPixelGetter clamp = new ClampPixelGetter();
304 	private WrapPixelGetter wrap = new WrapPixelGetter();
305 	private PixelGetter type = clamp;
306 	
307 	private final static int [] nonPixels = { 0xFF0000DA };
308 };
309 
310 /***
311  *
312  * $Log: ImageSkin.java,v $
313  * Revision 1.7  2005/03/26 04:50:12  brian
314  * more fun
315  *
316  * Revision 1.6  2005/03/26 03:37:04  brian
317  * make methods public
318  *
319  * Revision 1.5  2005/03/21 05:42:13  brian
320  * add toString method
321  *
322  * Revision 1.4  2005/03/19 17:50:02  brian
323  * repackaging
324  *
325  * Revision 1.3  2005/02/17 03:31:13  brian
326  * good stuff???
327  *
328  * Revision 1.2  2004/09/02 15:43:46  brian
329  * redefine PixelBufferReadInterface so BufferFx can use it as an interface rather than the RenderInterface
330  *
331  * Revision 1.1  2004/09/02 13:17:16  brian
332  * the big reorg
333  *
334  * Revision 1.9  2004/09/01 01:10:42  brian
335  * fix class level javadoc placement
336  *
337  * Revision 1.8  2004/09/01 00:11:06  brian
338  * author, log and header stuff
339  *
340  *
341  */