Greyscale images only require one channel per pixel (each pixel requires 1 byte), where 255 indicates white,
Question:
Greyscale images only require one channel per pixel (each pixel requires 1 byte), where 255
indicates white, 0 indicates black and the values between are varying shades of grey.
Images can be stored in many different formats, mostly in binary form. However, for debugging purposes, storing images in ASCII form is useful. A Portable Pixel Map (PPM) can store images in ASCII form.
Lines 1 to 4 form the header of the PPM file. They provide information about the image. Line 1 refers to the type of PPM file. In this case, “P3” means that the file is a color image stored in ASCII form (“P2” would mean an image is greyscale stored in ASCII form).
Comments for the image can be stored using the # symbol. The image above has a single comment in line 2. For simplicity, we will just use a 3-letter word to describe the image as a
comment.
The image size is stored in line 3. The first 64 refers to the number of rows in the image. The second 64 refers to the number of columns in this image. Multiplying the two values will provide you with the total number of pixels in the image (64 ????????????????∗64 ????????????????????????????=4096 ????????????????????????).
The remainder of the file consists of RGB values for each pixel, in ASCII form. Each line consists of 1 value, which will either be an R, G or B value for a pixel.
Here is the PPM of a greyscale file:
The structure in the greyscale PPM is almost identical to the color one. The only difference in the header is the type of file has changed in line 1 (P2). Note how each pixel only requires one line, whereas in the colour PPM, each pixel requires three lines.
Write a Java program (greyscale.java) that will read in a color PPM P3 image and convert this to a greyscale PPM P2 image. A greyscale pixel value is calculated by finding the average of its RGB values. Decimals are rounded down to the nearest whole number, e.g.: for a pixel with RGB values of 166, 186, and 181 respectively, the greyscale pixel value will be 177. You will need to change the file type in the header of the new greyscale file to “P2”.