CCS+C6678LE开发记录10:(图像)数据处理与传输框架
探索了这么久也该上一点料,本文关注PC<-->DSP数据处理与传输的简单框架。 这个框架其实也很简单,具体描述如下: 1.PC端通过ETH0发送(图像)数据到DSP 2.DSP收到完整数据后进行处理(Image Processing...) 3.DSP端数据处理完毕后将数据发回给PC 4.PC端收到发回的处理结果,然后保存
下面是一段代码,数据处理执行的操作很简单:对图像像素值取补 newValue=255-oldValue - int udpTransferTask( SOCKET s, UINT32 unused )
- {
- printf("TASK execution %d\n",++task_counter);
-
- struct timeval tv;
- tv.tv_sec = 1;
- tv.tv_usec = 0;
- setsockopt(s,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv));
- setsockopt(s,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv));
-
- struct sockaddr_in sin1;
- int sz=sizeof(sin1);
- int hlen=strlen(reply);
- int nr,total=0;
- HANDLE hBuffer=NULL;
- unsigned char* pBuf=NULL;
-
- int width,height,size;
- int i,n=sizeof(int);
- unsigned char* ptr=NULL;
-
- // Image width
- recvncfrom( s, (void**)&pBuf, 0, (PSA)&sin1, &sz, &hBuffer );
- recvncfree( hBuffer );
- ptr=(unsigned char*)&width;
- for(i=0;i<n;++i) ptr=pBuf;
-
- // Image height
- recvncfrom( s, (void**)&pBuf, 0, (PSA)&sin1, &sz, &hBuffer );
- recvncfree( hBuffer );
- ptr=(unsigned char*)&height;
- for(i=0;i<n;++i) ptr=pBuf;
-
- // Image data size
- size=width*height;
-
- printf("Image Size: %d * %d\n",width,height);
-
- // Data buffer
- unsigned char* data=(unsigned char*)malloc(size);
- if(data==NULL)
- {
- printf("Failed to allocate memory!\n");
- return 2;
- }
-
- ptr=data;
- while(1)
- {
- nr=recvncfrom( s, (void**)&pBuf, 0, (PSA)&sin1, &sz, &hBuffer );
- if(nr<=0) break;
- total+=nr;
- for(i=0;i<nr;++i)
- {
- ptr=pBuf;
- }
- ptr+=nr;
- sendto( s, reply, hlen, 0, (PSA)&sin1, sz );
- recvncfree( hBuffer );
- }
-
- if(total!=size)
- {
- free(data);
- size=0;
- printf("Data transfer is imcomplete!\n");
- return 1;
- }
-
- processDataFrame(data,width,height);
-
- total=0;
- int unit=64;
- int loop=size/unit;
- int residue=size-loop*unit;
- pBuf=data;
- for(i=1;i<=loop;++i)
- {
- nr=sendto(s,pBuf,unit,0,(PSA)&sin1, sz);
- pBuf+=unit;
- total+=nr;
- }
- if(residue>0)
- {
- nr=sendto(s,pBuf,residue,0,(PSA)&sin1, sz);
- total+=nr;
- }
- printf("Finished sending back %dB.\n",total);
-
- free(data);
- return 1;
- }
-
- void processDataFrame(unsigned char* data,int width,int height)
- {
- printf("Processing data...");
- int i, size=width*height;
- unsigned char tmp;
- for(i=0;i<size;++i)
- {
- tmp=data;
- data=255-tmp;
- }
- printf("Finished.\n");
- }
[color=rgb(51, 102, 153) !important]复制代码
发回数据时建议设置更小的分片大小,这里设置为64字节,实测效果尚可。
|