-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScaleDown.java
40 lines (39 loc) · 1.39 KB
/
ScaleDown.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.awt.image.BufferedImage;
public class ScaleDown {
public static void main(String[] args) {
try {
int scalingFactor = Integer.parseInt(args[1]);
int scalingFactorSqr = scalingFactor*scalingFactor;
BufferedImage input = ImageIO.read(new File(args[0]));
int height = input.getHeight();
int width = input.getWidth();
BufferedImage output = new BufferedImage(height/scalingFactor, width/scalingFactor, input.getType());
for (int i = 0; i < height; i += scalingFactor) {
for (int j = 0; j < width; j += scalingFactor) {
int red = 0, green = 0, blue = 0;
for (int ii = i; ii < i+scalingFactor; ii++) {
for (int jj = j; jj < j+scalingFactor; jj++) {
int color = input.getRGB(jj, ii);
red += (color & 0x00ff0000) >> 16;
green += (color & 0x0000ff00) >> 8;
blue += (color & 0x000000ff);
}
}
red /= scalingFactorSqr;
green /= scalingFactorSqr;
blue /= scalingFactorSqr;
int x = j/scalingFactor;
int y = i/scalingFactor;
output.setRGB(x, y, (red << 16) + (green << 8) + blue);
}
}
ImageIO.write(output, "png", new File(args[2]));
}
catch (IOException e) {
e.printStackTrace();
}
}
}