Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C-Programming Project For this project you will write a program that reads and m

ID: 3574694 • Letter: C

Question

C-Programming

Project For this project you will write a program that reads and manipulates images stored in PGM format. The PGM at is rather simple. The image file is made up of two parts. The first is a header: 1: image type (should always be P2 for this project P2 2: filename (really is just a comment) feep.pgma 3: width of image (in pixels), height of image (in pixels) 24 7 4: maximum pixel value (in this case, pixels will go from 0 to 15 (4 bit grayscale), you could also use 255 to have an 8-bit grayscale image with pixel values going from 0 to 255 15 And the second is the image body, which is just each pixel separated by whitespace (can be all on one line, but is easier to read by a person). The pixels start at the top-left of the image, and go in order left to right until the number of pixels in a row have been reached, the next pixel is the first pixel on the following row, and so on: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 11 0 15 15 15 15 0 1 0 15 0 15 0 1 0 15 15 15 15 1 11 1 0 11 0 15 0 1 0 15 1 11 1 1 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Explanation / Answer

/*

Following java code performs all operations mentioned in the problem statement.

This code take pgm file as an argument.

get_image_pixel() function returns value at that pixel(x,y)

set_image_pixel() function set value at pixel(x,y) to 0.

Program terminates when invalid pixel is given as input.

And in last step it diplays the content of that pgm file.

*/

import java.util.*;
import java.lang.*;
import java.io.*;
public class pgmfile
{
   private BufferedReader d;
   private String [][]in;
   private String line1,line2,line3,line4;
   private static int height,width;
   public void readFile(String arg)
   {
       try{
               d = new BufferedReader(new InputStreamReader(new FileInputStream(arg)));
                   line1 = d.readLine();
                   line2 = d.readLine();   
                     line3 = d.readLine();
                   Scanner s = new Scanner(line3);
                   width = s.nextInt();
                   height = s.nextInt();
          
           line4=d.readLine();
                   in= new String[height][width];
           for(int i=0;i<height;i++)
               in[i]=d.readLine().split(" ");
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
   }  
   public String get_image_pixel(int a,int b)
   {
           return in[a][b];
          
   }
   public void set_image_pixel(int a,int b)
   {
               in[a][b]="0";
      
   }
   public void display()
   {
           System.out.println(line1);
                   System.out.println(line2);
           System.out.println(line3);
           System.out.println(line4);
           for(int i=0;i<height;i++)
           {
               for(int j=0;j<width;j++)
               System.out.print(in[i][j]+" ");//im[i][j]+" ");
               System.out.println();
           }

   }
   public static void main(String args[]) throws Exception
   {
       pgmfile pg=new pgmfile();
       try
       {
       //   System.out.println(args[0]);
           pg.readFile(args[0]);
           
           while(true)
           {
               Scanner ss=new Scanner(System.in);
           System.out.println("Please enter the pixel value(x,y):");
               int x=ss.nextInt(); int y=ss.nextInt();
               if(x>height || y>width)break;
                   System.out.println(pg.get_image_pixel(x-1,y-1));
               pg.set_image_pixel(x-1,y-1);
           }
           pg.display();
     
       }
           catch(Throwable t)
       {
                   t.printStackTrace(System.err) ;
                   return ;
           }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote