Hello! I want to create an animation based on the greyscale values (0-255) of each pixel in an image.
Each pixel in the image would be represented by drawing a ‘grid’ (say, 4 rows * 3 columns) of squares (rects), with each square in the grid being ON (white) or OFF (black), where the number of squares ON represents the greyscale value of that pixel.
There are MANY permutations of (for example) 3 squares ON in a 12 pixel grid:
E.g. greyValue = 3 (3 squares ON):
{{1, 1, 1}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, // greyVal[2] - 3 pixels ON
{{0, 1, 0}, {0, 1, 0}, {0, 1, 0}, {0, 0, 0}},
{{0, 0, 1}, {0, 0, 0}, {0, 0, 0}, {1, 1, 0}} etc...
I want to ‘animate’ each grid, so that for each particular greyscale value, the corresponding grid will loop and draw ALL of the permutations in the corresponding sub-array.
Where I’m at:
I’ve generated some values in a python script - only a very few shown here as an example.
I’m trying to draw a couple of these grids, with each grid looping through ALL the configurations in the array for each grid’s particular greyValue.
I get ArrayIndexOutOfBoundsException
if I try to draw separate grids with different greyValues and therefore different numbers of pixelConfigurations.
I see that pixelConfiguration
is being incremented each time draw_single_grid
is called, and its value is being ‘shared’ by each call… How do I get each grid to draw all its configurations in sequence, independently of any other grid…?
Any insight as to where I’m getting mixed up gratefully received!. Thanks
int pixelSize = 12;
int pixelGap = 2;
int greyValue;
int pixelConfiguration = 0;
void draw() {
frameRate(1);
draw_single_grid(20, 20, greyValue = 1);
draw_single_grid(80, 20, greyValue = 0);
println("draw____");
}
void draw_single_grid(int xOffset, int yOffset, int greyVal) {
for(int row = 0; row < allPixelConfigs[greyVal][0].length; row++) {
for(int column = 0; column < allPixelConfigs[greyVal][0][0].length; column++) {
int fillNum = allPixelConfigs[greyVal][pixelConfiguration][row][column] * 255;
fill(fillNum);
int xPos = (column * pixelSize + xOffset) + (column * pixelGap);
int yPos = (row * pixelSize + yOffset) + (row * pixelGap);
rect(xPos, yPos, pixelSize, pixelSize);
}
}
pixelConfiguration++;
println("pixelConfiguration: ", pixelConfiguration);
if (pixelConfiguration >= allPixelConfigs[greyVal].length) {
pixelConfiguration = 0;
println("repeat");
}
}
int[][][][] allPixelConfigs = { // (4 rows x 3 columns) = 12 pixel grid
// some configurations of pixels ON
{
{{1, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, // greyVal[0] - 1 pixel ON
{{0, 1, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
{{0, 0, 1}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
{{0, 0, 0}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0}}
},
{
{{1, 1, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, // greyVal[1] - 2 pixels ON
{{1, 0, 0}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0}},
{{0, 0, 0}, {0, 0, 0}, {0, 1, 1}, {0, 0, 0}}
},
{
{{1, 1, 1}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, // greyVal[2] - 3 pixels ON
{{1, 1, 0}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0}},
{{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}},
{{1, 0, 1}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0}},
{{0, 0, 0}, {1, 0, 0}, {0, 1, 1}, {0, 0, 0}},
{{1, 0, 1}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0}},
{{0, 0, 0}, {0, 0, 0}, {1, 1, 1}, {0, 0, 0}}
},
{
{{1, 1, 1}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0}}, // greyVal[3] - 4 pixels ON
{{0, 0, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}}
},
};
This is further to my earlier question, beautifully answered by @tony and @Chrisir - although I don’t think I’ve fully grokked how draw()
actually works yet!