View Javadoc

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/TextDitherer.java,v 1.3 2005/03/19 17:50:02 brian Exp $
15   *
16   */
17  
18  public class TextDitherer {
19  
20  	/***
21    	 *	  
22    	 * Silly text dithering...
23    	 *	  
24    	 */
25  	public final static void dither( PixelBufferReadInterface pbuffer ) {
26  		int rowz = 40;
27  		int colz = 78;
28  		int hdiv = pbuffer.getHeight() / rowz;
29  		int wdiv = pbuffer.getWidth() / colz;
30  		String px = "  `````-_..:::^^===++++rrrz)xiitLTullFJZjjSwkq5VVymXGUKAgHRB$QQQWW@@NN";
31  		int pdiv = 255 / px.length() + 1;
32  
33  
34  		int r, g, b;
35  		int i, j, y, x, idx;
36  
37  		int ystart, ystop;
38  		int xstart, xstop;
39  			
40  		int c, color;
41  
42  		// may be expensive in some (lame!) cases...
43  		int [] pixels = pbuffer.getPixels();
44  
45  		for ( i = 0, ystart = 0, ystop = hdiv; i < rowz ; ++i, ystart = ystop, ystop += hdiv ) {
46  
47  //			ystart = i * hdiv;
48  //			ystop = ystart + hdiv;
49  
50  			for ( j = 0 ; j < colz ; ++j ) {
51  
52  				xstart = j * wdiv;
53  				xstop = xstart + wdiv;
54  
55  				color = 0;
56  
57  				for ( y = ystart ; y < ystop ; ++y ) {
58  
59  					for ( x = xstart ; x < xstop ; ++x ) {
60  
61  						c = pixels[ x + y * pbuffer.getWidth() ];
62  						color += ( 
63  							+ ( c >> 16 & 0xFF )
64  							+ ( c >> 8 & 0xFF )
65  							+ ( c & 0xFF )
66  						) / 3;
67  					}
68  				}
69  
70  				color /= ( wdiv * hdiv );
71  				//color = color * color / 255;
72  				//color = color * color / 255;
73  				//color = color * color / 255;
74  				//color = color * color / 255;
75  
76  				idx = color / pdiv;
77  				System.out.print( px.charAt( idx ) );
78  			}
79  			System.out.println();
80  		}
81  	}
82  };
83  
84  /***
85   *
86   * $Log: TextDitherer.java,v $
87   * Revision 1.3  2005/03/19 17:50:02  brian
88   * repackaging
89   *
90   * Revision 1.2  2004/09/16 10:58:47  brian
91   * be marginally foxier...
92   *
93   * Revision 1.1  2004/09/13 00:53:39  brian
94   * text dithering...
95   *
96   *
97   */