ECE 272 Organization of Computers Spring 2022ECE 2220 System Programming Concepts4 of 4 Lab 6 – Arrays, Strings, Pointers, Structures, and Files Objectives In this lab, each student is to write a...

1 answer below »

ECE 272 Organization of Computers
Spring 2022    ECE 2220 System Programming Concepts    4 of 4
Lab 6 – A
ays, Strings, Pointers, Structures, and Files
In this lab, each student is to write a program called p6.c that allows the user to operate on bitmap images. The student should gain and understanding of:
· Use of Pointers to Structures
· File Operations
· Use of Dynamic Memory Allocation
· Manipulation of Two-Dimensional A
· Use of Command Line Arguments
· String manipulation
The program should receive as command line arguments the operation to perform, the name of an input file, and the name of an output file if the command is read. The two possible operations are read and edge. For example,
prog6 read ImageFile OutputFile
prog6 edge ImageFile
The input file is to be a 24-bit color bitmap image of the windows .bmp format.
A 24-bit color “.bmp” file has two parts to its header describing the image file. The first header has the following structure:
struct HEADER
{ unsigned short int Type; /* Magic identifier *
unsigned int Size; /* File size in bytes *
unsigned short int Reserved1, Reserved2;
unsigned int Offset; /* Offset to data (in B) *
} Header; /* -- 14 Bytes -- *
The second part of the header has the following structure:
{ unsigned int Size; /* Header size in bytes *
int Width, Height; /* Width / Height of image *
unsigned short int Planes; /* Number of colour planes *
unsigned short int Bits; /* Bits per pixel *
unsigned int Compression; /* Compression type *
unsigned int ImageSize; /* Image size in bytes *
int xResolution, yResolution;/* Pixels per meter *
unsigned int Colors; /* Number of colors *
unsigned int ImportantColors;/* Important colors *
} InfoHeader; /* -- 40 Bytes -- *
Note: This structure is not “packed” in the file so the structure size might be larger than the sum of the structure elements. Therefore, you should access fields individually.
The actual image information follows as groups of three bytes representing the color of each pixel in RGB format. The pixels are stored as rows of columns just as a two-dimensional matrix is stored in C. 24-bit RGB format specifies the intensity of Red, Green, and Blue primary colors as a group of three bytes.
I will leave it to the student to determine whether Red is contained in the most significant byte of the least by reading example files I have created.
Since “three” is generally not a “good” computer number, the 24-bit pixel data is padded with a certain number of bytes equaling zero at the end of each row of RGB data if the number of columns times three does not equal a “nice” number. Therefore, when reading the data for each row, it may be necessary to read these padded “dummy” bytes. It will also be necessary to add these “dummy” bytes when creating a new image file.
I will leave it to the student to determine how many, if any, bytes are used to pad each row of data. To do this, I supply several different image examples with various numbers of columns. Reading in the pixel data will exhibit how this padding is done for various column sizes.
When the read command is supplied as an argument, the program is to read in header information and then use it to determine the number of rows and columns to then read in the pixel data of the image.
The program should then output to the output file given as an argument the name of the input file, all of the header data values, and the number of padded bytes. It should also print out each individual byte of the header data, and then print out the RGB data for each pixel denoting the row and column. Any padding should also be printed out.
For example, when reading the file OrangeSquare(97x97).bmp, the output file should look similar to the following:
Header.Type = B
Header.Type = M
Header.Size = 28378
Header.Offset = 54
InfoHeader.Size = 40
InfoHeader.Width = 97
InfoHeader.Height = 97
InfoHeader.Planes = 1
InfoHeader.Bits = 24
InfoHeader.Compression = 0
InfoHeader.ImageSize = 28324
InfoHeader.xResolution = 11811
InfoHeader.yResolution = 11811
InfoHeader.Colors = 0
InfoHeader.ImportantColors = 0
Padding = 1
Byte[0] = 066
Byte[1] = 077
Byte[2] = 218

Byte[51] = 000
Byte[52] = 000
Byte[53] = 000

Padding[0] = 000

When the edge command is supplied as an argument, the program is to read in header information and then use it to determine the number of rows and columns to then read in the pixel data of the image. The data is to be store it in a dynamically allocated two-dimensional a
ay of pixels where each pixel is stored as the following structure:
struct PIXEL { unsigned char Red, Green, Blue; };
The program should create an output file with the same name as the input file but with a “-edge” before the “.bmp” extension. Fore example, if “CU.bmp” is the input file, then “CU-edge.bmp” should be the output file.
This output file is used to contain a new 24-bit color .bmp image which uses the edge-detection transform matrix given below to detect the “edges” between differing colors.
char Matrix[3][3] =
{ { 0, -1, 0 },
{ -1, 4, -1 },
{ 0, -1, 0 }
This matrix above is to be used as a filter on each pixel of the image to produce new pixels. For example, consider an image that has the following data:
    Col 0    Col 1    Col 2    Col 3    Col 4 ....
Row 0    (0,0,0)     (0,2,0)    (3,2,1)    (1,0,0)    (0,0,1)
Row 1    (0,0,0)     (0,2,0)    (1,2,3)    (1,0,0)    (0,0,1)
Row 2    (5,0,0)     (0,4,0)    (7,2,4)    (9,0,2)    (8,0,1)
Row 3    (0,3,0)     (0,1,0)    (8,2,5)    (1,0,1)    (9,0,1)
The Row 2 - Col 3 pixel (9,0,2) would be transformed by summing the product of the overlaid filter at pixel [2][3]. That is, you would calculate the new value for pixel [2][3]as the sum of Matrix[0][0] multiplied by pixel[1][2] and Matrix[0][1] multiplied by pixel[1][3] and Matrix[0][2] multiplied by pixel[1][4]and Matrix[1][0] multiplied by pixel[2][2] and Matrix[1][1] multiplied by pixel [2][3], etc...
    Col 0    Col 1    Col 2    Col 3    Col 4 ....
Row 0    (0,0,0)     (0,2,0)    (3,2,1)    (1,0,0)    (0,0,1)
Row 1    (0,0,0)     (0,2,0)    (1,2,3)    (1,0,0)    (0,0,1)
Row 2    (5,0,0)     (0,4,0)    (7,2,4)    (9,0,2)    (8,0,1)
Row 3    (0,3,0)     (0,1,0)    (8,2,5)    (1,0,1)    (9,0,1)
The matrix factor is to be multiplied by each of the RGB components. So the new [2][3] pixel would be:
[(0) x(1,2,3)] + [(-1) x (1,0,0)] + [(0) x (0,0,1)]
+ [(-1) x (7,2,4)] + [(4) x (9,0,2)] + [(-1) x (8,0,1)]
+ [(0) x (8,2,5)] + [(-1) x (1,0,1)] + [(0) x (9,0,1)]
= (19, -2, 2)
The program should create one of two types of files: if “read” is entered, the output file also entered should contain the file data as given above; if “edge” is entered, a new bitmap file should be created using the edge detection algorithm given above. The edge-detection file should have the exact same header information along with the new pixel values.
Further Considerations
Note that the header data should be read in as individual fields, not all in one big chunk because the compiler may not pack the header structure.
The program should be structured neatly, easily readable, and well commented. Code should be modularized with functions, logically structured, and written to perform efficiently as possible. Furthermore, variable and function names should be such that the software is as “self-commenting” as possible.
The main function should mainly contain only functions called to complete the separate tasks given.
Creation and Submission
Work must be completed by each individual student, and only that student. Students must not use code from another student or copied from a book or internet source. Students must not allow other students to copy from their code or make it possible for students to do so by making their code available to be copied. Sharing of code will not be tolerated and will be tested for. Any such act of cheating will result in failing the class and may result in dismissal from the University.
Use the following line to compile your program
gcc -Wall -g p6.c -o p6
The code you submit must compile using the –Wall flag and no compiler e
ors or warnings should be printed. To receive credit for this assignment the code must compile and at a minimum perform some required function.
Code that does not compile or crashes before performing some required function will not be accepted or graded. All students must do a final check on one of the CES Ubuntu machines to verify that gcc using
Answered 4 days AfterApr 04, 2022


Sandeep Kumar answered on Apr 09 2022
12 Votes

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here