@@ -16,8 +16,12 @@ public static void main(String[] args) {
16
16
catch (IOException e ) { e .printStackTrace (System .out ); }
17
17
18
18
if (img != null ) {
19
+ //display(img);
20
+ img = toGrayScale (img );
21
+ //img = toGrayScale2(img);
19
22
display (img );
20
- img = toGrayScale2 (img );
23
+ img = pixelate (img );
24
+ //img = pixelate2(img, 3);
21
25
display (img );
22
26
}
23
27
}
@@ -34,6 +38,7 @@ public static void display (BufferedImage img) {
34
38
frame .pack ();
35
39
frame .setVisible (true );
36
40
}
41
+
37
42
// convert image to grayscale
38
43
public static BufferedImage toGrayScale (BufferedImage img ) {
39
44
System .out .println (" Converting to GrayScale." );
@@ -44,24 +49,73 @@ public static BufferedImage toGrayScale (BufferedImage img) {
44
49
g .dispose ();
45
50
return grayImage ;
46
51
}
52
+
47
53
public static BufferedImage toGrayScale2 (BufferedImage img ) {
48
54
System .out .println (" Converting to GrayScale2." );
49
55
BufferedImage grayImage = new BufferedImage (
50
56
img .getWidth (), img .getHeight (), BufferedImage .TYPE_BYTE_GRAY );
51
- int p = 0 , rgb =0 , r =0 , g =0 , b =0 ;
52
- for (int y =0 ; y <img .getHeight ()- 1 ; y ++) {
53
- for (int x =0 ; x <img .getWidth ()- 1 ; x ++) {
57
+ int rgb =0 , r =0 , g =0 , b =0 ;
58
+ for (int y =0 ; y <img .getHeight (); y ++) {
59
+ for (int x =0 ; x <img .getWidth (); x ++) {
54
60
rgb = (int )(img .getRGB (x , y ));
55
61
r = ((rgb >> 16 ) & 0xFF );
56
62
g = ((rgb >> 8 ) & 0xFF );
57
63
b = (rgb & 0xFF );
58
- p = (int )(0.2126 * r + 0.7152 * g + 0.0722 * b );
59
- p = (255 <<24 ) | (p <<16 ) | (p <<8 ) | p ;
60
- grayImage .setRGB (x ,y ,p );
64
+ rgb = (int )((r +g +b )/3 );
65
+ //rgb = (int)(0.299 * r + 0.587 * g + 0.114 * b);
66
+ rgb = (255 <<24 ) | (rgb <<16 ) | (rgb <<8 ) | rgb ;
67
+ grayImage .setRGB (x ,y ,rgb );
61
68
}
62
69
}
63
70
return grayImage ;
64
71
}
72
+
73
+ // apply 2x2 pixelation to a grayscale image
74
+ public static BufferedImage pixelate (BufferedImage img ) {
75
+ BufferedImage pixImg = new BufferedImage (
76
+ img .getWidth (), img .getHeight (), BufferedImage .TYPE_BYTE_GRAY );
77
+ int pix = 0 , p =0 ;
78
+ for (int y =0 ; y <img .getHeight ()-2 ; y +=2 ) {
79
+ for (int x =0 ; x <img .getWidth ()-2 ; x +=2 ) {
80
+ pix = (int )((img .getRGB (x , y )& 0xFF )
81
+ + (img .getRGB (x +1 , y )& 0xFF )
82
+ + (img .getRGB (x , y +1 )& 0xFF )
83
+ + (img .getRGB (x +1 , y +1 )& 0xFF ))/4 ;
84
+ p = (255 <<24 ) | (pix <<16 ) | (pix <<8 ) | pix ;
85
+ pixImg .setRGB (x ,y ,p );
86
+ pixImg .setRGB (x +1 ,y ,p );
87
+ pixImg .setRGB (x ,y +1 ,p );
88
+ pixImg .setRGB (x +1 ,y +1 ,p );
89
+ }
90
+ }
91
+ return pixImg ;
92
+ }
93
+
94
+ // apply nxn pixelation to a grayscale image
95
+ public static BufferedImage pixelate2 (BufferedImage img , int n ) {
96
+ BufferedImage pixImg = new BufferedImage (
97
+ img .getWidth (), img .getHeight (), BufferedImage .TYPE_BYTE_GRAY );
98
+ int pix = 0 , p =0 ;
99
+ for (int y =0 ; y <img .getHeight ()-n ; y +=n ) {
100
+ for (int x =0 ; x <img .getWidth ()-n ; x +=n ) {
101
+ for (int a =0 ; a <n ; a ++) {
102
+ for (int b =0 ; b <n ; b ++) {
103
+ pix += (img .getRGB (x +a , y +b )& 0xFF );
104
+ }
105
+ }
106
+ pix = (int )(pix /n /n );
107
+ for (int a =0 ; a <n ; a ++) {
108
+ for (int b =0 ; b <n ; b ++) {
109
+ p = (255 <<24 ) | (pix <<16 ) | (pix <<8 ) | pix ;
110
+ pixImg .setRGB (x +a ,y +b ,p );
111
+ }
112
+ }
113
+ pix = 0 ;
114
+ }
115
+ }
116
+ return pixImg ;
117
+ }
118
+
65
119
}
66
120
67
121
0 commit comments