Question: Background. Before computers and printers supported fancy graphics, and in fact even before computers, people have used typeset letters to create messages and even art
Background. Before computers and printers supported fancy graphics, and in fact even before computers, people have used typeset letters to create messages and even art (Links to an external site.)Links to an external site.. In this assignment, you will develop a program called Draw that creates images using only a handful of ASCII characters. Your program will create an image by reading a sequence of instructions and progressively building up an image. Your program supports three draw operations:
Draw a point - Adds a single point.
Draw a rectangle - Adds a filled rectangle.
Flood fill an area - Floods an area with a color.
Here is an example of the final output of the program for one particular data file:
% ./Draw < plus.txt +---------+ | @....| | o @..o.| | @....| |@@@@@@@@@| |oooo@OOOO| |oooo@OOOO| |oooo@OOOO| +---------+ Color range [0.10, 1.00], average 0.5524
Getting started. Download the file draw.zip. This contains a stub program Draw.c containing a completely functional main function. The main function handles reading in the data file and calling various functions that create the image, display it, and calculate the final line of statistics. There are also 6 functions declared in Draw.c. Your job is to add the correct parameters to each function and to implement the body of each of the functions. Rules:
Do NOT change the main function.
Do NOT use any global variables (i.e. a variable declared outside the body of any function).
Do NOT change the name of the 6 functions.
Do NOT change the return type of the 6 functions.
You may add additional helper functions of your own if you want (but you are not required to).
Program input. Your program is controlled by a file redirected to the program as standard input. While the provided main function handles reading this for you, it will be helpful to understand the format. Here is the data file for the above image:
% cat plus.txt 9 7 r 4 -10 1 30 1.0 r -10 3 30 1 1.0 p 1 1 0.5 p 7 1 0.5 p 1 5 0.5 p 7 5 0.5 f 0 0 0.1 f 8 0 0.3 f 0 6 0.5 f 8 6 0.7
The first line specifies that the image will be 9 characters wide by 7 characters tall. The main program assumes images are at least 1 character wide and 1 character tall. As is typical in computer graphics (and unlike graphs in math), we will treat (0, 0) as the top-left coordinate. The x-coordinate increases as you go right. The y-coordinate increases as you go down. The r lines draw a rectangle. After the r comes the left (x) and top (y) coordinate of the rectangle. This is followed by the rectangle's width and height. Finally the 1.0 specifies the greyscale color of the rectangle. Note that while both rectangles have a top-left coordinate outside the image's area of (0, 0) - (8, 6), the program still works and doesn't crash. The p lines draw a single point at the specified x- and y-coordinate with the given color. The f lines cause a flood fill that starts at the specified coordinate. A flow fill spreads out from this coordinate, changing pixels to the specified color. The flood fill stops when it hits a pixel that is at least as dark as the color of the flood fill. For example in the above example, the 4 flood fills only fill one of the 4 regions made by the plus since the plus is the darkest 1.0 greyscale color. Data format. The image is stored as a two-dimensional array of double values. Whenever the main program calls one of your functions, it passes the width, height, and the 2D array as the first three parameters to your function. Depending on the function, other additional parameters may be passed in (see the provided main program). The value of an element in the array represents the greyscale color of a pixel. You can assume all greyscales value will be between 0.0 and 1.0 (inclusive of 0.0 and 1.0). A greyscale value is converted to an ASCII character as follows:
If greyscale is in [0.0, 0.2), output as space
If greyscale is in [0.2, 0.4), output as .
If greyscale is in [0.4, 0.6), output as o
If greyscale is in [0.6, 0.8), output as O
If greyscale is in [0.8, 1.0], output as @
Functions. You need to implement 6 functions. All functions in the stub code have an empty parameter list. You need to figure out the correct order and types based on the provided main program. You can name your function parameter's whatever you like, but you should choose descriptive names. You should provide a comment at the top of each function you implement. While no specific format is required, the comment should clearly describe what the function does and the input it requires. This is a good habit to get into, it makes it much easier for others (and yourself!) to understand the code in the future. We now list the details of each function in the order you (probably) want to implement them:
initImage - The main program creates the 2D array, but does not initialize the array elements. Thus the array may contain just random garbage. Initializing the array elements is the job of this function. We assume the default color of pixels in our image is 0.0.
printImage - Prints out the image. The image has a border that uses + (plus) for the four corners, - (dash) for the top and bottom, and | (vertical bar) for the sides. As mentioned previously, the top and leftmost pixel is the element [0][0] in the image array. Pixels are displayed according to the greyscale rules given above.
drawPoint - Draws a point at the specified location with the specified color. drawPoint may get called with coordinates that are outside the image area. In this case the function should not do anything (but it shouldn't crash!). By "draw", we mean you update value(s) in the 2D pixel array. This function doesn't actually print anything out to the screen, that's the job of printImage.
drawRectangle - Draws a filled rectangle. The function is passed the leftmost (x-coordinate) and topmost (y-coordinate) of the rectangle. This is followed by the width and height of the rectangle. drawRectangle may get called with a parameters that place part (or all) of the rectangle outside the image area. The function should still end up coloring any pixels that are in the image area. You probably should consider calling drawPoint in drawRectangle to simplify this function and avoid repeated code. By "draw", we mean you update value(s) in the 2D pixel array. This function doesn't actually print anything out to the screen, that's the job of printImage.
getImageStats - Calculates the minimum and maximum greyscale value of any pixel in the image. Also computes the average greyscale value. Since this function effectively returns three different things, you will need to use pointers. Note this function simply does the calculation. The main program handles doing the actual printing of the statistics line.
floodFill - Floods the image with a specified greyscale color starting at a certain location. By far the easiest way to implement this is via recursion. Not including comments, blank lines, and curly braces, our solution is just 8 lines of code! Flooding spreads in the 4 cardinal directions (straight up, straight down, straight left, straight right). Flooding does NOT spread diagonally. Flooding stops if it encounters a color that has a greyscale value that is greater than or equal to the color of the flood fill. If floodFill is initially called on a pixel that has a greyscale value greater than or equal to the color of the flood fill, no fill is performed.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
