📄 picture_explorer.c
字号:
#include<fcntl.h>#include <unistd.h>
#include <stdio.h>
#include <linux/fb.h>
#include <sys/mman.h>
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
char *fbp = 0,*bitmapname="8.bmp",*bp="5.bmp",pixel;
int x = 0, y = 0,fb_color,x_axis,y_axis,fbfd = 0,xmin,xmax,ymin,ymax,color,i=0,j=0,screen_row=0,screen_column=0,interval=27,l=0,pixel_offset,BytesPerLine,bytes=0;
long int location = 0,screensize = 0;void framebuffer(int,int,unsigned char fb_color);void paint(int,int,unsigned char);void clear_screen();void show_string(int s_screen_row,int s_screen_column,int s_height,int s_interval,char string[100],unsigned char *s_after_convert_array);void char_init();void show_picture(int p_screen_row,int p_screen_column,int p_height,int p_width,unsigned char *p_after_convert_array);unsigned char convert(unsigned char ch);void bmp(int b_screen_row,int b_screen_column,char *file_name,int tag,char input_string[100]);void page_one();void page_two();void page_three();void main_page();
void print_head_info(FILE *fp);
FILE* judge_bmp(char *bmp_name);
long a,b,width,height,screensize;void display_char(int d_screen_row,int d_screen_column,int d_height,int d_interval,int d_offset,unsigned char *d_after_convert_array);FILE *fp_out;
unsigned rgbvalue;struct node{
char ch;
int offset;
}char_array[95];struct tagbitmapfileheader
{ /* bmfh */
short bftype;//位图文件类型,必须为"BM"
long bfsize;//位图文件大小,以字节为单位
short bfreserved1;//位图文件保留字,必须为0
short bfreserved2;//位图文件保留字,必须为0
long bfoffbits;//位图数据的起始位置,以相对于位图文件头的偏移量表示,以字节为单位
}bitmapfileheader;//该结构占据14个字节
struct tagbitmapinfoheader
{ /* bmih */
long bisize;//信息头文件的大小
long biwidth;//图象的宽,以像素为单位
long biheight;//图象的高,以像素为单位
short biplanes;//目标设备的平面数不清,必须为0
short bibitcount;//每个像素用几个比特,1,4,8,24
long bicompression;//图象是否被压缩,0,1
long bisizeimage;//位图的大小,以字节为单位
long bixpelspermeter;//位图水平分辨率,每米像素数
long biypelspermeter;//位图垂直分辨率,每米像素数
long biclrused;//图象所用的颜色数
long biclrimportant;//位图显示过程中重要的颜色数
}bitmapinfoheader;//该结构占据40个字节
struct tagrgbquad
{ /* rgbq */
unsigned char rgbblue;//兰色的亮度(0-255)
unsigned char rgbgreen;//绿色的亮度(0-255)
unsigned char rgbred;//红色的亮度(0-255)
unsigned char rgbreserved;//保留,必须为0
}rgbquad[256];///////////////////////////////////////////////////////////////////////////////////////////////////int main(){
//page_one(); //page_two(); main_page();
return 0;}///////////////////////////////////////////////////////////////////////////////////////////////////unsigned char convert(unsigned char ch){ unsigned char m,n,p; unsigned char blue,green,red; unsigned char value1,value2,value,final_value; int w; /*每个像素的rgbquad值 */ m=rgbquad[ch].rgbblue; n=rgbquad[ch].rgbgreen; p=rgbquad[ch].rgbred;/*格式之间的线性转换*/ blue=m/64;//兰色 green=n/32;//绿色 red=p/32;//红色 /*各个颜色的移位操作*/ //兰色位于framebuffer格式的后两位,不用移位 //绿色需要向左移3位 //红色需要向左移5位 value1=(green<<2); value2=(red<<5); /*将各个颜色值异或,得到framebuffer格式下的值*/
value=value1^value2;
value=value^blue; final_value=value%16; if(final_value==15) final_value=0; if(((m!=255)||(n!=255)||(p!=255))&&(final_value==0)) final_value=final_value-6; return final_value;
}///////////////////////////////////////////////////////////////////////////////////////////////////
void print_head_info(FILE *fp)
{
printf("The information of bitmapfileheader is:\n"
" bftype = %d;\n"
" bfsize = %d;\n"
" bfreserved1 = %d;\n"
" bfreserved2 = %d;\n"
" bfoffbits = %d;\n",
bitmapfileheader.bftype,bitmapfileheader.bfsize,bitmapfileheader.bfreserved1,bitmapfileheader.bfreserved2,bitmapfileheader.bfoffbits );
if(
fread(&bitmapinfoheader.bisize,sizeof(long),1,fp)!=1||
fread(&bitmapinfoheader.biwidth,sizeof(long),1,fp)!=1||
fread(&bitmapinfoheader.biheight,sizeof(long),1,fp)!=1||
fread(&bitmapinfoheader.biplanes,sizeof(short),1,fp)!=1||
fread(&bitmapinfoheader.bibitcount,sizeof(short),1,fp)!=1||
fread(&bitmapinfoheader.bicompression,sizeof(long),1,fp)!=1||
fread(&bitmapinfoheader.bisizeimage,sizeof(long),1,fp)!=1||
fread(&bitmapinfoheader.bixpelspermeter,sizeof(long),1,fp)!=1||
fread(&bitmapinfoheader.biypelspermeter,sizeof(long),1,fp)!=1||
fread(&bitmapinfoheader.biclrused,sizeof(long),1,fp)!=1||
fread(&bitmapinfoheader.biclrimportant,sizeof(long),1,fp)!=1
)
{
printf("fread error");
exit(1);
}
printf("The information of bitmapinfoheader is:\n");
printf(" biSize = %d;\n"
" biWidth = %d;\n"
" biHeight = %d;\n" " biPlanes =%d;\n" " biBitCount =%d;\n" " biCompression =%d;\n"
" biSizeImage = %d;\n"
" biXpelspermeter =%d;\n" " biYpelspermeter =%d;\n" " biClrUsed =%d;\n" " biClrImportant =%d;\n",
bitmapinfoheader.bisize,bitmapinfoheader.biwidth,bitmapinfoheader.biheight,bitmapinfoheader.biplanes,bitmapinfoheader.bibitcount,bitmapinfoheader.bicompression,bitmapinfoheader.bisizeimage,bitmapinfoheader.bixpelspermeter,bitmapinfoheader.biypelspermeter,bitmapinfoheader.biclrused,bitmapinfoheader.biclrimportant);
a=bitmapinfoheader.biwidth;
b=bitmapinfoheader.biheight;
/*if((a>1024) || (b>768)) //800*600
{ printf("This map can't be showed."); exit(1); }*/
if(fread(rgbquad,sizeof(rgbquad[0]),256,fp)!=256)
{
printf("fread error");
exit(1);
}
printf("offset of the file_descriptor is:%d.\n",ftell(fp));//location stream
}///////////////////////////////////////////////////////////////////////////////////////////////////
FILE* judge_bmp(char *bmp_name)
{ FILE *fp;
if((fp=fopen(bmp_name, "r"))==NULL)
{
printf("fopen error");
exit(1);
}
if(
fread(&bitmapfileheader.bftype,sizeof(short),1 ,fp)!=1 ||
fread(&bitmapfileheader.bfsize, sizeof(long), 1, fp) != 1 ||
fread(&bitmapfileheader.bfreserved1,sizeof(short),1,fp)!=1||
fread(&bitmapfileheader.bfreserved2,sizeof(short),1,fp)!=1||
fread(&bitmapfileheader.bfoffbits,sizeof(long),1,fp)!=1
)
{
printf("fread error");
exit(1);
}
else if(bitmapfileheader.bftype==19778)
printf("This is a .bmp file\nThe file name is %s\n",bmp_name);
else exit(1); return fp;
}///////////////////////////////////////////////////////////////////////////////////////////////////
void framebuffer(int x_axis,int y_axis,unsigned char fb_color)
{
/* Open the file for reading and writing */
fbfd = open("/dev/fb0", O_RDWR);
if (!fbfd) {
printf("Error: cannot open framebuffer device.\n");
exit(1);
}
//printf("The framebuffer device was opened successfully.\n");
/* Get fixed screen information */
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
printf("Error reading fixed information.\n");
exit(2);
}
/* Get variable screen information */
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
printf("Error reading variable information.\n");
exit(3);
}
/* Figure out the size of the screen in bytes */
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
/* Map the device to memory */
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,fbfd, 0);
if ((int)fbp == -1) { printf("Error: failed to map framebuffer device to memory.\n"); exit(4);
}
//printf("The framebuffer device was mapped to memory successfully.\n"); paint(x_axis,y_axis,fb_color);
munmap(fbp, screensize);
close(fbfd);
}void paint(int x,int y,unsigned char color){ /* Figure out where in memory to put the pixel */
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +(y+vinfo.yoffset) * finfo.line_length;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -