Linux C language displays the. BMP Format Image, linux.bmp

Source: Internet
Author: User
Tags bmp image fread

Linux C language displays the. BMP Format Image, linux.bmp

1/************************** 2 3 * bmp. h文4 4 5 *************************/6 7 # ifndef _ BMP _h _ 8 # define _ BMP _h _ 9 10 # include <unistd. h> 11 # include <stdio. h> 12 # include <stdlib. h> 13 # include <fcntl. h> 14 # include <string. h> 15 # include <linux/fb. h> 16 # include <sys/mman. h> 17 # include <sys/ioctl. h> 18 # include <arpa/inet. h> 19 20 // File Header structure 21 typedef struct 22 {23 unsigned char bfType [2]; // file type 24 un Signed long bfSize; // bitmap size 25 unsigned short bfReserved1; // bit 0 26 unsigned short bfReserved2; // bit 027 unsigned long bfOffBits; // to the data offset 28 }__ attribute _ (packed) BitMapFileHeader; // the compiler is not optimized, its size is 14 bytes 29 30 // information header Structure 31 typedef struct 32 {33 unsigned long biSize; // BitMapFileHeader number of bytes 34 long biWidth; // bitmap width 35 long biHeight; // The height of the bitmap, which is positive and vice versa. In the inverted figure 36, unsigned short biPlanes. // the value of this parameter is always set to 137 uns. Igned short biBitCount; // indicates the number of bits/pixels, which are 1, 4, 8, 16, 24, or 32. 38 unsigned long biCompression; // image data compression type without compression type: BI_RGB 39 unsigned long biSizeImage; // The image size, in bytes: 40 long biXPelsPerMeter; // The horizontal resolution is 41 long biYPelsPerMeter. // The vertical resolution is 42 unsigned long biClrUsed. // The number of color indexes in the color table used by the bitmap is 43 unsigned long biClrImportant; // The number of indexes that have an important impact on image display. 0 is important. 44} _ attribute _ (packed) BitMapInfoHeader; 45 46 // pixel structure 47 typedef struct 48 {49 unsigned char Blue; // The Blue weight of the color is 50 unsigned char Green; // The Green weight of the color is 51 unsigned char Red; // The Red weight of the color is 52 unsigned char Reserved; // reserved value (brightness) 53 }__ attribute _ (packed) RgbQuad; 54 55 56 57 int show_photo (const char * fbp, struct fb_var_screeninfo * scrinfo, const char * BMP name); 58 59 60 # endif // _ BMP _h __

 

 

 

