YARP
Yet Another Robot Platform
DeBayer.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 
7 #include <yarp/os/Log.h>
8 
9 bool deBayer_GRBG8_TO_BGR(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
10 {
11  yAssert(((pixelSize == 3) && (dest.getPixelCode() == VOCAB_PIXEL_BGR)) ||
12  ((pixelSize == 4 && dest.getPixelCode() == VOCAB_PIXEL_BGRA)))
13 
14  dest.resize(source.width(), source.height());
15  // perform conversion, skip borders
16  for (size_t r = 0; r < source.height() - 2; r += 2)
17  {
18  unsigned char *destRow = dest.getRow(r);
19  unsigned char *sourceRowCurrent = source.getRow(r);
20  unsigned char *sourceRowNext = source.getRow(r + 1);
21 
22  //row i GRGRGR...
23  for (size_t c = 0; c < dest.width() - 2; c += 2)
24  {
25  //source is on G pixel
26  destRow[0] = sourceRowNext[0]; //blue
27  destRow[1] = sourceRowCurrent[0]; //green
28  destRow[2] = sourceRowCurrent[1]; //red
29 
30  //jump a pixel in destination
31  destRow += pixelSize;
32  sourceRowCurrent++;
33  sourceRowNext++;
34 
35  //source is now on R pixel
36  destRow[0] = sourceRowNext[0]; //blue
37  destRow[1] = sourceRowCurrent[1]; //green
38  destRow[2] = sourceRowCurrent[0]; //red
39 
40  destRow += pixelSize;
41  sourceRowCurrent++;
42  sourceRowNext++;
43  }
44 
45  destRow = dest.getRow(r + 1);
46  sourceRowCurrent = source.getRow(r + 1);
47  sourceRowNext = source.getRow(r + 2);
48 
49  //row is now BGBGBG...
50  for (size_t c = 0; c < dest.width() - 2; c += 2)
51  {
52  //source is on B pixel
53  destRow[0] = sourceRowCurrent[0]; //blue
54  destRow[1] = sourceRowCurrent[1]; //green
55  destRow[2] = sourceRowNext[1];; //red
56 
57  //jump a pixel in destination
58  destRow += pixelSize;
59  sourceRowCurrent++;
60  sourceRowNext++;
61 
62  //source is now on G pixel
63  destRow[0] = sourceRowCurrent[1]; //blue
64  destRow[1] = sourceRowCurrent[0]; //green
65  destRow[2] = sourceRowNext[0]; //red
66 
67  destRow += pixelSize;
68  sourceRowCurrent++;
69  sourceRowNext++;
70  }
71  }
72  return true;
73 }
74 
75 bool deBayer_GRBG8_TO_RGB(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
76 {
77  yAssert(((pixelSize == 3) && (dest.getPixelCode() == VOCAB_PIXEL_RGB)) ||
78  ((pixelSize == 4 && dest.getPixelCode() == VOCAB_PIXEL_RGBA)))
79 
80  dest.resize(source.width(), source.height());
81  // perform conversion, skip borders
82  for (size_t r = 0; r < source.height() - 2; r += 2)
83  {
84  unsigned char *destRow = dest.getRow(r);
85  unsigned char *sourceRowCurrent = source.getRow(r);
86  unsigned char *sourceRowNext = source.getRow(r + 1);
87 
88  //row i GRGRGR...
89  for (size_t c = 0; c < source.width() - 2; c += 2)
90  {
91  //source is on G pixel
92  destRow[0] = sourceRowCurrent[1]; //red
93  destRow[1] = sourceRowCurrent[0]; //green
94  destRow[2] = sourceRowNext[0];; //blue
95 
96  //jump a pixel in destination
97  destRow += pixelSize;
98  sourceRowCurrent++;
99  sourceRowNext++;
100 
101  //source is now on R pixel
102  destRow[0] = sourceRowCurrent[0]; //red
103  destRow[1] = sourceRowCurrent[1]; //green
104  destRow[2] = sourceRowNext[0]; //red
105 
106  destRow += pixelSize;
107  sourceRowCurrent++;
108  sourceRowNext++;
109  }
110 
111  destRow = dest.getRow(r + 1);
112  sourceRowCurrent = source.getRow(r + 1);
113  sourceRowNext = source.getRow(r + 2);
114 
115  //row is now BGBGBG...
116  for (size_t c = 0; c < dest.width() - 2; c += 2)
117  {
118  //source is on B pixel
119  destRow[0] = sourceRowNext[1]; //red
120  destRow[1] = sourceRowCurrent[1]; //green
121  destRow[2] = sourceRowCurrent[0]; //blue
122 
123  //jump a pixel in destination
124  destRow += pixelSize;
125  sourceRowCurrent++;
126  sourceRowNext++;
127 
128  //source is now on G pixel
129  destRow[0] = sourceRowNext[0]; //red
130  destRow[1] = sourceRowCurrent[0]; //green
131  destRow[2] = sourceRowCurrent[1]; //blue
132 
133  destRow += pixelSize;
134  sourceRowCurrent++;
135  sourceRowNext++;
136  }
137  }
138  return true;
139 }
140 
141 bool deBayer_BGGR8_TO_RGB(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
142 {
143  YARP_FIXME_NOTIMPLEMENTED("convert_BGGR8_TO_RGB\n");
144  return false;
145 }
146 
147 bool deBayer_RGGB8_TO_RGB(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
148 {
149  YARP_FIXME_NOTIMPLEMENTED("convert_RGGB8_TO_RGB\n");
150  return false;
151 }
152 
153 bool deBayer_BGGR8_TO_BGR(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
154 {
155  YARP_FIXME_NOTIMPLEMENTED("convert_BGGR8_TO_BGR\n");
156  return false;
157 }
158 
159 bool deBayer_RGGB8_TO_BGR(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
160 {
161  YARP_FIXME_NOTIMPLEMENTED("convert_RGGB8_TO_BGR\n");
162  return false;
163 }
bool deBayer_GRBG8_TO_RGB(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
Definition: DeBayer.cpp:75
bool deBayer_BGGR8_TO_BGR(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
Definition: DeBayer.cpp:153
bool deBayer_RGGB8_TO_RGB(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
Definition: DeBayer.cpp:147
bool deBayer_GRBG8_TO_BGR(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
Definition: DeBayer.cpp:9
bool deBayer_RGGB8_TO_BGR(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
Definition: DeBayer.cpp:159
bool deBayer_BGGR8_TO_RGB(yarp::sig::Image &source, yarp::sig::Image &dest, int pixelSize)
Definition: DeBayer.cpp:141
#define YARP_FIXME_NOTIMPLEMENTED(what)
Definition: Log.h:307
#define yAssert(x)
Definition: Log.h:294
Base class for storing images.
Definition: Image.h:82
size_t width() const
Gets width of image in pixels.
Definition: Image.h:166
unsigned char * getRow(size_t r)
Get the address of a the first byte of a row in memory.
Definition: Image.h:216
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
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
@ VOCAB_PIXEL_RGBA
Definition: Image.h:48
@ VOCAB_PIXEL_BGRA
Definition: Image.h:49
@ VOCAB_PIXEL_BGR
Definition: Image.h:52
@ VOCAB_PIXEL_RGB
Definition: Image.h:47