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 & HelpSyntax Questions › PDF with loadPixels()
Page Index Toggle Pages: 1
PDF with loadPixels() (Read 431 times)
PDF with loadPixels()
Oct 3rd, 2008, 1:44am
 
hi!
I am trying to generate a .pdf out of a sketch that uses the set() or pixels[](same result) methods. But I always get the error: no set() for PDFGraphics.

Is there some workaround for the problem? 0148.

Thanks, Stephen

// Trying to generate some gradients. Wish me luck...


color c1 = color(178, 145, 23);
color c2 = color(188, 188, 177);


// constants
int Y_AXIS = 1;
int X_AXIS = 2;

void setup(){
 size(800, 800, PDF, "gradient.pdf");

 setGradient(0, 0, width, height, c1, 2, Y_AXIS);

}

void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ){
 // calculate differences between color components
 float deltaR = red(c2)-red(c1);
 float deltaG = green(c2)-green(c1);
 float deltaB = blue(c2)-blue(c1);

 // choose axis
 if(axis == Y_AXIS){
   /*nested for loops set pixels
    in a basic table structure */
   // column
   for (int i=x; i<=(x+w); i++){
     // row
     for (int j = y; j<=(y+h); j++){
       color c = color(
       (red(c1)+(j-y)*(deltaR/h)),
       (green(c1)+(j-y)*(deltaG/h)),
       (blue(c1)+(j-y)*(deltaB/h))
         );
       set(i, j, c);
     }
   }  
 }  
 else if(axis == X_AXIS){
   // column
   for (int i=y; i<=(y+h); i++){
     // row
     for (int j = x; j<=(x+w); j++){
       color c = color(
       (red(c1)+(j-x)*(deltaR/h)),
       (green(c1)+(j-x)*(deltaG/h)),
       (blue(c1)+(j-x)*(deltaB/h))
         );
       set(j, i, c);
     }
   }  
 }
}
Re: PDF with loadPixels()
Reply #1 - Oct 3rd, 2008, 2:12am
 
get(), set() and pixels[] don't work with PDF, just like the error says. PDF files are vector data, not pixels. If you want to work with pixels and write the result to PDF, create an image, draw into that, and then draw the image to your PDF file.
Re: PDF with loadPixels()
Reply #2 - Oct 3rd, 2008, 3:57am
 
yep,
that's just what I was looking for.
thanks!

for anyone interested. below is the quick+dirty modified code, to work with processing.pdf

#################################
import processing.pdf.*;

color c1 = color(178, 145, 23);
color c2 = color(188, 188, 178);


void setup(){
 size(800, 800,PDF,"gradient.pdf");

 smooth();
 noLoop();
}

void draw(){
 PImage img = createGradient(width, height, c1, c2);
 image(img,0,0);
 
 exit();
}


PImage createGradient(int w, int h, color c1, color c2){  
 
 PImage grad = createImage(w,h,RGB);
 
 int pixelIndex = 0;
   
 // calculate differences between color components
 float deltaR = red(c2)-red(c1);
 float deltaG = green(c2)-green(c1);
 float deltaB = blue(c2)-blue(c1);


 for (int i=0; i<=w; i++){
   // row
   for (int j = 0; j<=h; j++){
     color c = color(
     (red(c1)+(j)*(deltaR/h)),
     (green(c1)+(j)*(deltaG/h)),
     (blue(c1)+(j)*(deltaB/h))
       );
     
     // stupid outofbounds protection
     pixelIndex = j*w+i;
     if(pixelIndex<grad.pixels.length)
       grad.pixels[pixelIndex] = c;
   }
 }
 
 return grad;
}
Page Index Toggle Pages: 1