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 › arrayCopy() copying into pixels array
Page Index Toggle Pages: 1
arrayCopy() copying into pixels array (Read 2761 times)
arrayCopy() copying into pixels array
Jun 10th, 2010, 3:15pm
 
Hi everyone i have not been using processing that long and i have searched the references for help on this subject and if it has been answered elsewhere please just put in a link
to the answer and ignore this post.

QUESTION
is it possible to use arrayCopy() to copy the contents of an array into the pixels array. I'm experimenting with manipulating pixels in various ways.

i can get this to work arrayCopy(src array, dst array); on two arrays i declare myself.

but if i do this arrayCopy(myarray,pixels);
or any other form arrayCopy(myarray, pixels, pixels.length); etc

i get an "array store error message".

i have figured out that loadPixels() has to go in the draw function and if you don't first call it you get all sorts of error messages mostly NULL POINTER errors, and then after that you can use pixels.length to get the pixels array size. and that if you set an array size to  [width*height] it equals the same as pixels.length.

and i can now find any pixel on the screen and manipulate the pixels[] array by this formula or versions of it in a for loop

pixels[(width*y)+width] where width is screen width after setting size() and y is any value between 1 and the height of the screen has set in size()

so that on my screen that is 1920*1200 pixels THIS  would put a red spot on last row of the screen and in the centre column or width/2
size(screen.width/3,screen.height/3)  

// width now equals 640 height now equals 400 pixels
pixels[(width*399)+(width/2)] = color (255,0,0);

what i want is to store stuff in an array of floats or color values and alter them then when the alterations are complete directly or quickly copy this new data into the pixels array and then use updatePixels() to display changes. I'm after doing away with using a for loop to make changes to the pixels array

i'm wondering does the error message array store error mean that i have the data type for myarray wrong i'm using
float [] scratchPad = new float[pixels.length];

i also get the same error message if i experiment with the syntax and use

color [] scratchpad = new color[pixels.length];

finally i think maybe there is a typing error in the pixels() and UpdtatePixels() reference because it says to get an equivalent get(x,y) from pixels[] its (Y*width)+x but on my mac this gave the wrong pixels being changed and a few experiments showed my mac to be the opposite way around (width*y the number of rows down) +x i know it looks like the arithmetic is the same but trust me different pixels got changed and (Y*width)+x did not give the same pixel in pixels[ ] as get(x,y);
Re: arrayCopy() copying into pixels array
Reply #1 - Jun 10th, 2010, 10:12pm
 
procfreak wrote on Jun 10th, 2010, 3:15pm:
i'm wondering does the error message array store error mean that i have the data type for myarray wrong i'm using
float [] scratchPad = new float[pixels.length];

i also get the same error message if i experiment with the syntax and use

color [] scratchpad = new color[pixels.length];


Use int[] as the datatype for your array.  Pixels are stored as integers, and the "color" datatype is really int (Processing just disguises it a bit.)

Also, I've had much better luck getting pixel-fiddling operations to work with the P2D renderer instead of the default Java2D, e.g.:
Code:

size(300, 300, P2D);
// instead of
size(300, 300);

I can't answer all your questions, but hopefully this helps a little.

Re: arrayCopy() copying into pixels array
Reply #2 - Jun 11th, 2010, 1:37am
 
procfreak wrote on Jun 10th, 2010, 3:15pm:
is it possible to use arrayCopy() to copy the contents of an array into the pixels array.

Yes. As replied above, the arrays must have the same type...
Quote:
finally i think maybe there is a typing error in the pixels() and UpdtatePixels() reference because it says to get an equivalent get(x,y) from pixels[] its  but on my mac this gave the wrong pixels being changed and a few experiments showed my mac to be the opposite way around (width*y the number of rows down) +x i know it looks like the arithmetic is the same but trust me different pixels got changed and (Y*width)+x did not give the same pixel in pixels[ ] as get(x,y);

