OLD | NEW |
(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 |
| 35 if (bounding_box.has_top_left()) |
| 36 bounding_box_result->top_left = PointProtoToIdl(bounding_box.top_left()); |
| 37 |
| 38 if (bounding_box.has_bottom_right()) { |
| 39 bounding_box_result->bottom_right = |
| 40 PointProtoToIdl(bounding_box.bottom_right()); |
| 41 } |
| 42 |
| 43 return bounding_box_result; |
| 44 } |
| 45 |
| 46 EntityType EntityTypeProtoToIdl(const mri::Entity& entity) { |
| 47 if (entity.has_type()) { |
| 48 switch (entity.type()) { |
| 49 case mri::Entity::FACE: |
| 50 return ENTITY_TYPE_FACE; |
| 51 case mri::Entity::PERSON: |
| 52 return ENTITY_TYPE_PERSON; |
| 53 case mri::Entity::UNSPECIFIED: |
| 54 return ENTITY_TYPE_UNSPECIFIED; |
| 55 } |
| 56 NOTREACHED() << "Unknown entity type: " << entity.type(); |
| 57 } |
| 58 return ENTITY_TYPE_UNSPECIFIED; |
| 59 } |
| 60 |
| 61 Entity EntityProtoToIdl(const mri::Entity& entity) { |
| 62 Entity entity_result; |
| 63 if (entity.has_id()) |
| 64 entity_result.id = base::MakeUnique<int>(entity.id()); |
| 65 |
| 66 entity_result.type = EntityTypeProtoToIdl(entity); |
| 67 if (entity.has_confidence()) |
| 68 entity_result.confidence = base::MakeUnique<double>(entity.confidence()); |
| 69 |
| 70 if (entity.has_bounding_box()) |
| 71 entity_result.bounding_box = BoundingBoxProtoToIdl(entity.bounding_box()); |
| 72 |
| 73 return entity_result; |
| 74 } |
| 75 |
| 76 FramePerception FramePerceptionProtoToIdl( |
| 77 const mri::FramePerception& frame_perception) { |
| 78 FramePerception frame_perception_result; |
| 79 if (frame_perception.has_frame_id()) { |
| 80 frame_perception_result.frame_id = |
| 81 base::MakeUnique<int>(frame_perception.frame_id()); |
| 82 } |
| 83 |
| 84 if (frame_perception.has_frame_width_in_px()) { |
| 85 frame_perception_result.frame_width_in_px = |
| 86 base::MakeUnique<int>(frame_perception.frame_width_in_px()); |
| 87 } |
| 88 if (frame_perception.has_frame_height_in_px()) { |
| 89 frame_perception_result.frame_height_in_px = |
| 90 base::MakeUnique<int>(frame_perception.frame_height_in_px()); |
| 91 } |
| 92 if (frame_perception.has_timestamp()) { |
| 93 frame_perception_result.timestamp = |
| 94 base::MakeUnique<double>(frame_perception.timestamp()); |
| 95 } |
| 96 if (frame_perception.entity_size() > 0) { |
| 97 frame_perception_result.entities = base::MakeUnique<std::vector<Entity>>(); |
| 98 for (const auto& entity : frame_perception.entity()) |
| 99 frame_perception_result.entities->emplace_back(EntityProtoToIdl(entity)); |
| 100 } |
| 101 return frame_perception_result; |
| 102 } |
| 103 |
| 104 ImageFormat ImageFormatProtoToIdl(const mri::ImageFrame& image_frame) { |
| 105 if (image_frame.has_format()) { |
| 106 switch (image_frame.format()) { |
| 107 case mri::ImageFrame::RGB: |
| 108 return IMAGE_FORMAT_RGB; |
| 109 case mri::ImageFrame::PNG: |
| 110 return IMAGE_FORMAT_PNG; |
| 111 case mri::ImageFrame::JPEG: |
| 112 return IMAGE_FORMAT_JPEG; |
| 113 case mri::ImageFrame::FORMAT_UNSPECIFIED: |
| 114 return IMAGE_FORMAT_UNSPECIFIED; |
| 115 } |
| 116 NOTREACHED() << "Unknown image format: " << image_frame.format(); |
| 117 } |
| 118 return IMAGE_FORMAT_UNSPECIFIED; |
| 119 } |
| 120 |
| 121 ImageFrame ImageFrameProtoToIdl(const mri::ImageFrame& image_frame) { |
| 122 ImageFrame image_frame_result; |
| 123 if (image_frame.has_width()) |
| 124 image_frame_result.width = base::MakeUnique<int>(image_frame.width()); |
| 125 |
| 126 if (image_frame.has_height()) |
| 127 image_frame_result.height = base::MakeUnique<int>(image_frame.height()); |
| 128 |
| 129 if (image_frame.has_data_length()) { |
| 130 image_frame_result.data_length = |
| 131 base::MakeUnique<int>(image_frame.data_length()); |
| 132 } |
| 133 |
| 134 if (image_frame.has_pixel_data()) { |
| 135 image_frame_result.frame = base::MakeUnique<std::vector<char>>( |
| 136 image_frame.pixel_data().begin(), image_frame.pixel_data().end()); |
| 137 } |
| 138 |
| 139 image_frame_result.format = ImageFormatProtoToIdl(image_frame); |
| 140 return image_frame_result; |
| 141 } |
| 142 |
| 143 PerceptionSample PerceptionSampleProtoToIdl( |
| 144 const mri::PerceptionSample& perception_sample) { |
| 145 PerceptionSample perception_sample_result; |
| 146 if (perception_sample.has_frame_perception()) { |
| 147 perception_sample_result.frame_perception = |
| 148 base::MakeUnique<FramePerception>( |
| 149 FramePerceptionProtoToIdl(perception_sample.frame_perception())); |
| 150 } |
| 151 if (perception_sample.has_image_frame()) { |
| 152 perception_sample_result.image_frame = base::MakeUnique<ImageFrame>( |
| 153 ImageFrameProtoToIdl(perception_sample.image_frame())); |
| 154 } |
| 155 return perception_sample_result; |
| 156 } |
| 157 |
| 158 Status StateStatusProtoToIdl(const mri::State& state) { |
| 159 switch (state.status()) { |
| 160 case mri::State::UNINITIALIZED: |
| 161 return STATUS_UNINITIALIZED; |
| 162 case mri::State::STARTED: |
| 163 return STATUS_STARTED; |
| 164 case mri::State::RUNNING: |
| 165 return STATUS_RUNNING; |
| 166 case mri::State::SUSPENDED: |
| 167 return STATUS_SUSPENDED; |
| 168 case mri::State::STATUS_UNSPECIFIED: |
| 169 return STATUS_NONE; |
| 170 } |
| 171 NOTREACHED() << "Reached status not in switch."; |
| 172 return STATUS_NONE; |
| 173 } |
| 174 |
| 175 mri::State::Status StateStatusIdlToProto(const State& state) { |
| 176 switch (state.status) { |
| 177 case STATUS_UNINITIALIZED: |
| 178 return mri::State::UNINITIALIZED; |
| 179 case STATUS_STARTED: |
| 180 return mri::State::STARTED; |
| 181 case STATUS_RUNNING: |
| 182 return mri::State::RUNNING; |
| 183 case STATUS_SUSPENDED: |
| 184 return mri::State::SUSPENDED; |
| 185 case STATUS_NONE: |
| 186 return mri::State::STATUS_UNSPECIFIED; |
| 187 } |
| 188 NOTREACHED() << "Reached status not in switch."; |
| 189 return mri::State::STATUS_UNSPECIFIED; |
| 190 } |
| 191 |
| 192 } // namespace |
| 193 |
| 194 State StateProtoToIdl(const mri::State& state) { |
| 195 State state_result; |
| 196 if (state.has_status()) { |
| 197 state_result.status = StateStatusProtoToIdl(state); |
| 198 } |
| 199 if (state.has_device_context()) { |
| 200 state_result.device_context = |
| 201 base::MakeUnique<std::string>(state.device_context()); |
| 202 } |
| 203 return state_result; |
| 204 } |
| 205 |
| 206 mri::State StateIdlToProto(const State& state) { |
| 207 mri::State state_result; |
| 208 state_result.set_status(StateStatusIdlToProto(state)); |
| 209 if (state.device_context) |
| 210 state_result.set_device_context(*state.device_context); |
| 211 |
| 212 return state_result; |
| 213 } |
| 214 |
| 215 MediaPerception MediaPerceptionProtoToIdl( |
| 216 const mri::MediaPerception& media_perception) { |
| 217 MediaPerception media_perception_result; |
| 218 if (media_perception.has_timestamp()) { |
| 219 media_perception_result.timestamp = |
| 220 base::MakeUnique<double>(media_perception.timestamp()); |
| 221 } |
| 222 |
| 223 if (media_perception.frame_perception_size() > 0) { |
| 224 media_perception_result.frame_perceptions = |
| 225 base::MakeUnique<std::vector<FramePerception>>(); |
| 226 for (const auto& frame_perception : media_perception.frame_perception()) { |
| 227 media_perception_result.frame_perceptions->emplace_back( |
| 228 FramePerceptionProtoToIdl(frame_perception)); |
| 229 } |
| 230 } |
| 231 return media_perception_result; |
| 232 } |
| 233 |
| 234 Diagnostics DiagnosticsProtoToIdl(const mri::Diagnostics& diagnostics) { |
| 235 Diagnostics diagnostics_result; |
| 236 if (diagnostics.perception_sample_size() > 0) { |
| 237 diagnostics_result.perception_samples = |
| 238 base::MakeUnique<std::vector<PerceptionSample>>(); |
| 239 for (const auto& perception_sample : diagnostics.perception_sample()) { |
| 240 diagnostics_result.perception_samples->emplace_back( |
| 241 PerceptionSampleProtoToIdl(perception_sample)); |
| 242 } |
| 243 } |
| 244 return diagnostics_result; |
| 245 } |
| 246 |
| 247 } // namespace media_perception_private |
| 248 } // namespace api |
| 249 } // namespace extensions |
OLD | NEW |