We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all,
I'm writing a custom dithering algorithm based on: http://www.tannerhelland.com/4660/dithering-eleven-algorithms-source-code & https://github.com/dpiccone/dithering_algorithms. Somehow the pixels that are supposed to be white are not. It seems to be related to how processing handles colors or brightness. A workaround is commented out in the code, but i'd like to understand why this is happening.
PImage img;
float[][] errorDiffusion = {{1,0,1.0}};
float[][] floydSteinberg = {{-1, 1, 3.0/16}, {0, 1, 5.0/16}, {1, 0, 7.0/16}, {1, 1, 1.0/16}};
float[][] falseFloydSteinberg = {{0, -1, 3.0/8}, {1, 0, 3.0/8}, {1, 1, 2.0/8}};
float[][] atkinson = {{1, 0, 1.0/8}, {2, 0, 1.0/8}, {-1, 1, 1.0/8}, {0, 1, 1.0/8}, {1, 1, 1.0/8}, {0, 2, 1.0/8}};
void setup() {
size(800, 800, JAVA2D);
img = loadImage("mona.jpg");
img = dither(img, falseFloydSteinberg);
noStroke();
noSmooth();
}
void draw() {
background(255, 0, 0);
image(img, 0, 0);
color c = img.get(mouseX, mouseY);
println(brightness(c));
}
PImage dither(PImage inp, float[][] matrix) {
PImage src = inp.copy();
for (int x = 0; x < src.width; x++) {
for (int y = 0; y < src.height; y++) {
color oldpixel = src.get(x, y);
color newpixel = findClosestColor(oldpixel);
src.set(x, y, newpixel);
float quant_error = brightness(oldpixel) - brightness(newpixel);
for (int k=0; k<matrix.length; k++) {
int xi = int(x+matrix[k][0]);
int yi = int(y+matrix[k][1]);
src.set(xi, yi, color(brightness(src.get(xi, yi)) + matrix[k][2]* quant_error));
}
}
}
//doing a second pass gives a workaround
/*
for (int x = 0; x < src.width; x++) {
for (int y = 0; y < src.height; y++) {
color oldpixel = src.get(x, y);
color newpixel = findClosestColor(oldpixel);
src.set(x, y, newpixel);
}
}
*/
return src;
}
color findClosestColor(color c) {
color r;
if (brightness(c) < 127) {
r = color(0);
} else {
r = color(255);
}
return r;
}