Program with c language to read the text image in the BMP picture with 80 * 25 resolution and display it in the form of character dot matrix.
Before writing this program, you have to understand some basic knowledge of BMP bitmap file
Prerequisite knowledge:
1, Basic structure of 24 bit BMP(bitmap) file:
14 byte file header + 40 byte information header + Bitmap pixel data (3 bytes per pixel: blue component (1 byte), green component (1 byte), red component (1 byte)).
Note: when storing bitmap pixel data, it starts from the lower left pixel, from left to right, from bottom to top, and up to the top right pixel.
14 byte file header:
Bitmap flag (BM: 2 bytes), file size (4 bytes), reserved bytes (4 bytes reserved, value 0), partial migration of the first image pixel information from the file header (4 bytes).
40 byte header:
Header length (4 bytes, generally 40 bytes), image width (4 bytes, unit pixel), image height (4 bytes, unit pixel)
The number of color planes of the target device (2 bytes, fixed to 2 bytes, fixed to 1), the number of bits per pixel (2 bytes)
The type of image data compression (bitmap with 4 bytes and 24 bit color value, generally the value is 0, indicating that it is not compressed)
Number of bytes occupied by all pixels of the image (4 bytes, generally width * height * number of color bytes occupied by each pixel)
Horizontal resolution (4 bytes, expressed in pixels / m, generally 0 default), vertical resolution (4 bytes, expressed in pixels / m, generally 0 default)
The number of color indexes in the color table (4 bytes, generally 0 if not used) and the number of color indexes that have an important impact on the image display (4 bytes, generally 0 if not used).
Bitmap pixel data (24 bit bitmap): starting from the lower left corner pixel, from left to right, from bottom to top, to the top right corner pixel.
The first pixel in the last row in the lower left corner (3 bytes: 1 byte of blue component, 1 byte of green component and 1 byte of red component)
The second pixel in the last row in the lower left corner (3 bytes: 1 byte of blue component, 1 byte of green component and 1 byte of red component)
. . . . . .
The last pixel of the last row in the lower left corner (3 bytes: 1 byte of blue component, 1 byte of green component and 1 byte of red component)
. . . . . .
The first pixel in the first row in the upper left corner (3 bytes: 1 byte of blue component, 1 byte of green component and 1 byte of red component)
. . . . . .
The last pixel of the first row in the upper right corner (3 bytes: 1 byte of blue component, 1 byte of green component and 1 byte of red component).
Note: the number of bytes per scan line in bitmap data must be a multiple of 4. If it is insufficient, it shall be supplemented with 0.
Program implementation:
Command line parameters used
int main(int argc,char* argv[]) { /* Read the 24 bit BMP bitmap of 80 * 25 dot matrix with c language, and then display it in character mode*/ FILE* fp; //File pointer to image file int width, height; //Width and height of the image in pixels char pix, blank; //pix: store the read pixel component data, //blank: store the background color of the picture (the first value read out is used as the background, and we will draw all pixels that are not the background color in the future) if (argc != 2) { printf("Wrong number of incoming parameters"); exit(0); } if ((fp = fopen(argv[1], "r")) == NULL) { printf("%s Image file does not exist!\n",argv[1]); exit(0); } fseek(fp, 18, SEEK_SET);//Move the file read / write pointer to the file header offset of 18 bytes fread(&width, sizeof(int), 1, fp); //Width of read image fread(&height, sizeof(int), 1, fp); //Height of read image if (width != 80) { printf("The image width must be 80 pixels\n"); exit(0); } fseek(fp, 54, SEEK_SET); //Skip the previous 14 byte file header and 40 byte information header to the beginning of pixel data blank = fgetc(fp); //We specify that the data read in the first byte is the background color for (int i = 0; i < height; ++i) { fseek(fp, 54, SEEK_SET); fseek(fp, (height - i - 1) * 3 * 80, SEEK_CUR);//Skip the pixel data in the first height-1 line, because the pixels in the lower left corner are stored first when the file is stored, but I want to print from the upper left corner, so I have to make the file pointer point to the data of the corresponding pixel for (int j = 0; j < 80; ++j) //Print one line { pix = fgetc(fp); if (pix == blank) { printf(" "); //If the pixel is the background color, draw a space } else { printf("."); //On the contrary, draw a picture } //Note that after reading one byte of data of a pixel, we have to skip two bytes fseek(fp, sizeof(char) * 2, SEEK_CUR); } printf("\n"); //Wrap every printed line } fclose(fp); system("pause"); return 0; }