A PGM file can be created and manipulated using a text editor. You may not be ab
ID: 3539853 • Letter: A
Question
A PGM file can be created and manipulated using a text editor. You may not be able to view the grayscale image if you do not have an image viewer that can open a .pgm file (try Microsoft Office Picture Manager). That is okay, you can still complete the program and use a text editor to check results.
Below is an example of a PGM file representing a light gray block letter C on a black background:
A reversed or mirror image of a PGM file can be created by reversing the order of the values in each row of the image. Below is the reversed image of the PGM file shown above:
You must create a class called ReversePGM that will convert a PGM file that uses the P2 format as described above into its reverse image. The file containing the data shall be specified as a command-line argument to the program. This file will be overwritten with a reverse of the original image. For example,
If there is not exactly one argument on the command line, then the following usage message shall be displayed and the program will immediately exit. For example,
If the file on the command line does not end with .pgm, then the following message shall be displayed and the program will immediately exit. For example,
If the file on the command line cannot be accessed, then the following message shall be displayed and the program will immediately exit. For example,
You may assume that the PGM file uses the P2 format, is correctly formatted, and does not contain comments.
The program must contain and use the method listed below. You MUST use the exact method header in your program. You must replace the comments below with proper Javadoc documentation!
Explanation / Answer
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class ReversePGM {
public int[][] readPGM(String filename){
int[][] pixels = null;
try {
Scanner infile = new Scanner(new FileReader(filename));
// process the top 4 header lines
String filetype=infile.nextLine();
if (!filetype.trim().equalsIgnoreCase("p2")) {
System.out.println("[readPGM]Cannot load the image type of "+filetype);
}
infile.nextLine();
int cols = infile.nextInt();
int rows = infile.nextInt();
int maxValue = infile.nextInt();
pixels = new int[rows][cols];
System.out.println("Reading in image from " + filename + " of size " + rows + " by " + cols);
// process the rest lines that hold the actual pixel values
for (int r=0; r<rows; r++)
for (int c=0; c<cols; c++)
pixels[r][c] = (int)(infile.nextInt()*255.0/maxValue);
infile.close();
} catch(FileNotFoundException fe) {
System.out.println(filename+"(No such file or directory).");
} catch (Exception e) {
System.out.println(e.toString() + " caught in readPPM.");
e.printStackTrace();
}
return pixels;
}
public int[][] reversePgm(int[][] pixels){
int tmp, sym;
for(int row=0; row < pixels.length; ++row){
for(int col=0; col < pixels[row].length/2; ++col){
// find the column index of the horizontally symmetric pixel
sym = pixels[row].length-1 - col;
// swap the pixel value between the two
tmp = pixels[row][col];
pixels[row][col] = pixels[row][sym];
pixels[row][sym] = tmp;
}
}
return pixels;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
if(args == null)
System.out.println(" Usage: java ReversePGM filename");
else {
String fileName = args[0];
String extension = "";
int i = fileName.lastIndexOf('.');
int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\'));
if (i > p) {
extension = fileName.substring(i+1);
}
if(extension.trim().equalsIgnoreCase("pgm"))
System.out.println("Filename must have .pgm extension");
else {
ReversePGM pgm = new ReversePGM();
int[][] pixels = pgm.readPGM(fileName);
int[][] reverse = pgm.reversePgm(pixels);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.