PGM: 1/O and Polymorphism The purpose of this assignment is to continue practici
ID: 3711008 • Letter: P
Question
PGM: 1/O and Polymorphism The purpose of this assignment is to continue practicing inheritance and polymorphism in Java. PGM Image Format The PGM (or Portable Gray Map) image format is encoded in human-readable ASCII text. For those of you found here. Sample PGM File: who wish to have the experience of reading real documentation, the formal image specification can be P2 24 7 15 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 000 15 0 0 15 0 0 3 0 0 0 007000 00 11 0 000 0 15 0 0 00 Image Header Looking at the sample PGM file above, P2 is a "magic number". It indicates what type of PGM (ASCII encoding) image this is. For this assignment it will always be P2. Next comes the number of columns and the number of rows in the image (24 x 7). Finally, we have the maximum color value 15. This can be any value, but a common value is 255. The way you see the header presented is how it should be spaced outExplanation / Answer
package Chegg;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Scanner;
public class PGM extends Image{
private int pixels[][];
public int[][] getPixels() {
return pixels;
}
private void setPixels(int[][] arr) {
this.pixels= arr;
}
public PGM(String filename) {
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(filename);
Scanner scan = new Scanner(fileInputStream);
String P2= scan.nextLine();
this.setMagic(P2);
int row= scan.nextInt();
this.setHeight(row);
int col= scan.nextInt();
this.setWidth(col);
int maxValue= scan.nextInt();
this.setDepth(maxValue);
this.pixels=new int[row][col];
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
this.pixels[i][j]= scan.nextInt();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void flip_horizontally() {
int row= this.getHeight();
int col= this.getWidth();
// swap each row elements such that i'th element is swapped with (m-i)th element where m is the lenght of row
for (int i = 0; i < row; i++) {
for (int j = 0; j < col/2; j++)
{
int temp = this.pixels[i][j];
this.pixels[i][j] = this.pixels[i][col - 1 - j];
this.pixels[i][col - 1 - j] = temp;
}
}
}
@Override
public void flip_vertically() {
int row= this.getHeight();
int col= this.getWidth();
// swap each column elements such that j'th element is swapped with (n-j)th element where m is the lenght of column
for (int j = 0; j < col-1; j++)
{
for (int i = 0; i < row/2; i++)
{
int temp = this.pixels[i][j];
this.pixels[i][j] = this.pixels[row-1-i][j];
this.pixels[row-1-i][j] = temp;
}
}
}
@Override
public void rotate_right_90() {
int row= this.getHeight();
int col= this.getWidth();
// declare an array to replace the current pixels array
int arr[][]= new int[col][row];
for(int i=0; i< row; i++) {
for(int j=0; j<col; j++) { // transpose it
arr[j][i]= this.pixels[i][j];
}
}
this.setHeight(col);
this.setWidth(row);
this.setPixels(arr);
// flip it as it is to be rotated 90 deg towards Right
this.flip_horizontally();
}
// save the results
private void save(String filename) {
int row= this.getHeight();
int col= this.getWidth();
try {
File file = new File(filename);
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos);
Writer w = new BufferedWriter(osw);
w.write(getMagic()+" ");
w.write(getHeight() + " "+ this.getWidth() + " "+ this.getDepth()+ " ");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
w.write(this.pixels[i][j]+ " ");
}
w.write(" ");
}
w.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
PGM pgm= new PGM("input.pgm");
pgm.flip_vertically();
pgm.save("out.pgm");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.