Advertisement
Guest User

Untitled

a guest
Nov 17th, 2010
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1.    /**
  2.    * Method to blur the pixels
  3.    * @param numPixels the number of pixels to average in all  
  4.    * directions.  So if the numPixels is 2 then we will average  
  5.    * all pixels in the rectangle defined by 2 before the current  
  6.    * pixel to 2 after the current pixel
  7.    */
  8.   public void blur(int numPixels)
  9.   {
  10.     double starttime = System.currentTimeMillis();
  11.    
  12.     Pixel pixel = null;
  13.     Pixel samplePixel = null;
  14.     int redValue = 0;
  15.     int greenValue = 0;
  16.     int blueValue = 0;
  17.     int count = 0;
  18.  
  19.     // loop through the pixels
  20.     for (int x=0; x < this.getWidth(); x++) {
  21.       for (int y=0; y < this.getHeight(); y++) {
  22.  
  23.         // get the current pixel
  24.         pixel = this.getPixel(x,y);
  25.  
  26.         // reset the count and red, green, and blue values
  27.         count = 0;
  28.         redValue = greenValue = blueValue = 0;
  29.  
  30.         // loop through pixel numPixels before x to numPixels after x
  31.         for (int xSample = x - numPixels;  
  32.              xSample <= x + numPixels;  
  33.              xSample++) {
  34.           for (int ySample = y - numPixels;  
  35.                ySample <= y + numPixels;
  36.                ySample++) {
  37.  
  38.             // check that we are in the range of acceptable pixels
  39.             if (xSample >= 0 && xSample < this.getWidth() &&
  40.                 ySample >= 0 && ySample < this.getHeight()) {
  41.               samplePixel = this.getPixel(xSample,ySample);
  42.               redValue = redValue + samplePixel.getRed();
  43.               greenValue = greenValue + samplePixel.getGreen();
  44.               blueValue = blueValue + samplePixel.getBlue();
  45.               count = count + 1;
  46.             }
  47.           }
  48.         }
  49.  
  50.         // use average color of surrounding pixels
  51.         Color newColor = new Color(redValue / count,
  52.                                    greenValue / count,
  53.                                    blueValue / count);
  54.         pixel.setColor(newColor);
  55.       }
  56.     }
  57.     double finishtime = System.currentTimeMillis();
  58.     double executiontime = finishtime=starttime;
  59.     System.out.println("Funktionen använde " + executiontime + " ms");
  60.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement