I can't get my pixels to update properly.
in
Programming Questions
•
29 days ago
Hey folks. Consider the following code:
- PImage imgSrc; // PImage qui contiendra l'image source JPG
float nbrImages; // Nombre d'images sources
PImage imgPix; // PImage qui contiendra l'image pixelisee
float detail = 1.5; // Le plus haut la valeur, le moins de detail. Minimum = 1 - les decimals font une grande difference. Meme 1.01 enlevera du detail.;
void setup() {
// Nombre d'images
nbrImages = 1;
// On load une image aleatoire parmis nos images
imgSrc = loadImage(str(int(random(1, nbrImages+1))) + ".jpg");
// On set la taille du sketch a la meme taille que l'image choisie
size(imgSrc.width, imgSrc.height);
// On smooth!
smooth();
// On ajuste le fps
frameRate(30);
// On cree une image dans imgPix de la meme taille que l'image source en mode RGB. Nous la remplirons de pixels relatifs a l'image source.
imgPix = createImage(imgSrc.width, imgSrc.height, RGB);
}
void draw() {
// Background noir
background(0);
// Nous traversons chaque pixel de imgPix et lui attribuons le meme pixel que imgSrc mais avec un risque de ne pas lui attribuer de pixel (pour creer du "detail")
for (int i = 0; i < imgPix.width * imgPix.height; i++) {
if ( random(0, detail) < 1 ) {
imgPix.pixels[i] = imgSrc.pixels[i];
}
}
// On affiche l'image pixelisee
image(imgPix, 0, 0);
}
void mousePressed(){
println("old detail: " + detail);
detail += 0.01;
println("new detail: " + detail);
}
For this code to work, please add a random 1000x600 image named 1.jpg in your /data folder. For convenience: http://i.imgur.com/9EZEhS0.jpg
I'm trying to get my image to update the amount of pixels that are actually displayed depending on the value of detail. The higher the value, the less chance of a pixel being shown, hence less detail. Unfortunately this does not seem to work despite me clicking (notice mousePressed increases the value of detail).
I tried placing my imgPix.loadPixels(); and imgPix.updatePixels(); everywhere and I still can't figure the thing out.
Any help much appreciated!
1