Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Atkinson dither translated from a Python source
- PImage img;
- void setup()
- {
- img = loadImage("ball.jpg"); // add some image here!
- size( img.width, img.height );
- }
- void draw () {
- int[] threshold = new int[128+128];
- for ( int i = 0; i<threshold.length; i++ )
- {
- threshold[i] = i < 128 ? 0 : 255;
- }
- int[] idx = new int[]{
- 1,
- 2,
- - 1 + img.width,
- img.width,
- 1 + img.width,
- img.width + img.width
- };
- for ( int iy = 0 ; iy < img.height; iy++ )
- {
- for ( int ix = 0 ; ix < img.width; ix++ )
- {
- int index = iy*img.width + ix;
- int old = toGrey(img.pixels[index]); // blue only ..
- int now = threshold[old];
- int err = (old - now) >> 3; // devide by 8
- img.pixels[index] = color( now );
- for ( int i = 0; i<idx.length; i++ )
- {
- int ii = index + idx[i];
- if ( ii < img.pixels.length ) img.pixels[ii] = color( toGrey(img.pixels[ii]) + err );
- }
- }
- }
- image( img, 0, 0 );
- noLoop();
- }
- int toGrey ( int c )
- {
- return (((c>>16) & 0xFF) + ((c>>8) & 0xFF) + (c & 0xFF)) / 3;
- }
Add Comment
Please, Sign In to add comment