本实验需要给 开发板移植好系统,且系统需支持freebuffer,开发板配置合适的显示屏(24色或32色),也可以直接在pc的Linux系统下运行,前提也要支持freebuffer,且配置好vga参数,vga参数配置表请参见: Linux下设置VGA显示模式方法_Linux教程_Linux公社-Linux系统门户网站 http://www.linuxidc.com/Linux/2011-08/40279.htm***上,看不懂的地方可以直接@我
- #include
- #include
- #include
- #include
- #include
- struct fb_fix_screeninfo fbfix ={0
- };
- struct fb_var_screeninfo fbvar = {0
- };
- long screensize = 0;
- unsigned char *fb32=NULL,*my_fb32=NULL;
- struct bmp{
- unsigned char type[2]; // 0-1bit file type
- long file_size;// 2-5bit file size
- unsigned int off_size;//A-D sart to data offbit
- unsigned int data_info;//0E-11 data info
- int biwidth;//12-15 width
- int bihight;//16-19 hight
- unsigned int bicom;//1C-1D Ò»¸öÏñËØÓöàÉÙλ±íʾ
- unsigned int bisize;//1E-21
- unsigned int biclr;//2E-31
- unsigned int biclrimp;//32-35
- };
- struct bmp my_bmp;
- void init_bmp_struct(struct bmp *p , int fd)
- {
- unsigned char buff[54];
- lseek(fd,0,SEEK_SET); //Îļþ¶¨Î»
- read(fd , buff , sizeof(buff));
-
- p->type[0] = buff[0];
- p->type[1] = buff[1];
- p->file_size = (buff[5]<<24) | (buff[4]<<16) | (buff[3]<<8) | buff[2];
- p->off_size = (buff[13]<<24) | (buff[12]<<16) | (buff[11]<<8) | buff[10];
- p->data_info = (buff[17]<<24) | (buff[16]<<16) | (buff[15]<<8) | buff[14];
- p->biwidth = (buff[21]<<24) | (buff[20]<<16) | (buff[19]<<8) | buff[18];
- p->bihight = (buff[25]<<24) | (buff[24]<<16) | (buff[23]<<8) | buff[22];
- p->bicom = (buff[29]<<8) | buff[28];
- p->bisize = (buff[33]<<24) | (buff[32]<<16) | (buff[31]<<8) | buff[30];
- p->biclr = (buff[49]<<24) | (buff[48]<<16) | (buff[47]<<8) | buff[46];
- p->biclrimp = (buff[53]<<24) | (buff[52]<<16) | (buff[51]<<8) | buff[50];
-
- //printf("type is %c",p->type[0]);
- //printf("%cn",p->type[1]);
- //if(p->type[0] != 'B' && p->type[1] != 'M')
- //printf("wring: file not BMP filen");
- //printf("size is %dn",p->file_size);
- //printf("bicom is %dn",p->bicom);
- //printf("off is %dn",p->off_size);
- //printf("width is %dn",p->biwidth);
- //printf("hight is %dn",p->bihight);
-
- }
- int dis_bmp(unsigned char *dis_addr, struct bmp info,int fd,int loc_x,int loc_y)
- {
- int x,y,j,z,er=0;
- char tmp[4];
- if((loc_x+info.biwidth>fbvar.xres) || (loc_y+info.bihight)>fbvar.yres)
- {
- printf("set x y failedn");
- return 0;
- }
- x = info.biwidth*(fbvar.bits_per_pixel/8);
- y = info.bihight;
- lseek(fd,info.off_size,SEEK_SET); //Îļþ¶¨Î»
- if(info.bicom==24)
- {
- er=(info.biwidth*3)%4; //¼ÆËãÿһÐÐÌî³äµÄÊý¾Ý¸öÊý£¬Èç¹ûer=0£¬ÔòûÓÐÌî³ä
- er = 4-er;
- }
- for(j=fbvar.yres-1;j>((int)fbvar.yres-info.bihight) || j==(fbvar.yres-info.bihight);j--)
- {
- for(z=0;z
- read(fd , dis_addr+(j-loc_y)*fbvar.xres*(fbvar.bits_per_pixel/8)+z+loc_x*(fbvar.bits_per_pixel/8) ,info.bicom/8);
- if(er>0&&er<4)
- {
- read(fd ,tmp,er); //Èç¹û´æÔÚÌî³äµÄÊý¾ÝÔòÖ±½Ó¶ªÆúÌî³äµÄÊý¾Ý
- }
- }
-
-
- }
- int dis_bmp2(unsigned char *dis_addr, struct bmp info,FILE *fd,int loc_x,int loc_y)
- {
- int x,y,j,z,k=0,er=0;
- char tmp[4];
-
- x = info.biwidth*(fbvar.bits_per_pixel/8);
- y = info.bihight;
-
- if((loc_x+info.biwidth)>fbvar.xres)
- {
- printf("wring x!!!!!!!!!n");
- x=fbvar.xres*(fbvar.bits_per_pixel/8);
- loc_x=0;
- }
- if((loc_y+info.bihight)>fbvar.yres)
- {
- printf("wring y!!!!!!!!!n");
- y=fbvar.yres;
- loc_y=0;
- }
-
- if(info.bicom==24)
- {
- er=(info.biwidth*3)%4; //¼ÆËãÿһÐÐÌî³äµÄÊý¾Ý¸öÊý£¬Èç¹ûer=0£¬ÔòûÓÐÌî³
- er = 4-er;
- if(er==4)
- er=0;
- }
- unsigned char buffer[info.bihight][info.biwidth*(info.bicom/8)+er];
- fseek(fd,info.off_size,SEEK_SET); //Îļþ¶¨Î»
- fread(buffer,(info.biwidth*(info.bicom/8)+er)*info.bihight,1,fd);
- for(j=fbvar.yres-1;j>((int)fbvar.yres-info.bihight);j--)
- {
- // printf("%Xn",(buffer[0]));
- for(z=0,k=0;z
- {
- *(dis_addr+(j-loc_y)*fbvar.xres*(fbvar.bits_per_pixel/8)+z+loc_x*(fbvar.bits_per_pixel/8)) = buffer[fbvar.yres-j][k];
- *(dis_addr+(j-loc_y)*fbvar.xres*(fbvar.bits_per_pixel/8)+z+loc_x*(fbvar.bits_per_pixel/8)+1) = buffer[fbvar.yres-j][k+1];
- *(dis_addr+(j-loc_y)*fbvar.xres*(fbvar.bits_per_pixel/8)+z+loc_x*(fbvar.bits_per_pixel/8)+2) = buffer[fbvar.yres-j][k+2];
- *(dis_addr+(j-loc_y)*fbvar.xres*(fbvar.bits_per_pixel/8)+z+loc_x*(fbvar.bits_per_pixel/8)+3) = 0;
- }
- }
-
- }
- int main(int argc, char *argv[])
- {
- int z=0,j=0,fp=-1,fd=-1,x=0,y=0;
- FILE *bp=NULL;
- if (argc <4 )
- {
- printf("please enter the file PATH or x or y!n");
- return 0;
- }
- x=atoi(argv[2]);
- y=atoi(argv[3]);
- //printf(" x is %dn y is %dn",x,y);
- bp=fopen(argv[1],"r+");
- if(bp==NULL)
- {
- printf("not PATH or file!!n");
- return 0;
- }
- fp =open(argv[1],O_RDWR,0664);
- if(fp<0)
- {
- printf("not PATH or file!!n");
- fclose(bp);
- return 0;
- }
-
- fd = open("/dev/fb0",O_RDWR);
- if(fd<0)
- {
- printf("not dev!!n");
- close(fp);
- return 0;
- }
- ioctl(fd ,FBIOGET_FSCREENINFO,&fbfix);
- ioctl(fd ,FBIOGET_VSCREENINFO,&fbvar);
- screensize = fbvar.xres*fbvar.yres*(fbvar.bits_per_pixel/8);
- //printf("screensize %dnxres is %d n yres is %dn per is %d n",screensize,fbvar.xres,fbvar.yres,fbvar.bits_per_pixel);
- fb32 =(unsigned char *)mmap((unsigned char *)0x400f8000,screensize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
- init_bmp_struct(&my_bmp,fp);
- dis_bmp2(fb32,my_bmp,bp,x,y);
- close(fd);
- close(fp);
- fclose(bp);
- munmap(fb32,screensize);
- return 0;
- }
复制代码
代码输入完毕后,如果在开发板环境下运行则 arm-linux-gcc 文件名 -o 文件名 如果在pc环境则 gcc 文件名 -o 文件名
接下来就可以运行了,比如: ./dis_bmp.o 123.bmp 0 0
第一个参数是要显示的bmp图片,可以包含路径,第2 3个参数为坐标。
如果在pc环境下运行提示找不到deve,可能是你的vga参数没设好。
0
|
|
|
|