Write a program that takes at least two command-line arguments, in this order us
ID: 3621792 • Letter: W
Question
Write a program that takes at least two command-line arguments, in this order using the staring code provided:
* input file
* output file
* a series of commands, one per command-line argument:
o h: flip the picture horizontally. Someone facing left now faces right.
o v: flip the picture vertically. A portrait now stands on its head.
o n: negate (invert) the picture. Black becomes white, white becomes black, nearly black becomes nearly white, etc.
o rr: rotate the picture right (clockwise) 90 degrees.
o rl: rotate the picture left (counterclockwise) 90 degrees.
o b: this "bleeps" out a rectangle whose upper left corner is described by the index (upper_left_row, upper_left_col) and lower right corner is (lower_right_row lower_right_col). Bleep results in making all the pixels in the region bounded by the rectangle, including the edges, white (which is the max value used for gray shades in the PGM file).
Sample run:
% javac Pic.java
% rm -f out.pgm
% java Pic Cam.pgm out.pgm n h
% display out.pgm
This transformed the input file Cam.pgm into the output file out.pgm, after negating it and flipping it horizontally. The display command is then used to display the resulting file.
% rm -f out1.pgm
% java Pic Cam.pgm out1.pgm rr b
Enter four numbers for upper_left_row, upper_left_col, lower_right_row, and lower_right_col
10 20 30 40
% display out1.pgm
Code So Far....
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Pic
{
// Once we've read an image, it implicitly has this maximum value:
static final int MAXVAL = 255;
// This 2-d array contains the pixel values.
private int[][] imageData = null;
// Constructor for the class Pic
public Pic(String path) throws Exception {
imageData = readPGM(path);
}
// Read the picture from the given path.
//
// An ASCII PGM file looks like this:
// P2
// width height
// maxval
// one number per pixel
private int[][] readPGM(String path) throws Exception {
Scanner in = new Scanner(new File(path));
String magic = in.next();
if (!magic.equals("P2")) {
System.err.println("ERROR: "+path+" is not an ASCII PGM file");
System.exit(1);
}
int width = in.nextInt();
int height = in.nextInt();
int fileMaxval = in.nextInt();
int[][] pic = new int[height][width];
// Old, slow, readable code:
for (int y=0; y
for (int x=0; x
pic[y][x] = in.nextInt();
// New, fast, perplexing, code;
// String content = in.useDelimiter("\Z").next();
// StringTokenizer st = new StringTokenizer(content);
// int x=0, y=0;
// while (st.hasMoreTokens()) {
// pic[y][x] = Integer.valueOf(st.nextToken());
// if (++x >= width) {
// x = 0;
// y++;
// }
// }
in.close();
// We want our values to be in the range 0-MAXVAL. If the file data
// wasn't in that range, scale it up to be in that range.
if (fileMaxval != MAXVAL)
for (int j=0; j
for (int i=0; i
pic[j][i] = (pic[j][i] * MAXVAL)/fileMaxval;
return pic;
}
// Negate the image.
// That is, if a pixel has the value 0, replace it with MAXVAL.
// If a pixel has the value 1, replace it with MAXVAL-1.
// If a pixel has the value 7, replace it with MAXVAL-7, etc.
public void negate() {
}
// Flip the image horizontally.
// If it's a person looking left, they'll end up looking right.
public void horizontalFlip() {
}
// Flip the image vertically.
// A picture of a person will end up standing on their head.
public void verticalFlip() {
}
// Rotate the image right, i.e., 90 degrees clockwise.
// A picture of a person will end up getting
// rotated right by 90 degrees.
public void rotateRight() {
}
// Rotate the image left, i.e., 90 degrees counterclockwise.
// A picture of a person will end up getting
// rotated left by 90 degrees.
public void rotateLeft() {
}
// Bleep the image using a white rectangle.
public void bleep(int top_row,
int top_col,
int bottom_row,
int bottom_col) {
}
// Write the picture to the given path.
public void writePGM(String path) throws Exception {
int height = imageData.length;
int width = imageData[0].length;
PrintStream out = new PrintStream(new FileOutputStream(path));
out.println("P2");
out.println(width+" "+height);
out.println(MAXVAL); // maximum pixel value
for (int y=0; y
for (int x=0; x
out.println(imageData[y][x]); // Yeah, one pixel per line. So?
out.close();
}
public static void usage() {
System.err.println("usage: java Pic (h|v|n|rr|rl|b) ...");
System.exit(1);
}
public static void main(String[] args) throws Exception {
if (args.length < 2)
usage();
Scanner keyboard = new Scanner(System.in);
Pic picture = new Pic(args[0]);
for (int i=2; i
String op = args[i];
if (op.equals("h"))
picture.horizontalFlip();
else if (op.equals("v"))
picture.verticalFlip();
else if (op.equals("n"))
picture.negate();
else if (op.equals("rr"))
picture.rotateRight();
else if (op.equals("rl"))
picture.rotateLeft();
else if (op.equals("b")) {
System.out.println("Enter four numbers for upper_left_row, upper_left_col, lower_right_row, and lower_right_col");
int upperLeftRow = keyboard.nextInt();
int upperLeftCol = keyboard.nextInt();
int upperRightRow = keyboard.nextInt();
int upperRightCol = keyboard.nextInt();
picture.bleep(upperLeftRow, upperLeftCol, upperRightRow, upperRightCol);
}
else
System.err.println("Invalid operation ""+op+""");
}
picture.writePGM(args[1]);
}
}
Explanation / Answer
private int[][] arrayCopy(int [] [] model) {
int [] [] newArray = new int[model.length][0];
for (int y = 0; y
newArray[y] = new int[model[y].length];
for (int x=0; x
newArray[y][x] = model[y][x];
}
}
return newArray;
}
public void horizontalFlip() {
int [][] tempImage = arrayCopy(imageData);
for (int y = 0; y < imageData.length; y++) {
int width = imageData[y].length;
for (int x = 0; x < width; x++) {
imageData[y][x] = tempImage[y][width-x-1];
}
}
}
// Flip the image vertically.
// A picture of a person will end up standing on their head.
public void verticalFlip() {
int [][] tempImage = arrayCopy(imageData);
for (int y = 0; y < imageData.length; y++) {
int width = imageData[y].length;
for (int x=0; x < width; x++) {
imageData[y][x] = tempImage[imageData.length - y - 1][x];
}
}
}
// Rotate the image right, i.e., 90 degrees clockwise.
// A picture of a person will end up getting
// rotated right by 90 degrees.
public void rotateRight() {
int [][] tempImage = arrayCopy(imageData);
if (tempImage.length == 0) {
return;
}
imageData = new int [tempImage[0].length][tempImage.length];
for (int y = 0; y < imageData.length; y++) {
for (int x=0; x < imageData[0].length; x++) {
imageData[y][imageData[0].length-x-1] = tempImage[x][y];
}
}
}
// Rotate the image left, i.e., 90 degrees counterclockwise.
// A picture of a person will end up getting
// rotated left by 90 degrees.
public void rotateLeft() {
rotateRight();
rotateRight();
rotateRight();
}
// Bleep the image using a white rectangle.
public void bleep(int top_row,
int top_col,
int bottom_row,
int bottom_col) {
for (int y = top_row; y <= bottom_row; y++) {
for (int x=top_col; x<=bottom_col; x++) {
imageData[y][x] = MAXVAL;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.