We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › QUESTION ABOUT OUTPUT OF PDF !
Page Index Toggle Pages: 1
QUESTION ABOUT OUTPUT OF PDF ! (Read 828 times)
QUESTION ABOUT OUTPUT OF PDF !
Sep 22nd, 2009, 11:58pm
 
I want to out put pdf of line stroke.

but it doesn`t work.

please help me !!

THIS IS CODE OF 1/2




import processing.opengl.*;

import processing.pdf.*;


FixedPointBuffer buf;
// array of points to follow the mouse
Tracker [] tracks;
// how many points?
int ntracks;
// mouse-down flag
boolean isDrawing = false;
// HSB current color to rotate
float myhue=0.0, mysat, mybri;
// rate of hue rotation when in rotate mode
float myhueinc=0.25;
// RGB current color to plot with
int myred, mygrn, myblu;
// alpha value to plot with
float myalf;
// how fine to subdivide each frame
float timeslice = 0.2;
// flag the applet has mouse focus
boolean bGotMouse = false;
// flag if we are in "rainbow" mode
boolean bHueRotate = true;
boolean dosave=false;
int num;  
float pt[];  
int style[];

void setup() {
 size(480,480,OPENGL);
 colorMode(HSB);
 buf = new FixedPointBuffer(width,height);
 ntracks = 100;
 tracks = new Tracker[ntracks];
 float ctrx = (float)(width/2);
 float ctry = (float)(height/2);
 for (int i=0; i<ntracks; i++)
   tracks[i] = new Tracker(ctrx,ctry);
 config(2);
}

void config(int mode) {
 float basespeed=0.0, basefriction=0.0;
 // slightly desaturating and brightening allows white highlights to accumulate
 mysat = 224.0;
 mybri = 160.0;
 switch(mode) {
   case 1 : // blue, tight response
     myhue = 256.0*2.0/3.0;
     myred = 16 << buf.shift;
     mygrn = 64 << buf.shift;
     myblu = 255 << buf.shift;
     basespeed = 0.03;
     basefriction = 0.04;
     break;
   case 2 : // red, medium response
     myhue = 0.0;
     myred = 255 << buf.shift;
     mygrn = 16 << buf.shift;
     myblu = 64 << buf.shift;
     basespeed = 0.02;
     basefriction = 0.03;
     myhue = 0.0;
     break;
   case 3 : // green, loose response
     myhue = 256.0/3.0;
     myred = 64 << buf.shift;
     mygrn = 255 << buf.shift;
     myblu = 16 << buf.shift;
     basespeed = 0.011;
     basefriction = 0.02;
     myhue = 0.333 * 256.0;
     break;
   case 4 : // gray, very tight
     myhue = 0.0;
     bHueRotate = false; // force to false when start gray mode
     myred = 64 << buf.shift;
     mygrn = 64 << buf.shift;
     myblu = 64 << buf.shift;
     basespeed = 0.05;
     basefriction = 0.07;
     break;
 }
 myalf = 0.5 * timeslice;
 for (int i=0; i<ntracks; i++) {
   tracks[i].speed = basespeed - (float)(i) * 0.0001;
   tracks[i].friction = basefriction - (float)(i) * 0.0001;
 }
}

void rotatehue() {
 myhue += myhueinc;
 if (myhue >= 256.0) myhue -= 256.0;
 color c = color(myhue,mysat,mybri);
 myred = ((int)red(c)) << buf.shift;
 mygrn = ((int)green(c)) << buf.shift;
 myblu = ((int)blue(c)) << buf.shift;
}

void draw() {
 // don't move towards mouse until we're sure applet has been focused
 // (otherwise they all bunch up at 0,0 before first mouse down - yuck)
 if (bGotMouse) {
   if (bHueRotate)
     rotatehue();
   float targetx = (float)(mouseX);
   float targety = (float)(mouseY);
   float elapsed = 0.0;
   while (elapsed < 1.0) {
     for (int i=0; i<ntracks; i++) {
       tracks[i].movetoward( targetx, targety, timeslice );
       if (isDrawing)
         buf.add(tracks[i].x, tracks[i].y, myred, mygrn, myblu, myalf);
     }
     elapsed += timeslice;
   }
 
 
 buf.render();
 if(dosave) {  
   // set up PGraphicsPDF for use with beginRaw()  
   PGraphicsPDF pdf=(PGraphicsPDF)beginRaw(PDF, "pdf_complex_out.pdf");  
 
   // set default Illustrator stroke styles and paint background rect.  
   pdf.strokeJoin(MITER);  
   pdf.strokeCap(SQUARE);  
   pdf.fill(0);  
   pdf.noStroke();  
   pdf.rect(0,0, width,height);
 if(dosave) {  
   endRaw();  
   dosave=false;  
 }    
 }
}
}  

void mousePressed() {
 bGotMouse = true;
 if (mouseButton == LEFT) {
   // redundant? yes. but keeps right button from interfering (as with mousePressed)
   isDrawing = true;
 }
 else
 if (mouseButton == RIGHT) {
   buf.background(0,0,0);
 }
}

void mouseReleased() {
 if (mouseButton == LEFT) {
   isDrawing = false;
 }
}

void keyPressed() {
 if (key=='1') config(1);
 else
 if (key=='2') config(2);
 else
 if (key=='3') config(3);
 else
 if (key=='4') config(4);
 else
 if (key=='h') bHueRotate = !bHueRotate;
 else
 if (key=='s') dosave=true;
   
}

// an 8.16 fixed point rgb buffer with sub-pixel sampling
// (the extra precision is used to reduce rounding errors during accumulation)
class FixedPointBuffer {
 static final int shift = 16; // number of .fixed bits
 static final int fixone = 1<<shift; // the "scale" of fixed point numbers
 static final int maxrgb = (256<<shift)-1; // max allowed for rgb values
 int wid, hei, area;
 int [] redbuf;
 int [] grnbuf;
 int [] blubuf;
 FixedPointBuffer(int w, int h) {
   wid = w;
   hei = h;
   area = wid * hei;
   redbuf = new int[area];
   grnbuf = new int[area];
   blubuf = new int[area];
   loadPixels();
 }
 void background(int r, int g, int b) {
   for (int idx=0; idx<area; idx++) {
     redbuf[idx] = r;
     grnbuf[idx] = g;
     blubuf[idx] = b;
   }
 }      
Re: QUESTION ABOUT OUTPUT OF PDF !
Reply #1 - Sep 23rd, 2009, 12:00am
 
THIS IS CODE OF 2/2


// plot additively
 void add(float x, float y, int r, int g, int b, float alf) {
   // convert to continuous coordinates
   x -= 0.5;
   y -= 0.5;
   // bounds check
   if ((x<0.0) || (y<0.0) || (x>=wid-1.0) || (y>=hei-1.0)) return;
   // integral coordinates
   int ix1 = (int)(x);
   int iy1 = (int)(y);
   int ix2 = ix1 + 1;
   int iy2 = iy1 + 1;
   // fractional coordinates
   float fractx = x - (float)(ix1);
   float fracty = y - (float)(iy1);
   // reciprocal of fractional coordinates
   float recipx = 1.0 - fractx;
   float recipy = 1.0 - fracty;
   // preconvert color values to floats
   float fr = (float)(r);
   float fg = (float)(g);
   float fb = (float)(b);
   // plot it
   float ratio;
   int idx, c;
   // upper-left
   ratio = recipx * recipy * alf;
   idx = iy1 * width + ix1;
   c = (int)((ratio*fr) + redbuf[idx]); if (c>maxrgb) c=maxrgb; redbuf[idx] = c;
   c = (int)((ratio*fg) + grnbuf[idx]); if (c>maxrgb) c=maxrgb; grnbuf[idx] = c;
   c = (int)((ratio*fb) + blubuf[idx]); if (c>maxrgb) c=maxrgb; blubuf[idx] = c;
   // upper-right
   ratio = fractx * recipy * alf;
   idx = iy1 * width + ix2;
   c = (int)((ratio*fr) + redbuf[idx]); if (c>maxrgb) c=maxrgb; redbuf[idx] = c;
   c = (int)((ratio*fg) + grnbuf[idx]); if (c>maxrgb) c=maxrgb; grnbuf[idx] = c;
   c = (int)((ratio*fb) + blubuf[idx]); if (c>maxrgb) c=maxrgb; blubuf[idx] = c;
   // lower-left
   ratio = recipx * fracty * alf;
   idx = iy2 * width + ix1;
   c = (int)((ratio*fr) + redbuf[idx]); if (c>maxrgb) c=maxrgb; redbuf[idx] = c;
   c = (int)((ratio*fg) + grnbuf[idx]); if (c>maxrgb) c=maxrgb; grnbuf[idx] = c;
   c = (int)((ratio*fb) + blubuf[idx]); if (c>maxrgb) c=maxrgb; blubuf[idx] = c;
   // lower-right            
   ratio = fractx * fracty * alf;
   idx = iy2 * width + ix2;
   c = (int)((ratio*fr) + redbuf[idx]); if (c>maxrgb) c=maxrgb; redbuf[idx] = c;
   c = (int)((ratio*fg) + grnbuf[idx]); if (c>maxrgb) c=maxrgb; grnbuf[idx] = c;
   c = (int)((ratio*fb) + blubuf[idx]); if (c>maxrgb) c=maxrgb; blubuf[idx] = c;
 }
 // render converts 8.shift format back down to 8 bit rgb
 void render() {
   for (int idx=area-1; idx>=0; idx--) {
     /*
     // general-purpose conversion for any shift...
     int r = ((redbuf[idx] >> shift) & 0xFF) << 16;
     int g = ((grnbuf[idx] >> shift) & 0xFF) << 8;
     int b = ((blubuf[idx] >> shift) & 0xFF);
     pixels[idx] = 0xFF000000 | r | g | b;
     */
     // optimized for shift of 16
     pixels[idx] = 0xFF000000 |
                   (redbuf[idx] & 0xFF0000) |
                   ((grnbuf[idx] & 0xFF0000) >> 8) |
                   ((blubuf[idx] & 0xFF0000) >> 16);
   }
   updatePixels();
 }
}

// a simple "springy follower" thingy
class Tracker {
 float x, y, vx, vy;
 float speed, friction;
 Tracker(float _x, float _y) {
   moveto(_x, _y);
   speed = 0.01;
   friction = 0.01;
 }
 void moveto(float _x, float _y) {
   x = _x;
   y = _y;
   vx = vy = 0.0;
 }
 void movetoward(float targetx, float targety, float dt) {
   /*
   // long-winded version
   float dx = targetx - x;
   float dy = targety - y;
   float ax = (dx * speed);
   float ay = (dy * speed);
   // 2nd order Euler integrator, good enough
   x = x + vx * dt + 0.5 * ax * dt * dt;
   y = y + vy * dt + 0.5 * ay * dt * dt;
   vx = vx + ax * dt;
   vy = vy + ay * dt;
   vx = vx - vx * friction * dt;
   vy = vy - vy * friction * dt;
   */
   // slightly optimized
   float axdt = ((targetx-x) * speed) * dt;
   float aydt = ((targety-y) * speed) * dt;
   x += vx * dt + 0.5 * axdt * dt;
   y += vy * dt + 0.5 * aydt * dt;
   vx += axdt;
   vy += aydt;
   float frictiondt = friction * dt;
   vx -= vx * frictiondt;
   vy -= vy * frictiondt;
 }
}



That`s all !!!!

