Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: extensions/browser/api/media_perception_private/conversion_utils.cc

Issue 2858353002: MediaPerceptionPrivate API impl and testing. (Closed)
Patch Set: Addressed comments. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "extensions/browser/api/media_perception_private/conversion_utils.h"
6
7 #include "base/memory/ptr_util.h"
8
9 namespace extensions {
10 namespace api {
11 namespace media_perception_private {
12
13 namespace {
14
15 std::unique_ptr<Point> PointProtoToIdl(const mri::Point& point) {
16 std::unique_ptr<Point> point_result = base::MakeUnique<Point>();
17 if (point.has_x())
18 point_result->x = base::MakeUnique<double>(point.x());
19
20 if (point.has_y())
21 point_result->y = base::MakeUnique<double>(point.y());
22
23 return point_result;
24 }
25
26 std::unique_ptr<BoundingBox> BoundingBoxProtoToIdl(
27 const mri::BoundingBox& bounding_box) {
28 std::unique_ptr<BoundingBox> bounding_box_result =
29 base::MakeUnique<BoundingBox>();
30 if (bounding_box.has_normalized())
31 bounding_box_result->normalized =
32 base::MakeUnique<bool>(bounding_box.normalized());
33
34 if (bounding_box.has_top_left())
35 bounding_box_result->top_left = PointProtoToIdl(bounding_box.top_left());
36
37 if (bounding_box.has_bottom_right())
38 bounding_box_result->bottom_right =
39 PointProtoToIdl(bounding_box.bottom_right());
40
41 return bounding_box_result;
42 }
43
44 EntityType EntityTypeProtoToIdl(const mri::Entity& entity) {
45 if (entity.has_type()) {
46 switch (entity.type()) {
47 case mri::Entity::FACE:
48 return ENTITY_TYPE_FACE;
49 case mri::Entity::PERSON:
50 return ENTITY_TYPE_PERSON;
51 case mri::Entity::UNSPECIFIED:
52 return ENTITY_TYPE_UNSPECIFIED;
53 }
54 NOTREACHED() << "Unknown entity type: " << entity.type();
55 }
56 return ENTITY_TYPE_UNSPECIFIED;
57 }
58
59 Entity EntityProtoToIdl(const mri::Entity& entity) {
60 Entity entity_result;
61 if (entity.has_id())
62 entity_result.id = base::MakeUnique<int>(entity.id());
63
64 entity_result.type = EntityTypeProtoToIdl(entity);
65 if (entity.has_confidence())
66 entity_result.confidence = base::MakeUnique<double>(entity.confidence());
67
68 if (entity.has_bounding_box())
69 entity_result.bounding_box = BoundingBoxProtoToIdl(entity.bounding_box());
70
71 return entity_result;
72 }
73
74 FramePerception FramePerceptionProtoToIdl(
75 const mri::FramePerception& frame_perception) {
76 FramePerception frame_perception_result;
77 if (frame_perception.has_frame_id())
78 frame_perception_result.frame_id =
79 base::MakeUnique<int>(frame_perception.frame_id());
80
81 if (frame_perception.has_frame_width_in_px()) {
82 frame_perception_result.frame_width_in_px =
83 base::MakeUnique<int>(frame_perception.frame_width_in_px());
84 }
85 if (frame_perception.has_frame_height_in_px()) {
86 frame_perception_result.frame_height_in_px =
87 base::MakeUnique<int>(frame_perception.frame_height_in_px());
88 }
89 if (frame_perception.has_timestamp()) {
90 frame_perception_result.timestamp =
91 base::MakeUnique<double>(frame_perception.timestamp());
92 }
93 if (frame_perception.entity_size() > 0) {
94 frame_perception_result.entities = base::MakeUnique<std::vector<Entity>>();
95 for (const auto& entity : frame_perception.entity())
96 frame_perception_result.entities->emplace_back(EntityProtoToIdl(entity));
97 }
98 return frame_perception_result;
99 }
100
101 ImageFormat ImageFormatProtoToIdl(const mri::ImageFrame& image_frame) {
102 if (image_frame.has_format()) {
103 switch (image_frame.format()) {
104 case mri::ImageFrame::RGB:
105 return IMAGE_FORMAT_RGB;
106 case mri::ImageFrame::PNG:
107 return IMAGE_FORMAT_PNG;
108 case mri::ImageFrame::JPEG:
109 return IMAGE_FORMAT_JPEG;
110 case mri::ImageFrame::FORMAT_UNSPECIFIED:
111 return IMAGE_FORMAT_UNSPECIFIED;
112 }
113 NOTREACHED() << "Unknown image format: " << image_frame.format();
114 }
115 return IMAGE_FORMAT_UNSPECIFIED;
116 }
117
118 ImageFrame ImageFrameProtoToIdl(const mri::ImageFrame& image_frame) {
119 ImageFrame image_frame_result;
120 if (image_frame.has_width())
121 image_frame_result.width = base::MakeUnique<int>(image_frame.width());
122
123 if (image_frame.has_height())
124 image_frame_result.height = base::MakeUnique<int>(image_frame.height());
125
126 if (image_frame.has_data_length())
tbarzic 2017/05/18 19:28:01 nit: {} now that the body has more than a line (si
Luke Sorenson 2017/05/18 21:24:17 Done. This is why we just include them regardless
127 image_frame_result.data_length =
128 base::MakeUnique<int>(image_frame.data_length());
129
130 if (image_frame.has_pixel_data()) {
131 image_frame_result.frame = base::MakeUnique<std::vector<char>>(
132 image_frame.pixel_data().begin(), image_frame.pixel_data().end());
133 }
134
135 image_frame_result.format = ImageFormatProtoToIdl(image_frame);
136 return image_frame_result;
137 }
138
139 PerceptionSample PerceptionSampleProtoToIdl(
140 const mri::PerceptionSample& perception_sample) {
141 PerceptionSample perception_sample_result;
142 if (perception_sample.has_frame_perception()) {
143 perception_sample_result.frame_perception =
144 base::MakeUnique<FramePerception>(
145 FramePerceptionProtoToIdl(perception_sample.frame_perception()));
146 }
147 if (perception_sample.has_image_frame()) {
148 perception_sample_result.image_frame = base::MakeUnique<ImageFrame>(
149 ImageFrameProtoToIdl(perception_sample.image_frame()));
150 }
151 return perception_sample_result;
152 }
153
154 } // namespace
155
156 State StateProtoToIdl(const mri::State& state) {
157 State state_result;
158 if (state.has_status()) {
159 switch (state.status()) {
160 case mri::State::UNINITIALIZED:
161 state_result.status = STATUS_UNINITIALIZED;
162 break;
163 case mri::State::STARTED:
164 state_result.status = STATUS_STARTED;
165 break;
166 case mri::State::RUNNING:
167 state_result.status = STATUS_RUNNING;
168 break;
169 case mri::State::SUSPENDED:
170 state_result.status = STATUS_SUSPENDED;
171 break;
172 case mri::State::STATUS_UNSPECIFIED:
173 NOTREACHED() << "Status not set.";
tbarzic 2017/05/18 19:28:02 no NOTREACHED here - STATUS_UNSPECIFIED seems to b
Luke Sorenson 2017/05/18 21:24:17 Done.
174 break;
175 }
tbarzic 2017/05/18 19:28:01 add notreached after the switch (and this would b
Luke Sorenson 2017/05/18 21:24:17 Done.
176 }
177 if (state.has_device_context()) {
178 state_result.device_context =
179 base::MakeUnique<std::string>(state.device_context());
180 }
181 return state_result;
182 }
183
184 mri::State StateIdlToProto(const State& state) {
185 mri::State state_result;
186 switch (state.status) {
187 case STATUS_UNINITIALIZED:
188 state_result.set_status(mri::State::UNINITIALIZED);
189 break;
190 case STATUS_STARTED:
191 state_result.set_status(mri::State::STARTED);
192 break;
193 case STATUS_RUNNING:
194 state_result.set_status(mri::State::RUNNING);
195 break;
196 case STATUS_SUSPENDED:
197 state_result.set_status(mri::State::SUSPENDED);
198 break;
199 default:
tbarzic 2017/05/18 19:28:01 put notreached after the switch (see my comment fo
Luke Sorenson 2017/05/18 21:24:17 Done.
200 NOTREACHED() << "Status not set.";
201 break;
202 }
203 if (state.device_context)
204 state_result.set_device_context(*state.device_context);
205
206 return state_result;
207 }
208
209 MediaPerception MediaPerceptionProtoToIdl(
210 const mri::MediaPerception& media_perception) {
211 MediaPerception media_perception_result;
212 if (media_perception.has_timestamp()) {
213 media_perception_result.timestamp =
214 base::MakeUnique<double>(media_perception.timestamp());
215 }
216
217 if (media_perception.frame_perception_size() > 0) {
218 media_perception_result.frame_perceptions =
219 base::MakeUnique<std::vector<FramePerception>>();
220 for (const auto& frame_perception : media_perception.frame_perception()) {
221 media_perception_result.frame_perceptions->emplace_back(
222 FramePerceptionProtoToIdl(frame_perception));
223 }
224 }
225 return media_perception_result;
226 }
227
228 Diagnostics DiagnosticsProtoToIdl(const mri::Diagnostics& diagnostics) {
229 Diagnostics diagnostics_result;
230 if (diagnostics.perception_sample_size() > 0) {
231 diagnostics_result.perception_samples =
232 base::MakeUnique<std::vector<PerceptionSample>>();
233 for (const auto& perception_sample : diagnostics.perception_sample()) {
234 diagnostics_result.perception_samples->emplace_back(
235 PerceptionSampleProtoToIdl(perception_sample));
236 }
237 }
238 return diagnostics_result;
239 }
240
241 } // namespace media_perception_private
242 } // namespace api
243 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698