Assignment 8: Photo Manipulation (Due: Th, Dec 4)
The pictures at the end of the assignment were created by calling the
ManipulatedPhotos.java program with the following command.
java ManipulatedPhotos babies.jpg
- Provide the missing code to the given program.
- Introduce two new extended classes PixelA.java and PixelB.java of
PixelTransform.java, and introduce to the main method code that use
the extended classes to create two additional transformed photos.
Notes.
- Submit your source files with the ‘submit c214aa lab8 *Photo*.java
*Pixel*.java’ command.
- To view a photo ‘name.jpg’
- Place the file in your ~/WWW directory: ‘cp name.jpg ~/WWW/.’.
- Provide public access to the file: ‘chmod 644 ~/WWW/name.jpg’.
- Open the following file with Mozilla Firefox.
http://www.cse.ohio-state.edu/~username/name.jpg
- Your extended classes PixelA.java and PixelB.java should show creativity.
Their implementations should be original—they should not be just minor
variants of the given classes of the assignment.
- If questions in an email of yours refer to a program you wrote, please don’t
include the program in the email. Instead, put the program in your WWW
directory with an extension .txt added to the file name (e.g., Foo.java.txt),
change the access mode of the program to 644, and provide just the file name in
the email.
import java.util.ArrayList;
class ManipulatedPhotos{
public static void main(String [] args) throws Exception{
PixelTransform pixelTransform;
ArrayList<PhotoTransform> transform =
new ArrayList<PhotoTransform> ();
/*---- collect transformations ----*/
pixelTransform = new ReducePixel();
transform.add( new ScaledPhoto( pixelTransform, 0.3 ) );
transform.add( new ScaledPhoto( pixelTransform, 0.666 ) );
pixelTransform = new RedPixel();
transform.add( new ScaledPhoto( pixelTransform, 1 ) );
pixelTransform = new DistortPixel();
transform.add( new ScaledPhoto( pixelTransform, 1 ) );
pixelTransform = new LocationScalePixel(0.33, 0.5);
transform.add( new ScaledPhoto( pixelTransform, 0.5 ) );
pixelTransform = new LocationScalePixel(0.3, 0.7);
transform.add( new ScaledPhoto( pixelTransform, 0.7 ) );
/*---- load the picture ----*/
Photo src = new Photo( args[0] );
int idx = args[0].lastIndexOf(’.’);
String filename = args[0].substring(0,idx);
/*---- transform and store pictures ----*/
for(int i = 0; i<transform.size(); i++){
Photo photo = src.apply( transform.get(i) );
photo.print( filename + "-" + i + ".jpg" );
} } }
-_-_-
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
class Photo{
BufferedImage image;
Photo( String filename ) throws Exception {
File file = new File( filename );
image = ImageIO.read(file);
}
Photo( BufferedImage image ){
this.image = image;
}
void print( String filename ) throws Exception {
File file = new File( filename );
ImageIO.write(image, "jpg", file);
System.out.println( "--> New picture: " + filename );
}
Photo apply( PhotoTransform tr ){
BufferedImage newImage = tr.transform(image);
return new Photo( newImage );
} }
-_-_-
import java.awt.image.BufferedImage;
abstract class PhotoTransform{
PixelTransform tr;
double scale;
public BufferedImage transform( BufferedImage image ){
int width = (int) Math.round(image.getWidth() * scale);
int height = (int) Math.round(image.getHeight() * scale);
BufferedImage newImage = new BufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
tr.init(image);
for(int i = 0; i < width; i++) {
for(int j = 0; j < height; j++) {
newImage.setRGB(i, j, tr.getPixel( i, j ));
} }
return newImage;
} }
-_-_-
class ReducePixel extends PixelTransform{
int getPixel(int i, int j){
return
image.getRGB(
3 * i % width
,
3 * j % height );
} }
-_-_-
import java.awt.Color;
class RedPixel extends PixelTransform{
int getPixel(int i, int j){
int intColor = image.getRGB(i,j);
Color color = new Color( intColor );
return new Color ( color.getRed(), 0, 0 ) . getRGB();
} }
-_-_-
class DistortPixel extends PixelTransform{
int getPixel(int i, int j){
double alpha = i*1.0/width;
double beta = j*1.0/height;
i = (int) Math.round( alpha * alpha * i );
j = (int) Math.round( (1-(1-beta) *(1- beta)
+ alpha * alpha ) * j /2 );
return image.getRGB( i, j );
} }
-_-_-
class LocationScalePixel extends PixelTransform{
double location, scale;
LocationScalePixel(double location, double scale){
double sum = location + scale;
if( sum > 1 ){
System.err.println(
"--- Error --- location (" + location
+ ") scale (" + scale + ") > 1"
);
location /= sum;
scale /= sum;
};
this.location = location;
this.scale = scale;
}
int getPixel(int i, int j){
return
image.getRGB(
((int) (width*location)) + (i+width)%((int) (width*scale))
,
((int) (height*location)) + (j+height)%((int)(height*scale))
);
}
}
-_-_-
babies.jpg:
babies-0.jpg:
babies-1.jpg:
babies-2.jpg:
babies-3.jpg:
babies-4.jpg:
babies-5.jpg: