document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //Blurring a picture
  2. var inputImage = new SimpleImage("duvall.jpg");
  3. var outputImage = new SimpleImage(inputImage.getWidth(), inputImage.getHeight());
  4. for(var pixel of inputImage.values()){
  5.     var x = pixel.getX();
  6.     var y = pixel.getY();
  7.     if(Math.random() > 0.5){
  8.         //Changing half of the pixels only to blur the given picture.
  9.         var nearbyPixel = getNearbyPixel(inputImage, x, y, 10);
  10.         outputImage.setPixel(x, y, nearbyPixel);
  11.     }else{
  12.         outputImage.setPixel(x, y, pixel);
  13.     }
  14. }
  15. print(outputImage);
  16.  
  17. function getNearbyPixel(inputImage, x, y, diameter){
  18.     //Generating a random number between 0 and 10 (diameter = 10 in this case) but < 10
  19.     var dx = Math.random() * diameter;
  20.     //To cover both the sides we want to shift half of the window to the negative side
  21.     //so we subtract half of diameter. So we get a number between -5 and +5 instead of between 0 and 9.9
  22.     dx = dx - diameter/2;
  23.     var dy = Math.random() * diameter;
  24.     dy = dy - diameter/2;
  25.     //Generating the new nearyby pixel
  26.     nearbyX = ensureInsideImage(x + dx, inputImage.getWidth());
  27.     nearbyY = ensureInsideImage(y + dy, inputImage.getHeight());
  28.     return inputImage.getPixel(nearbyX, nearbyY);
  29. }
  30. function ensureInsideImage(coordinate, size){
  31.     //coordinate can not be negative
  32.     if(coordinate < 0){
  33.         return 0;
  34.     }
  35.     //coordinate must be in the range [0....size-1]
  36.     if(coordinate >= size){
  37.         return size - 1;
  38.     }
  39.     return coordinate;
  40. }
');