Created
February 24, 2020 17:42
-
-
Save Luctins/c67a51be26ff595937eb2683f08420ea to your computer and use it in GitHub Desktop.
Dithering code example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PImage img; | |
int x_max = 0, y_max = 0, _x_max = 0, _y_max = 0 ; | |
final int inc = 10; | |
final int color_num = 4; | |
final float e[] = {7.0/16.0, 3.0/16.0, 5.0/16.0, 1.0/16.0}; | |
void setup() | |
{ | |
img = loadImage("yourimagehere.jpg"); | |
size( 1920, 1080); | |
image(img, 0, 0); | |
colorMode(RGB, 255); | |
//print(e); | |
} | |
void draw() | |
{ | |
image(img, 0, 0); | |
loadPixels(); | |
/*Color reduction code*/ | |
for (int x = 1; x < x_max-1; ++x) | |
{ | |
for (int y = 1; y < y_max-1; ++y) | |
{ | |
color t, c = pixels[x+y*width]; | |
t = color(255*int(color_num*red(c)/255.0)/color_num, 255*int(color_num*green(c)/255.0)/color_num, 255*int(color_num*blue(c)/255.0)/color_num); | |
//println("c: \t", red(c), "\t" , green(c), "\t" , blue(c),"\t|| t: \t", red(t), "\t" , green(t), "\t" , blue(t)); | |
//println("c: ",hex(c)," | t: ", hex(t)); | |
pixels[x+y*width] = t; | |
if (x_max +10 >= width && y_max + 10 >= height) | |
{ | |
//compute error | |
int r_err = int(red(c) - red(t)); | |
int g_err = int(green(c) - green(t)); | |
int b_err = int(blue(c) - blue(t)); | |
//println("r_err :", r_err, "g_err :", g_err, "b_err :", b_err); | |
c = pixels[(x+1)+(y )*width]; | |
pixels[(x+1)+(y )*width] = color(red(c)+r_err*e[0], green(c)+g_err*e[0], blue(c)+b_err*e[0]); | |
c = pixels[(x-1)+(y+1)*width]; | |
pixels[(x-1)+(y+1)*width] = color(red(c)+r_err*e[1], green(c)+g_err*e[1], blue(c)+b_err*e[1]); | |
c = pixels[(x )+(y+1)*width]; | |
pixels[(x )+(y+1)*width] = color(red(c)+r_err*e[2], green(c)+g_err*e[2], blue(c)+b_err*e[2]); | |
c = pixels[(x+1)+(y+1)*width]; | |
pixels[(x+1)+(y+1)*width] = color(red(c)+r_err*e[3], green(c)+g_err*e[3], blue(c)+b_err*e[3]); | |
} | |
} | |
} | |
x_max = x_max + inc > width ? x_max : x_max+inc; | |
y_max = y_max + inc > height ? y_max : y_max+inc; | |
if (x_max +10 >= width && y_max + 10 >= height) | |
{ | |
_x_max = _x_max + 1 > width ? _x_max : _x_max+1; | |
_y_max = _y_max + 1 > height ? _y_max : _y_max+1; | |
} | |
updatePixels(); | |
} | |
//println("x_max:",x_max,"y_max:", y_max); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment