Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

画像をぼかす

f:id:aa_debdeb:20151022024536j:plain
/**
* Blurred Image
*
* @author aa_debdeb
* @date 2015/10/24
*/

int blurWidth = 15;

void setup(){
  size(512, 512);
  noStroke();
  PImage image = loadImage("lena.jpg");
  image(image, 0, 0);
  loadPixels();
  color[][] colors = new color[width][height];
  for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
      colors[x][y] = pixels[y * width + x];
    }
  }
  for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
      if(x < blurWidth || x > width - 1 - blurWidth ||
         y < blurWidth || y > height - 1 - blurWidth){
        pixels[y * width + x] = color(0);     
       } else {
         float colorR = 0;
         float colorG = 0;
         float colorB = 0;
         for(int xx = x - blurWidth; xx <= x + blurWidth; xx++){
           for(int yy = y - blurWidth; yy <= y + blurWidth; yy++){
             colorR += red(colors[xx][yy]);
             colorG += green(colors[xx][yy]);
             colorB += blue(colors[xx][yy]);
           }
         }
         pixels[y * width + x] = color(colorR / sq(blurWidth * 2 + 1), colorG / sq(blurWidth * 2 + 1), colorB / sq(blurWidth * 2 + 1));
       }
    }
  }
  updatePixels();
}