Question
Java Program PGM: I/O and Polymorphism The purpose of this assignment is to continue practicing inheritance and polymorphism in Java. PGM Image Format The PGM
Java Program
PGM: I/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 who wish to have the experience of reading real documentation, the formal image specification can be found here. Sample PGM File:
P2 24 7 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
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 out.
Image Body
The image body contains the actual picture information. Each pixel of the image is a tiny, gray or white square. Note that each pixel values must be separated by a space, but after than additional whitespace is ignored by the image viewer. In this assignment, pixel values will be separated by exactly one space. Also, each line will have exactly the same number of column specified in the image header. The example image above would look something like this:
Keep in mind, each square is one pixel, so the real thing is much smaller (the rendered image was 280%).
Image.java
public abstract class Image { // --------------------- // Image attributes // --------------------- protected String magic; protected int width; protected int height; protected int depth; // Accessors: getMagic(), getWidth(), etc... // Modifiers: setMagic(string m), setWidth(int w), etc... // ------------------- // abstract functions // ------------------- public abstract void flip_horizontally(); public abstract void flip_vertically(); public abstract void rotate_right_90(); }
TO DO
Your job is to implement a class named PGM.java that extends the abstarct class Image.java above. You will need to override all the abstarct methods. Also, your implementation should meet the following criteria:
PGM pgm = new PGM("input.pgm"); | creates a PGM object from input.pgm. |
int[][] = pgm.getPixels(); | returns a 2D integer array of pixel values. |
pgm.flip_vertically(); | flips the pixel values vertically. |
pgm.flip_horizontally(); | flips the pixel values horizontally. |
pgm.rotate_right_90(); | rotates the pixel values 90 degrees to the right. |
Note
You only need to submit the PGM.java file. A typical way to test your code:
import java.util.*; public class Sample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a PGM file name to read: "); String file = sc.nextLine(); PGM PGMImage = new PGM (file); PGMImage.flip_vertically (); PGMImage.save("out.pgm"); } }
Compiling your code with the above main program should change the above pgm example image into the following:
EEPStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started