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 if (frame_perception.has_frame_width_in_px()) { |
| 84 frame_perception_result.frame_width_in_px = |
| 85 base::MakeUnique<int>(frame_perception.frame_width_in_px()); |
| 86 } |
| 87 if (frame_perception.has_frame_height_in_px()) { |
| 88 frame_perception_result.frame_height_in_px = |
| 89 base::MakeUnique<int>(frame_perception.frame_height_in_px()); |
| 90 } |
| 91 if (frame_perception.has_timestamp()) { |
| 92 frame_perception_result.timestamp = |
| 93 base::MakeUnique<double>(frame_perception.timestamp()); |
| 94 } |
| 95 if (frame_perception.entity_size() > 0) { |
| 96 frame_perception_result.entities = base::MakeUnique<std::vector<Entity>>(); |
| 97 for (const auto& entity : frame_perception.entity()) |
| 98 frame_perception_result.entities->emplace_back(EntityProtoToIdl(entity)); |
| 99 } |
| 100 return frame_perception_result; |
| 101 } |
| 102 |
| 103 ImageFormat ImageFormatProtoToIdl(const mri::ImageFrame& image_frame) { |
| 104 if (image_frame.has_format()) { |
| 105 switch (image_frame.format()) { |
| 106 case mri::ImageFrame::RGB: |
| 107 return IMAGE_FORMAT_RAW; |
| 108 case mri::ImageFrame::PNG: |
| 109 return IMAGE_FORMAT_PNG; |
| 110 case mri::ImageFrame::JPEG: |
| 111 return IMAGE_FORMAT_JPEG; |
| 112 case mri::ImageFrame::FORMAT_UNSPECIFIED: |
| 113 return IMAGE_FORMAT_NONE; |
| 114 } |
| 115 NOTREACHED() << "Unknown image format: " << image_frame.format(); |
| 116 } |
| 117 return IMAGE_FORMAT_NONE; |
| 118 } |
| 119 |
| 120 ImageFrame ImageFrameProtoToIdl(const mri::ImageFrame& image_frame) { |
| 121 ImageFrame image_frame_result; |
| 122 if (image_frame.has_width()) |
| 123 image_frame_result.width = base::MakeUnique<int>(image_frame.width()); |
| 124 |
| 125 if (image_frame.has_height()) |
| 126 image_frame_result.height = base::MakeUnique<int>(image_frame.height()); |
| 127 |
| 128 if (image_frame.has_data_length()) { |
| 129 image_frame_result.data_length = |
| 130 base::MakeUnique<int>(image_frame.data_length()); |
| 131 } |
| 132 |
| 133 if (image_frame.has_pixel_data()) { |
| 134 image_frame_result.frame = base::MakeUnique<std::vector<char>>( |
| 135 image_frame.pixel_data().begin(), image_frame.pixel_data().end()); |
| 136 } |
| 137 |
| 138 image_frame_result.format = ImageFormatProtoToIdl(image_frame); |
| 139 return image_frame_result; |
| 140 } |
| 141 |
| 142 PerceptionSample PerceptionSampleProtoToIdl( |
| 143 const mri::PerceptionSample& perception_sample) { |
| 144 PerceptionSample perception_sample_result; |
| 145 if (perception_sample.has_frame_perception()) { |
| 146 perception_sample_result.frame_perception = |
| 147 base::MakeUnique<FramePerception>( |
| 148 FramePerceptionProtoToIdl(perception_sample.frame_perception())); |
| 149 } |
| 150 if (perception_sample.has_image_frame()) { |
| 151 perception_sample_result.image_frame = base::MakeUnique<ImageFrame>( |
| 152 ImageFrameProtoToIdl(perception_sample.image_frame())); |
| 153 } |
| 154 return perception_sample_result; |
| 155 } |
| 156 |
| 157 Status StateStatusProtoToIdl(const mri::State& state) { |
| 158 switch (state.status()) { |
| 159 case mri::State::UNINITIALIZED: |
| 160 return STATUS_UNINITIALIZED; |
| 161 case mri::State::STARTED: |
| 162 return STATUS_STARTED; |
| 163 case mri::State::RUNNING: |
| 164 return STATUS_RUNNING; |
| 165 case mri::State::SUSPENDED: |
| 166 return STATUS_SUSPENDED; |
| 167 case mri::State::STATUS_UNSPECIFIED: |
| 168 return STATUS_NONE; |
| 169 } |
| 170 NOTREACHED() << "Reached status not in switch."; |
| 171 return STATUS_NONE; |
| 172 } |
| 173 |
| 174 mri::State::Status StateStatusIdlToProto(const State& state) { |
| 175 switch (state.status) { |
| 176 case STATUS_UNINITIALIZED: |
| 177 return mri::State::UNINITIALIZED; |
| 178 case STATUS_STARTED: |
| 179 return mri::State::STARTED; |
| 180 case STATUS_RUNNING: |
| 181 return mri::State::RUNNING; |
| 182 case STATUS_SUSPENDED: |
| 183 return mri::State::SUSPENDED; |
| 184 case STATUS_NONE: |
| 185 return mri::State::STATUS_UNSPECIFIED; |
| 186 } |
| 187 NOTREACHED() << "Reached status not in switch."; |
| 188 return mri::State::STATUS_UNSPECIFIED; |
| 189 } |
| 190 |
| 191 } // namespace |
| 192 |
| 193 State StateProtoToIdl(const mri::State& state) { |
| 194 State state_result; |
| 195 if (state.has_status()) { |
| 196 state_result.status = StateStatusProtoToIdl(state); |
| 197 } |
| 198 if (state.has_device_context()) { |
| 199 state_result.device_context = |
| 200 base::MakeUnique<std::string>(state.device_context()); |
| 201 } |
| 202 return state_result; |
| 203 } |
| 204 |
| 205 mri::State StateIdlToProto(const State& state) { |
| 206 mri::State state_result; |
| 207 state_result.set_status(StateStatusIdlToProto(state)); |
| 208 if (state.device_context) |
| 209 state_result.set_device_context(*state.device_context); |
| 210 |
| 211 return state_result; |
| 212 } |
| 213 |
| 214 MediaPerception MediaPerceptionProtoToIdl( |
| 215 const mri::MediaPerception& media_perception) { |
| 216 MediaPerception media_perception_result; |
| 217 if (media_perception.has_timestamp()) { |
| 218 media_perception_result.timestamp = |
| 219 base::MakeUnique<double>(media_perception.timestamp()); |
| 220 } |
| 221 |
| 222 if (media_perception.frame_perception_size() > 0) { |
| 223 media_perception_result.frame_perceptions = |
| 224 base::MakeUnique<std::vector<FramePerception>>(); |
| 225 for (const auto& frame_perception : media_perception.frame_perception()) { |
| 226 media_perception_result.frame_perceptions->emplace_back( |
| 227 FramePerceptionProtoToIdl(frame_perception)); |
| 228 } |
| 229 } |
| 230 return media_perception_result; |
| 231 } |
| 232 |
| 233 Diagnostics DiagnosticsProtoToIdl(const mri::Diagnostics& diagnostics) { |
| 234 Diagnostics diagnostics_result; |
| 235 if (diagnostics.perception_sample_size() > 0) { |
| 236 diagnostics_result.perception_samples = |
| 237 base::MakeUnique<std::vector<PerceptionSample>>(); |
| 238 for (const auto& perception_sample : diagnostics.perception_sample()) { |
| 239 diagnostics_result.perception_samples->emplace_back( |
| 240 PerceptionSampleProtoToIdl(perception_sample)); |
| 241 } |
| 242 } |
| 243 return diagnostics_result; |
| 244 } |
| 245 |
| 246 } // namespace media_perception_private |
| 247 } // namespace api |
| 248 } // namespace extensions |
OLD | NEW |