前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >仿真实例2——BMP图片文件读写

仿真实例2——BMP图片文件读写

作者头像
瓜大三哥
发布2020-05-07 20:39:50
1.3K0
发布2020-05-07 20:39:50
举报
文章被收录于专栏:瓜大三哥瓜大三哥

BMP文件结构

BMP文件格式,又称为Bitmap(位图)它是一种图像文件格式。由于它可以不作任何变换地保存图像像素域的数据,因此我们经常使用它来保存RAW数据。BMP文件总体上由4部分组成,分别是位图文件头、位图信息头、调色板和图像数据,如下图所示

BMP文件格式

名称

描述

位图文件头(bitmap-file header)

包含BMP图像文件的类型、显示内容等信息

位图信息头(bitmap-information header)

包含有BMP图像的宽、高、压缩方法,以及定义颜色等信息

彩色表/调色板(color table)

这个部分是可选的,有些位图需要调色板,有些位图,比如真彩色图(24位的BMP)就不需要调色板

位图数据(bitmap-data)

这部分的内容根据BMP位图使用的位数不同而不同,在24位图中直接使用RGB,而其他的小于24位的使用调色板中颜色索引值

读写BMP文件

下面先给出一个图片,鼠标右键查看图片的属性,看到图片分辨率为334x254,位深度为24bit

源图片文件