No. BTW, the doc says: pixels[y*width+x], not (Y*width)+x. Y is different from y. But y*width+x isn't different from (width*y)+x
Re: arrayCopy() copying into pixels array
Reply #3 - Jun 11th, 2010, 1:59pm
 
Thank you both for your help i will try using P2D and see if that helps as well as making the arrays ints as suggested.

And perhaps i misread the reference i thought y referred to the y axis the up and down axis and initially i was doing as it said in the reference and the pixels i thought i was referring to were not where i expected, but (width*y+x) always got me where i wanted, that is go down the screen for y rows or lines, then add on the x axis. and i do have a tendency to put things inside their own parentheses even though it's not always needed. i can see that y*width is same as width*y, it's just that the array seems to be laid out such that the first pixel is at position 0 and then the next pixel is at the next x position and on the same line or row until the count reaches width and then the next index in the array references the first pixel in the next line ,

so that pixels(width*16+24) on my system takes you to the pixel on the 16th row and 24 pixels in on the x axis.

the code then reads as per the way the pixels array is laid out. whereas if i did this (16*width+24) i would get to the same pixel, in a single for loop, but it doesn't follow the way the array is laid out, and if i were to use a nested loop, i have a greater chance of coding an error via getting the y and x mixed up  resulting in array out of bounds errors.

i will let you know how i get on with your suggestions thanks again much appreciated
Re: arrayCopy() copying into pixels array
Reply #4 - Jun 11th, 2010, 3:48pm
 
i got it working with ints like you said.
i can see were i had the original errors it was because even though i first tried using ints i got array indexing errors because the sizes were not equal and processing could not find my array except in the function it was declared. To avoid this i had to make the array i was using as a scratchPad a global array equal in size to whatever size i later declared in setup() and then only after i had used loadPixels could i use pixels.length as the array length to search. and arrayCopy(scratchPad,pixels); works and i can use it anywhere here is quick test code only.
----

Quote:
 int Width=screen.width/3; // these must match whatever size we decare
 int Height= screen.height/3;
 int [] scratchPad = new int [Width*Height];
 // this is now equal to pixels.length after we call loadPixels()
 // and provided we keep size equal to width/3 height/3

void setup(){
 
  size(screen.width/3,screen.height/3,P2D);
 
   background(0);
   loadPixels();
  

  for(int fillup=0;fillup < pixels.length;fillup++){

   scratchPad[fillup] = int (random(255));
   // fill array with test dummy randomness
     }
     
}

void draw(){
  
    fill(255,212,68);
  
  beginShape(QUADS);
  vertex (234,128);
  vertex(534,128);
  vertex(534,328);
  vertex(234,328);
  endShape();
  
  // draw a yellow rectangle
  
  if(mouseButton ==LEFT){
      arrayCopy(scratchPad,pixels);
     }
  
//  fill screen with randomnes stored in scratchPad[]
//  if left mouse button pressed and restore to a yellow rectangle 
//  on right mouse button pressed
 
}



Re: arrayCopy() copying into pixels array
Reply #5 - Jun 12th, 2010, 1:56am
 
Note that you can declare variables globally and then assign their values in setup:

Code:
int [] scratchPad;

void setup(){

 size(screen.width/3, screen.height/3,

 scratchPad = new int [width * height];

  background(0);
  loadPixels();
}


This way you don't need to duplicate system variables like width and height...
Re: arrayCopy() copying into pixels array
Reply #6 - Jun 12th, 2010, 10:50am
 
yes i am getting used to doing things like you say  and then using

= new whatever in setup()

now i can have mouse buttons and key presses either copy pixels into my array and vice versa in combination with loadPixels() and updatePixels.

i can clear the screen or partially fill and/or clear bits of the screen  and draw into myarray and then show the changes much faster than waiting for a for loop to finish.

next i will experiment with applying some alpha values and masking . Thank you all for getting me back on track for some faster animations and screen changes.
Page Index Toggle Pages: 1