Question: Decoding PPM data The contents of a sample PPM file is shown in the following box. It encodes information about a small image. 1 P
Decoding PPM data
The contents of a sample PPM file is shown in the following box. It encodes information about a small image.
P
The PPM file is divided into two sections: a header section the first three lines and a data section everything that follows the header
The first line of a PPM file always contains the characters P This is called the file's magic number and is used to distinguish files of different types. If the first line does not contain the characters P then we know it's not a PPM file.
Line contains two numbers which are the width and height of the image in that order In the example above, the image is pixels wide and pixels high.
Line contains the number which is called the color depth, which is the maximum value for the red, green, and blue components of a pixel color.
Lines and are the data section of this PPM file. Since this image is pixels wide and pixels high, the total number of pixels is And since each pixel is saved as three integers RGB triplet there is a total of integers in the data section. In the example above, the first row contains three pixels that have colors red green and blue The second row has yellow white and black pixels.
To decode a PPM file, we follow these steps in sequence:
Read a line from the file and make sure it says P
Read an integer from the file call it w the width
Read another integer from the file call it h the height
Read another integer from the file call it d the color depth
Create a canvas with the given width and height.
For each row i from to h:
For each column j from to w:
Read an integer r
Read an integer g
Read an integer b
Set the pixel color at coordinates j i to
Encoding PPM Data
Encoding is the process of saving an image data to a PPM file. To do this, we follow these steps in sequence:
Open the file for writing.
Write P on a line.
Write the image's width and height on the following line.
Write the number on the following line.
For each row i from to h:
For each column j from to w:
Get the pixel at coordinates j i of the image.
Write the red, green, and blue numbers, separated by spaces.
If we're at the last column in the row, print a newline character. Otherwise, print a space.
Flush the file's output.
Interface and Implementation of Codecs
The following is the interface for ImageCodec coderdecoder
public interface ImageCodec
public MutableImage decodeInputStream input;
public void encodeImage image, OutputStream output;
public MutableImage loadString filename throws IOException;
public void saveImage img, String filename throws IOException;
The method decode takes an input stream containing image data, and returns a mutable image containing the decoded image. The method encode takes an image and an output stream and prints the image data into the output stream. The method load is like decode except that it takes a file name from which the data is loaded. The method save, similarly, saves the image data to a file.
Write the class PPMCodec that implements ImageCodec. It must encode and decode the streams according to the specifications listed in this assignment.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
