YARP
Yet Another Robot Platform
ImageUtils.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include <yarp/sig/ImageUtils.h>
7 #include <cstring>
8 #include <algorithm> // std::math
9 
10 using namespace yarp::sig;
11 
12 static bool checkImages(const Image& bigImg, const Image& smallImg1, const Image& smallImg2)
13 {
14  return bigImg.getPixelCode() == smallImg1.getPixelCode() &&
15  bigImg.getPixelCode() == smallImg2.getPixelCode() &&
16  smallImg1.width() == smallImg2.width() &&
17  smallImg1.height() == smallImg2.height() &&
18  bigImg.getRawImageSize() == 2*smallImg1.getRawImageSize();
19 
20 }
21 
22 
23 bool utils::vertSplit(const Image& inImg, Image& outImgL, Image& outImgR)
24 {
25  outImgL.resize(inImg.width()/2, inImg.height());
26  outImgR.resize(inImg.width()/2, inImg.height());
27 
28  if (!checkImages(inImg, outImgL, outImgR)) {
29  return false;
30  }
31 
32  size_t inHeight = inImg.height();
33  size_t singleImage_rowSizeByte = outImgL.getRowSize();
34  unsigned char *pixelLeft = outImgL.getRawImage();
35  unsigned char *pixelRight = outImgR.getRawImage();
36  unsigned char *pixelInput = inImg.getRawImage();
37 
38  for(size_t h=0; h<inHeight; h++)
39  {
40  // Copy the memory
41  memcpy(pixelLeft + h*singleImage_rowSizeByte, pixelInput, singleImage_rowSizeByte);
42  memcpy(pixelRight + h*singleImage_rowSizeByte, pixelInput+=singleImage_rowSizeByte, singleImage_rowSizeByte);
43 
44  // Update the pointers
45  pixelInput+= singleImage_rowSizeByte;
46  }
47  return true;
48 }
49 
50 bool utils::horzSplit(const Image& inImg, Image& outImgUp, Image& outImgDown)
51 {
52  outImgUp.resize(inImg.width(), inImg.height()/2);
53  outImgDown.resize(inImg.width(), inImg.height()/2);
54 
55  if (!checkImages(inImg, outImgUp, outImgDown)) {
56  return false;
57  }
58  // Copy the memory
59  size_t imgSize = outImgUp.getRawImageSize();
60  memcpy(outImgUp.getRawImage(), inImg.getRawImage(), imgSize);
61  memcpy(outImgDown.getRawImage(), inImg.getRawImage() + imgSize, imgSize);
62  return true;
63 }
64 
65 
66 
67 bool utils::horzConcat(const Image& inImgL, const Image& inImgR, Image& outImg)
68 {
69  outImg.resize(inImgL.width()*2, inImgL.height());
70 
71  if (!checkImages(outImg, inImgL, inImgR)) {
72  return false;
73  }
74 
75  size_t singleImage_rowSizeByte = inImgL.getRowSize();
76  unsigned char * pixelLeft = inImgL.getRawImage();
77  unsigned char * pixelRight = inImgR.getRawImage();
78  unsigned char * pixelOutLeft = outImg.getRawImage();
79  unsigned char * pixelOutRight = outImg.getRawImage() + singleImage_rowSizeByte;
80 
81  size_t height = inImgL.height();
82 
83  for(size_t h=0; h<height; h++)
84  {
85  // Copy the memory
86  memcpy(pixelOutLeft, pixelLeft, singleImage_rowSizeByte);
87  memcpy(pixelOutRight, pixelRight, singleImage_rowSizeByte);
88 
89  // Update the pointers
90  pixelOutLeft += 2*singleImage_rowSizeByte;
91  pixelOutRight += 2*singleImage_rowSizeByte;
92  pixelLeft += singleImage_rowSizeByte;
93  pixelRight += singleImage_rowSizeByte;
94  }
95  return true;
96 }
97 
98 bool utils::vertConcat(const Image& inImgUp, const Image& inImgDown, Image& outImg)
99 {
100  outImg.resize(inImgUp.width(), inImgUp.height()*2);
101 
102  if (!checkImages(outImg, inImgUp, inImgDown)) {
103  return false;
104  }
105 
106  // Copy the memory
107  size_t imgSize = inImgUp.getRawImageSize();
108  memcpy(outImg.getRawImage(), inImgUp.getRawImage(), imgSize);
109  memcpy(outImg.getRawImage() + imgSize, inImgDown.getRawImage(), imgSize);
110  return true;
111 }
112 
114  const std::pair<unsigned int, unsigned int>& vertex1,
115  const std::pair<unsigned int, unsigned int>& vertex2,
116  yarp::sig::Image& outImg)
117 {
118  if (inImg.getPixelCode() != outImg.getPixelCode()) {
119  return false;
120  }
121 
122  // Normalize vertices: upper-left (tlx,tly) and bottom-right (brx,bry) corners
123  auto tlx = std::min(vertex1.first, vertex2.first);
124  auto tly = std::min(vertex1.second, vertex2.second);
125  auto brx = std::max(vertex1.first, vertex2.first);
126  auto bry = std::max(vertex1.second, vertex2.second);
127 
128  if (!inImg.isPixel(brx, bry)) {
129  return false;
130  }
131 
132  outImg.resize(brx - tlx + 1, bry - tly + 1); // width, height
133 
134  auto * pixelOut = outImg.getRawImage();
135 
136  for (unsigned int row = 0; row < outImg.height(); row++) {
137  const auto * pixelIn = inImg.getPixelAddress(tlx, tly + row);
138  memcpy(pixelOut, pixelIn, outImg.getRowSize());
139  pixelOut += outImg.getRowSize();
140  }
141 
142  return true;
143 }
static bool checkImages(const Image &bigImg, const Image &smallImg1, const Image &smallImg2)
Definition: ImageUtils.cpp:12
Base class for storing images.
Definition: Image.h:82
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition: Image.h:240
size_t width() const
Gets width of image in pixels.
Definition: Image.h:166
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:192
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:541
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:550
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:452
bool isPixel(size_t x, size_t y) const
Check whether a coordinate lies within the image.
Definition: Image.h:250
size_t height() const
Gets height of image in pixels.
Definition: Image.h:172
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.cpp:440
bool vertSplit(const yarp::sig::Image &inImg, yarp::sig::Image &outImgL, yarp::sig::Image &outImgR)
Split vertically an image in two images of the same size.
Definition: ImageUtils.cpp:23
bool cropRect(const yarp::sig::Image &inImg, const std::pair< unsigned int, unsigned int > &vertex1, const std::pair< unsigned int, unsigned int > &vertex2, yarp::sig::Image &outImg)
Crop a rectangle area out of an image given two opposite vertices.
Definition: ImageUtils.cpp:113
bool horzSplit(const yarp::sig::Image &inImg, yarp::sig::Image &outImgUp, yarp::sig::Image &outImgDown)
Split horizontally an image in two images of the same size.
Definition: ImageUtils.cpp:50
bool vertConcat(const yarp::sig::Image &inImgUp, const yarp::sig::Image &inImgDown, yarp::sig::Image &outImg)
Concatenate vertically two images of the same size in one with double height.
Definition: ImageUtils.cpp:98
bool horzConcat(const yarp::sig::Image &inImgL, const yarp::sig::Image &inImgR, yarp::sig::Image &outImg)
Concatenate horizontally two images of the same size in one with double width.
Definition: ImageUtils.cpp:67
Signal processing.
Definition: Image.h:22