1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * File:   Image.cpp
 * Author: Oliver Welter
 *
 * Created on 11. Dezember 2008, 17:24
 */

#include <iterator>
#include <string>

#include "Image.h"
#include "Messages/Framework-Representations.pb.h"
#include <google/protobuf/io/zero_copy_stream_impl.h>

using namespace naoth;
using namespace std;

Image::Image()
  :
  selfCreatedImage(true),
  yuv422(NULL),
  timestamp(0),
  currentBuffer(0),
  bufferCount(0),
  wrongBufferSizeCount(0)
  
{
  yuv422 = new unsigned char[data_size()];

  // initialize the image with gray
  std::memset(yuv422, 128, data_size());
}

Image::Image(const Image& orig)
{
  cameraInfo = orig.cameraInfo;
  if(selfCreatedImage) {
    delete[] yuv422;
  }

  yuv422 = new unsigned char[data_size()];
  std::memcpy(yuv422, orig.yuv422, data_size());
  selfCreatedImage = true;
}

Image& Image::operator=(const Image& orig)
{
  cameraInfo = orig.cameraInfo;
  if(selfCreatedImage) {
    delete[] yuv422;
  }

  yuv422 = new unsigned char[data_size()];
  std::memcpy(yuv422, orig.yuv422, data_size());
  selfCreatedImage = true;

  return *this;
}

Image::~Image()
{
  if (selfCreatedImage) {
    delete[] yuv422;
  }
}

void Image::setCameraInfo(const CameraInfo& ci)
{
  if (cameraInfo.resolutionHeight != ci.resolutionHeight || cameraInfo.resolutionWidth != ci.resolutionWidth)
  {
    if(selfCreatedImage) {
      delete[] yuv422;
    }
    cameraInfo = ci;
    yuv422 = new unsigned char[data_size()];
  }
}

void Image::wrapImageDataYUV422(unsigned char* data, const unsigned int size)
{
  ASSERT(size == data_size());
  if(selfCreatedImage) {
    delete[] yuv422;
  }

  yuv422 = data;
  selfCreatedImage = false;
}

void Image::copyImageDataYUV422(const unsigned char* data, const unsigned int size)
{
  ASSERT(size == data_size());
  
  // create own buffer if necessary
  if(!selfCreatedImage) {
    yuv422 = new unsigned char[data_size()];
    selfCreatedImage = true;
  }

  // just overwrite the old image data
  memcpy(yuv422, data, size);
}

void Image::print(ostream& stream) const
{
  stream  << "Timestamp: " << timestamp << endl
          << "Image Buffer: "<< currentBuffer << " / " << bufferCount << endl
          << "Wrong Buffer Size Count: "<< wrongBufferSizeCount << endl
          << "Camera Info:"<< endl
          << "============"<< endl
          << cameraInfo << endl
          << "============"<< endl
          ;
}//end print


void Serializer<Image>::serialize(const Image& representation, std::ostream& stream)
{
  // the data has to be converted to a YUV (1 byte for each) array. no interlacing
  naothmessages::Image img;

  img.set_height(representation.height());
  img.set_width(representation.width());
  img.set_format(naothmessages::Image_Format_YUV422);
  // FIXME: set_data calls std::string(..) which creates a copy of the whole data.
  //        Can we make it zero-copy somehow?
  img.set_data(representation.data(), representation.data_size());

  google::protobuf::io::OstreamOutputStream buf(&stream);
  img.SerializeToZeroCopyStream(&buf);
}

void Serializer<Image>::deserialize(std::istream& stream, Image& representation)
{
  naothmessages::Image img;

  google::protobuf::io::IstreamInputStream buf(&stream);
  img.ParseFromZeroCopyStream(&buf);

  //TODO: deprecated
  if(img.width() != (int)naoth::IMAGE_WIDTH || img.height() != (int)naoth::IMAGE_HEIGHT) {
	  THROW("Image size (" << img.width() << "," << img.height() << ") doesn't correspond to the static values IMAGE_WIDTH (" << naoth::IMAGE_WIDTH << ") and IMAGE_HEIGHT (" << naoth::IMAGE_HEIGHT << ").");
  }

  // YUV444
  if(img.format() == naothmessages::Image_Format_YUV)
  {
    // check the integrity
    ASSERT(img.data().size() != Image::PIXEL_SIZE_YUV444 * img.width() * img.height());

    CameraInfo newCameraInfo;
    newCameraInfo.resolutionHeight = img.height();
    newCameraInfo.resolutionWidth = img.width();
    representation.setCameraInfo(newCameraInfo);

    const unsigned char* data = reinterpret_cast<const unsigned char*>(img.data().c_str());

    // HACK: copy the image pixel by pixel because the internal structure only 
    //       representa the image in the YUV422 format
    for(unsigned int i=0; i < static_cast<unsigned int>(img.width()*img.height()); i++)
    {
      unsigned int x = i % newCameraInfo.resolutionWidth;
      unsigned int y = i / newCameraInfo.resolutionWidth;

      Pixel p;
      p.y = data[i * 3];
      p.u = data[i * 3 + 1];
      p.v = data[i * 3 + 2];

      representation.set(x,y, p);
    }
  }
  // "native" YUV422 data
  else if(img.format() == naothmessages::Image_Format_YUV422)
  {
    // check the integrity
    ASSERT(img.data().size() == Image::PIXEL_SIZE_YUV422 * img.width() * img.height());

    CameraInfo newCameraInfo;
    newCameraInfo.resolutionHeight = img.height();
    newCameraInfo.resolutionWidth = img.width();
    representation.setCameraInfo(newCameraInfo);
    
    const unsigned char* data = reinterpret_cast<const unsigned char*>(img.data().c_str());
    const unsigned int size = static_cast<unsigned int>(img.data().size());
    representation.copyImageDataYUV422(data,size);
  } else {
    THROW("Unknown image format.");
  }
}