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 return media_perception::ENTITY_TYPE_UNSPECIFIED; |
| 51 } |
| 52 NOTREACHED() << "Unknown entity type: " << entity.type(); |
| 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 case mri::ImageFrame::FORMAT_UNSPECIFIED: |
| 111 return media_perception::IMAGE_FORMAT_UNSPECIFIED; |
| 112 } |
| 113 NOTREACHED() << "Unknown image format: " << image_frame.format(); |
| 114 } |
| 115 return media_perception::IMAGE_FORMAT_UNSPECIFIED; |
| 116 } |
| 117 |
| 118 media_perception::ImageFrame ImageFrameProtoToIdl( |
| 119 const mri::ImageFrame& image_frame) { |
| 120 media_perception::ImageFrame if_result; |
| 121 if (image_frame.has_width()) |
| 122 if_result.width = base::MakeUnique<int>(image_frame.width()); |
| 123 |
| 124 if (image_frame.has_height()) |
| 125 if_result.height = base::MakeUnique<int>(image_frame.height()); |
| 126 |
| 127 if (image_frame.has_data_length()) |
| 128 if_result.data_length = base::MakeUnique<int>(image_frame.data_length()); |
| 129 |
| 130 if (image_frame.has_pixel_data()) { |
| 131 if_result.frame = base::MakeUnique<std::vector<char>>( |
| 132 image_frame.pixel_data().begin(), image_frame.pixel_data().end()); |
| 133 } |
| 134 |
| 135 if_result.format = ImageFormatProtoToIdl(image_frame); |
| 136 return if_result; |
| 137 } |
| 138 |
| 139 media_perception::PerceptionSample PerceptionSampleProtoToIdl( |
| 140 const mri::PerceptionSample& perception_sample) { |
| 141 media_perception::PerceptionSample ps_result; |
| 142 if (perception_sample.has_frame_perception()) { |
| 143 ps_result.frame_perception = |
| 144 base::MakeUnique<media_perception::FramePerception>( |
| 145 FramePerceptionProtoToIdl(perception_sample.frame_perception())); |
| 146 } |
| 147 if (perception_sample.has_image_frame()) { |
| 148 ps_result.image_frame = base::MakeUnique<media_perception::ImageFrame>( |
| 149 ImageFrameProtoToIdl(perception_sample.image_frame())); |
| 150 } |
| 151 return ps_result; |
| 152 } |
| 153 |
| 154 } // namespace |
| 155 |
| 156 media_perception::State StateProtoToIdl(const mri::State& state) { |
| 157 media_perception::State s_result; |
| 158 if (state.has_status()) { |
| 159 switch (state.status()) { |
| 160 case mri::State::UNINITIALIZED: |
| 161 s_result.status = media_perception::STATUS_UNINITIALIZED; |
| 162 break; |
| 163 case mri::State::STARTED: |
| 164 s_result.status = media_perception::STATUS_STARTED; |
| 165 break; |
| 166 case mri::State::RUNNING: |
| 167 s_result.status = media_perception::STATUS_RUNNING; |
| 168 break; |
| 169 case mri::State::SUSPENDED: |
| 170 s_result.status = media_perception::STATUS_SUSPENDED; |
| 171 break; |
| 172 case mri::State::STATUS_UNSPECIFIED: |
| 173 default: |
| 174 NOTREACHED() << "Status not set."; |
| 175 break; |
| 176 } |
| 177 } |
| 178 if (state.has_device_context()) { |
| 179 s_result.device_context = |
| 180 base::MakeUnique<std::string>(state.device_context()); |
| 181 } |
| 182 return s_result; |
| 183 } |
| 184 |
| 185 mri::State StateIdlToProto(const media_perception::State& state) { |
| 186 mri::State s_result; |
| 187 switch (state.status) { |
| 188 case media_perception::STATUS_UNINITIALIZED: |
| 189 s_result.set_status(mri::State::UNINITIALIZED); |
| 190 break; |
| 191 case media_perception::STATUS_STARTED: |
| 192 s_result.set_status(mri::State::STARTED); |
| 193 break; |
| 194 case media_perception::STATUS_RUNNING: |
| 195 s_result.set_status(mri::State::RUNNING); |
| 196 break; |
| 197 case media_perception::STATUS_SUSPENDED: |
| 198 s_result.set_status(mri::State::SUSPENDED); |
| 199 break; |
| 200 default: |
| 201 NOTREACHED() << "Status not set."; |
| 202 break; |
| 203 } |
| 204 if (state.device_context) |
| 205 s_result.set_device_context(*state.device_context); |
| 206 |
| 207 return s_result; |
| 208 } |
| 209 |
| 210 media_perception::MediaPerception MediaPerceptionProtoToIdl( |
| 211 const mri::MediaPerception& media_perception) { |
| 212 media_perception::MediaPerception mp_result; |
| 213 if (media_perception.has_timestamp()) { |
| 214 mp_result.timestamp = |
| 215 base::MakeUnique<double>(media_perception.timestamp()); |
| 216 } |
| 217 |
| 218 if (media_perception.frame_perception_size() > 0) { |
| 219 mp_result.frame_perceptions = |
| 220 base::MakeUnique<std::vector<media_perception::FramePerception>>(); |
| 221 for (const auto& frame_perception : media_perception.frame_perception()) { |
| 222 mp_result.frame_perceptions->emplace_back( |
| 223 FramePerceptionProtoToIdl(frame_perception)); |
| 224 } |
| 225 } |
| 226 return mp_result; |
| 227 } |
| 228 |
| 229 media_perception::Diagnostics DiagnosticsProtoToIdl( |
| 230 const mri::Diagnostics& diagnostics) { |
| 231 media_perception::Diagnostics d_result; |
| 232 if (diagnostics.perception_sample_size() > 0) { |
| 233 d_result.perception_samples = |
| 234 base::MakeUnique<std::vector<media_perception::PerceptionSample>>(); |
| 235 for (const auto& perception_sample : diagnostics.perception_sample()) { |
| 236 d_result.perception_samples->emplace_back( |
| 237 PerceptionSampleProtoToIdl(perception_sample)); |
| 238 } |
| 239 } |
| 240 return d_result; |
| 241 } |
| 242 |
| 243 } // namespace extensions |
OLD | NEW |