Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: services/shape_detection/text_detection_impl_mac.mm

Issue 2726843006: ShapeDetection: add support for Text Detection in Mac (Closed)
Patch Set: cleanup Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "services/shape_detection/text_detection_impl_mac.h"
6
7 #include "base/mac/mac_util.h"
8 #include "base/mac/scoped_cftyperef.h"
9 #include "base/mac/sdk_forward_declarations.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "media/capture/video/scoped_result_callback.h"
12 #include "mojo/public/cpp/bindings/strong_binding.h"
13 #include "services/shape_detection/detection_utils_mac.h"
14 #include "services/shape_detection/text_detection_impl.h"
15
16 namespace shape_detection {
17
18 namespace {
19
20 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.
21 const shape_detection::mojom::TextDetection::DetectCallback& callback,
22 std::vector<shape_detection::mojom::TextDetectionResultPtr> results) {
23 callback.Run(std::move(results));
24 }
25
26 void RunCallbackWithNoTexts(
27 const shape_detection::mojom::TextDetection::DetectCallback& callback) {
28 callback.Run(std::vector<shape_detection::mojom::TextDetectionResultPtr>());
29 }
30
31 } // anonymous namespace
32
33 // static
34 void TextDetectionImpl::Create(
35 shape_detection::mojom::TextDetectionRequest request) {
36 // Text detection needs at least MAC OS X 10.11.
37 if (!base::mac::IsAtLeastOS10_11())
38 return;
39 mojo::MakeStrongBinding(base::MakeUnique<TextDetectionImplMac>(),
40 std::move(request));
41 }
42
43 TextDetectionImplMac::TextDetectionImplMac() {
44 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
45 detector_.reset([[CIDetector detectorOfType:CIDetectorTypeText
46 context:nil
47 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.
48 }
49
50 TextDetectionImplMac::~TextDetectionImplMac() {}
51
52 void TextDetectionImplMac::Detect(mojo::ScopedSharedBufferHandle frame_data,
53 uint32_t width,
54 uint32_t height,
55 const DetectCallback& callback) {
56 media::ScopedResultCallback<DetectCallback> scoped_callback(
57 base::Bind(&RunCallbackWithTexts, callback),
58 base::Bind(&RunCallbackWithNoTexts));
59
60 base::scoped_nsobject<CIImage> ci_image =
61 CreateCIImageFromSharedMemory(std::move(frame_data), width, height);
62 if (!ci_image)
63 return;
64
65 NSArray* const features = [detector_ featuresInImage:ci_image];
66
67 std::vector<mojom::TextDetectionResultPtr> results;
68 for (CIRectangleFeature* const f in features) {
69 // CIRectangleFeature only has bounding box information.
70 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.
71 shape_detection::mojom::TextDetectionResult::New();
72 // In the default Core Graphics coordinate space, the origin is located
73 // in the lower-left corner, and thus |ci_image| is flipped vertically.
74 // We need to adjust |y| coordinate of bounding box before sending it.
75 gfx::RectF boundingbox(f.bounds.origin.x,
76 height - f.bounds.origin.y - f.bounds.size.height,
77 f.bounds.size.width, f.bounds.size.height);
78 result->bounding_box = std::move(boundingbox);
79 results.push_back(std::move(result));
80 }
81 scoped_callback.Run(std::move(results));
82 }
83
84 } // namespace shape_detection
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698