00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "Vision.h"
00030 #include "VisionSerializer.h"
00031 #include "Wireless/Wireless.h"
00032 #include "Shared/Config.h"
00033 #include "Shared/get_time.h"
00034 #include <iostream>
00035
00036 VisionSerializer::VisionSerializer() : visRaw(NULL), visRLE(NULL) {
00037 visRaw=wireless->socket(SocketNS::SOCK_STREAM, 1024, 80000);
00038
00039 visRLE=wireless->socket(SocketNS::SOCK_STREAM, 1024, 40000);
00040
00041
00042 wireless->setDaemon(visRaw);
00043 wireless->setDaemon(visRLE);
00044 wireless->listen(visRaw, config->vision.raw_port);
00045 wireless->listen(visRLE, config->vision.rle_port);
00046 }
00047
00048 void
00049 VisionSerializer::serialize() {
00050 char *buf=(char*)visRaw->getWriteBuffer(79999);
00051 if (buf)
00052 switch (config->vision.raw_encoding) {
00053 case packet_visionraw_yfull_uvhalf:
00054 encodeHeader(&buf, packet_visionraw_yfull_uvhalf);
00055 encodeVisionRaw_YhalfUVquarter(buf,vision->img);
00056 visRaw->write(8+(176*144*3)/2);
00057 break;
00058 case packet_visionraw_full:
00059 encodeHeader(&buf, packet_visionraw_full);
00060 encodeVisionRaw(buf,vision->img,1);
00061 visRaw->write(8+176*144*3);
00062 break;
00063 case packet_visionraw_half:
00064 default:
00065 encodeHeader(&buf, packet_visionraw_half);
00066 encodeVisionRaw(buf,vision->img,2);
00067 visRaw->write(8+88*72*3);
00068 break;
00069 }
00070
00071 buf=(char*)visRLE->getWriteBuffer(39999);
00072
00073 if (buf) {
00074 encodeVisionRLE((char *)buf, vision->num_runs, vision->rmap);
00075 visRLE->write(4+vision->num_runs*3);
00076 }
00077 }
00078
00079 char *
00080 VisionSerializer::encodeVisionRun(char *buf, run *rn) {
00081 buf[0]=(uchar)rn->color;
00082 buf[1]=(uchar)rn->x;
00083 buf[2]=(uchar)rn->width;
00084 return buf+3;
00085 }
00086
00087 void
00088 VisionSerializer::encodeVisionRaw(char *buf,
00089 CMVision::image_yuv<const uchar> &img,
00090 int scale) {
00091 const uchar *row_y,*row_u,*row_v;
00092 int height, width;
00093 height = img.height;
00094 width = img.width;
00095
00096 for(int y=0; y<height; y+=scale) {
00097 row_y = img.buf_y + y*img.row_stride;
00098 row_u = img.buf_u + y*img.row_stride;
00099 row_v = img.buf_v + y*img.row_stride;
00100 for(int x=0; x<width; x+=scale) {
00101 *(buf )=row_y[x];
00102 *(buf+1)=row_u[x];
00103 *(buf+2)=row_v[x];
00104 buf+=3;
00105 }
00106 }
00107 }
00108
00109 void
00110 VisionSerializer::encodeVisionRaw_YhalfUVquarter(char *buf,
00111 CMVision::image_yuv<const uchar> &img) {
00112 const uchar *row_y,*row_u,*row_v;
00113 int height, width;
00114 height = img.height;
00115 width = img.width;
00116
00117 for(int y=0; y<height; y++) {
00118 row_y = img.buf_y + y*img.row_stride;
00119 row_u = img.buf_u + y*img.row_stride;
00120 row_v = img.buf_v + y*img.row_stride;
00121 for(int x=0; x<width; x++) {
00122 *(buf )=row_y[x];
00123 if (!(y&1) && !(x&1)) {
00124 *(buf+1)=row_u[x];
00125 *(buf+2)=row_v[x];
00126 buf+=3;
00127 } else {
00128 buf++;
00129 }
00130 }
00131 }
00132 }
00133
00134 void
00135 VisionSerializer::encodeVisionRLE(char *buf,int num_runs,run *runs) {
00136 encode(&buf,num_runs);
00137 for (; num_runs>0; num_runs--, runs++) {
00138 buf=encodeVisionRun(buf,runs);
00139 }
00140 };
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154