Guest User

Untitled

a guest
May 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. // Atkinson dither translated from a Python source
  2.  
  3. PImage img;
  4.  
  5. void setup()
  6. {
  7. img = loadImage("ball.jpg"); // add some image here!
  8.  
  9. size( img.width, img.height );
  10. }
  11.  
  12. void draw () {
  13.  
  14. int[] threshold = new int[128+128];
  15. for ( int i = 0; i<threshold.length; i++ )
  16. {
  17. threshold[i] = i < 128 ? 0 : 255;
  18. }
  19. int[] idx = new int[]{
  20. 1,
  21. 2,
  22. - 1 + img.width,
  23. img.width,
  24. 1 + img.width,
  25. img.width + img.width
  26. };
  27.  
  28. for ( int iy = 0 ; iy < img.height; iy++ )
  29. {
  30. for ( int ix = 0 ; ix < img.width; ix++ )
  31. {
  32. int index = iy*img.width + ix;
  33.  
  34. int old = toGrey(img.pixels[index]); // blue only ..
  35. int now = threshold[old];
  36. int err = (old - now) >> 3; // devide by 8
  37.  
  38. img.pixels[index] = color( now );
  39. for ( int i = 0; i<idx.length; i++ )
  40. {
  41. int ii = index + idx[i];
  42. if ( ii < img.pixels.length ) img.pixels[ii] = color( toGrey(img.pixels[ii]) + err );
  43. }
  44. }
  45. }
  46.  
  47. image( img, 0, 0 );
  48.  
  49. noLoop();
  50. }
  51.  
  52. int toGrey ( int c )
  53. {
  54. return (((c>>16) & 0xFF) + ((c>>8) & 0xFF) + (c & 0xFF)) / 3;
  55. }
Add Comment
Please, Sign In to add comment