1 /************************* 2 3 *bmp.h文件 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 //文件頭結構體 21 typedef struct 22 { 23 unsigned char bfType[2]; //文件類型 24 unsigned long bfSize; //位圖大小 25 unsigned short bfReserved1; //位0 26 unsigned short bfReserved2; //位0 27 unsigned long bfOffBits; //到數據偏移量 28 } __attribute__((packed)) BitMapFileHeader; //使編譯器不優化,其大小為14字節 29 30 //信息頭結構體 31 typedef struct 32 { 33 unsigned long biSize; // BitMapFileHeader 字節數 34 long biWidth; //位圖寬度 35 long biHeight; //位圖高度,正位正向,反之為倒圖 36 unsigned short biPlanes; //為目標設備說明位面數,其值將總是被設為1 37 unsigned short biBitCount; //說明比特數/象素,為1、4、8、16、24、或32。 38 unsigned long biCompression; //圖象數據壓縮的類型沒有壓縮的類型:BI_RGB 39 unsigned long biSizeImage; //說明圖象的大小,以字節為單位 40 long biXPelsPerMeter; //說明水平分辨率 41 long biYPelsPerMeter; //說明垂直分辨率 42 unsigned long biClrUsed; //說明位圖實際使用的彩色表中的顏色索引數 43 unsigned long biClrImportant; //對圖象顯示有重要影響的索引數,0都重要。 44 } __attribute__((packed)) BitMapInfoHeader; 45 46 //像素點結構體 47 typedef struct 48 { 49 unsigned char Blue; //該顏色的藍色分量 50 unsigned char Green; //該顏色的綠色分量 51 unsigned char Red; //該顏色的紅色分量 52 unsigned char Reserved; //保留值(亮度) 53 } __attribute__((packed)) RgbQuad; 54 55 56 57 int show_photo(const char *fbp, struct fb_var_screeninfo *scrinfo, const char *bmpname); 58 59 60 #endif //__BMP_H__
1 /************************* 2 3 *bmp.c文件 4 5 *************************/ 6 7 #include "bmp.h" 8 9 10 11 /************************* 12 13 *fbp,映射內存起始地址 14 15 *scrinfo,屏幕信息結構體 16 17 *bmpname,.bmp位圖文件名 18 19 *************************/ 20 int show_photo(const char *fbp, struct fb_var_screeninfo *scrinfo, const char *bmpname) 21 { 22 if(NULL == fbp || NULL == scrinfo || NULL == bmpname) 23 return -1; 24 25 int line_x = 0, line_y = 0; 26 unsigned long tmp = 0; 27 int xres = scrinfo->xres_virtual; //屏幕寬(虛擬) 28 int bits_per_pixel = scrinfo->bits_per_pixel; //屏幕位數 29 BitMapFileHeader FileHead; 30 BitMapInfoHeader InfoHead; 31 RgbQuad rgb; 32 33 unsigned long location = 0; 34 35 //打開.bmp文件 36 FILE *fb = fopen(bmpname, "rb"); 37 if (fb == NULL) 38 { 39 printf("fopen bmp error\r\n"); 40 return -1; 41 } 42 43 //讀文件信息 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 //讀位圖信息 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 //跳轉至數據區 66 fseek(fb, FileHead.bfOffBits, SEEK_SET); 67 68 int len = InfoHead.biBitCount / 8; //原圖一個像素占幾字節 69 int bits_len = bits_per_pixel / 8; //屏幕一個像素占幾字節 //循環顯示 70 71 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 //計算該像素在映射內存起始地址的偏移量 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文件 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 bmpname[20] = {0}; 16 17 //打開設備文件 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 //獲取屏幕信息 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 //計算需要的映射內存大小 50 screensize = scrinfo.xres_virtual * scrinfo.yres_virtual * scrinfo.bits_per_pixel / 8; 51 //printf("screensize=%lu!\r\n", screensize); 52 53 //內存映射 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", bmpname); 62 63 //顯示圖片 64 show_photo(fbp, &scrinfo, bmpname); 65 66 67 68 //取消映射,關閉文件 69 munmap(fbp, screensize); 70 close(devfb); 71 72 return 0; 73 }
/******************************************
*效果圖(我不是給這游戲打廣告,只是隨手拿了一張圖而已)
*說明:1.圖片是24位或32位bmp圖
2.屏幕是32位屏幕
3.不同的設備,可能設備文件不同
4.需要在root用戶下執行
*******************************************/
http://xxxxxx/Linuxjc/1180961.html TechArticle