LAB 16 - Modifying Pictures in a Matrix (part 4 - Copying and Transforming Pictures)

Topics

n      To copy and transform pixels (scaling image) in a matrix (picture) using nested loops

 

5.2 Copying and Transforming Pictures

Type in the following code in the Program window (changing the comments):

 

/* Program that copies and transforms pictures using nested loops

 * through scaling

 * Picture and Pixel classes are defined in bookClasses developed

 * at Georgia Tech by Mark Guzdial / Barbara Ericson

 *

 * @author Wayne Summers

 * @date   Sept. 22, 2007

 */

 

public class ScalePictures

{

  public static void main(String[] args)

  {

    String fileName1, fileName2;

    System.out.println(“Find the source file”);

    fileName1 = FileChooser.pickAFile();  // note that this pops up a window to select a jpg file

 

    Picture sourcePicture;

    sourcePicture = new Picture(fileName1);

 

    System.out.println(“Find the target file”);

    fileName2 = FileChooser.pickAFile();  // note that this pops up a window to select a jpg file

 

    Picture targetPicture;

   targetPicture = new Picture(fileName2);

 

// calls to methods that manipulate images go here

 

   targetPicture.show(); 

  }

}

 

5.2.5 Scaling

i) Type in the following code in the Program window below the main method and before the last }:

 

/**

  * Method that copies and shrinks a source picture onto a target picture

  * @param sourcePic - picture Object to be copied

  * @param targetPic – picture Object to be copied onto

 */

  public static void copySmallerPicture (Picture sourcePic, Picture targetPic)

  {

      Pixel sourcePixel = null;

      Pixel targetPixel = null;

      int width = sourcePic.getWidth();

      int height = sourcePic.getHeight();

 

      // loop through the columns

      for (int sourceX = 0, targetX = 0; sourceX < width; sourceX +=2, targetX++)  

      {

          // loop through the rows

          for (int sourceY = 0, targetY = 0; sourceY < height; sourceY+=2, targetY++)  

          {

              // set the target pixel color to the source pixel color

              sourcePixel = sourcePic.getPixel(sourceX,sourceY);

              targetPixel = targetPic.getPixel(targetX,targetY);

              targetPixel.setColor(sourcePixel.getColor());

          }

      }

 }

 

ii) Type in the following code in the Program window in the main method and before the pic.show();

 

copySmallerPicture (sourcePicture, targetPicture);

 

iii) Test your program.

 

 

iv) Rewrite copySmallerPicture as copyLargerPicture by incrementing sourceX and sourceY by 0.5 instead of 2. Make sure to declare these as double instead of int and to typecast sourceX, sourceY, targetX, and targetY in the getPixel methods.

 

v) Type in the following code in the Program window in the main method and before the pic.show();

 

copyLargerPicture (sourcePicture, targetPicture);

 

vi) Test your program.

 

 

 

QUESTIONS:

 

1)     What happens if you increment sourceX and sourceY by 4 instead of 2 in copySmallerPicture? Explain.

2)     What happens if you increment targetX and targetY by 2 as well in copySmallerPicture? Explain.

3)     What happens if you increment targetX and targetY by 0.5 (and use int to get the integer part) in copyLargerPicture? Explain.

 

 

Submit the .java file with both copySmallerPicture and copyLargerPicture and the answers to the questions through the DropBox in WebCT.