In this programming assignment, you will be writing a program that creates a ppm
ID: 3530476 • Letter: I
Question
In this programming assignment, you will be writing a program that creates a ppm image. When you view the created image with gimp (or an image editor that allows ppm type images), you should see two colored circles both having the same center on a colored background. The circles may or may not appear in the overall center of the image. The inner circle is composed of randomly generated colored pixels. The image above is an example. This is not an interactive program, all input should be read from a file by redirecting the input. The output should also be captured by redirecting the output to a file. The input specifications, line by line, are two integers representing the width and height of image. two integers representing the coordinates of the center of the circles. an integer representing the radius of the inner circle. an integer representing the radius of the outer circle. three integers representing the color components of the outer circle. three integers representing the background color. The output, if we continue to use the P3 image type is P3 #A comment about the image width height max color code remaining lines are the color components of each pixel starting in the top left corner of the image. Each color component of a pixel is on a line by itself, no spaces. To save disk space, we really should use the P6 image - unsigned character version of the color components. Again, the output file specifications, line by line, are P6 #A comment about the image width height max color code remaining lines are the color components of each pixel starting in the top left corner of the image. Again, output unsigned characters. The P6 image format is about 1/3 the size of a P3 image format. As noted, each pixel is stored as 3 chars and not three integers. For the create_ppm_ascii.c, the image it created was 76384 bytes in size. The exact same image in raw format was 22,534 bytes in size. Also note, if you vi into the raw image, you will only be able to read the header lines, the color componts will be viewable but not human readable. See create_ppm_raw.c for an example of the same program as create_ppm_ascii.c but using unsigned chars. See class notes and programming example for details. The following input data was used to create the above image: 200 200 100 100 50 100 0 0 100 50 50 175 Note: If the pixel is on the border of a circle, it is colored based on the color of the circle. Here is another example, please note that the center does not have to be on the image. The data that generate this image: 400 200 300 250 150 200 250 175 10 250 20 2 Comments: Please break the problem into several smaller parts. Think about the problem on pencil and paper to make sure your understand the problem. Review the input and see if you can match the input with the example images. Once you think you understand the problem, write the code on paper before you sit in front of a machine to enter the program. Let me encourage you to complete this before the break. We will spend the class on Friday working on the program. Work on a pencil/paper version before class and come in with questions. Like in the class example, the idea is to determine where each pixel in the image is located as it relates to the center of the circles. If the pixel is within the inner circle, then color it appropriately. If not in the inner circle, then check to see if it is in the outer circle, etc. Therefore, to solve this problem, you will need the distance formula and thus need to use the square root function. Here is a sample of code using the sqrt function. Don't forget to #include math.h. When you compile, use gcc sqrt.c -lm The -lm tells the compiler to link in the math libraries. ======================================================================== /* sqrt example */ #include #include //need this for the sqrt function. int main(void) { double param, result; param = 16.5; result = sqrt(param); printf("sqrt(%lf) = %lf ",param,result); return 0; } Output: sqrt(16.5) = 4.06202 ======================================================================== You will also need to use the random number generator function rand(). The function rand() returns a random number between 0 and RAND_MAX. RAND_MAX on our system is a number between 0 and 2^31-1 = 2147483647. The idea is to use the rand() function to randomly assign a color components to a pixel. But wait you say, there is a problem. What is the range of acceptable values for a color component? How can you use rand() to insure that the color component is within the range? See the code sample below for a suggestion. ======================================================================== /* random number generator example */ #include#include //need this for the rand() function /* The following generates 10 random numbers between 1 and 100. */ int main(void) { int i; int y; for(i=0;i<10;i++) { y=rand()0+1; printf("%d ", y); } printf(" "); return 0; }Explanation / Answer
http://www.dreamincode.net/forums/topic/252078-c-program-to-create-four-color-ppm-image/
http://cs.uky.edu/~paulp//CS115S11/programs/program4/ppm-disc.html
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.