The picture is given in a PPM file and your program should p
The picture is given in a PPM file and your program should put the converted one into another PPM file.
 •Use argv[1] for the given file and argv[2] for the converted file.In addition, you can use a temporary file called tmp.ppm.
 •The number of rows and columns are not fixed numbers.
 •The converted file should also follow the PPM format with the above simplification, and can be converted subsequently.
•Read the pixel matrix into a buffer.
 •For each row i( 1<=i < rows), use fork()to create a child process to calculate the new row between row i and i+1, and to write row i and this new row into the transformed file.
 •The parent process waits for the child process to terminate before processing the next row.
 •The original process will write the last row into the transformed file
•Let the given pixel matrix be rows*columns.
 The enlarged matrix should
 1 add one row between every two neighboring rows in the original matrix
 where the value of each new pixel is the average of the values of the two pixels in the neighboring rows and in the same column;
 2 analogously add one column between everytwo neighboring columns.
 •Change the header to reflect the change of the number of rows and number of columns.
The images to be transformed are given inPPM ASCIIformat
0.5: Proper code formatting (Proper indentation, comments where necessary, meaningful variable names)
 0.5: Used process control as described in the assignment (ie: Used fork(), child process calculates and writes to file, parent process waits for child process to finish)
 1.0: Program provides expected output for given test case, as well as for another hidden test case. This hidden test case will follow the same restrictions as mentioned in the assignment.
Solution
#include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
 #define SIZE 256
void writeP3 (FILE *inFile, FILE *outFile);
 void writeP6 (FILE *inFile, FILE *outFile);
//represents a single pixel object
 typedef struct RGB {
 unsigned char r, g, b;
 }RGBPix;
typedef struct Image {   //for storing image data int width, height;
 RGBPix *data;
 }PPMImage;
int main(int argc, char *argv[])
 {
 if(argc < 4 || argc > 4) {
 perror(\"usage: conversion-number in-file.ppm out-file.ppm \ \");
 }
char *outType;
 char fh[256];
   
 if(strcmp(argv[1], \"6\")) outType = \"P6\"; //specify type of conversion here
 else if(strcmp(argv[1], \"3\")) outType = \"P3\"; //error handling
 else {
 perror(\"Please specify the conversion type\  Enter 3 to convert to P3 or 6 to convert to P6\ \");
 return 0;
 }
//to check format of input file is ppm or not
 if(strstr(argv[2], \".ppm\") == NULL) {
 perror(\"Please provide a .ppm file for conversion\");
 return 0;
 }
 if(strstr(argv[3], \".ppm\") == NULL) {
 perror(\"Please provide a valid .ppm file name to write to\");
 return 0;
 }
 //open the files
 FILE *outFile = fopen(argv[3], \"wb\");
 FILE *inFile = fopen(argv[2], \"rb\");
//calling methods to read and write the data
 if(strcmp(outType,\"P6\")) writeP6(inFile, outFile);
 else writeP3(inFile, outFile);
//clean up
 (void) fclose(outFile);
 (void) fclose(inFile);
 return EXIT_SUCCESS;
 }
//write a new P6 file
 void writeP6 (FILE *inFile, FILE *outFile){
char buff[SIZE], *fh;
PPMImage image;
int read, i, counter = 1;
 unsigned int maxColors;
 fh = fgets(buff, SIZE, inFile); //Get the magic number first
 printf(\"%s\ \",buff );
 if ( (fh == NULL) || ( strncmp(buff, \"P3\ \", 3) != 0 ) ) perror(\"Please provide a P3 .ppm file for conversion\ \");
 (void) fprintf(outFile, \"P6\ \");
do
 {
 fh = fgets(buff, SIZE, inFile);
 if( strncmp(buff, \"#\", 1) == 0) fprintf(outFile, \"%s\", buff);
 printf(\"%s\",buff);
 if ( fh == NULL ) return;
 } while ( strncmp(buff, \"#\", 1) == 0 );
read = sscanf(buff, \"%u %u\", &image.width, &image.height);
if(read < 2) {
 perror(\"File Unreadable. Please check the file format\ \");
 return;
 }
//allocating memory for the image buffer
 image.data = (RGBPix *)malloc(sizeof(RGBPix) * image.width * image.height);
 //read in the max colors
 read = fscanf(inFile, \"%u\", &maxColors);
//check for 8 bit color representation
 if(maxColors != 255 || read != 1) {
 perror(\"Please provide an 24-bit color file\");
 return;
 }
fprintf(outFile, \"%u %u\ %u\ \",image.width, image.height, maxColors);
for (i = 0; i < image.width * image.height; i++)
 {
 int curVal;
 fscanf(inFile, \"%d\", &curVal);
 image.data[i].r = curVal;
 fscanf(inFile, \"%d\", &curVal);
 image.data[i].g = curVal;
 fscanf(inFile, \"%d\", &curVal);
 image.data[i].b = curVal;
}
 fwrite(image.data, 3 * image.width, image.height, outFile);
 }
void writeP3 (FILE *inFile, FILE *outFile){
char buff[SIZE], *fh;
PPMImage image;
int read, i, j, counter = 1;
 unsigned int maxColors;
 fh = (char *)malloc(sizeof(char) * SIZE);
 fh = fgets(buff, SIZE, inFile);   
 if ( (fh == NULL) || ( strncmp(buff, \"P6\ \", 3) != 0 ) ) perror(\"Please provide a P6 .ppm file for conversion\ \");
 (void) fprintf(outFile, \"P3\ \");
do
 {
 fh = fgets(buff, SIZE, inFile); //write the comments into the out file
 if( strncmp(buff, \"#\", 1) == 0) fprintf(outFile, \"%s\", buff);
 if ( fh == NULL ) return;
 } while ( strncmp(buff, \"#\", 1) == 0 );
read = sscanf(buff, \"%u %u\", &image.width, &image.height);
//error handling
 if(read < 2) {
 perror(\"File Unreadable. Please check the file format\ \");
 return;
 }
 image.data = (RGBPix *)malloc(sizeof(RGBPix) * image.width * image.height);
read = fscanf(inFile, \"%u\", &maxColors);
 
 if(maxColors != 255 || read != 1) {
 perror(\"Please provide an 24-bit color file\");
 return;
 }
 fprintf(outFile, \"%u %u\ %u\ \",image.width, image.height, maxColors);
//read the image into the buffer
 fread(image.data, sizeof(RGBPix), image.width * image.height, inFile);
for (i = 0; i < image.width * image.height; i++)
 {
 fprintf(outFile, \" %d %d %d \", image.data[i].g, image.data[i].b, image.data[i].r);
//format handling
 if(counter == image.width) {
 fprintf(outFile, \"\ \");
 counter = 1;
 }
 else counter += 1;
 }
}
![The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the conv The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the conv](/WebImages/37/the-picture-is-given-in-a-ppm-file-and-your-program-should-p-1110220-1761588519-0.webp)
![The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the conv The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the conv](/WebImages/37/the-picture-is-given-in-a-ppm-file-and-your-program-should-p-1110220-1761588519-1.webp)
![The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the conv The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the conv](/WebImages/37/the-picture-is-given-in-a-ppm-file-and-your-program-should-p-1110220-1761588519-2.webp)
![The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the conv The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the conv](/WebImages/37/the-picture-is-given-in-a-ppm-file-and-your-program-should-p-1110220-1761588519-3.webp)
