DSP

DM642学习点滴三:将DSP内存中的图像数据保存为BMP图像

2019-07-13 10:15发布

好久没做DM642的基本实验了,今天做了一下实验19——生成图像文件的实验,仔细看了下程序代码,觉得对以后的工作可能还有借鉴意义,就做些笔记吧。 实验基本步骤呢,比较简单。将program load至DSP后运行,再用GEL文件控制命令冻结当前图像帧,程序将YCbCr格式转换成RGB格式,再将RGB数据写入新创建的BMP图像中。由于要通过仿真器将数据传至PC机,而我的仿真器是比较一般的510,速度比较慢。大概整个过程需要2分钟左右。下面附上部分程序代码: void ICETEKDM642PCIYUVRGB() { int i,j; int r,g,b,y,u,v; unsigned char *py,*pu,*pv,*pr,*pg,*pb; py=m_dbFrameY; pu=m_dbFrameU; pv=m_dbFrameV; pr=m_dbTargetImageR; pg=m_dbTargetImageG; pb=m_dbTargetImageB; for ( i=0;i255 ) r=255; else if ( r<0 ) r=0; if ( g>255 ) g=255; else if ( g<0 ) g=0; if ( b>255 ) b=255; else if ( b<0 ) b=0; (*pr)=r; pr++; (*pg)=g; pg++; (*pb)=b; pb++; py++; y=(*py); r=y+1.402*u; g=y-0.34414*u-0.71414*v; b=y+1.772*v; if ( r>255 ) r=255; else if ( r<0 ) r=0; if ( g>255 ) g=255; else if ( g<0 ) g=0; if ( b>255 ) b=255; else if ( b<0 ) b=0; (*pr)=r; pr++; (*pg)=g; pg++; (*pb)=b; pb++; py++; pu++; pv++; } } SaveToBMP(); } void SaveToBMP() { FILE *fp; unsigned char *pRGB,*pr,*pg,*pb,*pR,*pG,*pB; int j,i; unsigned int *pk; pRGB=(unsigned char *)headerImage; for ( i=0;i<54;i++,pRGB++ ) (*pRGB)=0; pRGB=(unsigned char *)headerImage; (*pRGB)=0x42; pRGB++; // BM (*pRGB)=0x4d; pRGB++; pRGB+=2; pk=(unsigned int *)pRGB; (*pk)=14+40+180*144*3; // size pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=0; // reserved pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=54; // image offset pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=40; // struct size pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=720; // width pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=576; // height pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=0x180001; // Planes & BitCount pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=0; // Compression pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=720*576*3; // image size pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=3780; // x pixels/m pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=3780; // y pixels/m pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=0; // color used pRGB+=4; pk=(unsigned int *)pRGB; (*pk)=0; // important color if ( fp=fopen("Video.bmp","wb+") ) { printf("Create a file named Video.bmp "); pRGB=headerImage; fwrite(pRGB,2,1,fp); fwrite(pRGB+4,52,1,fp); pR=m_dbTargetImageR+720*575; pG=m_dbTargetImageG+720*575; pB=m_dbTargetImageB+720*575; printf("Saving image data... "); for ( j=0;j<576;j++ ) { pr=pR; pg=pG; pb=pB; for ( i=0;i<720;i++,pr++,pg++,pb++ ) { fwrite(pb,1,1,fp); fwrite(pg,1,1,fp); fwrite(pr,1,1,fp); } pR-=720; pG-=720; pB-=720; if ( j%57==0 ) printf(" completed %d%% ",j/57*10); } fclose(fp); printf("Finished. "); } } 这里涉及到了BMP文件格式的细节。我百度了下,看到这篇博文讲到了相关内容,可对应作参考,这里不再详述。 http://www.cnblogs.com/king-77024128/articles/2056805.html 程序最终生成的文件,可以用UEStudio去仔细分析他的数据结构。