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 › shiffman book questions  image brightness 15
Page Index Toggle Pages: 1
shiffman book questions?  image brightness 15 (Read 549 times)
shiffman book questions?  image brightness 15
Jan 20th, 2009, 1:41am
 
so I am stuck, not sure what is wrong with this sketch

keep getting an error, and not seeing my image materialize

heres the code


PImage img;


void setup (){
size(300,300);
 img = loadImage("BachataRoja_recordstore.jpg");
 background(0);
 //size(img.width, img.height);
 
}

void draw() {
 
 loadPixels();
 
 for(int x = 0; x < img.width; x++) {
   for(int y = 0; y < img.height; y++) {
     //calculate 1D pix position
     int loc = x + y*img.width;
     //get RGB from image
     float r = red (img.pixels[loc]);
     float g = green (img.pixels[loc]);
     float b = blue (img.pixels[loc]);
     //CALC an ammount to chg brightness
     //based on proximity to mouse
     
     float distance = dist(x,y,mouseX, mouseY);
     float adjustBrightness = (50-distance)/50;
    //float adjustBrightness = (distance);
     r *= adjustBrightness;
      g *= adjustBrightness;
       b *= adjustBrightness;
       
       //CONSTRAIN VALUES
       
       r = constrain(r,0,255);
       g = constrain(g,0,255);
       b = constrain(b,0,255);
       
       color c = color(r,g,b);
       pixels[loc] = c;
       
       //updatePixels();
   }
 }
 updatePixels();
}
     
     
Re: shiffman book questions?  image brightness 15
Reply #1 - Jan 20th, 2009, 1:42am
 
BTW -- the error i get is ArrayIndexOutOfBoundsException  91160

Re: shiffman book questions?  image brightness 15
Reply #2 - Jan 20th, 2009, 10:16am
 
You didn't do img.loadPixels(); to init the array.
Re: shiffman book questions?  image brightness 15
Reply #3 - Jan 20th, 2009, 8:33pm
 
d'oh!

does this only work when the image is the same size as the canvas?

i tried size(img.width, img.height); it didnt like that though, but it did work with size at the exact size of the image

how do i do this loadPixels > image processing > update pixels operation with images different sizes than the canvas?

also, am a little confused as what the operation of (50-distance)/50 is doing.  seems like say a pixel 300 away would make it be (300-50)/50 = 5, 1 pix away would be (1-50)/50= -.019

it would seem like the adjustBrightness would be making the further pixels brighter

thanks so much btw!
Re: shiffman book questions?  image brightness 15
Reply #4 - Jan 20th, 2009, 10:20pm
 
You can have any canvas and image size. It depends on what you plan to use: display a small image somewhere in a larger canvas, scale up or down the image to fit to display, or display only the part of the larger image fitting on the frame.
The first case is the simplest: just make the canvas larger, perhaps add an offset to loc to set a margin (H and V).
For other cases, a possible solution is to create another PImage (of same size as img), set the pixels there, and use image() to display the result on screen, with or without scaling.
Re: shiffman book questions?  image brightness 15
Reply #5 - Jan 20th, 2009, 10:41pm
 
would the offset be int loc = (canvas.x+100)+(canvas.y+100)*width?

can you use 3d translate in this way or is the cell size method better

also with the example we are talking about here, with a 2000x2000 image, would we expect redraw to be super slow?  

would there be any workaround to speeding up perf?

thanks
Re: shiffman book questions?  image brightness 15
Reply #6 - Jan 20th, 2009, 11:40pm
 
Offset: something like that, yes.
3D translate? We use 2D, no? Translate wouldn't work here because the script updates the canvas' pixels directly.
And yes, it is a slow method, but perhaps the algorithm can be optimized to update only pixels that change significantly. But Processing will still update all pixels each time.
But only the visible pixels.
Re: shiffman book questions?  image brightness 15
Reply #7 - Jan 21st, 2009, 1:24am
 
thanks a lot phil

still not getting the distance part -- as what the operation of (50-distance)/50 is doing.  seems like say a pixel 300 away would make it be (300-50)/50 = 5, 1 pix away would be (1-50)/50= -.019
Re: shiffman book questions?  image brightness 15
Reply #8 - Jan 21st, 2009, 3:10am
 
that's correct. the distance statement produces a 50 pixel radius circle with a linear falloff from 1 (at the centre), to 0 at the radius and negative values further out.

the 3 constrain calls deal with the negatively scaled rgb values, although for clarity (and I expect performance, but I'm not clear on performance characteristics) it's better to constrain the scaling factor to the range 0,1. that way you replace 3 calls with 1.

does that help?
Re: shiffman book questions?  image brightness 15
Reply #9 - Jan 21st, 2009, 3:20am
 
it just seems like the closer pixels would mean a smaller adjust brightness factor - and a darker color, which is the opposite of whats happening?

Re: shiffman book questions?  image brightness 15
Reply #10 - Jan 21st, 2009, 3:27am
 
correct, because the term is 50 - distance.

let's work through an example: the distance of the pixel at mouseX,mouseY is 0 from the mouse cursor location (since it's the same position). in this case adjustBrightness = (50 - 0) / 50 = 1. so the pixel at the mouse cursor is unchanged.

as you inspect pixels further away from the cursor the distance increases, and correspondingly adjustBrightness falls.

if you wanted a dark center which then ramps up to normal at the edge of a 50 pixel radius circle then:

float adjustBrightness = distance / 50

hth Smiley
Re: shiffman book questions?  image brightness 15
Reply #11 - Jan 21st, 2009, 3:46am
 
thanks much, makes sense now...
Page Index Toggle Pages: 1