Reference no: EM132230074
Program Assignment - The BMP (bitmap) Image-file Format
Read this whole section before starting, especially the bottom part of this assignment.
The BMP (bitmap) format is a standard image-file format developed by Microsoft and used by Windows Operating systems and others. This document describes how to construct a 24-bit BMP file. A 24-bit BMP file uses 24 bits to denote the color of each pixel in an image: an 8-bit value (0-255) for the blue component, an 8-bit value (0-255) for the green component, and an 8-bit value (0-255) for the red component of a pixel. Using BGR to denote the blue, green, and red components of a pixel, if the pixel is bright blue, it would have (B,G,R)=(255,0,0). If a pixel is bright green it would have (B,G,R)=(0,255,0). If the pixel is bright red, it would have (B,G,R)=(0,0,255).
Black is formed by the absence of color; so if a pixel is black it would have (B,G,R)=(0,0,0). White is formed by the superposition of the brightest blue, green, and red; so a white pixel would have (B,G,R)=(255,255,255).
Your 24-bit BMP image file will have the following 3 parts.
1) A bitmap file header (14 bytes) is the 1st 14 bytes in a BMP image-file. The bitmap-file header, called a BITMAPFILEHEADER, contains information about the type, size, and layout of the bitmap file. (More info below)
2) A bitmap information header (40 bytes) follows the BITMAPFILEHEADER in the *.bmp image file. The bitmap-information header specifies the dimensions, compression type, and color format for the bitmap file. (More info below)
3) Image-data, a sequence of bytes that define the BGR value at each pixel in the image. The image-data stores the BGR values of each pixel in order: beginning at the bottom row of the image, going across the row, then the 1st pixel of the next higher row of the image, etc.
The bytes in an image file are each unsigned values 0-255. We can use data type "unsigned char" to hold a single byte containing values 0-255.
unsigned char uc;
To assign a value, e.g. 23, to an unsigned char uc; you can simply use uc=23. You will be writing bytes (unsigned char values) into a file one byte at a time using a "%c" format. You can use e.g. fprintf(fin,"%c",&uc); to write a single byte to a file.
Now let's examine the first 14 bytes in your BMP file, i.e. the BITMAPFILEHEADER.
1) A bitmap file header (14 bytes) is the 1st 14 bytes in a BMP image-file. The bitmap-file header, called a BITMAPFILEHEADER, contains information about the type, size, and layout of the bitmap file.
The 1st two bytes in the file must be the values 66 and 77 to identify the file as being of type BMP. The next 4 bytes must correctly identify the size of the BMP file in bytes as an unsigned integer, least significant byte LSB 1st. The next 4 bytes must be zeros. The final 4 bytes of the BITMAPFILEHEADER should hold the number of bytes to skip over to reach the image data; i.e. 54 bytes. This value is again stored as an unsigned integer, least significant byte LSB 1st.
Now, let's examine the next 40 bytes in your BMP file, i.e. the BITMAPINFOHEADER.
2) A bitmap information header (40 bytes) follows the BITMAPFILEHEADER in the *.bmp image file. The bitmap-information header specifies the dimensions, compression type, and color format for the bitmap file. For this example, let's assume the size of the image is width=height=256.
To generate the image-data, you would store the blue-green-red value of each pixel as 3 consecutive bytes. Here, the image data will occupy 3*width*height bytes. If, for example, the width=height=256, then the image data will occupy 3*256*256=196,608 bytes.
The decimal value 196,608 can be represented as a 4-byte integer value as the 4 bytes (least-significant-byte 1st) 0, 0, 3, 0 which denotes the decimal value 0+0*256+3*256*256+0*256*256*256=196,608.
The total number of bytes in the BMP file will be 14+40+3*256*256=196,662.
The decimal value 196,662 can represented as a 4-byte integer value as the 4 bytes (least significant byte 1st) 54, 0, 3, 0 which denotes the decimal value 54+0*256+3*256*256+0*256*256*256=196,662. The first 14 bytes of your BMP file would therefore be 66, 77, 54, 0, 3, 0, 0, 0, 0, 0, 54, 0, 0, 0.
Of course, a different size image would have correspondingly different values for the header.
So for this example, the next 40 bytes in your BMP file would therefore be
40, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.
Now, let's examine the image data.
3) Image-data: a sequence of bytes that define the BGR value at each pixel in the image. The image-data will store the BGR values of each pixel in order: beginning at the bottom row of the image, going across the row, then the 1st pixel of the next higher row, etc.
Now for the specifications of your program.
Your program will create a pseudo-random color BMP (bitmap) file called "random.bmp" in the directory from which your program is executed.
1) The width and height of the image will be chosen by the program user. However, your program will "round-up" the width so that the width is an integer multiple of 4. For example, if the user enters 2 for the width, your program will round the width to 4. If the user enters 5 for the width, your program will round-up the width to 8. If the user enters 12 for the width, your program will leave the width at 12. Notify the user regarding the resulting size of image that your program will create.
2) Once the size of the image is established, open the output file, and fprintf( ) the appropriate 14-byte BITMAPFILEHEADER and 40-byte BITMAPINFOHEADER to the file.
Use nested for-loops to fprintf( ) the set of pseudo-random BGR image-data values into the file.
3) Use the rand() function to generate the random BGR values. The pseudo-random values from rand( ) will be within the range 0-RAND_MAX. Convert these values to the byte value range 0-255 to serve as BGR 1-byte unsigned char values 0-255.
Before the 1st rand( ) call occurs in your program, seed the rand( ) function (one and only one time) using the srand( ) function.
Set the argument of srand( ) to the time-of-day obtained from your computer using the following syntax.
The statement is srand( (unsigned int)time(NULL) );
You will need to include header file time.h at the top of your program.
#include<time.h>
in order to use the function call time(NULL).
4) Finally, fclose( ) the file.
5) So, now you understand a bit about the Bitmap image format, but there is one problem. It has to do with writing characters into a file.
Note - Answer question 1-5.
Attachment:- Assignment File.rar