YARP
Yet Another Robot Platform
Image.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 /*
11  This file is in a pretty hacky state. Sorry!
12 
13 */
14 
15 #include <yarp/sig/Image.h>
16 
17 #include <yarp/os/Bottle.h>
20 #include <yarp/os/Log.h>
21 #include <yarp/os/Time.h>
22 #include <yarp/os/Vocab.h>
23 
25 #include <yarp/sig/impl/IplImage.h>
26 #include <yarp/sig/impl/DeBayer.h>
27 
28 #include <cstdio>
29 #include <cstring>
30 #include <string>
31 
32 
33 using namespace yarp::sig;
34 using namespace yarp::os;
35 
36 #define DBGPF1 if (0)
37 
38 //inline int PAD_BYTES (int len, int pad)
39 //{
40 // const int rem = len % pad;
41 // return (rem != 0) ? (pad - rem) : rem;
42 //}
43 
48 inline bool readFromConnection(Image &dest, ImageNetworkHeader &header, ConnectionReader& connection)
49 {
50  dest.resize(header.width, header.height);
51  unsigned char *mem = dest.getRawImage();
52  size_t allocatedBytes = dest.getRawImageSize();
53  yAssert(mem != nullptr);
54  //this check is redundant with assertion, I would remove it
55  if (dest.getRawImageSize() != static_cast<size_t>(header.imgSize)) {
56  printf("There is a problem reading an image\n");
57  printf("incoming: width %zu, height %zu, code %zu, quantum %zu, size %zu\n",
58  static_cast<size_t>(header.width),
59  static_cast<size_t>(header.height),
60  static_cast<size_t>(header.id),
61  static_cast<size_t>(header.quantum),
62  static_cast<size_t>(header.imgSize));
63  printf("my space: width %zu, height %zu, code %d, quantum %zu, size %zu\n",
64  dest.width(), dest.height(), dest.getPixelCode(), dest.getQuantum(), allocatedBytes);
65  }
66  yAssert(allocatedBytes == (size_t) header.imgSize);
67  bool ok = connection.expectBlock(reinterpret_cast<char*>(mem), allocatedBytes);
68  return (!connection.isError() && ok);
69 }
70 
71 
72 
73 class ImageStorage {
74 public:
76  char **Data; // this is not IPL. it's char to maintain IPL compatibility
79  size_t quantum;
80  bool topIsLow;
81 
82 protected:
84 
85  int type_id;
86 
87  int is_owner;
88 
89  // ipl allocation is done in two steps.
90  // _alloc allocates the actual ipl pointer.
91  // _alloc_data allocates the image array and data.
92  // memory is allocated in a single chunk. Row ptrs are then
93  // made to point appropriately. This is compatible with IPL and
94  // SOMEONE says it's more efficient on NT.
95  void _alloc ();
96  void _alloc_extern (const void *buf);
97  void _alloc_data ();
98  void _free ();
99  void _free_data ();
100 
101  void _make_independent();
102  bool _set_ipl_header(size_t x, size_t y, int pixel_type, size_t quantum,
103  bool topIsLow);
104  void _free_ipl_header();
105  void _alloc_complete(size_t x, size_t y, int pixel_type, size_t quantum,
106  bool topIsLow);
107  void _free_complete();
108 
109 
110  // computes the # of padding bytes. These are always at the end of the row.
111  int _pad_bytes (size_t linesize, size_t align) const;
112 
113  inline int GetPadding() const {
114  return _pad_bytes (pImage->width * pImage->nChannels,
116  }
117 
118 public:
119  ImageStorage(Image& owner) : owner(owner) {
120  type_id = 0;
121  pImage = nullptr;
122  Data = nullptr;
123  is_owner = 1;
124  quantum = 0;
125  topIsLow = true;
126  extern_type_id = 0;
127  extern_type_quantum = -1;
128  }
129 
131  _free_complete();
132  }
133 
134  void resize(size_t x, size_t y, int pixel_type,
135  size_t quantum, bool topIsLow);
136 
137  void _alloc_complete_extern(const void *buf, size_t x, size_t y, int pixel_type,
138  size_t quantum, bool topIsLow);
139  int getTypeId();
140 
141 };
142 
143 
144 void ImageStorage::resize(size_t x, size_t y, int pixel_type,
145  size_t quantum, bool topIsLow) {
146  int need_recreation = 1;
147 
148  if (quantum==0) {
149  quantum = YARP_IMAGE_ALIGN;
150  }
151 
152  if (need_recreation) {
153  _free_complete();
154  DBGPF1 printf("HIT recreation for %p %p: %zu %zu %d\n", static_cast<void*>(this), static_cast<void*>(pImage), x, y, pixel_type);
155  _alloc_complete (x, y, pixel_type, quantum, topIsLow);
156  }
157  extern_type_id = pixel_type;
158  extern_type_quantum = quantum;
159 }
160 
166  return type_id;
167 }
168 
169 
170 // allocates an empty image.
172 
173 
174  _free(); // was iplDeallocateImage(pImage); but that won't work with refs
175 
176  if ((type_id == VOCAB_PIXEL_MONO_FLOAT) ||
177  (type_id == VOCAB_PIXEL_RGB_FLOAT) ||
178  (type_id == VOCAB_PIXEL_HSV_FLOAT)) {
179  iplAllocateImageFP(pImage, 0, 0);
180  } else {
181  iplAllocateImage (pImage, 0, 0);
182  }
183 
185 }
186 
187 // installs an external buffer as the image data
188 void ImageStorage::_alloc_extern (const void *buf)
189 {
190  yAssert(pImage != nullptr);
191  yAssert(Data==nullptr);
192 
193  if (pImage != nullptr) {
194  if (pImage->imageData != nullptr) {
195  iplDeallocateImage (pImage);
196  }
197  }
198 
199  //iplAllocateImage (pImage, 0, 0);
200  pImage->imageData = (char*)buf;
201  // probably need to do more for real IPL
202 
203  //iplSetBorderMode (pImage, IPL_BORDER_CONSTANT, IPL_SIDE_ALL, 0);
204 }
205 
206 // allocates the Data pointer.
208 {
209  DBGPF1 printf("alloc_data1\n"), fflush(stdout);
210  yAssert(pImage != nullptr);
211 
212  yAssert(Data==nullptr);
213 
214  char **ptr = new char *[pImage->height];
215 
216  Data = ptr;
217 
218  yAssert(Data != nullptr);
219 
220  yAssert(pImage->imageData != nullptr);
221 
222  int height = pImage->height;
223 
224  char * DataArea = pImage->imageData;
225 
226  for (int r = 0; r < height; r++)
227  {
228  if (topIsLow) {
229  Data[r] = DataArea;
230  } else {
231  Data[height-r-1] = DataArea;
232  }
233  DataArea += pImage->widthStep;
234  }
235  DBGPF1 printf("alloc_data4\n");
236 }
237 
239 {
240  if (pImage != nullptr) {
241  if (pImage->imageData != nullptr) {
242  if (is_owner) {
243  iplDeallocateImage (pImage);
244  if (Data!=nullptr) {
245  delete[] Data;
246  }
247  } else {
248  if (Data!=nullptr) {
249  delete[] Data;
250  }
251  }
252 
253  is_owner = 1;
254  Data = nullptr;
255  pImage->imageData = nullptr;
256  }
257  }
258 }
259 
261 {
262  yAssert(Data==nullptr); // Now always free Data at same time
263 }
264 
265 
267 {
268  _free();
269  _free_data();
270  _free_ipl_header();
271 }
272 
273 
275 {
276  if (pImage!=nullptr)
277  {
279  }
280  pImage = nullptr;
281 }
282 
283 
284 void ImageStorage::_alloc_complete(size_t x, size_t y, int pixel_type, size_t quantum,
285  bool topIsLow)
286 {
287  _make_independent();
288  _free_complete();
289  _set_ipl_header(x, y, pixel_type, quantum, topIsLow);
290  _alloc ();
291  _alloc_data ();
292 }
293 
294 
295 
297 {
298  // actually I think this isn't really needed -paulfitz
299 }
300 
302 {
304  int depth;
305  const char* colorModel;
306  const char* channelSeq;
307 };
308 
311 
312 const std::map<int, pixelTypeIplParams> pixelCode2iplParams = {
327  {VOCAB_PIXEL_RGB, {3, IPL_DEPTH_8U, "RGB", "RGB" }},
328  {VOCAB_PIXEL_RGBA, {4, IPL_DEPTH_8U, "RGBA", "RGBA"}},
329  {VOCAB_PIXEL_BGRA, {4, IPL_DEPTH_8U, "BGRA", "BGRA"}},
330  {VOCAB_PIXEL_INT, {1, IPL_DEPTH_32S, "GRAY", "GRAY"}},
331  {VOCAB_PIXEL_HSV, {3, IPL_DEPTH_8U, "HSV", "HSV" }},
332  {VOCAB_PIXEL_BGR, {3, IPL_DEPTH_8U, "RGB", "BGR" }},
333  {VOCAB_PIXEL_MONO_SIGNED, {1, IPL_DEPTH_8S, "GRAY", "GRAY"}},
334  {VOCAB_PIXEL_RGB_INT, {3, IPL_DEPTH_32S, "RGB", "RGB" }},
335  {VOCAB_PIXEL_MONO_FLOAT, {1, IPL_DEPTH_32F, "GRAY", "GRAY"}},
336  {VOCAB_PIXEL_RGB_FLOAT, {3, IPL_DEPTH_32F, "RGB", "RGB" }},
337  {-2, iplPixelTypeMono16},
338  {-4, {1, IPL_DEPTH_32S, "GRAY", "GRAY"}}
339 };
340 
341 bool ImageStorage::_set_ipl_header(size_t x, size_t y, int pixel_type, size_t quantum,
342  bool topIsLow)
343 {
344  if (pImage != nullptr) {
345  iplDeallocateImage(pImage);
346  pImage = nullptr;
347  }
348 
349  if (pixel_type == VOCAB_PIXEL_INVALID) {
350  // not a type!
351  printf ("*** Trying to allocate an invalid pixel type image\n");
352  std::exit(1);
353  }
354  if (pixelCode2iplParams.find(pixel_type) == pixelCode2iplParams.end()) {
355  // unknown pixel type. Should revert to a non-IPL mode... how?
356  return false;
357  }
358 
359  const pixelTypeIplParams& param = pixelCode2iplParams.at(pixel_type);
360 
361  if (quantum==0) {
362  quantum = IPL_ALIGN_QWORD;
363  }
364  int origin = topIsLow ? IPL_ORIGIN_TL : IPL_ORIGIN_BL;
365 
366  pImage = iplCreateImageHeader(param.nChannels, 0, param.depth, const_cast<char*>(param.colorModel), const_cast<char*>(param.channelSeq), IPL_DATA_ORDER_PIXEL, origin, quantum, x, y, nullptr, nullptr, nullptr, nullptr);
367 
368  type_id = pixel_type;
369  this->quantum = quantum;
370  this->topIsLow = topIsLow;
371  return true;
372 }
373 
374 void ImageStorage::_alloc_complete_extern(const void *buf, size_t x, size_t y, int pixel_type, size_t quantum, bool topIsLow)
375 {
376  if (quantum==0) {
377  quantum = 1;
378  }
379  this->quantum = quantum;
380  this->topIsLow = topIsLow;
381 
382  _make_independent();
383  _free_complete();
384  _set_ipl_header(x, y, pixel_type, quantum, topIsLow);
385  Data = nullptr;
386  _alloc_extern (buf);
387  _alloc_data ();
388  is_owner = 0;
389 }
390 
391 
392 
393 int ImageStorage::_pad_bytes (size_t linesize, size_t align) const
394 {
395  return yarp::sig::PAD_BYTES (linesize, align);
396 }
397 
398 const std::map<YarpVocabPixelTypesEnum, size_t> Image::pixelCode2Size = {
399  {VOCAB_PIXEL_INVALID, 0 },
422  {VOCAB_PIXEL_YUV_420, 1},
423  {VOCAB_PIXEL_YUV_444, 1},
424  {VOCAB_PIXEL_YUV_422, 1},
426 };
427 
429  initialize();
430 }
431 
432 void Image::initialize() {
433  implementation = nullptr;
434  data = nullptr;
435  imgWidth = imgHeight = 0;
436  imgPixelSize = imgRowSize = 0;
437  imgPixelCode = 0;
438  imgQuantum = 0;
439  topIsLow = true;
440  implementation = new ImageStorage(*this);
441  yAssert(implementation!=nullptr);
442 }
443 
444 
446  if (implementation!=nullptr) {
447  delete (ImageStorage*)implementation;
448  implementation = nullptr;
449  }
450 }
451 
452 
453 size_t Image::getPixelSize() const {
454  return imgPixelSize;
455 }
456 
457 
458 int Image::getPixelCode() const {
459  return imgPixelCode;
460 }
461 
462 
463 void Image::zero() {
464  if (getRawImage()!=nullptr) {
465  memset(getRawImage(),0,getRawImageSize());
466  }
467 }
468 
469 
470 void Image::resize(size_t imgWidth, size_t imgHeight) {
471 
472  int code = getPixelCode();
473  bool change = false;
474  if (code!=imgPixelCode) {
475  setPixelCode(code);
476  change = true;
477  }
478  if (imgPixelCode!=((ImageStorage*)implementation)->extern_type_id) {
479  change = true;
480  }
481  if (imgQuantum!=((ImageStorage*)implementation)->extern_type_quantum) {
482  change = true;
483  }
484 
485  if (imgWidth!=width()||imgHeight!=height()) {
486  change = true;
487  }
488 
489  if (change) {
490  ((ImageStorage*)implementation)->resize(imgWidth,imgHeight,
491  imgPixelCode,
492  imgQuantum,
493  topIsLow);
494  synchronize();
495  //printf("CHANGE! %ld\n", (long int)(this));
496  }
497 }
498 
499 void Image::setPixelSize(size_t imgPixelSize) {
500  if(imgPixelSize == pixelCode2Size.at((YarpVocabPixelTypesEnum)imgPixelCode)) {
501  return;
502  }
503 
504  setPixelCode(-imgPixelSize);
505 }
506 
507 void Image::setPixelCode(int imgPixelCode) {
508  this->imgPixelCode = imgPixelCode;
509  this->imgPixelSize = (imgPixelCode < 0) ? -imgPixelCode : pixelCode2Size.at((YarpVocabPixelTypesEnum)imgPixelCode);
510 }
511 
512 
513 void Image::setQuantum(size_t imgQuantum) {
514  this->imgQuantum = imgQuantum;
515 }
516 
517 
518 void Image::synchronize() {
519  auto* impl = (ImageStorage*)implementation;
520  yAssert(impl!=nullptr);
521  if (impl->pImage!=nullptr) {
522  imgWidth = impl->pImage->width;
523  imgHeight = impl->pImage->height;
524  data = impl->Data;
525  imgQuantum = impl->quantum;
526  imgRowSize = impl->pImage->widthStep;
527  setPixelCode(impl->getTypeId());
528  topIsLow = impl->pImage->origin == IPL_ORIGIN_TL;
529  } else {
530  data = nullptr;
531  imgWidth = 0;
532  imgHeight = 0;
533  }
534 }
535 
536 
537 unsigned char *Image::getRawImage() const {
538  auto* impl = (ImageStorage*)implementation;
539  yAssert(impl!=nullptr);
540  if (impl->pImage!=nullptr) {
541  return (unsigned char *)impl->pImage->imageData;
542  }
543  return nullptr;
544 }
545 
546 size_t Image::getRawImageSize() const {
547  auto* impl = (ImageStorage*)implementation;
548  yAssert(impl!=nullptr);
549  if (impl->pImage!=nullptr) {
550  return impl->pImage->imageSize;
551  }
552  return 0;
553 }
554 
555 #ifndef YARP_NO_DEPRECATED // Since YARP 3.2.0
557  return ((ImageStorage*)implementation)->pImage;
558 }
559 
560 const void *Image::getIplImage() const {
561  return ((const ImageStorage*)implementation)->pImage;
562 }
563 
564 void Image::wrapIplImage(void *iplImage) {
565  yAssert(iplImage!=nullptr);
566  auto* p = (IplImage *)iplImage;
567  std::string str = p->colorModel;
568  int code = -1;
569  int color_code = -1;
570  if (str=="rgb"||str=="RGB"||
571  str=="bgr"||str=="BGR"||
572  str=="gray"||str=="GRAY"||
573  str=="graygray"||str=="GRAYGRAY") {
574  str = p->channelSeq;
575  if (str=="rgb"||str=="RGB") {
576  color_code = VOCAB_PIXEL_RGB;
577  } else if (str=="bgr"||str=="BGR") {
578  color_code = VOCAB_PIXEL_BGR;
579  } else if (str=="gray"||str=="GRAY"||
580  str=="graygray"||str=="GRAYGRAY") {
581  color_code = VOCAB_PIXEL_MONO;
582  } else {
583  printf("specific IPL RGB order (%s) is not yet supported\n",
584  str.c_str());
585  printf("Try RGB, BGR, or \n");
586  printf("Or fix code at %s line %d\n",__FILE__,__LINE__);
587  std::exit(1);
588  }
589  }
590 
591  // Type translation is approximate. Patches welcome to flesh out
592  // the types available.
593  if (p->depth == IPL_DEPTH_8U) {
594  code = color_code;
595  } else if (p->depth == IPL_DEPTH_8S) {
596  switch (color_code) {
597  case VOCAB_PIXEL_MONO:
599  break;
600  case VOCAB_PIXEL_RGB:
601  code = VOCAB_PIXEL_RGB_SIGNED;
602  break;
603  case VOCAB_PIXEL_BGR:
604  code = color_code; // nothing better available
605  break;
606  }
607  } else if (p->depth == IPL_DEPTH_16U || p->depth == IPL_DEPTH_16S) {
608  switch (color_code) {
609  case VOCAB_PIXEL_MONO:
610  code = VOCAB_PIXEL_MONO16;
611  break;
612  case VOCAB_PIXEL_RGB:
613  case VOCAB_PIXEL_BGR:
614  fprintf(stderr,"No translation currently available for this pixel type\n");
615  std::exit(1);
616  break;
617  }
618  } else if (p->depth == IPL_DEPTH_32S) {
619  switch (color_code) {
620  case VOCAB_PIXEL_MONO:
621  code = VOCAB_PIXEL_INT;
622  break;
623  case VOCAB_PIXEL_RGB:
624  case VOCAB_PIXEL_BGR:
625  code = VOCAB_PIXEL_RGB_INT;
626  break;
627  }
628  } else if (p->depth == IPL_DEPTH_32F) {
629  switch (color_code) {
630  case VOCAB_PIXEL_MONO:
631  code = VOCAB_PIXEL_MONO_FLOAT;
632  break;
633  case VOCAB_PIXEL_RGB:
634  case VOCAB_PIXEL_BGR:
635  code = VOCAB_PIXEL_RGB_FLOAT;
636  break;
637  }
638  }
639 
640  if (code==-1) {
641  fprintf(stderr,"IPL pixel type / depth combination is not yet supported\n");
642  fprintf(stderr,"Please email a YARP developer to complain, quoting this:\n");
643  fprintf(stderr," %s:%d\n", __FILE__, __LINE__);
644  }
645 
646  if (getPixelCode()!=code && getPixelCode()!=-1) {
647  printf("your specific IPL format (%s depth %d -> %s) does not match your YARP format (%s)\n",
648  str.c_str(),
649  p->depth,
650  Vocab::decode(code).c_str(),
651  Vocab::decode(getPixelCode()).c_str());
652  printf("Making a copy instead of just wrapping...\n");
653  FlexImage img;
654  img.setQuantum(p->align);
655  img.setPixelCode(code);
656  img.setExternal(p->imageData,p->width,p->height);
657  copy(img);
658  } else {
659  setQuantum(p->align);
660  setPixelCode(code);
661  setExternal(p->imageData,p->width,p->height);
662  }
663 }
664 
665 #endif // YARP_NO_DEPRECATED
666 
667 
669 
670  // auto-convert text mode interaction
671  connection.convertTextMode();
672 
673  ImageNetworkHeader header;
674 
675  bool ok = connection.expectBlock((char*)&header,sizeof(header));
676  if (!ok) return false;
677 
678  //first check that the received image size is reasonable
679  if (header.width == 0 || header.height == 0)
680  {
681  // I maintain the previous logic, although we should probably return false
682  return !connection.isError();
683  }
684 
685  setPixelCode(header.id);
686 
687  size_t q = getQuantum();
688  if (q==0) {
689  //q = YARP_IMAGE_ALIGN;
690  setQuantum(header.quantum);
691  q = getQuantum();
692  }
693  if (q!=(size_t) header.quantum) {
694  if ((header.depth*header.width)%header.quantum==0 &&
695  (header.depth*header.width)%q==0) {
696  header.quantum = q;
697  }
698  }
699 
700  // handle easy case, received and current image are compatible, no conversion needed
701  if (getPixelCode() == header.id && q == (size_t) header.quantum && imgPixelSize == (size_t) header.depth)
702  {
703  return readFromConnection(*this, header, connection);
704  }
705 
706  // image is bayer 8 bits, current image is MONO, copy as is (keep raw format)
707  if (getPixelCode() == VOCAB_PIXEL_MONO && isBayer8(header.id))
708  {
709  return readFromConnection(*this, header, connection);
710  }
711  // image is bayer 16 bits, current image is MONO16, copy as is (keep raw format)
712  if (getPixelCode() == VOCAB_PIXEL_MONO16 && isBayer16(header.id))
713  {
714  return readFromConnection(*this, header, connection);
715  }
716 
718  // Received and current images are binary incompatible do our best to convert
719  //
720 
721  // handle here all bayer encoding 8 bits
722  if (isBayer8(header.id))
723  {
724  FlexImage flex;
726  flex.setQuantum(header.quantum);
727 
728  bool ok = readFromConnection(flex, header, connection);
729  if (!ok)
730  return false;
731 
732  if (getPixelCode() == VOCAB_PIXEL_BGR && header.id==VOCAB_PIXEL_ENCODING_BAYER_GRBG8)
733  return deBayer_GRBG8_TO_BGR(flex, *this, 3);
734  else if (getPixelCode() == VOCAB_PIXEL_BGRA && header.id == VOCAB_PIXEL_ENCODING_BAYER_GRBG8)
735  return deBayer_GRBG8_TO_BGR(flex, *this, 4);
736  if (getPixelCode() == VOCAB_PIXEL_RGB && header.id==VOCAB_PIXEL_ENCODING_BAYER_GRBG8)
737  return deBayer_GRBG8_TO_RGB(flex, *this, 3);
738  if (getPixelCode() == VOCAB_PIXEL_RGBA && header.id == VOCAB_PIXEL_ENCODING_BAYER_GRBG8)
739  return deBayer_GRBG8_TO_RGB(flex, *this, 4);
740  else
741  {
742  YARP_FIXME_NOTIMPLEMENTED("Conversion from bayer encoding not yet implemented\n");
743  return false;
744  }
745  }
746 
747  // handle here all bayer encodings 16 bits
748  if (isBayer16(header.id))
749  {
750  // as bayer16 seems unlikely we defer implementation for later
751  YARP_FIXME_NOTIMPLEMENTED("Conversion from bayer encoding 16 bits not yet implemented\n");
752  return false;
753  }
754 
755  // Received image has valid YARP pixels and can be converted using Image primitives
756  // prepare a FlexImage, set it to be compatible with the received image
757  // read new image into FlexImage then copy from it.
758  FlexImage flex;
759  flex.setPixelCode(header.id);
760  flex.setQuantum(header.quantum);
761  ok = readFromConnection(flex, header, connection);
762  if (ok)
763  copy(flex);
764 
765  return ok;
766 }
767 
768 
769 bool Image::write(yarp::os::ConnectionWriter& connection) const {
770  ImageNetworkHeader header;
771  header.setFromImage(*this);
772  /*
773  header.listTag = BOTTLE_TAG_LIST;
774  header.listLen = 4;
775  header.paramNameTag = BOTTLE_TAG_VOCAB;
776  header.paramName = yarp::os::createVocab('m','a','t');
777  header.paramIdTag = BOTTLE_TAG_VOCAB;
778  header.id = getPixelCode();
779  header.paramListTag = BOTTLE_TAG_LIST + BOTTLE_TAG_INT32;
780  header.paramListLen = 5;
781  header.depth = getPixelSize();
782  header.imgSize = getRawImageSize();
783  header.quantum = getQuantum();
784  header.width = width();
785  header.height = height();
786  header.paramBlobTag = BOTTLE_TAG_BLOB;
787  header.paramBlobLen = getRawImageSize();
788  */
789 
790  connection.appendBlock((char*)&header,sizeof(header));
791  unsigned char *mem = getRawImage();
792  if (header.width!=0&&header.height!=0) {
793  yAssert(mem!=nullptr);
794 
795  // Note use of external block.
796  // Implies care needed about ownership.
797  connection.appendExternalBlock((char *)mem,header.imgSize);
798  }
799 
800  // if someone is foolish enough to connect in text mode,
801  // let them see something readable.
802  connection.convertTextMode();
803 
804  return !connection.isError();
805 }
806 
807 
808 Image::Image(const Image& alt) : Portable()
809 {
810  initialize();
811  copy(alt);
812 }
813 
814 Image::Image(Image&& other) noexcept
815  : implementation(other.implementation)
816 {
817  other.implementation = nullptr;
818  synchronize();
819 }
820 
821 Image& Image::operator=(Image&& other) noexcept
822 {
823  Image moved(std::move(other));
824  std::swap(moved.implementation, implementation);
825  synchronize();
826  return *this;
827 }
828 
829 
830 const Image& Image::operator=(const Image& alt)
831 {
832  if (&alt != this) {
833  copy(alt);
834  }
835  return *this;
836 }
837 
838 
839 bool Image::copy(const Image& alt)
840 {
841  if (&alt != this)
842  {
843  int myCode = getPixelCode();
844  if (myCode==0) {
845  setPixelCode(alt.getPixelCode());
846  setQuantum(alt.getQuantum());
847  }
848  resize(alt.width(),alt.height());
849  int q1 = alt.getQuantum();
850  int q2 = getQuantum();
851  if (q1==0) { q1 = YARP_IMAGE_ALIGN; }
852  if (q2==0) { q2 = YARP_IMAGE_ALIGN; }
853 
854  bool o1 = alt.topIsLowIndex();
855  bool o2 = topIsLowIndex();
856 
857  yAssert(width()==alt.width());
858  yAssert(height()==alt.height());
859  if (getPixelCode()==alt.getPixelCode()) {
860  if (getQuantum()==alt.getQuantum()) {
862  yAssert(q1==q2);
863  }
864  }
865 
866  copyPixels(alt.getRawImage(),alt.getPixelCode(),
868  width(),height(),
869  getRawImageSize(),q1,q2,o1,o2);
870  }
871  return true;
872 }
873 
874 
875 void Image::setExternal(const void *data, size_t imgWidth, size_t imgHeight) {
876  if (imgQuantum==0) {
877  imgQuantum = 1;
878  }
879  ((ImageStorage*)implementation)->_alloc_complete_extern(data,
880  imgWidth,
881  imgHeight,
882  getPixelCode(),
883  imgQuantum,
884  topIsLow);
885  synchronize();
886 }
887 
888 
889 bool Image::copy(const Image& alt, size_t w, size_t h) {
890  if (getPixelCode()==0) {
891  setPixelCode(alt.getPixelCode());
892  setQuantum(alt.getQuantum());
893  }
894  if (&alt==this) {
895  FlexImage img;
896  img.copy(alt);
897  return copy(img,w,h);
898  }
899 
900  if (getPixelCode()!=alt.getPixelCode()) {
901  FlexImage img;
902  img.setPixelCode(getPixelCode());
903  img.setQuantum(getQuantum());
904  img.copy(alt);
905  return copy(img,w,h);
906  }
907 
908  resize(w,h);
909  size_t d = getPixelSize();
910 
911  size_t nw = w;
912  size_t nh = h;
913  w = alt.width();
914  h = alt.height();
915 
916  float di = ((float)h)/nh;
917  float dj = ((float)w)/nw;
918 
919  for (size_t i=0; i<nh; i++)
920  {
921  auto i0 = (size_t)(di*i);
922  for (size_t j=0; j<nw; j++)
923  {
924  auto j0 = (size_t)(dj*j);
925  memcpy(getPixelAddress(j,i),
926  alt.getPixelAddress(j0,i0),
927  d);
928  }
929  }
930  return true;
931 }
yarp::sig::Image::setPixelCode
void setPixelCode(int imgPixelCode)
Definition: Image.cpp:507
IPL_BORDER_CONSTANT
#define IPL_BORDER_CONSTANT
Definition: IplImage.h:286
ImageStorage::_make_independent
void _make_independent()
Definition: Image.cpp:296
yarp::sig::Image::write
bool write(yarp::os::ConnectionWriter &connection) const override
Write image to a connection.
Definition: Image.cpp:769
VOCAB_PIXEL_RGB_INT
@ VOCAB_PIXEL_RGB_INT
Definition: Image.h:58
IPL_DATA_ORDER_PIXEL
#define IPL_DATA_ORDER_PIXEL
Definition: IplImage.h:70
yarp::os::ConnectionWriter::appendBlock
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
yarp::os::Portable
Definition: Portable.h:28
ImageStorage::_alloc_extern
void _alloc_extern(const void *buf)
Definition: Image.cpp:188
VOCAB_PIXEL_RGB_FLOAT
@ VOCAB_PIXEL_RGB_FLOAT
Definition: Image.h:60
yarp::sig::ImageNetworkHeader
Byte order in image header for network transmission.
Definition: ImageNetworkHeader.h:32
yarp::sig::Image::setPixelSize
void setPixelSize(size_t imgPixelSize)
Definition: Image.cpp:499
pixelTypeIplParams::channelSeq
const char * channelSeq
Definition: Image.cpp:306
IPL_ALIGN_QWORD
#define IPL_ALIGN_QWORD
Definition: IplImage.h:82
yarp::sig::Image::~Image
virtual ~Image()
Destructor.
Definition: Image.cpp:445
deBayer_GRBG8_TO_RGB
bool deBayer_GRBG8_TO_RGB(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
Definition: DeBayer.cpp:78
ImageNetworkHeader.h
yarp::sig
Signal processing.
Definition: Image.h:25
ImageStorage::extern_type_quantum
size_t extern_type_quantum
Definition: Image.cpp:78
VOCAB_PIXEL_RGB_SIGNED
@ VOCAB_PIXEL_RGB_SIGNED
Definition: Image.h:57
ImageStorage::_set_ipl_header
bool _set_ipl_header(size_t x, size_t y, int pixel_type, size_t quantum, bool topIsLow)
Definition: Image.cpp:341
ImageStorage::_alloc_complete_extern
void _alloc_complete_extern(const void *buf, size_t x, size_t y, int pixel_type, size_t quantum, bool topIsLow)
Definition: Image.cpp:374
iplDeallocateImage
void iplDeallocateImage(IplImage *image)
Definition: IplImage.cpp:731
ImageStorage::type_id
int type_id
Definition: Image.cpp:85
ConnectionWriter.h
IPL_ORIGIN_TL
#define IPL_ORIGIN_TL
Definition: IplImage.h:73
yarp::sig::Image::zero
void zero()
Set all pixels to 0.
Definition: Image.cpp:463
VOCAB_PIXEL_ENCODING_BAYER_BGGR16
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR16
Definition: Image.h:65
DeBayer.h
ImageStorage::_alloc_data
void _alloc_data()
Definition: Image.cpp:207
pixelTypeIplParams::colorModel
const char * colorModel
Definition: Image.cpp:305
iplPixelTypeMono
const pixelTypeIplParams iplPixelTypeMono
Definition: Image.cpp:309
yarp::sig::ImageNetworkHeader::width
yarp::os::NetInt32 width
Definition: ImageNetworkHeader.h:47
yarp::sig::Image::getQuantum
size_t getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
Definition: Image.h:186
yarp::sig::PixelMonoSigned
char PixelMonoSigned
Signed byte pixel type.
Definition: Image.h:517
ImageStorage::extern_type_id
int extern_type_id
Definition: Image.cpp:77
iplDeallocate
void iplDeallocate(IplImage *image, int flag)
Definition: IplImage.cpp:900
yarp::sig::PAD_BYTES
size_t PAD_BYTES(size_t len, size_t pad)
computes the padding of YARP images.
Definition: Image.h:36
yarp::sig::PixelBgr
Packed RGB pixel type, with pixels stored in reverse order.
Definition: Image.h:496
yarp::sig::PixelRgbSigned
Signed, packed RGB pixel type.
Definition: Image.h:523
yarp::sig::ImageNetworkHeader::setFromImage
void setFromImage(const Image &image)
Definition: ImageNetworkHeader.h:58
IPL_DEPTH_8S
#define IPL_DEPTH_8S
Definition: IplImage.h:66
yarp::sig::Image::copy
bool copy(const Image &alt)
Copy operator.
Definition: Image.cpp:839
IPL_IMAGE_HEADER
#define IPL_IMAGE_HEADER
Definition: IplImage.h:300
yarp::sig::ImageNetworkHeader::id
yarp::os::NetInt32 id
Definition: ImageNetworkHeader.h:41
yarp::sig::PixelBgra
Packed BGRA pixel type.
Definition: Image.h:481
yarp::sig::PixelHsvFloat
Floating point HSV pixel type.
Definition: Image.h:562
ImageStorage::_alloc_complete
void _alloc_complete(size_t x, size_t y, int pixel_type, size_t quantum, bool topIsLow)
Definition: Image.cpp:284
ImageStorage::_free_complete
void _free_complete()
Definition: Image.cpp:266
ImageStorage::_pad_bytes
int _pad_bytes(size_t linesize, size_t align) const
Definition: Image.cpp:393
ImageStorage::resize
void resize(size_t x, size_t y, int pixel_type, size_t quantum, bool topIsLow)
Definition: Image.cpp:144
yarp::sig::Image::wrapIplImage
void wrapIplImage(void *iplImage)
Act as a wrapper around an IPL/OpenCV image.
Definition: Image.cpp:564
yarp::sig::Image::read
bool read(yarp::os::ConnectionReader &connection) override
Read image from a connection.
Definition: Image.cpp:668
yarp::sig::Image::getPixelSize
virtual size_t getPixelSize() const
Gets pixel size in memory in bytes.
Definition: Image.cpp:453
pixelTypeIplParams
Definition: Image.cpp:301
yarp::sig::Image::getRawImageSize
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
Definition: Image.cpp:546
VOCAB_PIXEL_ENCODING_BAYER_RGGB8
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB8
Definition: Image.h:68
iplAllocateImage
void iplAllocateImage(IplImage *image, int doFill, int fillValue)
Definition: IplImage.cpp:661
yarp::sig::Image::getIplImage
void * getIplImage()
Returns IPL/OpenCV view of image, if possible.
Definition: Image.cpp:556
yarp::sig::Image::Image
Image()
Default constructor.
Definition: Image.cpp:428
yarp::sig::Image::width
size_t width() const
Gets width of image in pixels.
Definition: Image.h:153
VOCAB_PIXEL_RGBA
@ VOCAB_PIXEL_RGBA
Definition: Image.h:51
yarp::sig::PixelRgbInt
Integer RGB pixel type.
Definition: Image.h:549
yarp::sig::Image::setExternal
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition: Image.cpp:875
yarp::sig::Image::topIsLowIndex
bool topIsLowIndex() const
Definition: Image.h:340
yarp::os::Vocab::decode
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:36
yarp::sig::FlexImage::setQuantum
void setQuantum(size_t imgQuantum)
Definition: Image.h:418
_IplImage::nChannels
int nChannels
Most of OpenCV functions support 1,2,3 or 4 channels.
Definition: IplImage.h:87
VOCAB_PIXEL_HSV_FLOAT
@ VOCAB_PIXEL_HSV_FLOAT
Definition: Image.h:61
Log.h
yarp::os::ConnectionWriter::isError
virtual bool isError() const =0
IPL_DEPTH_8U
#define IPL_DEPTH_8U
Definition: IplImage.h:62
ImageStorage::ImageStorage
ImageStorage(Image &owner)
Definition: Image.cpp:119
VOCAB_PIXEL_YUV_422
@ VOCAB_PIXEL_YUV_422
Definition: Image.h:72
YARP_FIXME_NOTIMPLEMENTED
#define YARP_FIXME_NOTIMPLEMENTED(what)
Definition: Log.h:310
yarp::sig::PixelMono
unsigned char PixelMono
Monochrome pixel type.
Definition: Image.h:436
ImageStorage
Definition: Image.cpp:73
ImageStorage::_alloc
void _alloc()
Definition: Image.cpp:171
yarp::sig::PixelFloat
float PixelFloat
Floating point pixel type.
Definition: Image.h:531
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:39
VOCAB_PIXEL_YUV_420
@ VOCAB_PIXEL_YUV_420
Definition: Image.h:70
yarp::sig::FlexImage
Image class with user control of representation details.
Definition: Image.h:403
yarp::sig::ImageNetworkHeader::height
yarp::os::NetInt32 height
Definition: ImageNetworkHeader.h:48
iplPixelTypeMono16
const pixelTypeIplParams iplPixelTypeMono16
Definition: Image.cpp:310
VOCAB_PIXEL_ENCODING_BAYER_RGGB16
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB16
Definition: Image.h:69
yarp::sig::Image::getPixelAddress
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition: Image.h:227
yarp::sig::FlexImage::setPixelCode
void setPixelCode(int imgPixelCode)
Definition: Image.h:406
IPL_DEPTH_32S
#define IPL_DEPTH_32S
Definition: IplImage.h:68
yarp::os::ConnectionReader::isError
virtual bool isError() const =0
yarp::sig::PixelInt
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
Definition: Image.h:446
VOCAB_PIXEL_YUV_444
@ VOCAB_PIXEL_YUV_444
Definition: Image.h:71
VOCAB_PIXEL_ENCODING_BAYER_GBRG16
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG16
Definition: Image.h:67
VOCAB_PIXEL_BGRA
@ VOCAB_PIXEL_BGRA
Definition: Image.h:52
IPL_SIDE_ALL
#define IPL_SIDE_ALL
Definition: IplImage.h:296
readFromConnection
bool readFromConnection(Image &dest, ImageNetworkHeader &header, ConnectionReader &connection)
This helper function groups code to avoid duplication.
Definition: Image.cpp:48
yarp::sig::Image::resize
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition: Image.cpp:470
yarp::sig::Image::height
size_t height() const
Gets height of image in pixels.
Definition: Image.h:159
ImageStorage::Data
char ** Data
Definition: Image.cpp:76
yarp::os::ConnectionReader::convertTextMode
virtual bool convertTextMode()=0
Reads in a standard description in text mode, and converts it to a standard description in binary.
ImageStorage::GetPadding
int GetPadding() const
Definition: Image.cpp:113
deBayer_GRBG8_TO_BGR
bool deBayer_GRBG8_TO_BGR(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
Definition: DeBayer.cpp:12
yarp::sig::PixelHsv
Packed HSV (hue/saturation/value pixel type.
Definition: Image.h:509
pixelCode2iplParams
const std::map< int, pixelTypeIplParams > pixelCode2iplParams
Definition: Image.cpp:312
VOCAB_PIXEL_ENCODING_BAYER_GRBG16
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG16
Definition: Image.h:63
ImageStorage::pImage
IplImage * pImage
Definition: Image.cpp:75
ImageStorage::owner
Image & owner
Definition: Image.cpp:83
IplImage.h
YARP_IMAGE_ALIGN
#define YARP_IMAGE_ALIGN
Definition: IplImage.h:316
ImageStorage::_free
void _free()
Definition: Image.cpp:238
VOCAB_PIXEL_ENCODING_BAYER_GBRG8
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG8
Definition: Image.h:66
VOCAB_PIXEL_INVALID
@ VOCAB_PIXEL_INVALID
Definition: Image.h:47
_IplImage
Definition: IplImage.h:84
yarp::os::ConnectionWriter::convertTextMode
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
VOCAB_PIXEL_MONO_SIGNED
@ VOCAB_PIXEL_MONO_SIGNED
Definition: Image.h:56
isBayer8
bool isBayer8(int v)
Basic implementations of debayering functions.
Definition: DeBayer.h:22
Image.h
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:39
_IplImage::width
int width
image width in pixels
Definition: IplImage.h:99
yarp::sig::PixelRgba
Packed RGBA pixel type.
Definition: Image.h:466
ImageStorage::quantum
size_t quantum
Definition: Image.cpp:79
IPL_DEPTH_16S
#define IPL_DEPTH_16S
Definition: IplImage.h:67
VOCAB_PIXEL_INT
@ VOCAB_PIXEL_INT
Definition: Image.h:53
yarp::sig::ImageNetworkHeader::quantum
yarp::os::NetInt32 quantum
Definition: ImageNetworkHeader.h:46
ImageStorage::getTypeId
int getTypeId()
ImageStorage::getTypeId.
Definition: Image.cpp:165
VOCAB_PIXEL_MONO
@ VOCAB_PIXEL_MONO
Definition: Image.h:48
yarp::os
Definition: AbstractCarrier.h:17
yarp::sig::PixelRgbFloat
Floating point RGB pixel type.
Definition: Image.h:537
IPL_ORIGIN_BL
#define IPL_ORIGIN_BL
Definition: IplImage.h:74
yarp::sig::PixelRgb
Packed RGB pixel type.
Definition: Image.h:452
yarp::sig::PixelMono16
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
Definition: Image.h:441
yarp::sig::Image
Definition: Image.h:85
Vocab.h
ImageStorage::_free_data
void _free_data()
Definition: Image.cpp:260
VOCAB_PIXEL_RGB
@ VOCAB_PIXEL_RGB
Definition: Image.h:50
IPL_DEPTH_32F
#define IPL_DEPTH_32F
Definition: IplImage.h:64
ImageStorage::~ImageStorage
~ImageStorage()
Definition: Image.cpp:130
implementation
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
ImageStorage::topIsLow
bool topIsLow
Definition: Image.cpp:80
Time.h
yarp::os::ConnectionWriter::appendExternalBlock
virtual void appendExternalBlock(const char *data, size_t len)=0
Send a block of data to the network connection, without making a copy.
VOCAB_PIXEL_YUV_411
@ VOCAB_PIXEL_YUV_411
Definition: Image.h:73
iplAllocateImageFP
void iplAllocateImageFP(IplImage *image, int doFill, float fillValue)
Definition: IplImage.cpp:694
pixelTypeIplParams::depth
int depth
Definition: Image.cpp:304
ImageStorage::is_owner
int is_owner
Definition: Image.cpp:87
pixelTypeIplParams::nChannels
int nChannels
Definition: Image.cpp:303
yarp::os::ConnectionReader::expectBlock
virtual bool expectBlock(char *data, size_t len)=0
Read a block of data from the network connection.
isBayer16
bool isBayer16(int v)
Definition: DeBayer.h:33
IPL_DEPTH_16U
#define IPL_DEPTH_16U
Definition: IplImage.h:63
YarpVocabPixelTypesEnum
YarpVocabPixelTypesEnum
Definition: Image.h:45
VOCAB_PIXEL_ENCODING_BAYER_BGGR8
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR8
Definition: Image.h:64
yAssert
#define yAssert(x)
Definition: Log.h:297
ImageStorage::_free_ipl_header
void _free_ipl_header()
Definition: Image.cpp:274
iplSetBorderMode
void iplSetBorderMode(IplImage *src, int mode, int border, int constVal)
Definition: IplImage.cpp:922
VOCAB_PIXEL_MONO16
@ VOCAB_PIXEL_MONO16
Definition: Image.h:49
VOCAB_PIXEL_BGR
@ VOCAB_PIXEL_BGR
Definition: Image.h:55
Bottle.h
yarp::sig::ImageNetworkHeader::imgSize
yarp::os::NetInt32 imgSize
Definition: ImageNetworkHeader.h:45
yarp::sig::Image::getRawImage
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:537
yarp::sig::Image::operator=
Image & operator=(Image &&other) noexcept
Move assignment operator.
Definition: Image.cpp:821
iplCreateImageHeader
IplImage * iplCreateImageHeader(int nChannels, int alphaChannel, int depth, char *colorModel, char *channelSeq, int dataOrder, int origin, int align, int width, int height, IplROI *roi, IplImage *maskROI, void *imageId, IplTileInfo *tileInfo)
Definition: IplImage.cpp:786
VOCAB_PIXEL_HSV
@ VOCAB_PIXEL_HSV
Definition: Image.h:54
DBGPF1
#define DBGPF1
Definition: Image.cpp:36
ConnectionReader.h
VOCAB_PIXEL_ENCODING_BAYER_GRBG8
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG8
Definition: Image.h:62
VOCAB_PIXEL_MONO_FLOAT
@ VOCAB_PIXEL_MONO_FLOAT
Definition: Image.h:59
yarp::sig::Image::setQuantum
void setQuantum(size_t imgQuantum)
Definition: Image.cpp:513
yarp::sig::Image::getPixelCode
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.cpp:458