Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Monday, 18 May 2015

New JOGL P2D looks good on linux

Change back to jogl in latest processing seems good (P2D looks much better than JAVA2D with this sketch) note size has moved to settings() and sketchRenderer(), sketchHeight() and sketchWidth() are gone running in netbeans.
  1 import processing.core.*;
  2 import processing.video.*;
  3 
  4 public class AsciiVideo extends PApplet {
  5 
  6     /**
  7      * ASCII Video by Ben Fry.
  8      *
  9      *
 10      * Text characters have been used to represent images since the earliest
 11      * computers. This sketch is a simple homage that re-interprets live video
 12      * as ASCII text. See the keyPressed function for more options, like
 13      * changing the font size.
 14      */
 15     Capture video;
 16     boolean cheatScreen;
 17 
 18 // All ASCII characters, sorted according to their visual density
 19     String letterOrder
 20             = " .`-_':,;^=+/\"|)\\<>)iv%xclrs{*}I?!][1taeo7zjLu"
 21             + "nT#JCwfy325Fp6mqSghVd4EgXPGZbYkOA&8U$@KHDBWNMR0Q";
 22     char[] letters;
 23 
 24     float[] bright;
 25     char[] chars;
 26 
 27     PFont font;
 28     float fontSize = 1.5f;
 29 
 30     @Override
 31     public void setup() {
 32 
 33   // This the default video input, see the GettingStartedCapture 
 34         // example if it creates an error
 35         video = new Capture(this, 160, 120);
 36 
 37         // Start capturing the images from the camera
 38         video.start();
 39 
 40         int count = video.width * video.height;
 41         //println(count);
 42 
 43         font = loadFont("UniversLTStd-Light-48.vlw");
 44 
 45   // for the 256 levels of brightness, distribute the letters across
 46         // the an array of 256 elements to use for the lookup
 47         letters = new char[256];
 48         for (int i = 0; i < 256; i++) {
 49             int index = PApplet.parseInt(map(i, 0, 256, 0, letterOrder.length()));
 50             letters[i] = letterOrder.charAt(index);
 51         }
 52 
 53         // current characters for each position in the video
 54         chars = new char[count];
 55 
 56         // current brightness for each point
 57         bright = new float[count];
 58         for (int i = 0; i < count; i++) {
 59             // set each brightness at the midpoint to start
 60             bright[i] = 128;
 61         }
 62     }
 63 
 64     public void captureEvent(Capture c) {
 65         c.read();
 66     }
 67 
 68     @Override
 69     public void draw() {
 70         background(0);
 71 
 72         pushMatrix();
 73 
 74         float hgap = width / PApplet.parseFloat(video.width);
 75         float vgap = height / PApplet.parseFloat(video.height);
 76 
 77         scale(max(hgap, vgap) * fontSize);
 78         textFont(font, fontSize);
 79 
 80         int index = 0;
 81         video.loadPixels();
 82         for (int y = 1; y < video.height; y++) {
 83 
 84             // Move down for next line
 85             translate(0, 1.0f / fontSize);
 86 
 87             pushMatrix();
 88             for (int x = 0; x < video.width; x++) {
 89                 int pixelColor = video.pixels[index];
 90                 // Faster method of calculating r, g, b than red(), green(), blue() 
 91                 int r = (pixelColor >> 16) & 0xff;
 92                 int gr = (pixelColor >> 8) & 0xff;
 93                 int b = pixelColor & 0xff;
 94 
 95       // Another option would be to properly calculate brightness as luminance:
 96                 // luminance = 0.3*red + 0.59*green + 0.11*blue
 97                 // Or you could instead red + green + blue, and make the the values[] array
 98                 // 256*3 elements long instead of just 256.
 99                 int pixelBright = max(r, gr, b);
100 
101                 // The 0.1 value is used to damp the changes so that letters flicker less
102                 float diff = pixelBright - bright[index];
103                 bright[index] += diff * 0.1f;
104 
105                 fill(pixelColor);
106                 int num = PApplet.parseInt(bright[index]);
107                 text(letters[num], 0, 0);
108 
109                 // Move to the next pixel
110                 index++;
111 
112                 // Move over for next character
113                 translate(1.0f / fontSize, 0);
114             }
115             popMatrix();
116         }
117         popMatrix();
118 
119         if (cheatScreen) {
120     image(video, 0, height - video.height);
121             // set() is faster than image() when drawing untransformed images
122            // set(0, height - video.height, video);
123         }
124     }
125 
126     /**
127      * Handle key presses: 'c' toggles the cheat screen that shows the original
128      * image in the corner 'g' grabs an image and saves the frame to a tiff
129      * image 'f' and 'F' increase and decrease the font size
130      */
131     @Override
132     public void keyPressed() {
133         switch (key) {
134             case 'g':
135                 saveFrame();
136                 break;
137             case 'c':
138                 cheatScreen = !cheatScreen;
139                 break;
140             case 'f':
141                 fontSize *= 1.1f;
142                 break;
143             case 'F':
144                 fontSize *= 0.9f;
145                 break;
146         }
147     }
148 
149     @Override
150     public void settings() {
151         size(640, 480, P2D);
152     }
153 
154     static public void main(String[] passedArgs) {
155         String[] appletArgs = new String[]{"AsciiVideo"};
156         if (passedArgs != null) {
157             PApplet.main(concat(appletArgs, passedArgs));
158         } else {
159             PApplet.main(appletArgs);
160         }
161     }
162 }

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2