Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Method to blur the pixels
- * @param numPixels the number of pixels to average in all
- * directions. So if the numPixels is 2 then we will average
- * all pixels in the rectangle defined by 2 before the current
- * pixel to 2 after the current pixel
- */
- public void blur(int numPixels)
- {
- double starttime = System.currentTimeMillis();
- Pixel pixel = null;
- Pixel samplePixel = null;
- int redValue = 0;
- int greenValue = 0;
- int blueValue = 0;
- int count = 0;
- // loop through the pixels
- for (int x=0; x < this.getWidth(); x++) {
- for (int y=0; y < this.getHeight(); y++) {
- // get the current pixel
- pixel = this.getPixel(x,y);
- // reset the count and red, green, and blue values
- count = 0;
- redValue = greenValue = blueValue = 0;
- // loop through pixel numPixels before x to numPixels after x
- for (int xSample = x - numPixels;
- xSample <= x + numPixels;
- xSample++) {
- for (int ySample = y - numPixels;
- ySample <= y + numPixels;
- ySample++) {
- // check that we are in the range of acceptable pixels
- if (xSample >= 0 && xSample < this.getWidth() &&
- ySample >= 0 && ySample < this.getHeight()) {
- samplePixel = this.getPixel(xSample,ySample);
- redValue = redValue + samplePixel.getRed();
- greenValue = greenValue + samplePixel.getGreen();
- blueValue = blueValue + samplePixel.getBlue();
- count = count + 1;
- }
- }
- }
- // use average color of surrounding pixels
- Color newColor = new Color(redValue / count,
- greenValue / count,
- blueValue / count);
- pixel.setColor(newColor);
- }
- }
- double finishtime = System.currentTimeMillis();
- double executiontime = finishtime=starttime;
- System.out.println("Funktionen använde " + executiontime + " ms");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement