1 package com.insanityengine.ghia.pixelops;
2
3 import com.insanityengine.ghia.pixels.PixelBufferReadInterface;
4
5 /***
6 *
7 * <P>
8 * Various "post-production", usually 2d effects can be applied to (some)
9 * implementations of the PixelBufferReadInterface
10 * </P>
11 *
12 * @author BrianHammond
13 *
14 * $Header: /usr/local/cvsroot/ghia/src/java/com/insanityengine/ghia/pixelops/BufferFx.java,v 1.4 2005/03/19 17:50:02 brian Exp $
15 *
16 */
17
18 public class BufferFx {
19
20 /***
21 *
22 * Constructor
23 *
24 */
25 public BufferFx() {
26 }
27
28 /***
29 *
30 * Constructor
31 *
32 * @param object to try to use
33 *
34 */
35 public BufferFx( Object object ) {
36 tryToSet( object );
37 }
38
39 /***
40 *
41 * Constructor
42 *
43 * @param pixelBuffer to use
44 *
45 */
46 public BufferFx( PixelBufferReadInterface pixelBuffer ) {
47 set( pixelBuffer );
48 }
49
50 /***
51 *
52 * Set the PixelBufferReadInterface
53 *
54 * @param pixelBuffer to use
55 *
56 */
57 public void set( PixelBufferReadInterface pixelBuffer ) {
58 this.pixelBuffer = pixelBuffer;
59 w = pixelBuffer.getWidth();
60 h = pixelBuffer.getHeight();
61 max = w * h;
62 pixels = pixelBuffer.getPixels();
63 clearColor = pixelBuffer.getClearColor();
64 if ( null == pixels ) w = h = max = 0;
65 }
66
67 /***
68 *
69 * Try to set the PixelBufferReadInterface
70 *
71 * @param object to try with
72 *
73 */
74 public void tryToSet( Object object ) {
75 if ( objectSupportsFx( object ) ) set( ( PixelBufferReadInterface ) object );
76 }
77
78 /***
79 *
80 */
81 public void shift() {
82 if ( null == pixels ) return;
83
84 int l = pixels[ max - 1 ];
85
86 System.arraycopy( pixels, 0, pixels, 1, max - 1);
87 pixels[ 0 ] = l;
88 }
89
90 /***
91 *
92 */
93 public PixelBufferReadInterface getBuffer() {
94 return pixelBuffer;
95 }
96
97 /***
98 *
99 */
100 public void or( int n ) {
101 if ( null == pixels ) return;
102 for ( int i = 0 ; i < max ; i++ ) pixels[ i ] |= n;
103 }
104
105 /***
106 *
107 */
108 public void and( int n ) {
109 if ( null == pixels ) return;
110 for ( int i = 0 ; i < max ; i++ ) pixels[ i ] &= n;
111 }
112
113 /***
114 *
115 */
116 public void shiftLeft( int n ) {
117 if ( null == pixels ) return;
118 for ( int i = 0 ; i < max ; i++ ) pixels[ i ] = pixels[ i ] << n;
119 }
120
121 /***
122 *
123 */
124 public void shiftRight( int n ) {
125 if ( null == pixels ) return;
126 for ( int i = 0 ; i < max ; i++ ) pixels[ i ] = pixels[ i ] >> n;
127 }
128
129
130 /***
131 *
132 */
133 public void fade() {
134 fade( 1, 2 );
135 }
136
137 /***
138 *
139 */
140 public void fade( int m, int d ) {
141 if ( null == pixels ) return;
142 int r, g, b;
143 for ( int i = 0 ; i < max ; i++ ) {
144 r = ( ( pixels[ i ] >> 16 ) & 0xFF ) * m / d;
145 g = ( ( pixels[ i ] >> 8 ) & 0xFF ) * m / d;
146 b = ( ( pixels[ i ] >> 0 ) & 0xFF ) * m / d;
147 pixels[ i ] = ( 255 << 24 | r << 16 | g << 8 | b );
148 }
149 }
150
151 /***
152 *
153 */
154 public void smooth() {
155 if ( null == pixels ) return;
156 int i, r, g, b;
157
158 for ( i = 0 ; i < w + 1 ; i++ ) pixels[ i ] = clearColor;
159 for ( i = w + 1 ; i < max ; i++ ) {
160 r = (
161 ( ( pixels[ i - w - 1 ] >> 16 ) & 0xFF ) +
162 ( ( pixels[ i - w - 0 ] >> 16 ) & 0xFF ) +
163 ( ( pixels[ i - 0 - 1 ] >> 16 ) & 0xFF ) +
164 ( ( pixels[ i - 0 - 0 ] >> 16 ) & 0xFF )
165 ) >> 2;
166 g = (
167 ( ( pixels[ i - w - 1 ] >> 8 ) & 0xFF ) +
168 ( ( pixels[ i - w - 0 ] >> 8 ) & 0xFF ) +
169 ( ( pixels[ i - 0 - 1 ] >> 8 ) & 0xFF ) +
170 ( ( pixels[ i - 0 - 0 ] >> 8 ) & 0xFF )
171 ) >> 2;
172 b = (
173 ( pixels[ i - w - 1 ] & 0xFF ) +
174 ( pixels[ i - w - 0 ] & 0xFF ) +
175 ( pixels[ i - 0 - 1 ] & 0xFF ) +
176 ( pixels[ i - 0 - 0 ] & 0xFF )
177 ) >> 2;
178
179 pixels[ i ] = ( 255 << 24 | r << 16 | g << 8 | b );
180 }
181 }
182
183 /***
184 *
185 */
186 public void smoothoDoomo() {
187 if ( null == pixels ) return;
188 int i, r, g, b;
189
190 pixels = pixelBuffer.getPixels();
191
192 for ( i = 0 ; i < w + 1 ; i++ ) pixels[ i ] = clearColor;
193 for ( i = w + 1 ; i < max ; i++ ) {
194 pixels[ i ] = (
195 pixels[ i - w - 1 ] +
196 pixels[ i - w - 0 ] +
197 pixels[ i - 0 - 1 ] +
198 pixels[ i - 0 - 0 ]
199
200 ) >> 2;
201 }
202 }
203
204 /***
205 *
206 */
207 public void eoPerturb() {
208 if ( null == pixels ) return;
209 int i, j, idx;
210 for ( i = idx = 0 ; i < h ; ++i ) {
211 for ( j = 0 ; j < w ; ++j, ++idx ) {
212 pixels[ idx ] += ( i + j ) % 2 * 2 - 1;
213 }
214 }
215 }
216
217 /***
218 *
219 */
220 public void hedge() {
221 if ( null == pixels ) return;
222 int i, j, idx;
223 for ( i = idx = 0 ; i < h ; ++i ) {
224 for ( j = 0 ; j < w ; ++j, ++idx ) {
225 pixels[ idx ] += ( i + j ) % 2 * 2 - 1;
226 }
227 }
228 }
229
230 /***
231 *
232 */
233 public void topEdge() {
234 if ( null == pixels ) return;
235 for ( int i = 0 ; i < w ; ++i ) pixels[ i + w * 3 ] += 100 * ( 2 * i % 2 - 1 );
236 }
237
238 /***
239 *
240 */
241 public void statiq() {
242 if ( null == pixels ) return;
243 for ( int i = 0 ; i < max ; ++i ) pixels[ i ] += ( int ) ( 0x00FFFFFF * Math.random() );
244 }
245
246 /***
247 *
248 */
249 public void avg( int c ) {
250 if ( null == pixels ) return;
251 for ( int i = 0 ; i < max ; ++i ) {
252 pixels[ i ] = ( pixels[ i ] | c ) / 2;
253 }
254 }
255
256 /***
257 *
258 * Test the renderer to see if it support effects
259 *
260 * @param renderer to test
261 *
262 * @return true if the renderer supports effects
263 *
264 */
265 public final static boolean objectSupportsFx( Object object ) {
266 boolean support = false;
267 PixelBufferReadInterface reader;
268
269 try {
270
271 reader = ( PixelBufferReadInterface ) object;
272
273 if ( null != reader.getPixels() ) {
274 try {
275 int idx = 0;
276 reader.getPixels()[ idx ] = reader.getPixels()[ idx ];
277 idx = reader.getWidth() * reader.getHeight() - 1;
278 reader.getPixels()[ idx ] = reader.getPixels()[ idx ];
279 support = true;
280 } catch ( Exception e ) {
281 }
282 }
283
284 } catch ( Exception e ) {
285 }
286 return support;
287 }
288
289
290
291 private PixelBufferReadInterface pixelBuffer;
292 private int w, h, max;
293 private int pixels[];
294 private int clearColor;
295
296 };
297
298 /***
299 *
300 * $Log: BufferFx.java,v $
301 * Revision 1.4 2005/03/19 17:50:02 brian
302 * repackaging
303 *
304 * Revision 1.3 2004/09/02 23:32:26 brian
305 * dont make the RendererInterface extend PixelBufferInterface
306 *
307 * Revision 1.2 2004/09/02 15:43:46 brian
308 * redefine PixelBufferReadInterface so BufferFx can use it as an interface rather than the RenderInterface
309 *
310 * Revision 1.1 2004/09/02 13:17:16 brian
311 * the big reorg
312 *
313 * Revision 1.8 2004/09/01 01:10:42 brian
314 * fix class level javadoc placement
315 *
316 * Revision 1.7 2004/09/01 00:11:06 brian
317 * author, log and header stuff
318 *
319 *
320 */