Homepage Demos Overview Downloads Tutorials Reference
Credits

VisionSerializer.cc

Go to the documentation of this file.
00001 /*=========================================================================
00002     CMPack'02 Source Code Release for OPEN-R SDK v1.0
00003     Copyright (C) 2002 Multirobot Lab [Project Head: Manuela Veloso]
00004     School of Computer Science, Carnegie Mellon University
00005   -------------------------------------------------------------------------
00006     This software is distributed under the GNU General Public License,
00007     version 2.  If you do not have a copy of this licence, visit
00008     www.gnu.org, or write: Free Software Foundation, 59 Temple Place,
00009     Suite 330 Boston, MA 02111-1307 USA.  This program is distributed
00010     in the hope that it will be useful, but WITHOUT ANY WARRANTY,
00011     including MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00012   -------------------------------------------------------------------------
00013     Additionally licensed to Sony Corporation under the following terms:
00014 
00015     This software is provided by the copyright holders AS IS and any
00016     express or implied warranties, including, but not limited to, the
00017     implied warranties of merchantability and fitness for a particular
00018     purpose are disclaimed.  In no event shall authors be liable for
00019     any direct, indirect, incidental, special, exemplary, or consequential
00020     damages (including, but not limited to, procurement of substitute
00021     goods or services; loss of use, data, or profits; or business
00022     interruption) however caused and on any theory of liability, whether
00023     in contract, strict liability, or tort (including negligence or
00024     otherwise) arising in any way out of the use of this software, even if
00025     advised of the possibility of such damage.
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   // 80000 bytes for use upto 176x144 only
00039   visRLE=wireless->socket(SocketNS::SOCK_STREAM, 1024, 40000);
00040   // 40000 bytes is an approximation intended for 176x144
00041   // increase or decrease depending on missing lines
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 /*! @file
00143  * @brief Implements VisionSerializer, which encodes and transmits camera images
00144  * @author CMU RoboSoccer 2001-2002 (Creator)
00145  * @author alokl (Ported)
00146  * 
00147  * @verbinclude CMPack_license.txt
00148  *
00149  * $Author: alokl $
00150  * $Name: tekkotsu-1_5 $
00151  * $Revision: 1.6 $
00152  * $State: Rel $
00153  * $Date: 2003/07/28 06:53:09 $
00154  */

Tekkotsu v1.5
Generated Fri Oct 10 15:52:00 2003 by Doxygen 1.3.4