文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>操作framebuffer--画任意两点间直线

操作framebuffer--画任意两点间直线

时间:2010-05-19  来源:flyfordream_h

用改进的Bresenham算法实现画任意两点间直线 OSD_header.h  

#pragma pack(1)
#define T 3.1415926897
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <termios.h>
#define WINDOW_HEIGHT 480

line.c

#include "OSD_header.h"

struct fb_fix_screeninfo finfo; //OSD静态属性

struct fb_var_screeninfo vinfo; //OSD动态参数

static int fbfd = 0;
char *fbp = 0;
int opframebuffer();
int background();
int putpixel(int x,int y,long int p_color);
int line(int x0,int y0,int x1,int y1,long int p_color);

/*以指定颜色来填充像素点*/
int putpixel(int x,int y,long int p_color)
{
    long int fb_offset;
    fb_offset =(y*vinfo.xres + x)*(vinfo.bits_per_pixel/8);
    *((long*)(fbp+fb_offset)) = p_color;
    close(fbfd);
    return 0;
    
}

/*设置framebuffer的背景色*/
int background()
{
    int w, h, p_width, p_height;
    long int p_color,fb_offset;
    opframebuffer(); //调用framebuffer初始化函数

    printf("%dx%d\n",vinfo.xres,vinfo.yres);//显示OSD的列数、行数

    printf("bits_per_pixel is %d\n",vinfo.bits_per_pixel);    
    printf("line_length is %d\n",finfo.line_length);
    p_width = vinfo.xres;     //提取framebuffer每行像素数

    p_height = vinfo.yres;     //提取framebuffer每列像素数

    fbp = (char *)mmap(0, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);//framebuffer基址指针

    p_color = 0x0000FF; //填充颜色=0000FF(blue)

    fb_offset = 0; //framebuffer内部偏移量, 从0开始

    for( h = 0; h < p_height; h++ )
    {
        for(w = 0; w < p_width; w++)
        {
            *((long *)(fbp + fb_offset)) = p_color;//填充指定颜色

            fb_offset += vinfo.bits_per_pixel/8;
            
        }
    }
    return 0;
}

/*framebuffer初始化*/
int opframebuffer()
{
    fbfd = open("/dev/fb0", O_RDWR); //打开framebuffer文件

    ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo);//关联framebuffer静态属性

    ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo);//关联framebuffer动态属性

      return 0;
}


int main(int argc ,char ** argv )
{
     int x0,y0,x1,y1;
     printf("input the two points of the line:\n");
     scanf("%d%d%d%d",&x0,&y0,&x1,&y1);
     background();
     line(x0,y0,x1,y1,0xFFFF00); // yellow

    
     return 0;
}


int line(int x0,int y0,int x1,int y1,long int p_color)
{
    int x,y,dx,dy,e,k,i;
    opframebuffer(); //调用framebuffer初始化函数

    fbp = (char *)mmap(0, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);//framebuffer基址指针

    dx=x1-x0; dy=y1-y0; x= x0; y= y0;
    if( !dx)
     {
        if(y1 > y0)
          {
           for(i=0;i<y1-y0;i++)
              {

               putpixel(x,y+i,p_color);
              }
         }
       else
         {
           for(i=0;i<y0-y1;i++)
              {
               putpixel(x,y-i,p_color);
              }
         }
     }
     else
     {
         k = dy/dx;
         if( !k && !dy)
            {
            if(x1 > x0)
                {
                  for(i=0;i<x1-x0;i++)
                      {
                      putpixel(x+i,y,p_color);
                      }
                }
            else
                {
                   for(i=0;i<x0-x1;i++)
                       {
                      putpixel(x-i,y,p_color);
                      }
                }
            }
            
         else if(k<=1 && k >=0 && dy*dx >0 )
            {
              e = -dx;
           while(x<=x1)
                  {
                         putpixel(x,y,p_color);
                            x++;
                     e=e+2*dy;
                     if(e>0)
                     {
                         y++;
                         e=e-2*dx;
                     }
                 if(x >x1 || y > y1) break;
          }
            }
         else if( k> 1)
            {
              e= -dy;
              while(y<=y1)
                  {
                       putpixel(x,y,p_color);
                       y++;
                       e = e + 2*dx;
                       if(e > 0)
                              {
                             x++;
                             e = e - 2*dy;
                              }
                       if(x > x1 || y > y1) break;
                  }
            }
           
         else if(k >= -1 && k <= 0 && dy*dx < 0)
            {
              e = -dx;
           while(x <= x1)
                  {
                         putpixel(x,y,p_color);
                            x++;
                     e=e-2*dy;
                     if(e>0)
                     {
                             y--;
                             e=e-2*dx;
                     }
                 if(x > x1 || y < y1) break;
          }
            }
            
         else // k< -1

            {
              e= -dy;
              while(y >= y1)
                  {
                  putpixel(x,y,p_color);
                  y--;
                  e = e - 2*dx;
                  if(e > 0)
                       {
                       x++;
                       e = e - 2*dy;
                       }
                 if(x > x1 || y < y1) break;
                  }
            }
     }
   
    close(fbfd);
    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载