] > Assignment 8: Photo Manipulation (Due: Th, Dec 4)

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

  1. Provide the missing code to the given program.
  2. 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.

<..ManipulatedPhotos.java..>
 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" );
 }  }  }
-_-_-
<..Photo.java..>
 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 );
 }  }
-_-_-
<..PhotoTransform.java..>
 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;
 }  }
-_-_-
<..ReducePixel.java..>
 class ReducePixel extends PixelTransform{
   int getPixel(int i, int j){
      return
          image.getRGB(
             3 * i % width
             ,
             3 * j % height );
 } }
-_-_-
<..RedPixel.java..>
 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();
 } }
-_-_-
<..DistortPixel.java..>
 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 );
 } }
-_-_-
<..LocationScalePixel.java..>
 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: [Picture]

babies-0.jpg: [Picture]

babies-1.jpg: [Picture]

babies-2.jpg: [Picture]

babies-3.jpg: [Picture]

babies-4.jpg: [Picture]

babies-5.jpg: [Picture]