Question
COKKE.PNG PLEASE IN C ONLY In this assignment you will use arrays and loops to create ASCII art from a PNG image. PNG Image Reading
COKKE.PNG
PLEASE IN C ONLY
In this assignment you will use arrays and loops to create ASCII art from a PNG image. PNG Image Reading The first thing you will need to do is to read in a PNG image from a file. This is a somewhat complex process and so I am providing you code that does the reading for you. Use the lodepng.cPreview the document and lodepng.hPreview the document files provided. Place both these files into the same directory as your main .c code. You should not have to modify (or even open) these files. You will, however, need to include them so that your code can access its functionality. To do this properly, you need to do 2 things. The first is to include the .h file: #include "lodepng.h" This goes at the top of your code with your other #includes. Note that you should use double quotes rather than angle brackets since this is a file in your local directory rather than a standard system file we are including. The other part is to tell the compiler to also compile the lodepng.c file and link it together with your .c file to form the executable. Do this by adding lodepng.c to the add_executables line of the CMakeLists.txt file in your project: add_executable(asciigenerator main.c lodepng.c) Next, you will need to use the lodepng code to read in the raw bytes of a PNG image. Use the following code: unsigned int error; // unsigned just means the int can't be negative unsigned char* rawImageBytes; // bytes are just unsigned chars (0..255) // and the * is similar to saying [] - that is, it // is array but we just don't know the size yet unsigned int width, height; // can create space for 2 ints in one line // This function from lodepng will read in the image and fill in the bytes and size error = lodepng_decode32_file(&rawImageBytes, &width, &height, filename); if (error) { // If there was an problem reading in the image then display the error and quit printf("error loading: %s ", lodepng_error_text(error)); return 1; } //the pixels are now in the char array "rawImageBytes", // 4 bytes per pixel, ordered RGBARGBA..., row-major order filename should be a string (i.e. char array) you create first with the location of the file. It is probably best to use relative pathnames. Since your executable file is placed and run from the cmake-build-debug folder, you will need to make all pathnames relative to this directory. So, if you place cookie.png in the same directory as your .c code, then this would be ../cookie.png. Note that on windows machines you should use \\ instead of / between directories in your pathname. Note that for my examples below I will be using the cookie.png file provided to you. Once this code is finished, you will have the image information in 3 variables. Width and height will be the width and height of the image in pixels. For cookie.png this should be 225 x 225. rawImageBytes will hold all the bytes that make up the image. This is stored in a 1d array. It is stored in row-major order, meaning that the first row is stored at the beginning of the array, the next row is stored next, etc. And for each pixel in the PNG, there are 4 bytes stored: red, green, blue and alpha. We will want the red, green and blue information but we wont care about the alpha (transparency) information for this assignment. Working with the image in its 1d array of raw byes will not be convenient for us in later steps. Thus, your next step is to transform the raw byte information into a 2d array of gray-scale values. Do this by first creating a 2d array of the correct width and height. Next use nested loops to run through each of the 2d values. Inside the loops you will select the associated red, green and blue values from the raw bytes vector. Since we want only a gray-scale value, we will average the red, green and blue values to form a single intensity value which will be stored in the 2d array. I wont give every gray-scale value for cookie.png since there are over 50k of them, but the correct value for location 50,50 is 114. It would be a good idea to check this particular value to make sure you have this step working ASCII Art Creation After you have created the gray-scale array it is time to create a smaller 2d array for ASCII art creation. Clearly we will not use 1 symbol per pixel as that would be way too many symbols. We will have a scale factor that represents how many pixels per symbol we will use. For my example below I picked 5 for this scale factor. That is, each 5x5 grid of pixels forms a single symbol in our final output. Thus, the first step in this process is to create a smaller 2d array of the correct scaled size. The scaled array should be filled with average gray-scale intensity values from each 5x5 grid of pixels it is representing. Just like when you made the previous array, you should use a nested loop structure to fill in every location in the scaled array. However, inside this loop you will need another nested loop to walk through the grid of pixels to compute their average. This will give you an average intensity for every location in the scaled array. However, we also want to eventually replace these values with symbols and we are not going to use 255 different symbols. We will be using only 9 symbols. Thus, you need to divide your average intensity values (in the range 0..255) into 9 different values (0..8). Once you have created the scaled 2d array, you should write a nested loop to print out all the values in the array. You can see my results below for cookie.png. Im providing this so you know if your code is working properly or not. And lastly, create another set of nested loops to display the image again, but this time replacing each of the 9 values with the correct symbol. We will use the following 9 symbols: char grayLevels[] = {'@', '#', '8', '&', 'o', ':', '*', '.', ' '}; These symbols were selected to match darkest to lightest. You can see my results for cookie.png below. Cookie.png Example Cookie.png: cookie.png The scaled 9-level intensity 2d array: 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8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 3 3 3 3 3 3 3 3 2 5 8 8 8 8 8 8 8 8 8 8 8 7 1 2 2 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 2 1 6 8 8 8 8 8 8 8 8 8 8 8 8 5 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 1 7 8 8 8 8 8 8 8 8 8 8 8 8 8 6 4 1 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 4 6 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 5 1 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 1 1 5 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 6 5 4 1 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 2 4 6 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 7 5 2 1 1 2 3 3 3 3 3 3 3 3 3 2 1 1 1 4 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 7 6 5 2 1 1 1 1 1 1 1 1 1 2 5 5 6 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 7 6 5 5 5 5 5 4 4 5 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
The final ACSII art: : & o o o & . * * . & * o : . & o * * o & . & . o * . & o * * : & & . : . & . & o * . @ @ @ * . o & : 8 o * & @ @ @ & & & * @ @ @ o . o @ @ @ o & 8 o . & o @ @ @ 8 : & # & & . : # & 8 # o . @ @ @ : # . & o 8 8 8 & & & 8 * . o * & & & o 8 . 8 8 & & & & & & & 8 o . 8 & & 8 : * 8 & 8 8 8 8 & . & @ & & & & & & & & & 8 & & 8 8 & & & & 8 & . . o 8 & & & & & & 8 # # # 8 & & & & & & & & & & & & & & & & & & & & & 8 8 8 8 & & & & & & & & 8 8 . . 8 & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & o # & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & # . . # & 8 & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & # . & & & @ 8 & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & 8 o 8 & & & 8 8 & & & & 8 8 & & & & & & & # & & # 8 & & & & & & & & & & & & & & & & & & # . # & & & & 8 @ # @ @ @ 8 8 @ # & & # @ 8 # @ @ @ # & # 8 & & & & & & & & & & 8 8 & @ o * @ 8 & & & & & @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ # & & 8 8 & & & & 8 # 8 & 8 * . 8 & & & & & & # @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ # # & & & & o & & & & & & & & @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ # & & & & & & & o & & & & & & & @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ & & & & & & @ & : 8 & & & & & & # @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ # & & & & & & 8 : * 8 & & & & & & & @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ 8 & & & & & & & # * o & & & & & & & & @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ & & & & & & # # # * # # # & & & & & & 8 @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ # & & & & & & # : * . & o # & & & & & & & @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ & & & & & & & 8 . . . 8 & & & & & & & 8 @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ 8 & & & & & & & 8 o 8 & & & & & & & # @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ & & & & & & # # : * # & & & & & & & & @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ & & & & & & & # : . . & # & & & & & & & & @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ 8 & & & & & & # : * 8 & & & & & & & & & # @ @ @ @ @ @ @ @ @ @ @ @ @ @ & & & & & & & & 8 . o 8 & & & & & & & & & & # @ @ @ @ @ @ @ @ @ # 8 & & & & & & & & 8 : . # 8 8 & & & & & & & & & & & & 8 8 8 8 & & & & & & & & & & & 8 # * : # # & & & & & & & & & & & & & & & & & & & & & & & & & & & @ # . * o # & 8 & & & & & & & & & & & & & & & & & & & & & & & # o * : # # # & & & & & & & & & & & & & & & & & & & @ # # : . * : o # 8 8 & & & & & & & & & & & & & & & # 8 o * . : 8 # # 8 & & & & & & & & & 8 # # # o . . * : 8 # # # # # # # # # 8 : : * . . * : : : : : o o : .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started