-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blur.java
88 lines (77 loc) · 3.17 KB
/
Blur.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* Blur.java */
/* DO NOT CHANGE THIS FILE. */
/* YOUR SUBMISSION MUST WORK CORRECTLY WITH _OUR_ COPY OF THIS FILE. */
/* You may wish to make temporary changes or insert println() statements */
/* while testing your code. When you're finished testing and debugging, */
/* though, make sure your code works with the original version of this file. */
/**
* The Blur class is a program that reads an image file in TIFF format, blurs
* it with a 3x3 box blurring kernel, writes the blurred image as a TIFF file,
* and displays both images.
*
* The Blur program takes up to two parameters. The first parameter is
* the name of the TIFF-format file to read. (The output image file is
* constructed by adding "blur_" to the beginning of the input filename.)
* An optional second parameter specifies the number of iterations of the
* box blurring operation. (The default is one iteration.) For example, if
* you run
*
* java Blur engine.tiff 5
*
* then Blur will read engine.tiff, perform 5 iterations of blurring, and
* write the blurred image to blur_engine.tiff .
*
* @author Joel Galenson and Jonathan Shewchuk
*/
public class Blur {
/**
* blurFile() reads a TIFF image file, blurs it, write the blurred image to
* a new TIFF image file, and displays both images.
*
* @param filename the name of the input TIFF image file.
* @param numIterations the number of iterations of blurring to perform.
*/
private static void blurFile(String filename, int numIterations) {
System.out.println("Reading image file " + filename);
PixImage image = ImageUtils.readTIFFPix(filename);
System.out.println("Blurring image file.");
PixImage blurred = image.boxBlur(numIterations);
String blurname = "blur_" + filename;
System.out.println("Writing blurred image file " + blurname);
TIFFEncoder.writeTIFF(blurred, blurname);
/*
TIFFEncoder.writeTIFF(new RunLengthEncoding(edges), "rle" + blurname);
*/
System.out.println("Displaying input image and blurred image.");
System.out.println("Close the image to quit.");
ImageUtils.displayTIFFs(new PixImage[] { image, blurred });
}
/**
* main() reads the command-line arguments and initiates the blurring.
*
* The first command-line argument is the name of the image file.
* An optional second argument is number of iterations of blurring.
*
* @param args the usual array of command-line argument Strings.
*/
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("usage: java Blur imagefile [iterations]");
System.out.println(" imagefile is an image in TIFF format.");
System.out.println(" interations is the number of blurring iterations" +
" (default 1).");
System.out.println("The blurred image is written to blur_imagefile.");
System.exit(0);
}
int numIterations = 1;
if (args.length > 1) {
try {
numIterations = Integer.parseInt(args[1]);
} catch (NumberFormatException ex) {
System.err.println("The second argument must be a number.");
System.exit(1);
}
}
blurFile(args[0], numIterations);
}
}