press key "s" = output pdf.
Re: QUESTION ABOUT OUTPUT OF PDF !
Reply #2 - Sep 23rd, 2009, 12:59pm
 
this bit looks odd

Code:

if (dosave) {
// set up PGraphicsPDF for use with beginRaw()
PGraphicsPDF pdf=(PGraphicsPDF)beginRaw(PDF, "pdf_complex_out.pdf");

// set default Illustrator stroke styles and paint background rect.
pdf.strokeJoin(MITER);
pdf.strokeCap(SQUARE);
pdf.fill(0);
pdf.noStroke();
pdf.rect(0,0, width,height);
if (dosave) {
endRaw();
dosave=false;
}
}


you have an if (dosave) inside an if (dosave) - the latter will always be true. makes me think there's some code missing around there.

and please, don't rely on formatting to delineate loops or conditions, especially on forums which can mangle indenting

this is bad:

if (condition)
 something();
something();

this is so much clearer:

if (condition) {
 something();
}
something()

(sorry, personal bugbear)
Re: QUESTION ABOUT OUTPUT OF PDF !
Reply #3 - Sep 23rd, 2009, 1:17pm
 
looking at the code i doubt there'll be much you can do better than saving a frame (which is what the original code did) - it's adding (sub)pixels as it goes, building up the image on the screen over time, not really something you can save as a pdf.

original code here btw:

http://www.davebollinger.com/works/fungi/applet_fungi02/Fungi02.pde.txt

// Fungi02
// David Bollinger, July 2006
// http://www.davebollinger.com
// for Processing 0015 Beta
//
/** Fungi #02<br>
(minor variant of Fungi 01 with motion blur)<br>
Click to advance to next set of coefficients.
*/

dave posts here sometimes so you might be lucky. or he might be a bit miffed about you posting his code whilst leaving the credits off the top.
Page Index Toggle Pages: 1