以前做过一个项目 ,就是从屏幕上抓图片下来。我先把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&quot;rgb2bmp.h&quot;
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(&quot;argv[1]= %s\n&quot;,argv[1]);
printf(&quot;argv[2]= %s\n&quot;,argv[2]);
char * filename = &quot;rgb565_800_480_woman&quot;;
int nWidth = 800;
int nHeight = 480;
char *newFile = &quot;rgb565_800_480_woman_0x7f.bmp&quot;;
if(argc!=3){
printf(&quot;Usage:./a.out <picturename> <newpicturename.bmp>\n&quot;);
return -1;
}
filename = argv[1];
newFile = argv[2];
p = fopen(filename,&quot;rb&quot;);
if( p == NULL){
printf(&quot;open file %s error \n&quot;,filename);
return 0;
}
printf(&quot;open file %s success\n&quot;,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(&quot;rgb_buffer[384000] = %d\n&quot;,*(rgb_buffer+383980));
read_size = fread(rgb_buffer,2,384000,p);
//printf(&quot;rgb_buffer[384000] = %d\n&quot;,*(rgb_buffer+383980));
printf(&quot;fread 读取到的字节数是 %ld\n&quot;,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(&quot;read file over. nData = %ld\n&quot;,nData);
fclose(p);
p = NULL;
/*************************************/
/**************** write file *********/
FILE *result = fopen(newFile,&quot;wb&quot;);
if(result == NULL){
printf(&quot;open new file failed&quot;);
return -1;
}
RGB2BMP(tmp,nWidth,nHeight,result);
printf(&quot;total = %ld\n&quot;,total);
/*
write_size = fwrite(((char *)pVisit),1,total/3,result);
printf(&quot;write_size = %ld \n&quot;,write_size);
write_size = fwrite(((char *)pVisit)+total/3,1,total/3,result);
printf(&quot;write_size = %ld \n&quot;,write_size);
write_size = fwrite(((char *)pVisit)+total/3*2,1,total/3,result);
*/
write_size = fwrite(free2,1,1152000,result);
printf(&quot;write_size = %ld \n&quot;,write_size);
/*
write_size = fwrite(pVisit+752000,1,500000,result);
printf(&quot;write_size = %ld \n&quot;,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;
}
复制代码 |