打印

RGB565转BMP格式 C语言程序

[复制链接]
182|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
你说说说说|  楼主 | 2018-9-26 17:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
以前做过一个项目 ,就是从屏幕上抓图片下来。我先把RGB图片转BMP的程序分享给大家,这个程序可以在LINUX 系统下执行。压缩包中有750K的RGB 文件,也有1126K的已转化文件。大家可以自己试试。tu0 tu1 tu3 zzz.txt都是原始图片  .bmp的是经过a.out 转化过后的输出文件。我是从某火的429开发板的配套屏幕上截取的图,用的是某火配套的摄像头模块。











单片机源程序如下:

#include<string.h>

#include<stdlib.h>

#include<stdio.h>

#include"rgb2bmp.h"



int RGB2BMP(char *,int ,int ,FILE *);



int main(int argc,char *argv[]){



        double  num_tmp = 0;



        FILE *p;

/***************  input data  ***********

filename      :RGB数据文件名称

nWidth        :所生成文件的水平像素

nHeight       :所生成文件的垂直像素

newFile       :最终生成文件的名称

**********************************************/

        size_t read_size;

        size_t write_size;

        printf("argv[1]= %s\n",argv[1]);

        printf("argv[2]= %s\n",argv[2]);

        char * filename = "rgb565_800_480_woman";

        int nWidth = 800;

        int nHeight = 480;

        char *newFile = "rgb565_800_480_woman_0x7f.bmp";

        if(argc!=3){

                printf("Usage:./a.out <picturename> <newpicturename.bmp>\n");

                return -1;

        }

        filename = argv[1];

        newFile = argv[2];

        p = fopen(filename,"rb");

        if( p == NULL){

                printf("open file %s error \n",filename);

                return 0;

        }

        printf("open file %s success\n",filename);

/*************** read Image Data **********/

        long nData = nWidth *nHeight;

        unsigned short * rgb_buffer = malloc(nData * sizeof(short));

        //read_size = fread(rgb_buffer,2,nData,p);

        //printf("rgb_buffer[384000] = %d\n",*(rgb_buffer+383980));

        read_size = fread(rgb_buffer,2,384000,p);

        //printf("rgb_buffer[384000] = %d\n",*(rgb_buffer+383980));



        printf("fread 读取到的字节数是 %ld\n",read_size);

        unsigned long total = nWidth*nHeight*3;

        unsigned char *pVisit = malloc(total*sizeof(char));

        unsigned char *tmp = pVisit;

        long i = 0;

        unsigned char R,G,B;

        unsigned short *free1= rgb_buffer;

        unsigned char *free2= pVisit;

        while(i<nData){

                R = *rgb_buffer&0x1f;

                G = (*rgb_buffer>>5)&0x3f;

                B = (*rgb_buffer>>11)&0x1f;

                /*

                B<<3;

                G<<2;

                R<<3;

                */

                num_tmp = R;

                num_tmp/= 31;

                R = num_tmp * 255;



                num_tmp = G;

                num_tmp/= 63;

                G = num_tmp * 255;



                num_tmp = B;

                num_tmp/= 31;

                B = num_tmp * 255;



                *pVisit = R;pVisit++;

                *pVisit = G;pVisit++;

                *pVisit = B;pVisit++;

                rgb_buffer++;

                i++;

        }

        //free(free1);

        printf("read file over. nData = %ld\n",nData);

        fclose(p);

        p = NULL;

        /*************************************/

        /**************** write file *********/



        FILE *result = fopen(newFile,"wb");

        if(result == NULL){

                printf("open new file failed");

                return -1;

        }

        RGB2BMP(tmp,nWidth,nHeight,result);

        printf("total = %ld\n",total);

        /*

        write_size =  fwrite(((char *)pVisit),1,total/3,result);

        printf("write_size = %ld \n",write_size);

        write_size =  fwrite(((char *)pVisit)+total/3,1,total/3,result);

        printf("write_size = %ld \n",write_size);

        write_size =  fwrite(((char *)pVisit)+total/3*2,1,total/3,result);

        */

        write_size =  fwrite(free2,1,1152000,result);

        printf("write_size = %ld \n",write_size);

        /*

        write_size =  fwrite(pVisit+752000,1,500000,result);

        printf("write_size = %ld \n",write_size);

        */

        fclose(result);

        result = NULL;

        free(free2);

        return 0;

}



int RGB2BMP(char *rgb_buffer,int nWidth,int nHeight,FILE *fp1){

        BmpHead m_BMPHeader;

        char bfType[2] = {'B','M'};

        m_BMPHeader.imageSize = 3*nWidth*nHeight + 54;

        m_BMPHeader.blank=0;

        m_BMPHeader.startPosition=54;

        //m_BMPHeader.startPosition=1078;



        fwrite(bfType,sizeof(bfType),1,fp1);

        fwrite(&m_BMPHeader.imageSize,sizeof(m_BMPHeader.imageSize),1,fp1);

        fwrite(&m_BMPHeader.blank,sizeof(m_BMPHeader.blank),1,fp1);

        fwrite(&m_BMPHeader.startPosition,sizeof(m_BMPHeader.startPosition),1,fp1);



        InfoHead m_BMPInfoHeader;

        m_BMPInfoHeader.Length=40;

        m_BMPInfoHeader.width = nWidth;

        m_BMPInfoHeader.height = nHeight;

        m_BMPInfoHeader.colorPlane = 1;

        m_BMPInfoHeader.bitColor = 24;

        m_BMPInfoHeader.zipFormat = 0;

        m_BMPInfoHeader.realSize = 3*nHeight*nWidth;

        m_BMPInfoHeader.xPels=2835;

        m_BMPInfoHeader.yPels=2835;

        m_BMPInfoHeader.colorUse=0;

        m_BMPInfoHeader.colorImportant=0;



        fwrite(&m_BMPInfoHeader.Length,sizeof(m_BMPInfoHeader.Length),1,fp1);

        fwrite(&m_BMPInfoHeader.width,sizeof(m_BMPInfoHeader.width),1,fp1);

        fwrite(&m_BMPInfoHeader.height,sizeof(m_BMPInfoHeader.height),1,fp1);

        fwrite(&m_BMPInfoHeader.colorPlane,sizeof(m_BMPInfoHeader.colorPlane),1,fp1);

        fwrite(&m_BMPInfoHeader.bitColor,sizeof(m_BMPInfoHeader.bitColor),1,fp1);

        fwrite(&m_BMPInfoHeader.zipFormat,sizeof(m_BMPInfoHeader.zipFormat),1,fp1);

        fwrite(&m_BMPInfoHeader.realSize,sizeof(m_BMPInfoHeader.realSize),1,fp1);

        fwrite(&m_BMPInfoHeader.xPels,sizeof(m_BMPInfoHeader.xPels),1,fp1);

        fwrite(&m_BMPInfoHeader.yPels,sizeof(m_BMPInfoHeader.yPels),1,fp1);

        fwrite(&m_BMPInfoHeader.colorUse,sizeof(m_BMPInfoHeader.colorUse),1,fp1);

        fwrite(&m_BMPInfoHeader.colorImportant,sizeof(m_BMPInfoHeader.colorImportant),1,fp1);

        return 0;

}





复制代码

所有资料51hei提供下载:







RGB2.zip

(1.9 MB, 下载次数: 9)



2019-2-16 10:45 上传
点击文件名下载附件

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

355

主题

355

帖子

1

粉丝