53 printf(
"There is a problem reading an image\n");
54 printf(
"incoming: width %zu, height %zu, code %zu, quantum %zu, topIsLow %zu, size %zu\n",
55 static_cast<size_t>(header.
width),
56 static_cast<size_t>(header.
height),
57 static_cast<size_t>(header.
id),
58 static_cast<size_t>(header.
quantum),
59 static_cast<size_t>(header.
topIsLow),
60 static_cast<size_t>(header.
imgSize));
61 printf(
"my space: width %zu, height %zu, code %d, quantum %zu, size %zu\n",
65 bool ok = connection.
expectBlock(
reinterpret_cast<char*
>(mem), allocatedBytes);
66 return (!connection.
isError() && ok);
93 void _alloc_extern (
const void *buf);
98 void _make_independent();
99 bool _set_ipl_header(
size_t x,
size_t y,
int pixel_type,
size_t quantum,
101 void _free_ipl_header();
102 void _alloc_complete(
size_t x,
size_t y,
int pixel_type,
size_t quantum,
104 void _free_complete();
108 int _pad_bytes (
size_t linesize,
size_t align)
const;
124 extern_type_quantum = -1;
131 void resize(
size_t x,
size_t y,
int pixel_type,
132 size_t quantum,
bool topIsLow);
134 void _alloc_complete_extern(
const void *buf,
size_t x,
size_t y,
int pixel_type,
135 size_t quantum,
bool topIsLow);
140 size_t quantum,
bool topIsLow) {
141 int need_recreation = 1;
147 if (need_recreation) {
149 DBGPF1 printf(
"HIT recreation for %p %p: %zu %zu %d\n",
static_cast<void*
>(
this),
static_cast<void*
>(pImage), x, y, pixel_type);
150 _alloc_complete (x, y, pixel_type, quantum, topIsLow);
152 extern_type_id = pixel_type;
153 extern_type_quantum = quantum;
179 if (pImage !=
nullptr) {
180 if (pImage->imageData !=
nullptr) {
186 pImage->imageData =
const_cast<char*
>(
reinterpret_cast<const char*
>(buf));
195 DBGPF1 printf(
"alloc_data1\n"), fflush(stdout);
200 char **ptr =
new char *[pImage->height];
206 yAssert(pImage->imageData !=
nullptr);
208 int height = pImage->height;
210 char * DataArea = pImage->imageData;
212 for (
int r = 0; r < height; r++)
217 Data[height-r-1] = DataArea;
219 DataArea += pImage->widthStep;
221 DBGPF1 printf(
"alloc_data4\n");
226 if (pImage !=
nullptr) {
227 if (pImage->imageData !=
nullptr) {
237 pImage->imageData =
nullptr;
271 _set_ipl_header(x, y, pixel_type, quantum, topIsLow);
326 if (pImage !=
nullptr) {
333 printf (
"*** Trying to allocate an invalid pixel type image\n");
348 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);
350 type_id = pixel_type;
351 this->quantum = quantum;
352 this->topIsLow = topIsLow;
361 this->quantum = quantum;
362 this->topIsLow = topIsLow;
366 _set_ipl_header(x, y, pixel_type, quantum, topIsLow);
380 const std::map<YarpVocabPixelTypesEnum, size_t> Image::pixelCode2Size = {
414 void Image::initialize() {
417 imgWidth = imgHeight = 0;
418 imgPixelSize = imgRowSize = 0;
446 if (getRawImage()!=
nullptr) {
447 memset(getRawImage(),0,getRawImageSize());
454 int code = getPixelCode();
456 if (code!=imgPixelCode) {
467 if (imgWidth!=width()||imgHeight!=height()) {
487 setPixelCode(-imgPixelSize);
491 this->imgPixelCode = imgPixelCode;
492 this->imgPixelSize = (imgPixelCode < 0) ? -imgPixelCode : pixelCode2Size.at(
static_cast<YarpVocabPixelTypesEnum>(imgPixelCode));
502 this->imgQuantum = imgQuantum;
522 void Image::synchronize() {
525 if (impl->pImage!=
nullptr) {
526 imgWidth = impl->pImage->width;
527 imgHeight = impl->pImage->height;
529 imgQuantum = impl->quantum;
530 imgRowSize = impl->pImage->widthStep;
531 setPixelCode(impl->type_id);
544 if (impl->pImage!=
nullptr) {
545 return reinterpret_cast<unsigned char*
>(impl->pImage->imageData);
553 if (impl->pImage!=
nullptr) {
554 return impl->pImage->imageSize;
559 #ifndef YARP_NO_DEPRECATED
570 auto* p =
static_cast<IplImage *
>(iplImage);
574 if (str==
"rgb"||str==
"RGB"||
575 str==
"bgr"||str==
"BGR"||
576 str==
"gray"||str==
"GRAY"||
577 str==
"graygray"||str==
"GRAYGRAY") {
579 if (str==
"rgb"||str==
"RGB") {
581 }
else if (str==
"bgr"||str==
"BGR") {
583 }
else if (str==
"gray"||str==
"GRAY"||
584 str==
"graygray"||str==
"GRAYGRAY") {
587 printf(
"specific IPL RGB order (%s) is not yet supported\n",
589 printf(
"Try RGB, BGR, or \n");
590 printf(
"Or fix code at %s line %d\n",__FILE__,__LINE__);
600 switch (color_code) {
612 switch (color_code) {
618 fprintf(stderr,
"No translation currently available for this pixel type\n");
623 switch (color_code) {
633 switch (color_code) {
645 fprintf(stderr,
"IPL pixel type / depth combination is not yet supported\n");
646 fprintf(stderr,
"Please email a YARP developer to complain, quoting this:\n");
647 fprintf(stderr,
" %s:%d\n", __FILE__, __LINE__);
650 if (getPixelCode()!=code && getPixelCode()!=-1) {
651 printf(
"your specific IPL format (%s depth %d -> %s) does not match your YARP format (%s)\n",
656 printf(
"Making a copy instead of just wrapping...\n");
661 img.
setExternal(p->imageData, p->width, p->height);
664 setQuantum(p->align);
667 setExternal(p->imageData, p->width, p->height);
681 bool ok = connection.
expectBlock(
reinterpret_cast<char*
>(&header),
sizeof(header));
693 setPixelCode(header.
id);
695 size_t q = getQuantum();
701 if (q !=
static_cast<size_t>(header.
quantum)) {
708 setTopIsLowIndex(header.
topIsLow == 0);
711 if (getPixelCode() == header.
id && q ==
static_cast<size_t>(header.
quantum) && imgPixelSize ==
static_cast<size_t>(header.
depth))
788 connection.
appendBlock(
reinterpret_cast<char*
>(&header),
sizeof(header));
789 unsigned char *mem = getRawImage();
895 std::swap(alt.implementation, implementation);
907 (
static_cast<ImageStorage*
>(implementation))->_alloc_complete_extern(data,
926 return copy(img,w,h);
935 return copy(img,w,h);
946 float di = (
static_cast<float>(h))/nh;
947 float dj = (
static_cast<float>(w))/nw;
949 for (
size_t i=0; i<nh; i++)
951 auto i0 =
static_cast<size_t>(di*i);
952 for (
size_t j=0; j<nw; j++)
954 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)
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.
bool swap(Image &alt)
swap operator.
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)
bool move(Image &&alt) noexcept
move operator.
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.
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.
void setTopIsLowIndex(bool flag)
control whether image has origin at top left (default) or bottom left.
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 origin
0 - top-left origin, 1 - bottom-left origin (Windows bitmaps style)
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.