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