Question
Background: Images on computer screens are composed of many tiny dots, called pixels, which can take on a range of colors. The simplest way to
Background: Images on computer screens are composed of many tiny dots, called pixels, which can take on a range of colors. The simplest way to store an image is through bitmapping, where each pixel is represented by some number of bits, and these collections of bits are basically stored in a 2D array that maps directly to the image. For example, the below 2x3 array of color values would represent something like the image to the right (but the pixels would be much, much smaller when displayed on a modern screen). This idea of an image being an array of pixels is what we mean when, for example, we say an image is 256x256 pixels. Similarly, when we call a display "4K," we're saying it is approximately 4000 pixels wide. The next question is, how do we represent the colors? What value do we store to indicate "red" or "orange?" The most popular method of color representation is RGB. This represents a color as a combination of a red component (R), a green component (G), and a blue component (B). In a typical system, we use one byte for each component. Therefore, the values for the components come from the range [0:255]. The higher the value, the more that color shows up in the final pixel color. Think of mixing paint: the proportion of each color of paint that you put in the mixture determines the final color. Interestingly (and perhaps not coincidentally), this digital representation of separating the RGB components is very similar to how these colors are actually, physically, created in many types of displays. For some examples, if we write a color in the form R.G.B., a plain blue would be 0.0.255 (zero red, zero green, and 255 blue). Black is 0.0.0, white is 255.255.255, and 127.127.127 is a shade of gray halfway between white and black. Play around with a colorpicker to better understand how the R, G, and B components combine to create the final color. Assignment: Part 1 (60 pts): 1. Create a new project with img.c, attached to this assignment. Also download cy.bmp and place it in the same folder where your project's img.c is saved. If you're using Visual Studio, you'll also need to add pch.h or stdafx.h to img.c. 2. Look through the code of img.c. First find the main() function. It opens an input file (cy.bmp) and an output file (cy2.bmp). The file cy.bmp is an image stored in an uncompressed, bitmapped format. The main() function then calls load_image(). This function extracts the pixel information of the image and stores it in three arrays: r_in, g_in, and b_in. Each of these arrays is two-dimensional. The first dimension is the row in the image, and the second dimension is the column in the image. Array r_in stores the red information for each pixel in the image, and similar for green and blue in g_in and b_in. The pixel information is also copied to a second set of arrays: r_out, g_out, and b_out. The next section of main() is labeled as "filter code." You will write code here later. Finally, main() calls write_image(). This function writes the image defined by r_out, g_out, and b_out to the output file. 3. Compile and run img.c. It should create an identical copy of cy.bmp, called cy2.bmp. Open cy2.bmp with a photo viewer program to verify that it looks like cy.bmp. Additional instructions for XCode: you'll probably find that it won't let you write to cy2.bmp (you'll get an lldb error at runtime). You can get around this by changing the location of the file. One file location that will definitely work is /tmp, the system temporary directory. At the top of img.c, change input_file_name and output_file_name to "/tmp/cy.bmp" and "/tmp/cy2.bmp". You can open "/tmp" in Finder by pressing command-shift-G in a Finder window. 4. Now you are ready to write an image filter! The basic idea is that, in the area marked for "filter code," you can modify the pixel information in r_out, g_out, and b_out, and your changes will be written as a viewable image in cy2.bmp. For your first filter, try the following to convert the image to grayscale: a. First, we'll need to do something for each pixel. This means we want to touch each column in each row. Try an outer for loop that iterates over each row, and an inner for loop that iterates over each column. Note that you access, for example, row row and column col of the red output array as follows: r_out[row][col] b. For each pixel, find the maximum of the three color values in the input arrays, i.e. the max of r_in[row][col], g_in[row][col], and b_in[row][col]. Set the value of all three color components to that max in the output arrays. c. That's it! Compile and run, and your cy2.bmp should be a grayscale version of cy.bmp. Show the TA your grayscale filter.
IMPORTANT THINGS :# just need to write the filter part code and using c program pls.
#include
#include
#include
#define IMG_ROWS 900
#define IMG_COLS 900
char input_file_name[] = "cy.bmp";
char output_file_name[] = "cy2.bmp";
uint16_t bpp;
uint32_t pixel_arr_loc;
unsigned int r_in[IMG_ROWS][IMG_COLS];
unsigned int g_in[IMG_ROWS][IMG_COLS];
unsigned int b_in[IMG_ROWS][IMG_COLS];
unsigned int a_in[IMG_ROWS][IMG_COLS];
unsigned int r_out[IMG_ROWS][IMG_COLS];
unsigned int g_out[IMG_ROWS][IMG_COLS];
unsigned int b_out[IMG_ROWS][IMG_COLS];
unsigned int a_out[IMG_ROWS][IMG_COLS];
int load_image(FILE *fp) {
unsigned char signature[2];
uint32_t file_size;
uint32_t dib_size;
uint16_t comp_method;
if(fp == NULL) {
printf("Unable to open file. ");
return 1;
}
fread(signature, 1, 2, fp);
printf("sig %c %c ", signature[0], signature[1]);
if(signature[0] != 'B' || signature[1] != 'M') {
printf("Not a valid BMP file. ");
return 1;
}
fseek(fp, 2, SEEK_SET);
fread(&file_size, 4, 1, fp);
printf("file size %u ", file_size);
fseek(fp, 10, SEEK_SET);
fread(&pixel_arr_loc, 4, 1, fp);
printf("pixel array location %u ", pixel_arr_loc);
fseek(fp, 14, SEEK_SET);
fread(&dib_size, 4, 1, fp);
printf("dib size %u ", dib_size);
if(dib_size != 40) {
printf("BMP version not supported. ");
return 1;
}
fseek(fp, 28, SEEK_SET);
fread(&bpp, 2, 1, fp);
printf("bits per pixel %u ", bpp);
if(bpp != 32) {
printf("Bits per pixel not supported. ");
return 1;
}
fseek(fp, 30, SEEK_SET);
fread(&comp_method, 2, 1, fp);
printf("compression method %u ", comp_method);
if(comp_method != 0) {
printf("Compression method not supported. ");
return 1;
}
int bytes_per_row = IMG_COLS * bpp / 8;
int padding = 0;
if(bytes_per_row % 4 != 0) {
padding = 4 - bytes_per_row % 4;
}
printf("input padding %u ", padding);
fseek(fp, pixel_arr_loc, SEEK_SET);
int row;
int col;
for(row = IMG_ROWS - 1; row >= 0; row--) {
for(col = 0; col < IMG_COLS; col++) {
fread(&b_in[row][col], 1, 1, fp);
fread(&g_in[row][col], 1, 1, fp);
fread(&r_in[row][col], 1, 1, fp);
fread(&a_in[row][col], 1, 1, fp);
b_out[row][col] = b_in[row][col];
g_out[row][col] = g_in[row][col];
r_out[row][col] = r_in[row][col];
a_out[row][col] = a_in[row][col];
fseek(fp, padding, SEEK_CUR);
}
}
return 0;
}
int write_image(FILE *out, FILE *in) {
int c;
rewind(out);
rewind(in);
c = fgetc(in);
while(c != EOF) {
fputc(c, out);
c = fgetc(in);
}
int bytes_per_row = IMG_COLS * bpp / 8;
int padding = 0;
if(bytes_per_row % 4 != 0) {
padding = 4 - bytes_per_row % 4;
}
int row;
int col;
printf("output padding %u ", padding);
fseek(out, pixel_arr_loc, SEEK_SET);
for(row = IMG_ROWS - 1; row >= 0; row--) {
for( col = 0; col < IMG_COLS; col++) {
fwrite(&b_out[row][col], 1, 1, out);
fwrite(&g_out[row][col], 1, 1, out);
fwrite(&r_out[row][col], 1, 1, out);
fwrite(&a_out[row][col], 1, 1, out);
fseek(out, padding, SEEK_CUR);
}
}
return 0;
}
int main() {
FILE *in = fopen(input_file_name, "rb");
FILE *out = fopen(output_file_name, "wb");
load_image(in);
/*------------Start of filter code-------------*/
/*------------End of filter code-------------*/
write_image(out, in);
fclose(in);
fclose(out);
}
Annotations
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