代码语言:matlab
复制
接下来进行文件的读写操作。
`timescale 1ns / 1ps
module bmp_file();

    integer fileID      ;//读BMP图片文件的指针
    integer cc          ;
    integer out_image   ;//写BMP图片文件的指针
    integer out_file    ;
    integer i           ;//计数器
    integer j           ;

    reg [7:0]   rd_bmp_data [0:2100000]    ;//读图片数据
    reg [7:0]   wr_bmp_data [0:2100000]    ;//写图片数据
    reg         clk                     ;
    reg [7:0]   rd_data                 ;//用于波形显示
    reg [7:0]   wr_data                 ;//用于波形显示

    integer R_bmp_width   ;//读出图片的宽度
    integer R_bmp_hight   ;//读出图片的高度
    integer R_data_start_index   ;//读出图片的宽度
    integer R_bmp_size   ;/读出BMP文件的大小

    reg rd_en;
    reg wr_en;
    reg rst ;

    parameter   CLK_PERIOD      =5                                                      ;
    parameter   W_BMP_WIDTH     =32'h014e                                               ;
    // parameter   W_BMP_WIDTH     =32'd1920                                               ;
    parameter   W_BMP_HIGHT     =32'h00fe                                               ;
    // parameter   W_BMP_HIGHT     =32'd1080                                               ;
    parameter   PIXEL_BITS      =16'h0018                                               ;   //24bits
    parameter   PIXEL_BYTES     =PIXEL_BITS/8                                           ;   //3bytes
    parameter   IMAGE_SIZE      =((((W_BMP_WIDTH*PIXEL_BYTES)>>2)+1)<<2)*W_BMP_HIGHT    ;
    parameter   BMP_FILE_HEAD   = 32'd54                                                ; 
    parameter   BM_WINDOWS      = 16'h4d42                                              ;
    parameter   FILE_SIZE       =IMAGE_SIZE+BMP_FILE_HEAD                               ;

    initial begin
        clk =1'b0;
        #(CLK_PERIOD/2);
        forever
            #(CLK_PERIOD/2) clk = ~clk;
    end

    initial begin
        rst =   1'b1;
        #2000
        rst=1'b0;
    end

    initial begin
    //BMP文件地址,选择自己所需要的文件地址
    fileID                  =   $fopen("F:\\file\\simulation_platform\\ImageSencorPipeline\\picture\\whiteandblack1.bmp","rb");
    out_image               =   $fopen("F:\\file\\simulation_platform\\ImageSencorPipeline\\picture\\output_file.bmp","wb");
    out_file                =   $fopen("F:\\file\\simulation_platform\\ImageSencorPipeline\\picture\\output_file.txt","w+");
    cc                      =   $fread(rd_bmp_data,fileID);
    R_bmp_width             =   {rd_bmp_data[21],rd_bmp_data[20],rd_bmp_data[19],rd_bmp_data[18]};
    R_bmp_hight             =   {rd_bmp_data[25],rd_bmp_data[24],rd_bmp_data[23],rd_bmp_data[22]};
    R_data_start_index      =   {rd_bmp_data[13],rd_bmp_data[12],rd_bmp_data[11],rd_bmp_data[10]};
    R_bmp_size              =   {rd_bmp_data[5],rd_bmp_data[4],rd_bmp_data[3],rd_bmp_data[2]};


    wait(rst==1'b0);
    for(i=R_data_start_index;i<R_bmp_size;i=i+1)
    begin
        @(posedge clk)
        rd_en   =   1'b1;
        wr_data =   rd_bmp_data[i];
    end
    @(posedge clk)
    rd_en = 1'b0;

    //BM
    wr_bmp_data[0 ]  =   BM_WINDOWS[0+:8]   ;
    wr_bmp_data[1 ]  =   BM_WINDOWS[8+:8]   ;
    //bmp file size
    wr_bmp_data[2 ]  =   FILE_SIZE[0 +:8]   ;
    wr_bmp_data[3 ]  =   FILE_SIZE[8 +:8]   ;
    wr_bmp_data[4 ]  =   FILE_SIZE[16+:8]   ;
    wr_bmp_data[5 ]  =   FILE_SIZE[24+:8]   ;
    //reserved
    wr_bmp_data[6 ]  =   8'h00   ;
    wr_bmp_data[7 ]  =   8'h00   ;
    wr_bmp_data[8 ]  =   8'h00   ;
    wr_bmp_data[9 ]  =   8'h00   ;

    //offset
    wr_bmp_data[10]  =   BMP_FILE_HEAD[0 +:8]   ;    
    wr_bmp_data[11]  =   BMP_FILE_HEAD[8 +:8]   ;
    wr_bmp_data[12]  =   BMP_FILE_HEAD[16+:8]   ;
    wr_bmp_data[13]  =   BMP_FILE_HEAD[24+:8]   ;

    //bmp information struct
    wr_bmp_data[14]  =   8'h28   ;
    wr_bmp_data[15]  =   8'h00   ;

    wr_bmp_data[16]  =   8'h00   ;
    wr_bmp_data[17]  =   8'h00   ;

    //write bmp width
    wr_bmp_data[18]  =   W_BMP_WIDTH[0+:8]   ;
    wr_bmp_data[19]  =   W_BMP_WIDTH[8+:8]   ;
    wr_bmp_data[20]  =   W_BMP_WIDTH[16+:8]   ;
    wr_bmp_data[21]  =   W_BMP_WIDTH[24+:8]   ;
    //write bmp hight
    wr_bmp_data[22]  =   W_BMP_HIGHT[0+:8]    ;
    wr_bmp_data[23]  =   W_BMP_HIGHT[8+:8]    ;
    wr_bmp_data[24]  =   W_BMP_HIGHT[16+:8]   ;
    wr_bmp_data[25]  =   W_BMP_HIGHT[24+:8]   ;

    //bit planes
    wr_bmp_data[26]  =   8'h01   ;
    wr_bmp_data[27]  =   8'h00   ;

    //one pixel use bits
    wr_bmp_data[28]  =   PIXEL_BITS[0+:8]   ;
    wr_bmp_data[29]  =   PIXEL_BITS[8+:8]   ;

    //compress
    wr_bmp_data[30]  =   8'h00   ;
    wr_bmp_data[31]  =   8'h00   ;
    wr_bmp_data[32]  =   8'h00   ;
    wr_bmp_data[33]  =   8'h00   ;

    //bmp image size
    wr_bmp_data[34]  =   IMAGE_SIZE[0 +:8]   ;
    wr_bmp_data[35]  =   IMAGE_SIZE[8 +:8]   ;
    wr_bmp_data[36]  =   IMAGE_SIZE[16+:8]   ;
    wr_bmp_data[37]  =   IMAGE_SIZE[24+:8]   ;

    wr_bmp_data[38]  =   8'hC4   ;    
    wr_bmp_data[39]  =   8'h0e   ;
    wr_bmp_data[40]  =   8'h00   ;
    wr_bmp_data[41]  =   8'h00   ;

    wr_bmp_data[42]  =   8'hC4   ;
    wr_bmp_data[43]  =   8'h0e   ;
    wr_bmp_data[44]  =   8'h00   ;
    wr_bmp_data[45]  =   8'h00   ;
    //use color board
    wr_bmp_data[46]  =   8'h00   ;
    wr_bmp_data[47]  =   8'h00   ;
    //important color 
    wr_bmp_data[48]  =   8'h00   ;
    wr_bmp_data[49]  =   8'h00   ;
    wr_bmp_data[50]  =   8'h00   ;
    wr_bmp_data[51]  =   8'h00   ;
    wr_bmp_data[52]  =   8'h00   ;
    wr_bmp_data[53]  =   8'h00   ;

    j=0;
    for(i=0;i<FILE_SIZE;i=i+1)
    begin
        if(j%(W_BMP_WIDTH*3)==0)begin //RGB888=24bit
        j=0;
        end


        rd_data[7:0]=wr_bmp_data[i];
        wr_en=1;

        if(i>=54)   begin
        rd_data[7:0]=j/3;
        j=j+1;

        rd_data[7:0]=8'hff-rd_bmp_data[i];
        end     


        @(posedge clk)
        // write image file
        $fwrite(out_image,"%c",rd_data[7:0]);

        //write data txt file
        $fwrite(out_file,"%d ",wr_bmp_data[i]);
        if(j%(W_BMP_WIDTH*3)==0)begin
        $fwrite(out_file,"\n");end            
    end

    wr_en=0;
    $fclose(fileID);
    $fclose(out_image);
    $fclose(out_file);

    end
endmodule   

现在已经完成BMP文件的读写操作,并且对图片进行了像素取反rd_data[7:0]=8'hff-rd_bmp_data[i];

图像结果

处理后的图片结果存放output_file.bmp,显示如下图

处理前和处理后结果对比

读写BMP文件显示波形如下图

获取工程

小编已经将读写BMP文件的工程放在了网盘上,话不多说来干货,下面给出链接。

工程链接

百度网盘链接:https://pan.baidu.com/s/1uzDckGoEUk7NGwWm7jhXLw提取码:zpiq

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-04-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 瓜大三哥 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • BMP文件结构
  • 读写BMP文件
  • 图像结果
  • 获取工程
相关产品与服务
图数据库 KonisGraph
图数据库 KonisGraph(TencentDB for KonisGraph)是一种云端图数据库服务,基于腾讯在海量图数据上的实践经验,提供一站式海量图数据存储、管理、实时查询、计算、可视化分析能力;KonisGraph 支持属性图模型和 TinkerPop Gremlin 查询语言,能够帮助用户快速完成对图数据的建模、查询和可视化分析。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档