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