YARP
Yet Another Robot Platform
ImageUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <yarp/sig/ImageUtils.h>
10 #include <cstring>
11 
12 using namespace yarp::sig;
13 
14 static bool checkImages(const Image& bigImg, const Image& smallImg1, const Image& smallImg2)
15 {
16  return bigImg.getPixelCode() == smallImg1.getPixelCode() &&
17  bigImg.getPixelCode() == smallImg2.getPixelCode() &&
18  smallImg1.width() == smallImg2.width() &&
19  smallImg1.height() == smallImg2.height() &&
20  bigImg.getRawImageSize() == 2*smallImg1.getRawImageSize();
21 
22 }
23 
24 
25 bool utils::vertSplit(const Image& inImg, Image& outImgL, Image& outImgR)
26 {
27  outImgL.resize(inImg.width()/2, inImg.height());
28  outImgR.resize(inImg.width()/2, inImg.height());
29 
30  if (!checkImages(inImg, outImgL, outImgR)) {
31  return false;
32  }
33 
34  size_t inHeight = inImg.height();
35  size_t singleImage_rowSizeByte = outImgL.getRowSize();
36  unsigned char *pixelLeft = outImgL.getRawImage();
37  unsigned char *pixelRight = outImgR.getRawImage();
38  unsigned char *pixelInput = inImg.getRawImage();
39 
40  for(size_t h=0; h<inHeight; h++)
41  {
42  // Copy the memory
43  memcpy(pixelLeft + h*singleImage_rowSizeByte, pixelInput, singleImage_rowSizeByte);
44  memcpy(pixelRight + h*singleImage_rowSizeByte, pixelInput+=singleImage_rowSizeByte, singleImage_rowSizeByte);
45 
46  // Update the pointers
47  pixelInput+= singleImage_rowSizeByte;
48  }
49  return true;
50 }
51 
52 bool utils::horzSplit(const Image& inImg, Image& outImgUp, Image& outImgDown)
53 {
54  outImgUp.resize(inImg.width(), inImg.height()/2);
55  outImgDown.resize(inImg.width(), inImg.height()/2);
56 
57  if (!checkImages(inImg, outImgUp, outImgDown)) {
58  return false;
59  }
60  // Copy the memory
61  size_t imgSize = outImgUp.getRawImageSize();
62  memcpy(outImgUp.getRawImage(), inImg.getRawImage(), imgSize);
63  memcpy(outImgDown.getRawImage(), inImg.getRawImage() + imgSize, imgSize);
64  return true;
65 }
66 
67 
68 
69 bool utils::horzConcat(const Image& inImgL, const Image& inImgR, Image& outImg)
70 {
71  outImg.resize(inImgL.width()*2, inImgL.height());
72 
73  if (!checkImages(outImg, inImgL, inImgR)) {
74  return false;
75  }
76 
77  size_t singleImage_rowSizeByte = inImgL.getRowSize();
78  unsigned char * pixelLeft = inImgL.getRawImage();
79  unsigned char * pixelRight = inImgR.getRawImage();
80  unsigned char * pixelOutLeft = outImg.getRawImage();
81  unsigned char * pixelOutRight = outImg.getRawImage() + singleImage_rowSizeByte;
82 
83  size_t height = inImgL.height();
84 
85  for(size_t h=0; h<height; h++)
86  {
87  // Copy the memory
88  memcpy(pixelOutLeft, pixelLeft, singleImage_rowSizeByte);
89  memcpy(pixelOutRight, pixelRight, singleImage_rowSizeByte);
90 
91  // Update the pointers
92  pixelOutLeft += 2*singleImage_rowSizeByte;
93  pixelOutRight += 2*singleImage_rowSizeByte;
94  pixelLeft += singleImage_rowSizeByte;
95  pixelRight += singleImage_rowSizeByte;
96  }
97  return true;
98 }
99 
100 bool utils::vertConcat(const Image& inImgUp, const Image& inImgDown, Image& outImg)
101 {
102  outImg.resize(inImgUp.width(), inImgUp.height()*2);
103 
104  if (!checkImages(outImg, inImgUp, inImgDown)) {
105  return false;
106  }
107 
108  // Copy the memory
109  size_t imgSize = inImgUp.getRawImageSize();
110  memcpy(outImg.getRawImage(), inImgUp.getRawImage(), imgSize);
111  memcpy(outImg.getRawImage() + imgSize, inImgDown.getRawImage(), imgSize);
112  return true;
113 }
yarp::sig::utils::horzSplit
bool horzSplit(const yarp::sig::Image &inImg, yarp::sig::Image &outImgUp, yarp::sig::Image &outImgDown)
horzSplit, split horizontally an image in two images of the same size.
Definition: ImageUtils.cpp:52
yarp::sig::utils::vertConcat
bool vertConcat(const yarp::sig::Image &inImgUp, const yarp::sig::Image &inImgDown, yarp::sig::Image &outImg)
vertConcat, concatenate vertically two images of the same size in one with double height.
Definition: ImageUtils.cpp:100
yarp::sig
Signal processing.
Definition: Image.h:25
yarp::sig::utils::vertSplit
bool vertSplit(const yarp::sig::Image &inImg, yarp::sig::Image &outImgL, yarp::sig::Image &outImgR)
vertSplit, split vertically an image in two images of the same size.
Definition: ImageUtils.cpp:25
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:543
yarp::sig::Image::getRowSize
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:179
yarp::sig::Image::width
size_t width() const
Gets width of image in pixels.
Definition: Image.h:153
checkImages
static bool checkImages(const Image &bigImg, const Image &smallImg1, const Image &smallImg2)
Definition: ImageUtils.cpp:14
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:466
yarp::sig::Image::height
size_t height() const
Gets height of image in pixels.
Definition: Image.h:159
ImageUtils.h
yarp::sig::Image
Base class for storing images.
Definition: Image.h:85
yarp::sig::utils::horzConcat
bool horzConcat(const yarp::sig::Image &inImgL, const yarp::sig::Image &inImgR, yarp::sig::Image &outImg)
horzConcat, concatenate horizontally two images of the same size in one with double width.
Definition: ImageUtils.cpp:69
yarp::sig::Image::getRawImage
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:534
yarp::sig::Image::getPixelCode
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.cpp:454