Apollo  6.0
Open source self driving car software
image_8u.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 #pragma once
17 
18 #include <map>
19 #include <memory>
20 
23 
24 namespace apollo {
25 namespace perception {
26 namespace base {
27 
28 enum class Color {
29  NONE = 0x00,
30  GRAY = 0x01,
31  RGB = 0x02,
32  BGR = 0x03,
33 };
34 
35 const std::map<Color, int> kChannelsMap{
36  {Color::GRAY, 1}, {Color::RGB, 3}, {Color::BGR, 3}};
37 
44 class Image8U {
45  public:
47  : rows_(0),
48  cols_(0),
49  type_(Color::NONE),
50  channels_(0),
51  width_step_(0),
52  blob_(nullptr),
53  offset_(0) {}
54 
55  Image8U(int rows, int cols, Color type, std::shared_ptr<Blob<uint8_t>> blob,
56  int offset = 0)
57  : rows_(rows), cols_(cols), type_(type), blob_(blob), offset_(offset) {
58  channels_ = kChannelsMap.at(type);
59  CHECK_EQ(blob_->num_axes(), 3);
60  CHECK_EQ(blob_->shape(2), channels_);
61  CHECK_LE(offset_ + blob_->offset({rows - 1, cols - 1, channels_ - 1}),
62  (int)(blob_->count()));
63  width_step_ = blob_->offset({1, 0, 0}) * static_cast<int>(sizeof(uint8_t));
64  }
65 
66  Image8U(int rows, int cols, Color type)
67  : rows_(rows), cols_(cols), type_(type), offset_(0) {
68  channels_ = kChannelsMap.at(type);
69  blob_.reset(new Blob<uint8_t>({rows_, cols_, channels_}));
70  width_step_ = blob_->offset({1, 0, 0}) * static_cast<int>(sizeof(uint8_t));
71  }
72 
73  Image8U(const Image8U &src)
74  : rows_(src.rows_),
75  cols_(src.cols_),
76  type_(src.type_),
77  channels_(src.channels_),
78  width_step_(src.width_step_),
79  blob_(src.blob_),
80  offset_(src.offset_) {}
81 
82  Image8U &operator=(const Image8U &src) {
83  this->rows_ = src.rows_;
84  this->cols_ = src.cols_;
85  this->type_ = src.type_;
86  this->channels_ = src.channels_;
87  this->width_step_ = src.width_step_;
88  this->blob_ = src.blob_;
89  this->offset_ = src.offset_;
90  return *this;
91  }
92 
93  ~Image8U() {}
94 
95  uint8_t *mutable_cpu_data() { return mutable_cpu_ptr(0); }
96 
97  uint8_t *mutable_gpu_data() { return mutable_gpu_ptr(0); }
98 
99  const uint8_t *cpu_data() const { return cpu_ptr(0); }
100 
101  const uint8_t *gpu_data() const { return gpu_ptr(0); }
102 
103  const uint8_t *cpu_ptr(int row = 0) const {
104  return blob_->cpu_data() + blob_->offset({row, 0, 0}) + offset_;
105  }
106 
107  const uint8_t *gpu_ptr(int row = 0) const {
108  return blob_->gpu_data() + blob_->offset({row, 0, 0}) + offset_;
109  }
110 
111  uint8_t *mutable_cpu_ptr(int row = 0) {
112  return blob_->mutable_cpu_data() + blob_->offset({row, 0, 0}) + offset_;
113  }
114 
115  uint8_t *mutable_gpu_ptr(int row = 0) {
116  return blob_->mutable_gpu_data() + blob_->offset({row, 0, 0}) + offset_;
117  }
118 
119  Color type() const { return type_; }
120  int rows() const { return rows_; }
121  int cols() const { return cols_; }
122  int channels() const { return channels_; }
123  int width_step() const { return width_step_; }
124  // @brief: returns the total number of pixels.
125  int total() const { return rows_ * cols_ * channels_; }
126 
128  int offset = offset_ + blob_->offset({roi.y, roi.x, 0});
129  // return Image8U(roi.height, roi.width, type_, blob_, offset);
130  return Image8U(roi.height, roi.width, type_, blob_, offset);
131  }
132 
133  std::shared_ptr<Blob<uint8_t>> blob() { return blob_; }
134 
135  // DONOT return `std::shared_ptr<const Blob<uint8_t>> &` or `const std::... &`
136  std::shared_ptr<const Blob<uint8_t>> blob() const { return blob_; }
137 
138  protected:
139  int rows_;
140  int cols_;
144  std::shared_ptr<Blob<uint8_t>> blob_;
145  int offset_;
146 }; // class Image8U
147 
148 typedef std::shared_ptr<Image8U> Image8UPtr;
149 typedef std::shared_ptr<const Image8U> Image8UConstPtr;
150 
151 } // namespace base
152 } // namespace perception
153 } // namespace apollo
int cols_
Definition: image_8u.h:140
int offset_
Definition: image_8u.h:145
Color type_
Definition: image_8u.h:141
Image8U operator()(const Rect< int > &roi)
Definition: image_8u.h:127
T width
Definition: box.h:119
Color type() const
Definition: image_8u.h:119
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
uint8_t * mutable_gpu_data()
Definition: image_8u.h:97
A wrapper around Blob holders serving as the basic computational unit for images. ...
Definition: image_8u.h:44
const uint8_t * gpu_ptr(int row=0) const
Definition: image_8u.h:107
int channels() const
Definition: image_8u.h:122
uint8_t * mutable_gpu_ptr(int row=0)
Definition: image_8u.h:115
std::shared_ptr< Blob< uint8_t > > blob()
Definition: image_8u.h:133
~Image8U()
Definition: image_8u.h:93
std::shared_ptr< const Image8U > Image8UConstPtr
Definition: image_8u.h:149
const uint8_t * cpu_ptr(int row=0) const
Definition: image_8u.h:103
T x
Definition: box.h:117
int rows_
Definition: image_8u.h:139
std::shared_ptr< Image8U > Image8UPtr
Definition: image_8u.h:148
T y
Definition: box.h:118
int cols() const
Definition: image_8u.h:121
std::shared_ptr< Blob< uint8_t > > blob_
Definition: image_8u.h:144
const uint8_t * cpu_data() const
Definition: image_8u.h:99
T height
Definition: box.h:120
Image8U(int rows, int cols, Color type, std::shared_ptr< Blob< uint8_t >> blob, int offset=0)
Definition: image_8u.h:55
int width_step() const
Definition: image_8u.h:123
const std::map< Color, int > kChannelsMap
Definition: image_8u.h:35
Image8U()
Definition: image_8u.h:46
Image8U(const Image8U &src)
Definition: image_8u.h:73
uint8_t * mutable_cpu_ptr(int row=0)
Definition: image_8u.h:111
uint8_t * mutable_cpu_data()
Definition: image_8u.h:95
const uint8_t * gpu_data() const
Definition: image_8u.h:101
Image8U(int rows, int cols, Color type)
Definition: image_8u.h:66
std::shared_ptr< const Blob< uint8_t > > blob() const
Definition: image_8u.h:136
Image8U & operator=(const Image8U &src)
Definition: image_8u.h:82
int rows() const
Definition: image_8u.h:120
int width_step_
Definition: image_8u.h:143
int total() const
Definition: image_8u.h:125
int channels_
Definition: image_8u.h:142
Color
Definition: image_8u.h:28