@@ -20,8 +20,10 @@ public static void main(String[] args) {
2020 img = toGrayScale (img );
2121 //img = toGrayScale2(img);
2222 display (img );
23- img = pixelate (img );
23+ // img = pixelate(img);
2424 //img = pixelate2(img, 3);
25+ //img = resize(img, 150);
26+ img = blur (img );
2527 display (img );
2628 }
2729 }
@@ -116,6 +118,41 @@ public static BufferedImage pixelate2 (BufferedImage img, int n) {
116118 return pixImg ;
117119 }
118120
121+ // scale a grayscale image
122+ public static BufferedImage resize (BufferedImage img , int newHeight ) {
123+ System .out .println (" Scaling image." );
124+ double scaleFactor = (double ) newHeight /img .getHeight ();
125+ BufferedImage scaledImg = new BufferedImage (
126+ (int )(scaleFactor *img .getWidth ()), newHeight , BufferedImage .TYPE_BYTE_GRAY );
127+ AffineTransform at = new AffineTransform ();
128+ at .scale (scaleFactor , scaleFactor );
129+ AffineTransformOp scaleOp = new AffineTransformOp (at , AffineTransformOp .TYPE_BILINEAR );
130+ return scaleOp .filter (img , scaledImg );
131+ }
132+
133+ // apply Gaussian blur to a grayscale image
134+ public static BufferedImage blur (BufferedImage img ) {
135+ BufferedImage blurImg = new BufferedImage (
136+ img .getWidth ()-2 , img .getHeight ()-2 , BufferedImage .TYPE_BYTE_GRAY );
137+ int pix = 0 ;
138+ for (int y =0 ; y <blurImg .getHeight (); y ++) {
139+ for (int x =0 ; x <blurImg .getWidth (); x ++) {
140+ pix = (int )(4 *(img .getRGB (x +1 , y +1 )& 0xFF )
141+ + 2 *(img .getRGB (x +1 , y )& 0xFF )
142+ + 2 *(img .getRGB (x +1 , y +2 )& 0xFF )
143+ + 2 *(img .getRGB (x , y +1 )& 0xFF )
144+ + 2 *(img .getRGB (x +2 , y +1 )& 0xFF )
145+ + (img .getRGB (x , y )& 0xFF )
146+ + (img .getRGB (x , y +2 )& 0xFF )
147+ + (img .getRGB (x +2 , y )& 0xFF )
148+ + (img .getRGB (x +2 , y +2 )& 0xFF ))/16 ;
149+ int p = (255 <<24 ) | (pix <<16 ) | (pix <<8 ) | pix ;
150+ blurImg .setRGB (x ,y ,p );
151+ }
152+ }
153+ return blurImg ;
154+ }
155+
119156}
120157
121158
0 commit comments