Question

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.

A sample file is given for testing.The following images are from this sample and its first transformation

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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 \n");
}

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\n Enter 3 to convert to P3 or 6 to convert to P6\n");
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\n",buff );
if ( (fh == NULL) || ( strncmp(buff, "P3\n", 3) != 0 ) ) perror("Please provide a P3 .ppm file for conversion\n");
(void) fprintf(outFile, "P6\n");

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\n");
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\n%u\n",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\n", 3) != 0 ) ) perror("Please provide a P6 .ppm file for conversion\n");
(void) fprintf(outFile, "P3\n");

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\n");
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\n%u\n",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, "\n");
counter = 1;
}
else counter += 1;
}

}

Add a comment
Know the answer?
Add Answer to:
The picture is given in a PPM file and your program should put the converted one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • System Programming in C

    Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.#include  #include  #include  #include  int main(int argc, char *argv[]){ int row = 0; int column = 0; row = atoi(argv[1]); column = atoi(argv[2]); int *A = ...

  • Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix...

    Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...

  • Write the following C++ program that searches for specified words hidden within a matrix of letters....

    Write the following C++ program that searches for specified words hidden within a matrix of letters. 1) Read a file that the user specifies. The file will first specify the dimensions of the matrix (e.g., 20 30). These two numbers specify the number of rows in the matrix and then the number of columns in the matrix. 2) Following these two numbers, there will be a number of rows of letters. These letters should be read into your matrix row...

  • Your program should be capable of creating a ppm image of the flag of Benin • The new flag gener...

    Your program should be capable of creating a ppm image of the flag of Benin • The new flag generated should use the proper width-to-height ratio as defined on Wikipedia for the flag. o For the colors, use pure red, and yellow, and a value of 170 for green. ▪ Pure color values are talked about in Lab 6. o The left green field should be 6/15ths of the width of the flag. Variables country_code should also include a new...

  • problem2, the file "Test_Data.xlsx" contains in cylinder pressure data from a firing engine. the data is...

    problem2, the file "Test_Data.xlsx" contains in cylinder pressure data from a firing engine. the data is resolved in crank angle degree and is taken every 1/2 degree. O tittps/ualearn blackboard.com/bbcswebdav/pid plot a new function on the same graph overwriting the orizinal function 1 of11ρ Functions to choose from: y A sin(Bx C)+ D y A sin2(Bx+ C) + D y A In(Bx + C)D y A exp(Bx + C)+ D ž. The file "Test Data.xlsx" contains in-cylinder pressure data from...

  • use MATLAB to upload the following: an image that you want to process (can be taken...

    use MATLAB to upload the following: an image that you want to process (can be taken yourself or downloaded from the internet) a script that processes the image in TWO ways. manipulates the colors averages pixels together Please make sure the script displays the images (like how I did with the 40 and 80 pixel averaging) so I can easily compare them to the original. Make sure to COMMENT your code as well. Homework 13 Please upload the following: an...

  • 1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically...

    1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically test both algorithms of the matrix math program for each of the array sizes shown in Table. Also, extend your script to automatically parse the output of the program. Table (Array Sizes in Test Suite) Algorithm 1 Algorithm 2 256 256 512 512 768 768 1024 1024 1280 1280 1536 1536 1792 1792 2048 2048 C source file // Adapted from https://gustavus.edu/+max/courses/F2011/MCS-284/labs/lab3/ // Max...

  • Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in...

    Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in a file. Your program should prompt the user for the name of the file to use and then request values be entered until the user is done. This data should be written to two files: a text file, user_file_name.txt and a comma separated value file, user_file_name.csv. To help you with decision making, we suggest that you ask the user to input data representing students’...

  • Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

  • the picture above is the one you are supposed to use for the MATlab code please...

    the picture above is the one you are supposed to use for the MATlab code please help Problems. Grayscale images are composed of a 2D matrix of light intensities from O (Black) to 255 (White). In this lab you will be using grayscale images and do 2D-array operations along with loops and iflelse branches. 1. We can regard 2D grayscale images as 2D arrays. Now load the provided image of scientists at the Solvay Conference in 1927 using the imread)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT