guys, I know it's simple but I cannot figure it out. It results in color.
I didn't see any simple working sketch about dithering in Processing, so it could be useful for beginners
- PImage img;
- void setup() {
- size(640, 480);
- colorMode(RGB, 255);
- img = loadImage("http://i.imgur.com/Jm28XUW.png");
- img.loadPixels();
- for (int x = 0; x < img.width; x++) {
- for (int y = 0; y < img.height; y++) {
- int loc = x + y*img.width;
- color c = img.pixels[loc];
- color newC;
- if (brightness(c) < 120)
- newC = color(0);
- else newC = color(255);
- int error = c - newC;
- img.pixels[loc]=newC;
- //floyd-steinberg
- if(x+1 < img.width) img.pixels[loc+1] += error*7/16; //vecino derecha
- if(y+1 == img.height) continue; //ultima linea
- if(x>0) {
- img.pixels[loc+img.width-1] += error*3/16; //vecino izq abajo
- img.pixels[loc+img.width] += error*5/16; //vecino abajo
- }
- if(x+1 < img.width) img.pixels[loc+img.width+1] += error*1/16; //vecino derecha abajo
- }
- }
- img.updatePixels();
- image(img,0,0);
- }
1