|
Author |
Topic: Copying Arrays (Read 251 times) |
|
kevin
|
Copying Arrays
« on: Aug 29th, 2003, 1:44pm » |
|
Hi, Hopefully, simple thing. Is it possible to copy an image into an array? I've crated a 2D array of colors, and tried using system.copyarray*, but I'm not entirely sure on the syntax with regard to 2D arrays. Is it possible? Thanks, - Kevin * http://java.sun.com/docs/books/tutorial/java/data/copyingarrays.html
|
|
|
|
fry
|
Re: Copying Arrays
« Reply #1 on: Aug 29th, 2003, 5:54pm » |
|
in general, you'll actually want to use 1D arrays for images. but to convert a 2D array to a 1D BImage (image array used by p5). Code:int theWidth = 320; int theHeight = 240; // note that height is first, otherwise can't use arraycopy int theImage[][] = new int[theHeight][theWidth]; // assuming you stuff something fancy into theImage here int pixels1D[] = new int[theWidth*theHeight]; for (int row = 0; row < theHeight; row++) { // copy one row of pixels System.arraycopy(theImage[row], 0, pixels1D, row*theWidth, theWidth); } BImage image = new BImage(pixels1D, theWidth, theHeight, RGB); // or RGBA |
| but if your array is array[width][height], then System.arraycopy() won't actually help you, in which case you'd replace the loop with: Code: int index = 0; for (int y = 0; y < theHeight; y++) { for (int x = 0; x < theWidth; x++) { pixels1D[index++] = theImage[x][y]; } } |
| but that's prolly much slower.
|
|
|
|
|