提取bmp图片的颜色信息,可直接framebuffer显示

稍微了解了下linux的framebuffer,这是一种很简单的显示接口,直接写入像素信息即可

配置好的内核,会有/dev/fbn 的接口,于是想能否提前生成一个文件,比如logo.fb,里面仅包含像素信息,从而可以直接送入framebuffer显示

搜索了一下,有不少文章介绍,如何解析bmp图片并送给framebuffer显示,但没有找到预处理工具,都是直接处理完就送入framebuffer

于是参考了一篇文章,改动了下代码,将直接送入framebuffer变成写到一个文件中。

原代码地址为

http://blog.csdn.net/xsckernel/article/details/49992315

测试得到的文件,是可以直接  cat logo.fb > /dev/fb0 进行显示的

#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>

//author:http://blog.csdn.net/xsckernel/article/details/49992315
//modify :zqb-all

//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
    int  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_buf = NULL;
    char *bmp_buf_dst = NULL;
    char * buf = NULL;
    int flen = 0;
    int ret = -1;
    int total_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(fp == NULL){
        printf("load cursor file open failed\n");
        return -1;
    }

    /* 求解文件长度 */
    fseek(fp,0,SEEK_SET);
    fseek(fp,0,SEEK_END);

    flen = ftell(fp);
    printf("flen is %d\n",flen);

    bmp_buf = (char*)calloc(1,flen - 54);
    if(bmp_buf == NULL){
        printf("load > malloc bmp out of memory!\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);

    total_length = width * height *3;

    printf("total_length = %d\n", total_length);

    //跳转的数据区
    fseek(fp, FileHead.cfoffBits, SEEK_SET);
    printf(" FileHead.cfoffBits = %d\n",  FileHead.cfoffBits);
    printf(" InfoHead.ciBitCount = %d\n",  InfoHead.ciBitCount);

    //每行字节数
    buf = bmp_buf;
    while ((ret = fread(buf,1,total_length,fp)) >= 0) {
        if (ret == 0) {
            usleep(100);
            continue;
        }
        printf("ret = %d\n", ret);
        buf = ((char*) buf) + ret;
        total_length = total_length - ret;
        if(total_length == 0)
            break;
    }

    ///重新计算,很重要!!
    total_length = width * height *3;
    bmp_buf_dst = (char*)calloc(1,total_length );
    if(bmp_buf_dst == NULL){
        printf("load > malloc bmp out of memory!\n");
        return -1;
    }

    cursor_bitmap_format_convert(bmp_buf_dst, bmp_buf);
    fwrite(bmp_buf_dst,1,total_length,fb_file);
    free(bmp_buf);
    free(bmp_buf_dst);

    fclose(fp);
    fclose(fb_file);

    printf("show logo return 0\n");
    return 0;
}

int main()
{

    show_bmp( "./logo.bmp","./logo.fb");

}

  

时间: 2024-08-10 01:58:05

提取bmp图片的颜色信息,可直接framebuffer显示的相关文章

【C】用C语言提取bmp图片像素,并进行K-means聚类分析——容易遇到的问题

关于bmp图片的格式,网上有很多文章,具体可以参考百度百科,也有例子程序.这里只提要注意的问题. (1)结构体定义问题:首先按照百度百科介绍的定义了结构体,但是编译发现重定义BITMAPFILEHEADER等.其实只要包含了Windows.h,里面的wingdi.h就已经定义了处理bmp的结构体,故不需要自己再重复定义. (2)读取文件的字节对其问题:要使用#pragma pack (1)来方便读取文件头的结构体,否则结构体的大小会由于字节对齐问题改变.不知是否头文件中已经使用了该宏,在我的代码

提取颜色信息

RGB转为灰度图是为了丢掉图片的颜色信息,而保留亮度信息 #include <opencv2/opencv.hpp> #include<opencv2/imgproc/imgproc.hpp> using namespace cv; // 显示灰度直方图 Mat getHistImg(const MatND& hist) { double maxVal=0; double minVal=0; //找到直方图中的最大值和最小值 minMaxLoc(hist,&minV

IOS7以后无需自定义,改变UITabbarItem的图片文字颜色

在IOS7以前,UITabbarItem的图片都是被固定渲染为蓝色,想要改变UITabbarItem的图片颜色就必须要自定义,在IOS7以后,得到了更新,方便大家自己去设定颜色,下面给出代码! 1.创建UITabbarItem的默认图片和选中图片 //第一个界面 ChildViewController *childvc = [[ChildViewController alloc]initWithNibName:nil bundle:nil]; UINavigationController *ch

BMP图片转换为JPEG图片

原文:BMP图片转换为JPEG图片 昨天在家学习,发现很多人把BMP图片转换为其它图片格式,有些人写得简单,有些人写得复杂. Insus.NET在想,一直在做文件上传,下载,或是图片剪切,都有进行过文件另存,在另存时,还需指定ContentType. 你可以在MSDN: http://msdn.microsoft.com/en-us/library/system.drawing.image(v=vs.110).aspx  找到相关方法: 好的,我们就写一个方法:为了看到效果,Insus.NET举

bmp图片数据提取

仿照别人的程序写的bmp数据提取C代码,并将提取的数据放到txt文档中 1 /* 2 date : 2014/06/24 3 designer :pengxiaoen 4 version : dev4.9.9.0 5 function :读取bmp图片的图片数据信息到文件txt中.参考了其他的程序 6 7 bmp 文件格式: 第10个字节处存放图像数据起始地址 —— 此程序中25行用到 8 第36h - 0436h 字节处存放调色板 —— 此程序中没有用到 9 10 **************

mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息 获得图片 jpeg bmp 图片转换等

mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息 获得图片 jpeg bmp 图片转换(上) MP3文件格式(二)---ID3v2 图:ID3V1标签结构 图:ID3V2标签结构 图:ID3V2头结构 图:ID3V2帧头结构 1.帧标识  用四个字符标识一个帧,说明一个帧的内容含义,常用的对照如下:  TIT2=标题 表示内容为这首歌的标题,下同  TPE1=作者  TALB=专集  TRCK=音轨 格式:N/M  其中N为专集中的第N首,M为专集中共M首,N和M为AS

Linux framebuffer显示bmp图片

framebuffer简介     帧缓冲(framebuffer)是Linux为显示设备提供的一个接口,把显存抽象后的一种设备,他允许上层应用程序在图形模式下直接对显示缓冲区进行读写操作.framebuffer是LCD对应的一中HAL(硬件抽象层),提供抽象的,统一的接口操作,用户不必关心硬件层是怎么实施的.这些都是由Framebuffer设备驱动来完成的.     帧缓冲设备对应的设备文件为/dev/fb*,如果系统有多个显示卡,Linux下还可支持多个帧缓冲设备,最多可达32个,分别为/d

【VC++技术杂谈006】截取电脑桌面并将其保存为bmp图片

本文主要介绍如何截取电脑桌面并将其保存为bmp图片. 1. Bmp图像文件组成 Bmp是Windows操作系统中的标准图像文件格式. Bmp图像文件由四部分组成: (1)位图头文件数据结构,包含Bmp图像文件的类型.文件大小等信息. (2)位图信息数据结构,包含Bmp图像的宽.高.压缩类型等信息. (3)颜色表,该部分可选,有些位图需要,有些位图(如24位真彩色位图)不需要. (4)位图数据. 1.1位图头文件数据结构 位图头文件数据结构包含Bmp图像文件的类型.文件大小等信息,占用14个字节.

日式冒险游戏《巫女小镜大冒险》PIC 图片转 BMP 图片

----------------------------------------------------------------------------- !!警告!! 游戏资源所有权,归游戏开发商所有 以下转换算法与代码,仅供学习交流使用,请勿用于非法或商业用途 由此引起的一切后果,与博主(我)无关 ----------------------------------------------------------------------------- 这游戏的资源本身没有被打包,直接就可以看