iCub-main
VideoTexture.cpp
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4 * Copyright (C) 2010 RobotCub Consortium, European Commission FP6 Project IST-004370
5 * Author: Paul Fitzpatrick
6 * email: paulfitz@alum.mit.edu
7 * website: www.robotcub.org
8 * Permission is granted to copy, distribute, and/or modify this program
9 * under the terms of the GNU General Public License, version 2 or any
10 * later version published by the Free Software Foundation.
11 *
12 * A copy of the license can be found at
13 * http://www.robotcub.org/icub/license/gpl.txt
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18 * Public License for more details
19 */
20 
28 #include "VideoTexture.h"
29 
30 #include "SDL.h"
31 #include "SDL_opengl.h"
32 
33 using namespace yarp::os;
34 using namespace yarp::sig;
35 
36 #define SQUARE_DIM 512
37 
38 
40  for (unsigned int i=0; i< inputs.size(); i++) {
41  delete inputs[i];
42  inputs[i] = NULL;
43  }
44  inputs.clear();
45 }
46 
47 
48 void VideoTexture::apply(unsigned int *textures) {
49  for (unsigned int i=0; i<inputs.size(); i++) {
50  inputs[i]->apply(textures);
51  }
52 }
53 bool VideoTexture::add(Searchable& config) {
54  TextureInput *entry = new TextureInput;
55  entry->setName(videoPort);
56 
57  if (entry!=NULL) {
58  bool ok = entry->open(config);
59  if (!ok) {
60  delete entry;
61  entry = NULL;
62  } else {
63  inputs.push_back(entry);
64 
65  return true;
66  }
67  }
68  return false;
69 }
70 
71 void VideoTexture::setName(string name) {
72  this->videoPort = name;
73 }
74 
75 void TextureInput::setName(string name) {
76  this->moduleName = name;
77 }
78 
79 bool TextureInput::open(Searchable& config) {
80  textureIndex = config.check("textureIndex",Value(-1),
81  "texture index").asInt();
82 
83  string texturePort = config.check("port",Value("/texture"),"local port name").asString();
84 
85  string portStr = this->moduleName + texturePort.c_str();
86  port.open( portStr.c_str() );
87 
88  return true;
89 }
90 
91 
92 
93 void TextureInput::apply(unsigned int *textures) {
94  if (cache.width()!=SQUARE_DIM || cache.height()!=SQUARE_DIM) {
95  cache.setQuantum(1);
96  cache.setTopIsLowIndex(false);
97  cache.resize(SQUARE_DIM,SQUARE_DIM);
98  cache.zero();
99  }
100  ImageOf<PixelRgb> *img = port.read(false);
101  unsigned char *data = (unsigned char*)cache.getRawImage();
102  int width = SQUARE_DIM;
103  int height = SQUARE_DIM;
104  double now = Time::now();
105  bool haveUpdate = false;
106  if (img!=NULL) {
107  cache.copy(*img,cache.width(),cache.height());
108  lastData = now;
109  haveUpdate = true;
110  }
111  /*if (now-lastData>2000) {
112  for (int x=0; x<width; x++) {
113  for (int y=0; y<height; y++) {
114  unsigned char *pix1 = data + (y*height+x)*3;
115  unsigned char *pix2 = pix1+1;
116  unsigned char *pix3 = pix1+2;
117  *pix1 = (x%25+t)*10;;
118  *pix2 = 255;
119  *pix3 = (y*25+t/2)*10;
120  }
121  }
122  t++;
123  haveUpdate = true;
124  }
125  */
126  if (haveUpdate && port.getInputCount()>0) {
127  glBindTexture(GL_TEXTURE_2D, textures[textureIndex]);
128  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height,
129  GL_RGB, GL_UNSIGNED_BYTE, data);
130  }
131 
132 }
133 
134 
void setName(string module)
Header file for the video stream texture.
void setName(string module)
bool add(const char *port, int textureIndex)
Definition: VideoTexture.h:78
static int width
Definition: iCub_Sim.cpp:53
static int height
Definition: iCub_Sim.cpp:54
bool open(yarp::os::Searchable &config)
void apply(unsigned int *textures)
#define SQUARE_DIM
void apply(unsigned int *textures)