C program to create a binary ppm file
Assignment 2
Write a C program to create a binary ppm file with 1000 by 1000 pixels. It uses five input
colors to draw a picture. See sample runs. The input colors can be: red, green, blue, yellow,
orange, cyan, magenta, ocean, violet. The corners of the central part should use the 250th and
750th row/column.
Restrictions on the file format:
• no comment line after the file type specification
• width (number of columns) and height (number of rows) formatted in ASCII decimal in
the second line, separated by a space
• maximal value of color components given in the third line
Requirements on parallel execution:
• The original process should write the header information into the image file.
• Use fork() to create 10 child processes. Each child process can use about 300KB of
memory to calculate 100 rows and write the data into the image file.
• The original process does not work on the pixels. It creates the 10 child processes to do
so. To avoid having multiple child processes simultaneously accessing the image file,
the parent process simply waits for one child process to terminate before creating the
next.
Other requirements:
• You are not allowed to use standard I/O library functions or graphics related libraries.
• The command line arguments include the following in order: (i) the name of the image
file; (ii) color for center; (iii) color for top-left corner; (iv) color for top-right corner; (v)
color for bottom-left corner; (vi) color for bottom-right corner.
Advanced requirements (optional):
• The numbers of rows and columns of the image are not fixed numbers. They are also
command line arguments.
• The number of child processes is not a fixed number. It is also a command line
argument.
Sample runs:
>>>>> kindergarten drawing.ppm blue green red yellow magenta
>>>>> gimp drawing.ppm
>>>>> kindergarten drawing.ppm orange ocean yellow violet cyan
>>>>> gimp drawing.ppm
>>>>> kindergarten drawing.ppm orange ocean yellow violet cyan
>>>>> gimp drawing.ppm
Answer
/* Assignment 2 Description: uses five input colors to draw a picture */ #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> char colorArr[5][3]; //grobal array void determineColor(int i,char *color[]); //function to determine color int main(int argc, char *argv[]){ int i, status, width, height, pid; int sizeA, sizeB; char header[] = ("P6\n1000 1000\n255\n"); // header contain first 3 lines int fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0755); // opens file // parse arguments for(i = 0; i<5; i++){ determineColor(i,&argv[i+2]); } write(fd, &header, 17); // write header //create 10 children to into file for(i=1; i <= 10; i++){ pid = fork(); if(pid > 0) { //parent wait(&status); // waits for child to finish }else if(pid == 0){ // child for (height = (i-1)*100; height <= (i*100); height++){ for (width = 1; width <= 1000; width++){ int center = 0; // flag // coloring center if (height >= 250 && height <= 750 && width >= 250 && width <= 750){ if (height <= 500){ sizeA = 750 - height; // finds the position wheres the center starts for each row sizeB = 250 + height; // finds the position wheres the center ends for each row if (width >= sizeA && width <= sizeB){ write(fd,&colorArr[0],3); // color 1 center = 1; } } else if (height > 500){ sizeA = height - 250; // finds the position wheres the center starts for each row sizeB = 1250 - height; // finds the position wheres the center ends for each row if (width >= sizeA && width <= sizeB){ write(fd,&colorArr[0],3); center = 1; } } } // coloring top-left / top-right if (height <= 500 && !center){ if (width <= 500){ write(fd,&colorArr[1],3); // color 2 }else{ write(fd,&colorArr[2],3); // color 3 } } // coloring bottom-left / bottom-right else if (height > 500 && !center){ if (width <= 500){ write(fd,&colorArr[3],3); // color 4 }else{ write(fd,&colorArr[4],3); // color 5 } } } } exit(0); //terminate child when finish writing } } return 0; } // determine color, and records it into colorArr void determineColor(int i,char *color[]){ // The input colors can be: red, green, blue, yellow, orange, cyan, magenta, ocean, violet if (strcmp(*color,"red") == 0){ colorArr[i][0]= 255; colorArr[i][1]= 0; colorArr[i][2]= 0; }else if (strcmp(*color,"green") == 0){ colorArr[i][0]= 0; colorArr[i][1]= 255; colorArr[i][2]= 0; }else if (strcmp(*color,"blue") == 0){ colorArr[i][0]= 0; colorArr[i][1]= 0; colorArr[i][2]= 255; }else if (strcmp(*color,"yellow") == 0){ colorArr[i][0]= 255; colorArr[i][1]= 255; colorArr[i][2]= 0; }else if (strcmp(*color,"orange") == 0){ colorArr[i][0]= 255; colorArr[i][1]= 165; colorArr[i][2]= 0; }else if (strcmp(*color,"cyan") == 0){ colorArr[i][0]= 0; colorArr[i][1]= 255; colorArr[i][2]= 255; }else if (strcmp(*color,"magenta") == 0){ colorArr[i][0]= 255; colorArr[i][1]= 0; colorArr[i][2]= 255; }else if (strcmp(*color,"ocean") == 0){ colorArr[i][0]= 0; colorArr[i][1]= 71; colorArr[i][2]= 171; }else if (strcmp(*color,"violet") == 0){ colorArr[i][0]= 238; colorArr[i][1]= 130; colorArr[i][2]= 238; } }
Leave a reply