Chromium Code Reviews| Index: services/shape_detection/text_detection_impl_mac.mm |
| diff --git a/services/shape_detection/text_detection_impl_mac.mm b/services/shape_detection/text_detection_impl_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0a6a4c8fa5f5568c20d726c98fd331a444759c8c |
| --- /dev/null |
| +++ b/services/shape_detection/text_detection_impl_mac.mm |
| @@ -0,0 +1,84 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "services/shape_detection/text_detection_impl_mac.h" |
| + |
| +#include "base/mac/mac_util.h" |
| +#include "base/mac/scoped_cftyperef.h" |
| +#include "base/mac/sdk_forward_declarations.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "media/capture/video/scoped_result_callback.h" |
| +#include "mojo/public/cpp/bindings/strong_binding.h" |
| +#include "services/shape_detection/detection_utils_mac.h" |
| +#include "services/shape_detection/text_detection_impl.h" |
| + |
| +namespace shape_detection { |
| + |
| +namespace { |
| + |
| +void RunCallbackWithTexts( |
|
Reilly Grant (use Gerrit)
2017/03/02 22:10:26
I would call this RunCallbackWithResults and RunCa
mcasas
2017/03/02 23:27:50
Done.
|
| + const shape_detection::mojom::TextDetection::DetectCallback& callback, |
| + std::vector<shape_detection::mojom::TextDetectionResultPtr> results) { |
| + callback.Run(std::move(results)); |
| +} |
| + |
| +void RunCallbackWithNoTexts( |
| + const shape_detection::mojom::TextDetection::DetectCallback& callback) { |
| + callback.Run(std::vector<shape_detection::mojom::TextDetectionResultPtr>()); |
| +} |
| + |
| +} // anonymous namespace |
| + |
| +// static |
| +void TextDetectionImpl::Create( |
| + shape_detection::mojom::TextDetectionRequest request) { |
| + // Text detection needs at least MAC OS X 10.11. |
| + if (!base::mac::IsAtLeastOS10_11()) |
| + return; |
| + mojo::MakeStrongBinding(base::MakeUnique<TextDetectionImplMac>(), |
| + std::move(request)); |
| +} |
| + |
| +TextDetectionImplMac::TextDetectionImplMac() { |
| + NSDictionary* const opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh}; |
|
Reilly Grant (use Gerrit)
2017/03/02 22:10:26
DCHECK(base::mac::IsAtLeastOS10_11());
mcasas
2017/03/02 23:27:50
Done.
mcasas
2017/03/03 01:23:28
Actually I'd rather not have it because the _unitt
|
| + detector_.reset([[CIDetector detectorOfType:CIDetectorTypeText |
| + context:nil |
| + options:opts] retain]); |
|
Reilly Grant (use Gerrit)
2017/03/02 22:10:26
I do not believe a retain is necessary here. +[CID
mcasas
2017/03/02 23:27:50
If I don't retain here the use in l.65 crashes.
|
| +} |
| + |
| +TextDetectionImplMac::~TextDetectionImplMac() {} |
| + |
| +void TextDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data, |
| + uint32_t width, |
| + uint32_t height, |
| + const DetectCallback& callback) { |
| + media::ScopedResultCallback<DetectCallback> scoped_callback( |
| + base::Bind(&RunCallbackWithTexts, callback), |
| + base::Bind(&RunCallbackWithNoTexts)); |
| + |
| + base::scoped_nsobject<CIImage> ci_image = |
| + CreateCIImageFromSharedMemory(std::move(frame_data), width, height); |
| + if (!ci_image) |
| + return; |
| + |
| + NSArray* const features = [detector_ featuresInImage:ci_image]; |
| + |
| + std::vector<mojom::TextDetectionResultPtr> results; |
| + for (CIRectangleFeature* const f in features) { |
| + // CIRectangleFeature only has bounding box information. |
| + shape_detection::mojom::TextDetectionResultPtr result = |
|
Reilly Grant (use Gerrit)
2017/03/02 22:10:26
You can use auto here.
mcasas
2017/03/02 23:27:50
Done.
|
| + shape_detection::mojom::TextDetectionResult::New(); |
| + // In the default Core Graphics coordinate space, the origin is located |
| + // in the lower-left corner, and thus |ci_image| is flipped vertically. |
| + // We need to adjust |y| coordinate of bounding box before sending it. |
| + gfx::RectF boundingbox(f.bounds.origin.x, |
| + height - f.bounds.origin.y - f.bounds.size.height, |
| + f.bounds.size.width, f.bounds.size.height); |
| + result->bounding_box = std::move(boundingbox); |
| + results.push_back(std::move(result)); |
| + } |
| + scoped_callback.Run(std::move(results)); |
| +} |
| + |
| +} // namespace shape_detection |