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.
Page Index Toggle Pages: 1
Improper syntax? (Read 1075 times)
Improper syntax?
Oct 22nd, 2009, 6:18pm
 
Hey all,

I'm taking a class on Processing, so I'm by no means great at it. I've taken programming classes before so I know the logic behind what I'm trying to do but the friggin' syntax is killing me.

Anyway, with this function, it's got 2 parameters, one to pass an image into and the other for the desired size of a block of pixels (the whole point of this function is to take an image, and when the left mouse button is pressed, it causes the image to pixellate until it is no longer clear, and the right mouse button to go in the other direction until the image is very clear. ANYWAY, here's the code:

PImage img;
int howMuch = 5;


void setup()
{
 size(1000, 1000);
 img = loadImage("zombie.jpg");
}

void pixImage(img, 5)
{
 PImage imgCopy = new PImage(img.width, img.height);
 imgCopy.copy(img, 0, 0, img.width, img.height, 0, 0, img.width, img.height);

 for (int y = 0; y < imgCopy.height; y += howMuch) {
   for (int x = 0; x < imgCopy.width; x += howMuch) {

     float r      = 0.0, g = 0.0, b = 0.0;
     int   yChunk = min(howMuch, imgCopy.height - y);
     int   xChunk = min(howMuch, imgCopy.width  - x);
     int   count  = xChunk * yChunk;

     for (int j = 0; j < yChunk; ++j) {
       for (int i = 0; i < xChunk; ++i) {
         color c = imgCopy.get(x+i, y+j);
         r += red(c);
         g += green(c);
         b += blue(c);
       }
     }
     
     color c = color(r / count, g / count, b / count);
     
     for (int j = 0; j < yChunk; ++j) {
       for (int i = 0; i < xChunk; ++i) {
         imgCopy.set(x+i, y+j, c);
       }
     }
   }
 }
 
 image(imgCopy, 0, 0);
}


I'm pretty confused obviously, any help would be hot.
Thanks
Re: Improper syntax?
Reply #1 - Oct 22nd, 2009, 6:35pm
 
i want to add when i try to run this it highlights the line 'pixImage(img, 5) and the error comes up: expecting IDENT, found ','. thats part of the problem.
Re: Improper syntax?
Reply #2 - Oct 22nd, 2009, 6:58pm
 
you cant specify values for parameters in the definition of a function.
the line with void pixImage(img, 5), is what the line should be when you call the function, not when you define the function.

try this:
Code:
PImage img;

void setup()
{
img = loadImage("zombie.jpg");
size(img.width,img.height);
}

void draw()
{
 background(0);
 pixImage(img,int(map(mouseX,0,width,1,20)));
}

void pixImage(PImage img, int howMuch)
{
PImage imgCopy = new PImage(img.width, img.height);
imgCopy.copy(img, 0, 0, img.width, img.height, 0, 0, img.width, img.height);

for (int y = 0; y < imgCopy.height; y += howMuch) {
  for (int x = 0; x < imgCopy.width; x += howMuch) {

    float r      = 0.0, g = 0.0, b = 0.0;
    int   yChunk = min(howMuch, imgCopy.height - y);
    int   xChunk = min(howMuch, imgCopy.width  - x);
    int   count  = xChunk * yChunk;

    for (int j = 0; j < yChunk; ++j) {
      for (int i = 0; i < xChunk; ++i) {
        color c = imgCopy.get(x+i, y+j);
        r += red(c);
        g += green(c);
        b += blue(c);
      }
    }
   
    color c = color(r / count, g / count, b / count);
   
    for (int j = 0; j < yChunk; ++j) {
      for (int i = 0; i < xChunk; ++i) {
        imgCopy.set(x+i, y+j, c);
      }
    }
  }
}
image(imgCopy, 0, 0);
}
Page Index Toggle Pages: 1