56 printf(
"There is a problem reading an image\n");
57 printf(
"incoming: width %zu, height %zu, code %zu, quantum %zu, topIsLow %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.
topIsLow),
63 static_cast<size_t>(header.
imgSize));
64 printf(
"my space: width %zu, height %zu, code %d, quantum %zu, size %zu\n",
68 bool ok = connection.
expectBlock(
reinterpret_cast<char*
>(mem), allocatedBytes);
69 return (!connection.
isError() && ok);
97 void _alloc_extern (
const void *buf);
102 void _make_independent();
103 bool _set_ipl_header(
size_t x,
size_t y,
int pixel_type,
size_t quantum,
105 void _free_ipl_header();
106 void _alloc_complete(
size_t x,
size_t y,
int pixel_type,
size_t quantum,
108 void _free_complete();
112 int _pad_bytes (
size_t linesize,
size_t align)
const;
128 extern_type_quantum = -1;
135 void resize(
size_t x,
size_t y,
int pixel_type,
136 size_t quantum,
bool topIsLow);
138 void _alloc_complete_extern(
const void *buf,
size_t x,
size_t y,
int pixel_type,
139 size_t quantum,
bool topIsLow);
146 size_t quantum,
bool topIsLow) {
147 int need_recreation = 1;
153 if (need_recreation) {
155 DBGPF1 printf(
"HIT recreation for %p %p: %zu %zu %d\n",
static_cast<void*
>(
this),
static_cast<void*
>(pImage), x, y, pixel_type);
156 _alloc_complete (x, y, pixel_type, quantum, topIsLow);
158 extern_type_id = pixel_type;
159 extern_type_quantum = quantum;
194 if (pImage !=
nullptr) {
195 if (pImage->imageData !=
nullptr) {
201 pImage->imageData =
const_cast<char*
>(
reinterpret_cast<const char*
>(buf));
210 DBGPF1 printf(
"alloc_data1\n"), fflush(stdout);
215 char **ptr =
new char *[pImage->height];
221 yAssert(pImage->imageData !=
nullptr);
223 int height = pImage->height;
225 char * DataArea = pImage->imageData;
227 for (
int r = 0; r < height; r++)
232 Data[height-r-1] = DataArea;
234 DataArea += pImage->widthStep;
236 DBGPF1 printf(
"alloc_data4\n");
241 if (pImage !=
nullptr) {
242 if (pImage->imageData !=
nullptr) {
252 pImage->imageData =
nullptr;
286 _set_ipl_header(x, y, pixel_type, quantum, topIsLow);
341 if (pImage !=
nullptr) {
348 printf (
"*** Trying to allocate an invalid pixel type image\n");
363 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);
365 type_id = pixel_type;
366 this->quantum = quantum;
367 this->topIsLow = topIsLow;
376 this->quantum = quantum;
377 this->topIsLow = topIsLow;
381 _set_ipl_header(x, y, pixel_type, quantum, topIsLow);
395 const std::map<YarpVocabPixelTypesEnum, size_t> Image::pixelCode2Size = {
429 void Image::initialize() {
432 imgWidth = imgHeight = 0;
433 imgPixelSize = imgRowSize = 0;
461 if (getRawImage()!=
nullptr) {
462 memset(getRawImage(),0,getRawImageSize());
469 int code = getPixelCode();
471 if (code!=imgPixelCode) {
482 if (imgWidth!=width()||imgHeight!=height()) {
502 setPixelCode(-imgPixelSize);
506 this->imgPixelCode = imgPixelCode;
507 this->imgPixelSize = (imgPixelCode < 0) ? -imgPixelCode : pixelCode2Size.at(
static_cast<YarpVocabPixelTypesEnum>(imgPixelCode));
512 this->imgQuantum = imgQuantum;
516 void Image::synchronize() {
519 if (impl->pImage!=
nullptr) {
520 imgWidth = impl->pImage->width;
521 imgHeight = impl->pImage->height;
523 imgQuantum = impl->quantum;
524 imgRowSize = impl->pImage->widthStep;
525 setPixelCode(impl->getTypeId());
538 if (impl->pImage!=
nullptr) {
539 return reinterpret_cast<unsigned char*
>(impl->pImage->imageData);
547 if (impl->pImage!=
nullptr) {
548 return impl->pImage->imageSize;
553 #ifndef YARP_NO_DEPRECATED
564 auto* p =
static_cast<IplImage *
>(iplImage);
568 if (str==
"rgb"||str==
"RGB"||
569 str==
"bgr"||str==
"BGR"||
570 str==
"gray"||str==
"GRAY"||
571 str==
"graygray"||str==
"GRAYGRAY") {
573 if (str==
"rgb"||str==
"RGB") {
575 }
else if (str==
"bgr"||str==
"BGR") {
577 }
else if (str==
"gray"||str==
"GRAY"||
578 str==
"graygray"||str==
"GRAYGRAY") {
581 printf(
"specific IPL RGB order (%s) is not yet supported\n",
583 printf(
"Try RGB, BGR, or \n");
584 printf(
"Or fix code at %s line %d\n",__FILE__,__LINE__);
594 switch (color_code) {
606 switch (color_code) {
612 fprintf(stderr,
"No translation currently available for this pixel type\n");
617 switch (color_code) {
627 switch (color_code) {
639 fprintf(stderr,
"IPL pixel type / depth combination is not yet supported\n");
640 fprintf(stderr,
"Please email a YARP developer to complain, quoting this:\n");
641 fprintf(stderr,
" %s:%d\n", __FILE__, __LINE__);
644 if (getPixelCode()!=code && getPixelCode()!=-1) {
645 printf(
"your specific IPL format (%s depth %d -> %s) does not match your YARP format (%s)\n",
650 printf(
"Making a copy instead of just wrapping...\n");
657 setQuantum(p->align);
659 setExternal(p->imageData,p->width,p->height);
673 bool ok = connection.
expectBlock(
reinterpret_cast<char*
>(&header),
sizeof(header));
685 setPixelCode(header.
id);
687 size_t q = getQuantum();
693 if (q !=
static_cast<size_t>(header.
quantum)) {
700 setTopIsLowIndex(header.
topIsLow == 0);
703 if (getPixelCode() == header.
id && q ==
static_cast<size_t>(header.
quantum) && imgPixelSize ==
static_cast<size_t>(header.
depth))
798 connection.
appendBlock(
reinterpret_cast<char*
>(&header),
sizeof(header));
799 unsigned char *mem = getRawImage();
825 other.implementation =
nullptr;
831 Image moved(std::move(other));
887 (
static_cast<ImageStorage*
>(implementation))->_alloc_complete_extern(data,
905 return copy(img,w,h);
913 return copy(img,w,h);
924 float di = (
static_cast<float>(h))/nh;
925 float dj = (
static_cast<float>(w))/nw;
927 for (
size_t i=0; i<nh; i++)
929 auto i0 =
static_cast<size_t>(di*i);
930 for (
size_t j=0; j<nw; j++)
932 auto j0 =
static_cast<size_t>(dj*j);
bool deBayer_GRBG8_TO_RGB(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
bool deBayer_GRBG8_TO_BGR(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
bool isBayer8(int v)
Basic implementations of debayering functions.
const std::map< int, pixelTypeIplParams > pixelCode2iplParams
const pixelTypeIplParams iplPixelTypeMono16
bool readFromConnection(Image &dest, ImageNetworkHeader &header, ConnectionReader &connection)
This helper function groups code to avoid duplication.
const pixelTypeIplParams iplPixelTypeMono
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)
void iplDeallocateImage(IplImage *image)
void iplAllocateImageFP(IplImage *image, int doFill, float fillValue)
void iplSetBorderMode(IplImage *src, int mode, int border, int constVal)
void iplDeallocate(IplImage *image, int flag)
void iplAllocateImage(IplImage *image, int doFill, int fillValue)
#define YARP_FIXME_NOTIMPLEMENTED(what)
RandScalar * implementation(void *t)
int getTypeId()
ImageStorage::getTypeId.
ImageStorage(Image &owner)
void resize(size_t x, size_t y, int pixel_type, size_t quantum, bool topIsLow)
size_t extern_type_quantum
void _alloc_complete_extern(const void *buf, size_t x, size_t y, int pixel_type, size_t quantum, bool topIsLow)
int _pad_bytes(size_t linesize, size_t align) const
void _alloc_complete(size_t x, size_t y, int pixel_type, size_t quantum, bool topIsLow)
bool _set_ipl_header(size_t x, size_t y, int pixel_type, size_t quantum, bool topIsLow)
void _alloc_extern(const void *buf)
An interface for reading from a network connection.
virtual bool expectBlock(char *data, size_t len)=0
Read a block of data from the network connection.
virtual bool convertTextMode()=0
Reads in a standard description in text mode, and converts it to a standard description in binary.
virtual bool isError() const =0
An interface for writing to a network connection.
virtual bool isError() const =0
virtual void appendExternalBlock(const char *data, size_t len)=0
Send a block of data to the network connection, without making a copy.
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
Image class with user control of representation details.
void setQuantum(size_t imgQuantum)
void setPixelCode(int imgPixelCode)
Base class for storing images.
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Image & operator=(const Image &alt)
Assignment operator.
bool topIsLowIndex() const
void wrapIplImage(void *iplImage)
Act as a wrapper around an IPL/OpenCV image.
void setQuantum(size_t imgQuantum)
size_t width() const
Gets width of image in pixels.
bool read(yarp::os::ConnectionReader &connection) override
Read image from a connection.
void setPixelCode(int imgPixelCode)
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
unsigned char * getRawImage() const
Access to the internal image buffer.
Image()
Default constructor.
virtual size_t getPixelSize() const
Gets pixel size in memory in bytes.
void setTopIsLowIndex(bool flag)
control whether image has origin at top left (default) or bottom left.
bool write(yarp::os::ConnectionWriter &connection) const override
Write image to a connection.
bool copy(const Image &alt)
Copy operator.
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
~Image() override
Destructor.
void setPixelSize(size_t imgPixelSize)
void * getIplImage()
Returns IPL/OpenCV view of image, if possible.
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
size_t getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
void zero()
Set all pixels to 0.
size_t height() const
Gets height of image in pixels.
virtual int getPixelCode() const
Gets pixel type identifier.
#define IPL_BORDER_CONSTANT
#define IPL_DATA_ORDER_PIXEL
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR16
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR8
@ VOCAB_PIXEL_MONO_SIGNED
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB8
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG8
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG16
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG16
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG8
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB16
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
An interface to the operating system, including Port based communication.
char PixelMonoSigned
Signed byte pixel type.
size_t PAD_BYTES(size_t len, size_t pad)
computes the padding of YARP images.
yarp::os::NetUint16 PixelMono16
16-bit monochrome pixel type.
unsigned char PixelMono
Monochrome pixel type.
float PixelFloat
Floating point pixel type.
yarp::os::NetInt32 PixelInt
32-bit integer pixel type.
int nChannels
Most of OpenCV functions support 1,2,3 or 4 channels.
char colorModel[4]
ignored by OpenCV
int width
image width in pixels
Packed RGB pixel type, with pixels stored in reverse order.
Floating point HSV pixel type.
Packed HSV (hue/saturation/value pixel type.
Floating point RGB pixel type.
Signed, packed RGB pixel type.