Uploaded by siddheshpisal76

201080065 DIP LAB E1

advertisement
Name : Vinay Ratan Datir
Reg no: 201080065
Branch : Information Technology
DIP Lab Experiment 1
Aim : Take your own image and display it on screen without using library
in C
Theory :
The header of a bitmap (BMP) file is a block of information at the
beginning of the file that provides important details about the image, such
as its dimensions, bit depth, color encoding, and more.
The format of the BMP header is well defined, and typically consists of the
following fields:
1. Signature: This is a 2-byte field that contains the characters "BM" to
identify the file as a BMP file.
2. File size: This is a 4-byte field that specifies the size of the entire BMP
file in bytes.
3. Reserved: This is a 4-byte field that is reserved for future use and
should be set to zero.
4. Data offset: This is a 4-byte field that specifies the offset, in bytes,
from the beginning of the file to the start of the image data.
5. Header size: This is a 4-byte field that specifies the size of the header,
in bytes.
6. Image width: This is a 4-byte field that specifies the width of the image
in pixels.
7. Image height: This is a 4-byte field that specifies the height of the
image in pixels.
8. Planes: This is a 2-byte field that specifies the number of color planes
in the image. This field is typically set to 1.
9. Bit depth: This is a 2-byte field that specifies the number of bits used to
represent the color of each pixel.
10. Compression: This is a 4-byte field that specifies the type of
compression used for the image data.
11. Image size: This is a 4-byte field that specifies the size of the image
data, in bytes.
12. X resolution: This is a 4-byte field that specifies the horizontal
resolution of the image, in pixels per meter.
13. Y resolution: This is a 4-byte field that specifies the vertical resolution
of the image, in pixels per meter.
14. Colors: This is a 4-byte field that specifies the number of colors in the
color table.
15. Important colors: This is a 4-byte field that specifies the number of
important colors in the color table.
16. Color table: This is an optional section of the header that contains the
color table, which maps the pixel values in the image to specific colors.
The size of this section depends on the bit depth of the image.
Functions used in this program:
Fread :
The fread() function in C++ reads the block of data from the stream. This
function first, reads the count number of objects, each one with a size of
size bytes from the given input stream. The total amount of bytes reads if
successful is (size*count). According to the no. of characters read, the
indicator file position is incremented. If the objects read are not trivially
copy-able, then the behavior is undefined and if the value of size or count
is equal to zero, then this program will simply return 0.
Fwrite :
The function fwrite() is a standard library function in C language, present in
the stdio.h header file, which allows us to write data into a file, generally, the
file here is a binary file, but we can use it with text files as well.
Fclose:
fclose() function is a C library function that releases the memory stream
opened by the fopen() function.
getch:
The getc function in C is a standard library function that reads a character
from a given file stream. It takes as its argument a pointer to a FILE
object that represents the file stream to be read from. The getc function
returns the next character from the file stream. If the end of the file has
been reached, the function returns EOF (end-of-file), which is a special
value defined in the <stdio.h> header.
Code :
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *inputfile;
inputfile = fopen("vinay.bmp","r"); // open file
char head[54]; // store image head
char color[1024]; // store color,only if it exists.
for(int i=0;i<54;i++){
head[i] = getc(inputfile); // strip BMP head, as byte
printf("%d: %d \n", i, head[i]);
}
int w = *(int*)&head[18]; // read the w from image head
int h = *(int*)&head[22]; // read the h from image head
int bDepth = *(int*)&head[28]; // read the bDepth from image head
if(bDepth < 8){
fread(color, sizeof(char), 1024, inputfile);
}
char buf[h * w * 3]; // store image data
fread(buf, sizeof(char), (h * w * 3), inputfile);
FILE *of = fopen("output.bmp","wb"); // output file name
fwrite(head, sizeof(char), 54, of); // write the image head to output file
fwrite(buf, sizeof(char), (h * w * 3), of);
fclose(of);
fclose(inputfile);
return 0;
}
Input :
Output :
Conclusion :
In this experiment, we explored bitmap basics and how to use them to
display our own image in c programming without using any inbuild library.
Download