Linux下提取bmp图片颜色信息生成fb文件并用framebuffer显示

【本人尊重原作者劳动成果,如涉及侵权请联系删除】

Linux的framebuffer是一种很简单的显示接口,直接写入像素信息即可。

 

配置好的内核,会有/dev/fbn(其中n=0,1,2,3……)的接口,于是有一个想法:能否提前生成一个文件,比如data.fb,里面仅包含像素信息,从而可以直接送入framebuffer显示。

 

搜索了一下,有不少文章介绍如何解析bmp图片并送给framebuffer显示,但都是直接处理完就送入framebuffer,没有找到预处理工具来实现上面所说的想法。于是参考了一篇文章,改动了下代码,将直接送入framebuffer的方式变成写到一个文件中再显示。原代码地址为http://blog.csdn.net/xsckernel/article/details/49992315

代码在虚拟机上,位置为/home/ubuntu/cuda-workspace/24to32/main.c

 

测试得到的文件,尝试使用cat logo.fb > /dev/fb0进行显示,但效果很差如下图(原图为Lena图):


猜测可能出现的问题:main.c这个程序是给24位的屏用的,我的屏是是32位颜色深度,需要修改才能正常显示,但我还没找到修改的教程。

 

最后附上main.c源码:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <errno.h>
 
//14byte文件头
typedef struct
{
    char    cfType[2];//文件类型,"BM"(0x4D42)
    int     cfSize;//文件大小(字节)
    int     cfReserved;//保留,值为0
    int     cfoffBits;//数据区相对于文件头的偏移量(字节)
}__attribute__((packed)) BITMAPFILEHEADER;
//__attribute__((packed))的作用是告诉编译器取消结构在编译过程中的优化对齐
 
//40byte信息头
typedef struct
{
    char ciSize[4];//BITMAPFILEHEADER所占的字节数
    int  ciWidth;//宽度
    int  ciHeight;//高度
    char ciPlanes[2];//目标设备的位平面数,值为1
    short ciBitCount;//每个像素的位数
    char ciCompress[4];//压缩说明
    char ciSizeImage[4];//用字节表示的图像大小,该数据必须是4的倍数
    char ciXPelsPerMeter[4];//目标设备的水平像素数/米
    char ciYPelsPerMeter[4];//目标设备的垂直像素数/米
    char ciClrUsed[4]; //位图使用调色板的颜色数
    char ciClrImportant[4]; //指定重要的颜色数,当该域的值等于颜色数时(或者等于0时),表示所有颜色都一样重要
}__attribute__((packed)) BITMAPINFOHEADER;
 
typedef struct
{
    unsigned char blue;
    unsigned char green;
    unsigned char red;
    unsigned char reserved;
}__attribute__((packed)) PIXEL;//颜色模式RGB
 
BITMAPFILEHEADER FileHead;
BITMAPINFOHEADER InfoHead;
 
static char *fbp = 0;
static int xres = 0;
static int yres = 0;
static int bits_per_pixel = 0;
int width,height;
 
int show_bmp();
int fbfd = 0;
 
 
static int cursor_bitmap_format_convert(char *dst,char *src)
{
    int i ,j ;
    char *psrc = src ;
    char *pdst = dst;
    char *p = psrc;
 
    /* 由于bmp存储是从后面往前面,所以需要倒序进行转换 */
    pdst += (width * height * 3);
    for(i=0;i<height;i++){
        p = psrc + (i+1) * width * 3;
        for(j=0;j<width;j++){
            pdst -= 3;
            p -= 3;
            pdst[0] = p[0];
            pdst[1] = p[1];
            pdst[2] = p[2];
        }
    }
    return 0;
}
 
int show_bmp(char *path,char *fb_path)
{
    int i;
    FILE *fp,*fb_file;
    int rc;
    int line_x, line_y;
    long int location = 0, BytesPerLine = 0;
    char *bmp_data_buf = NULL;
    char *bmp_buf_dst = NULL;
    char * buf = NULL;
    int flen = 0;
    int ret = -1;
    int bmp_data_length = 0;
 
    printf("into show_bmp function\n");
    if(path == NULL || fb_path == NULL)
    {
        printf("path Error,return\n");
        return -1;
    }
    printf("path = %s\n", path);
    fp = fopen( path, "rb" );
    if(fp == NULL){
        printf("load cursor file open failed\n");
        return -1;
    }
 
    printf("fb_path = %s\n", fb_path);
    fb_file = fopen( fb_path, "wb" );
    if(fb_file == NULL){
        printf("load cursor file open failed\n");
        return -1;
    }
 
    /* 求解文件长度 */
    fseek(fp,0,SEEK_SET);
    fseek(fp,0,SEEK_END);
 
    flen = ftell(fp);
    printf("the length of file is %d\n",flen);
 
    bmp_data_buf = (char*)calloc(1,flen - 54);//位图纯数据部分
    if(bmp_data_buf == NULL){
        printf("load > malloc bmp out ofmemory!\n");
        return -1;
    }
 
 
    /* 再移位到文件头部 */
    fseek(fp,0,SEEK_SET);
 
    rc = fread(&FileHead, sizeof(BITMAPFILEHEADER),1, fp);
    if ( rc != 1)
    {
        printf("read header error!\n");
        fclose( fp );
        return( -2 );
    }
 
    //检测是否是bmp图像
    if (memcmp(FileHead.cfType, "BM", 2) != 0)
    {
        printf("it's not a BMP file\n");
        fclose( fp );
        return( -3 );
    }
    rc = fread( (char *)&InfoHead, sizeof(BITMAPINFOHEADER),1, fp );
    if ( rc != 1)
    {
        printf("read infoheader error!\n");
        fclose( fp );
        return( -4 );
    }
    width = InfoHead.ciWidth;
    height = InfoHead.ciHeight;
 
    printf("FileHead.cfSize =%d byte\n",FileHead.cfSize);
    printf("flen = %d\n", flen);
    printf("width = %d, height = %d\n", width, height);
 
    bmp_data_length = width * height *3;
 
    printf("bmp_data_length = %d\n", bmp_data_length);
 
    //跳转的数据区
    fseek(fp, FileHead.cfoffBits, SEEK_SET);
    printf(" FileHead.cfoffBits = %d\n", FileHead.cfoffBits);
    printf(" InfoHead.ciBitCount = %d\n", InfoHead.ciBitCount);
 
    //每行字节数
    buf = bmp_data_buf;
    while ((ret = fread(buf,1,bmp_data_length,fp)) >= 0) {
        if (ret == 0) {
            usleep(100);
            continue;
        }
        printf("ret = %d\n", ret);
        buf = ((char*) buf) + ret;
        bmp_data_length = bmp_data_length -ret;
        if(bmp_data_length == 0)
            break;
    }
 
    ///重新计算,很重要!!
    bmp_data_length = width * height *3;
    bmp_buf_dst = (char*)calloc(1,bmp_data_length );
    if(bmp_buf_dst == NULL){
        printf("load > malloc bmp out ofmemory!\n");
        return -1;
    }
 
    cursor_bitmap_format_convert(bmp_buf_dst,bmp_data_buf);
    fwrite(bmp_buf_dst,1,bmp_data_length,fb_file);
    free(bmp_data_buf);
    free(bmp_buf_dst);
 
    fclose(fp);
    fclose(fb_file);
 
    printf("show logo return 0\n");
    return 0;
}
 
 
int main()
{
 
    show_bmp( "/home/ubuntu/data.bmp","/home/ubuntu/data.fb");
 
    printf("the size of char is %d byte, the size of intis %d byte\n",sizeof(char),sizeof(int));
}
 


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值