Skip to content

Commit 175b4b0

Browse files
author
bjrara
committed
initial commit pj1 1st part
1 parent f9731a8 commit 175b4b0

12 files changed

+2463
-0
lines changed

build.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ project(':homework') {
1515
testCompile 'junit:junit:4.11'
1616
}
1717
}
18+
19+
project(':project') {
20+
dependencies {
21+
testCompile 'junit:junit:4.11'
22+
compile files('libs/jai_core.jar')
23+
}
24+
}

project/libs/jai_codec.jar

252 KB
Binary file not shown.

project/libs/jai_core.jar

1.81 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.offline.training.project.pj1;
2+
3+
/* Blur.java */
4+
5+
/* DO NOT CHANGE THIS FILE. */
6+
/* YOUR SUBMISSION MUST WORK CORRECTLY WITH _OUR_ COPY OF THIS FILE. */
7+
8+
/* You may wish to make temporary changes or insert println() statements */
9+
/* while testing your code. When you're finished testing and debugging, */
10+
/* though, make sure your code works with the original version of this file. */
11+
12+
/**
13+
* The Blur class is a program that reads an image file in TIFF format, blurs
14+
* it with a 3x3 box blurring kernel, writes the blurred image as a TIFF file,
15+
* and displays both images.
16+
*
17+
* The Blur program takes up to two parameters. The first parameter is
18+
* the name of the TIFF-format file to read. (The output image file is
19+
* constructed by adding "blur_" to the beginning of the input filename.)
20+
* An optional second parameter specifies the number of iterations of the
21+
* box blurring operation. (The default is one iteration.) For example, if
22+
* you run
23+
*
24+
* java Blur engine.tiff 5
25+
*
26+
* then Blur will read engine.tiff, perform 5 iterations of blurring, and
27+
* write the blurred image to blur_engine.tiff .
28+
*
29+
* @author Joel Galenson and Jonathan Shewchuk
30+
*/
31+
32+
public class Blur {
33+
34+
/**
35+
* blurFile() reads a TIFF image file, blurs it, write the blurred image to
36+
* a new TIFF image file, and displays both images.
37+
*
38+
* @param filename the name of the input TIFF image file.
39+
* @param numIterations the number of iterations of blurring to perform.
40+
*/
41+
private static void blurFile(String filename, int numIterations) {
42+
System.out.println("Reading image file " + filename);
43+
PixImage image = ImageUtils.readTIFFPix(filename);
44+
45+
System.out.println("Blurring image file.");
46+
PixImage blurred = image.boxBlur(numIterations);
47+
48+
String blurname = "blur_" + filename;
49+
System.out.println("Writing blurred image file " + blurname);
50+
TIFFEncoder.writeTIFF(blurred, blurname);
51+
/*
52+
TIFFEncoder.writeTIFF(new RunLengthEncoding(edges), "rle" + blurname);
53+
*/
54+
55+
System.out.println("Displaying input image and blurred image.");
56+
System.out.println("Close the image to quit.");
57+
ImageUtils.displayTIFFs(new PixImage[] { image, blurred });
58+
}
59+
60+
/**
61+
* main() reads the command-line arguments and initiates the blurring.
62+
*
63+
* The first command-line argument is the name of the image file.
64+
* An optional second argument is number of iterations of blurring.
65+
*
66+
* @param args the usual array of command-line argument Strings.
67+
*/
68+
public static void main(String[] args) {
69+
if (args.length == 0) {
70+
System.out.println("usage: java Blur imagefile [iterations]");
71+
System.out.println(" imagefile is an image in TIFF format.");
72+
System.out.println(" interations is the number of blurring iterations" +
73+
" (default 1).");
74+
System.out.println("The blurred image is written to blur_imagefile.");
75+
System.exit(0);
76+
}
77+
78+
int numIterations = 1;
79+
if (args.length > 1) {
80+
try {
81+
numIterations = Integer.parseInt(args[1]);
82+
} catch (NumberFormatException ex) {
83+
System.err.println("The second argument must be a number.");
84+
System.exit(1);
85+
}
86+
}
87+
88+
blurFile(args[0], numIterations);
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package com.offline.training.project.pj1;
2+
3+
/* ImageUtils.java */
4+
5+
/* DO NOT CHANGE THIS FILE. */
6+
/* YOUR SUBMISSION MUST WORK CORRECTLY WITH _OUR_ COPY OF THIS FILE. */
7+
8+
/* You may wish to make temporary changes or insert println() statements */
9+
/* while testing your code. When you're finished testing and debugging, */
10+
/* though, make sure your code works with the original version of this file. */
11+
12+
/**
13+
* The ImageUtils class reads and writes TIFF file, converting to and from
14+
* pixel arrays in PixImage format or run-length encodings in
15+
* RunLengthEncoding format. Methods are also included for displaying images
16+
* in PixImage format.
17+
*
18+
* @author Joel Galenson
19+
**/
20+
21+
import java.awt.Color;
22+
import java.awt.event.WindowAdapter;
23+
import java.awt.event.WindowEvent;
24+
import java.awt.image.BufferedImage;
25+
26+
import javax.media.jai.JAI;
27+
import javax.media.jai.RenderedImageAdapter;
28+
import javax.swing.Box;
29+
import javax.swing.ImageIcon;
30+
import javax.swing.JFrame;
31+
import javax.swing.JLabel;
32+
33+
/**
34+
* ImageUtils contains utilities for reading, writing, and displaying images.
35+
*
36+
* It uses JAI to read and write TIFF files, as the standard libraries cannot
37+
* read them.
38+
*
39+
* All image data is in RGB format (see BufferedImage.getRGB).
40+
*/
41+
public class ImageUtils {
42+
43+
/**
44+
* buffer2PixImage() converts a BufferedImage to a PixImage.
45+
* @param bImage the image to convert.
46+
* @return a PixImage with the same pixels as the BufferedImage.
47+
*/
48+
private static PixImage buffer2PixImage(BufferedImage bImage) {
49+
PixImage pImage = new PixImage(bImage.getWidth(), bImage.getHeight());
50+
for (int x = 0; x < bImage.getWidth(); x++) {
51+
for (int y = 0; y < bImage.getHeight(); y++) {
52+
Color color = new Color(bImage.getRGB(x, y));
53+
pImage.setPixel(x, y, (short) color.getRed(), (short) color.getGreen(),
54+
(short) color.getBlue());
55+
}
56+
}
57+
return pImage;
58+
}
59+
60+
/**
61+
* pixImage2buffer() converts a PixImage to a BufferedImage.
62+
* @param pImage the image to convert.
63+
* @return a BufferedImage with the same pixels as the PixImage.
64+
*/
65+
static BufferedImage pixImage2buffer(PixImage pImage) {
66+
BufferedImage bImage = new BufferedImage(pImage.getWidth(),
67+
pImage.getHeight(),
68+
BufferedImage.TYPE_INT_ARGB);
69+
for (int x = 0; x < bImage.getWidth(); x++) {
70+
for (int y = 0; y < bImage.getHeight(); y++) {
71+
bImage.setRGB(x, y, new Color(pImage.getRed(x, y),
72+
pImage.getGreen(x, y),
73+
pImage.getBlue(x, y)).getRGB());
74+
}
75+
}
76+
return bImage;
77+
}
78+
79+
/**
80+
* readTIFF() reads an image from a file and formats it as a BufferedImage.
81+
* @param filename the name of the file to read.
82+
* @return a BufferedImage of the file
83+
*/
84+
private static BufferedImage readTIFF(String filename) {
85+
return (new RenderedImageAdapter(JAI.create("fileload", filename)))
86+
.getAsBufferedImage();
87+
}
88+
89+
/**
90+
* readTIFFPix() reads an image from a file and formats it as a PixImage.
91+
* @param filename the name of the file to read.
92+
* @return a PixImage of the file
93+
*/
94+
public static PixImage readTIFFPix(String filename) {
95+
return buffer2PixImage(readTIFF(filename));
96+
}
97+
98+
/**
99+
* readTIFFRLE() reads an image from a file and formats it as a run-length
100+
* encoding.
101+
* @param filename the name of the file to read.
102+
* @return a RunLengthEncoding of the file.
103+
*/
104+
public static RunLengthEncoding readTIFFRLE(String filename) {
105+
return new RunLengthEncoding(readTIFFPix(filename));
106+
}
107+
108+
/**
109+
* writeTIFF() writes a BufferedImage to a specified file in TIFF format.
110+
* @param rle the input BufferedImage.
111+
* @param filename the output filename.
112+
*/
113+
private static void writeTIFF(BufferedImage image, String filename) {
114+
JAI.create("filestore", image, filename, "tiff");
115+
}
116+
117+
/**
118+
* writeTIFF() writes a PixImage to a specified file in TIFF format.
119+
* @param image the input PixImage.
120+
* @param filename the output filename.
121+
*/
122+
public static void writeTIFF(PixImage image, String filename) {
123+
writeTIFF(pixImage2buffer(image), filename);
124+
}
125+
126+
/**
127+
* writeTIFF() writes a run-length encoding to a specified file in TIFF
128+
* format.
129+
* @param rle the input run-length encoded image.
130+
* @param filename the output filename.
131+
*/
132+
public static void writeTIFF(RunLengthEncoding rle, String filename) {
133+
writeTIFF(rle.toPixImage(), filename);
134+
}
135+
136+
/**
137+
* displayFrame displays a JFrame and pauses until the window is closed.
138+
* @param frame a JFrame to display.
139+
*/
140+
private static void displayFrame(final JFrame frame) {
141+
try {
142+
synchronized (ImageUtils.class) {
143+
frame.setResizable(false);
144+
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
145+
frame.addWindowListener(new WindowAdapter() {
146+
@Override
147+
public void windowClosing(WindowEvent event) {
148+
synchronized (ImageUtils.class) {
149+
ImageUtils.class.notify();
150+
frame.dispose();
151+
}
152+
}
153+
});
154+
frame.pack();
155+
frame.setVisible(true);
156+
ImageUtils.class.wait();
157+
}
158+
} catch (InterruptedException e) {
159+
System.out.println("Interrupted Exception in displayFrame().");
160+
e.printStackTrace();
161+
}
162+
}
163+
164+
/**
165+
* displayTIFFs displays a sequence of PixImages and pauses until the window
166+
* is closed.
167+
* @param images an array of PixImages to display.
168+
*/
169+
public static void displayTIFFs(PixImage[] images) {
170+
JFrame frame = new JFrame();
171+
Box box = Box.createHorizontalBox();
172+
for (int i = 0; i < images.length; i++) {
173+
box.add(new JLabel(new ImageIcon(pixImage2buffer(images[i]))));
174+
if (i < images.length - 1) {
175+
box.add(Box.createHorizontalStrut(10));
176+
}
177+
}
178+
frame.add(box);
179+
displayFrame(frame);
180+
}
181+
182+
/**
183+
* displayTIFF displays a PixIamge and pauses until the window is closed.
184+
* @param image the PixImage to display.
185+
*/
186+
public static void displayTIFF(PixImage image) {
187+
displayTIFFs(new PixImage[] { image });
188+
}
189+
}

0 commit comments

Comments
 (0)