image.cc
Go to the documentation of this file.
1 /*
2  * This file is part of the rc_genicam_api package.
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Heiko Hirschmueller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include "image.h"
37 
38 #include "exception.h"
39 #include "pixel_formats.h"
40 
41 #include <cstring>
42 
43 namespace rcg
44 {
45 
46 Image::Image(const Buffer *buffer, std::uint32_t part)
47 {
48  if (buffer->getImagePresent(part))
49  {
50  timestamp=buffer->getTimestampNS();
51 
52  width=buffer->getWidth(part);
53  height=buffer->getHeight(part);
54  xoffset=buffer->getXOffset(part);
55  yoffset=buffer->getYOffset(part);
56  xpadding=buffer->getXPadding(part);
57  ypadding=buffer->getYPadding();
58  frameid=buffer->getFrameID();
59  pixelformat=buffer->getPixelFormat(part);
60  bigendian=buffer->isBigEndian();
61 
62  const size_t size=buffer->getSize(part);
63 
64  pixel.reset(new uint8_t [size]);
65 
66  memcpy(pixel.get(), reinterpret_cast<uint8_t *>(buffer->getBase(part)), size);
67  }
68  else
69  {
70  throw GenTLException("Image::Image(): Now image available.");
71  }
72 }
73 
74 namespace
75 {
76 
81 inline unsigned char clamp8(int v)
82 {
83  const int v2=v<0 ? 0:v;
84  return static_cast<unsigned char>(v2>255 ? 255:v2);
85 }
86 
87 }
88 
89 void convYCbCr411toRGB(uint8_t rgb[3], const uint8_t *row, int i)
90 {
91  const uint32_t j=static_cast<uint32_t>((i>>2)*6);
92  const uint32_t js=static_cast<uint32_t>(i&0x3);
93 
94  int Y=row[j+js];
95  if (js > 1)
96  {
97  Y=row[j+js+1];
98  }
99 
100  const int Cb=static_cast<int>(row[j+2])-128;
101  const int Cr=static_cast<int>(row[j+5])-128;
102 
103  const int rc=(90*Cr+32)>>6;
104  const int gc=(-22*Cb-46*Cr+32)>>6;
105  const int bc=(113*Cb+32)>>6;
106 
107  rgb[0]=clamp8(Y+rc);
108  rgb[1]=clamp8(Y+gc);
109  rgb[2]=clamp8(Y+bc);
110 }
111 
112 void convYCbCr411toQuadRGB(uint8_t rgb[12], const uint8_t *row, int i)
113 {
114  i=(i>>2)*6;
115 
116  const int Y[4]={row[i], row[i+1], row[i+3], row[i+4]};
117  const int Cb=static_cast<int>(row[i+2])-128;
118  const int Cr=static_cast<int>(row[i+5])-128;
119 
120  const int rc=(90*Cr+32)>>6;
121  const int gc=(-22*Cb-46*Cr+32)>>6;
122  const int bc=(113*Cb+32)>>6;
123 
124  for (int j=0; j<4; j++)
125  {
126  *rgb++=clamp8(Y[j]+rc);
127  *rgb++=clamp8(Y[j]+gc);
128  *rgb++=clamp8(Y[j]+bc);
129  }
130 }
131 
132 void getColor(uint8_t rgb[3], const std::shared_ptr<const rcg::Image> &img,
133  uint32_t ds, uint32_t i, uint32_t k)
134 {
135  i*=ds;
136  k*=ds;
137 
138  if (img->getPixelFormat() == Mono8) // convert from monochrome
139  {
140  size_t lstep=img->getWidth()+img->getXPadding();
141  const uint8_t *p=img->getPixels()+k*lstep+i;
142 
143  uint32_t g=0, n=0;
144 
145  for (uint32_t kk=0; kk<ds; kk++)
146  {
147  for (uint32_t ii=0; ii<ds; ii++)
148  {
149  g+=p[ii];
150  n++;
151  }
152 
153  p+=lstep;
154  }
155 
156  rgb[2]=rgb[1]=rgb[0]=static_cast<uint8_t>(g/n);
157  }
158  else if (img->getPixelFormat() == YCbCr411_8) // convert from YUV
159  {
160  size_t lstep=(img->getWidth()>>2)*6+img->getXPadding();
161  const uint8_t *p=img->getPixels()+k*lstep;
162 
163  uint32_t r=0;
164  uint32_t g=0;
165  uint32_t b=0;
166  uint32_t n=0;
167 
168  for (uint32_t kk=0; kk<ds; kk++)
169  {
170  for (uint32_t ii=0; ii<ds; ii++)
171  {
172  uint8_t v[3];
173  rcg::convYCbCr411toRGB(v, p, static_cast<int>(i+ii));
174 
175  r+=v[0];
176  g+=v[1];
177  b+=v[2];
178  n++;
179  }
180 
181  p+=lstep;
182  }
183 
184  rgb[0]=static_cast<uint8_t>(r/n);
185  rgb[1]=static_cast<uint8_t>(g/n);
186  rgb[2]=static_cast<uint8_t>(b/n);
187  }
188 }
189 
190 }
size_t getYPadding() const
Returns vertical padding of the data in the buffer in bytes.
Definition: buffer.cc:336
size_t ypadding
Definition: image.h:101
Definition: PFNC.h:272
size_t getXPadding(std::uint32_t part) const
Returns horizontal padding of the data in the buffer in bytes.
Definition: buffer.cc:323
void convYCbCr411toRGB(uint8_t rgb[3], const uint8_t *row, int i)
Conversion of one pixel from YCbCr411 format (6 bytes for four pixels) to RGB.
Definition: image.cc:89
bool bigendian
Definition: image.h:104
size_t getWidth(std::uint32_t part) const
Returns the width of the image in pixel.
Definition: buffer.cc:271
void convYCbCr411toQuadRGB(uint8_t rgb[12], const uint8_t *row, int i)
Conversion of a group of four pixels from YCbCr411 format (6 bytes for four pixels) to RGB...
Definition: image.cc:112
The buffer class encapsulates a Genicam buffer that is provided by a stream.
Definition: buffer.h:115
void * getBase(std::uint32_t part) const
Returns the base address of the specified part of the multi-part buffer.
Definition: buffer.cc:174
uint64_t getPixelFormat(std::uint32_t part) const
Returns the pixel format of the specified part as defined in the PFNC.
Definition: buffer.cc:388
uint64_t timestamp
Definition: image.h:95
size_t xpadding
Definition: image.h:100
Image(const Buffer *buffer, std::uint32_t part)
Copies the image information of the buffer.
Definition: image.cc:46
size_t xoffset
Definition: image.h:98
void getColor(uint8_t rgb[3], const std::shared_ptr< const rcg::Image > &img, uint32_t ds, uint32_t i, uint32_t k)
Expects an image in Mono8 or YCbCr411_8 format and returns the color as RGB value at the given pixel ...
Definition: image.cc:132
size_t yoffset
Definition: image.h:99
uint64_t getTimestampNS() const
Returns the acquisition timestamp of the data in this buffer in ns.
Definition: buffer.cc:489
bool isBigEndian() const
Returns if the data is given as big or little endian.
Definition: buffer.cc:460
Definition: buffer.cc:42
bool getImagePresent(std::uint32_t part) const
Returns if a 2D, 3D or confidence image is present in the specified part.
Definition: buffer.cc:346
uint64_t getFrameID() const
Returns the sequentially incremented number of the frame.
Definition: buffer.cc:341
size_t getYOffset(std::uint32_t part) const
Returns the vertical offset of the data in the buffer in lines from the image origin to handle areas ...
Definition: buffer.cc:310
size_t height
Definition: image.h:97
std::unique_ptr< uint8_t[]> pixel
Definition: image.h:93
size_t getXOffset(std::uint32_t part) const
Returns the horizontal offset of the data in the buffer in pixels from the image origin to handle are...
Definition: buffer.cc:297
size_t getHeight(std::uint32_t part) const
Returns the height of the image in pixel.
Definition: buffer.cc:284
uint64_t frameid
Definition: image.h:102
uint64_t pixelformat
Definition: image.h:103
size_t getSize(std::uint32_t part) const
Returns the size of the specified part of the mult-part buffer.
Definition: buffer.cc:197


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Thu Jun 6 2019 19:10:54