1/************************** 2 3 * bmp. c file 4 5 *************************/6 7 # include "bmp. h "8 9 10 11/************************** 12 13 * fbp, ing memory start address 14 15 * scrinfo, screen information struct 16 17 * BMP name.bmp bitmap file name 18 19 ************************/20 int show_photo (const char * fbp, struct fb_var_screeninfo * scrinfo, const char * BMP name) 21 {22 if (NULL = fbp | NULL = scrinfo | NULL = BMP name) 23 return-1; 24 25 int l Ine_x = 0, line_y = 0; 26 unsigned long tmp = 0; 27 int xres = scrinfo-> xres_virtual; // screen width (virtual) 28 int bits_per_pixel = scrinfo-> bits_per_pixel; // screen digits 29 BitMapFileHeader FileHead; 30 BitMapInfoHeader InfoHead; 31 RgbQuad rgb; 32 33 unsigned long location = 0; 34 35 // open .bmp FILE 36 FILE * fb = fopen (BMP name, "rb"); 37 if (fb = NULL) 38 {39 printf ("fopen bmp error \ r \ n"); 40 return-1; 41} 42 43 // read file information 44 if (1! = Fread (& FileHead, sizeof (BitMapFileHeader), 1, fb) 45 {46 printf ("read BitMapFileHeader error! \ N "); 47 fclose (fb); 48 return-1; 49} 50 if (memcmp (FileHead. bfType," BM ", 2 )! = 0) 51 {52 printf ("it's not a BMP file \ n"); 53 fclose (fb); 54 return-1; 55} 56 57 // read bitmap information 58 if (1! = Fread (char *) & InfoHead, sizeof (BitMapInfoHeader), 1, fb) 59 {60 printf ("read BitMapInfoHeader error! \ N "); 61 fclose (fb); 62 return-1; 63} 64 65 // jump to data zone 66 fseek (fb, FileHead. bfOffBits, SEEK_SET); 67 68 int len = InfoHead. biBitCount/8; // The number of bytes of a pixel in the source image: 69 int bits_len = bits_per_pixel/8; // The number of bytes of a pixel in the screen: // display 70 71 in a loop while (! Feof (fb) 72 {73 tmp = 0; 74 rgb. Reserved = 0xFF; 75 76 if (len! = Fread (char *) & rgb, 1, len, fb) 77 break; 78 79 // calculate the offset of the pixel at the start address of the ing memory 80 location = line_x * bits_len + (InfoHead. biHeight-line_y-1) * xres * bits_len; 81 82 tmp | = rgb. reserved <24 | rgb. red <16 | rgb. green <8 | rgb. blue; 83 84 * (unsigned long *) (fbp + location) = tmp; 85 86 line_x ++; 87 if (line_x = InfoHead. biWidth) 88 {89 line_x = 0; 90 line_y ++; 91 if (line_y = InfoHead. biHeight) 92 break; 93} 94} 95 96 fclose (fb); 97 98 return 0; 99}

 

 

1/************************** 2 3 * main. c file 4 5 *************************/6 7 # include "bmp. h "8 9 int main () 10 {11 int devfb, filefb; 12 struct fb_var_screeninfo scrinfo; 13 unsigned long screensize; 14 char * fbp; 15 char BMP name [20] = {0}; 16 17 // open the device file 18 devfb = open ("/dev/fb0", O_RDWR); 19 if (! Devfb) 20 {21 printf ("devfb open error! \ R \ n "); 22 return-1; 23} 24 // printf (" devfb open OK! % D \ r \ n ", devfb); 25 26 27 28 // obtain screen information 29 30 // ioctl (devfb, FBIOPUT_VSCREENINFO, & scrinfo) 31 if (ioctl (devfb, FBIOGET_VSCREENINFO, & scrinfo) 32 {33 printf ("get screen infomation error! \ R \ n "); 34 return-1; 35} 36 37 // printf (". xres = % d ,. yres = % d ,. bit = % d \ r \ n ", scrinfo. xres, scrinfo. yres, scrinfo. bits_per_pixel); 38 39 // printf (". xres_virtual = % d ,. yres_virtual = % d \ r \ n ", scrinfo. xres_virtual, scrinfo. yres_virtual); 40 41 if (32! = Scrinfo. bits_per_pixel) 42 {43 printf ("screen infomation. bits error! \ R \ n "); 44 return-1; 45} 46 47 48 49 // calculate the required ing memory size 50 screensize = scrinfo. xres_virtual * scrinfo. yres_virtual * scrinfo. bits_per_pixel/8; 51 // printf ("screensize = % lu! \ R \ n ", screensize); 52 53 // memory ing 54 fbp = (char *) mmap (NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, devfb, 0 ); 55 if (-1 = (int) fbp) 56 {57 printf ("mmap error! \ R \ n "); 58 return-1; 59} 60 61 scanf (" % s ", BMP name); 62 63 // display image 64 show_photo (fbp, & scrinfo, BMP name); 65 66 67 68 // cancel ing, close the file 69 munmap (fbp, screensize); 70 close (devfb); 71 72 return 0; 73}

 

 

 

/*************************************** ***

* (I did not advertise this game, but just took a picture)

* Description: 1. The image is a 24-bit or 32-bit bmp image.

2. The screen is a 32-bit screen.

3. Different devices may have different device files.

4. Run the following command under the root user:

**************************************** ***/

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.