blob: bd14925b99a135fcded95c9468ccf5c4422fcf93 [file] [log] [blame]
mcasas9eba8ff2017-03-06 02:58:291// 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"
mcasas9eba8ff2017-03-06 02:58:2911#include "mojo/public/cpp/bindings/strong_binding.h"
12#include "services/shape_detection/detection_utils_mac.h"
13#include "services/shape_detection/text_detection_impl.h"
14
15namespace shape_detection {
16
mcasas9eba8ff2017-03-06 02:58:2917// static
Ben Goodger21ada1e2017-07-19 14:53:0118void TextDetectionImpl::Create(mojom::TextDetectionRequest request) {
mcasas9eba8ff2017-03-06 02:58:2919 // Text detection needs at least MAC OS X 10.11.
erikchend2b3c762017-07-18 00:12:4820 if (@available(macOS 10.11, *)) {
Jeremy Roman83533d7a2017-11-15 23:39:0621 mojo::MakeStrongBinding(std::make_unique<TextDetectionImplMac>(),
erikchend2b3c762017-07-18 00:12:4822 std::move(request));
23 }
mcasas9eba8ff2017-03-06 02:58:2924}
25
26TextDetectionImplMac::TextDetectionImplMac() {
27 NSDictionary* const opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh};
erikchend2b3c762017-07-18 00:12:4828 detector_.reset(
29 [[CIDetector detectorOfType:CIDetectorTypeText context:nil options:opts]
30 retain]);
mcasas9eba8ff2017-03-06 02:58:2931}
32
33TextDetectionImplMac::~TextDetectionImplMac() {}
34
junwei.fu31eb1a52017-06-02 07:35:5635void TextDetectionImplMac::Detect(const SkBitmap& bitmap,
Reilly Grant006dafc2017-07-07 15:18:4236 DetectCallback callback) {
mcasas9eba8ff2017-03-06 02:58:2937 DCHECK(base::mac::IsAtLeastOS10_11());
mcasas9eba8ff2017-03-06 02:58:2938
junwei.fu31eb1a52017-06-02 07:35:5639 base::scoped_nsobject<CIImage> ci_image = CreateCIImageFromSkBitmap(bitmap);
Daniel Chengcf396c952017-12-18 02:46:4240 if (!ci_image) {
41 std::move(callback).Run({});
mcasas9eba8ff2017-03-06 02:58:2942 return;
Daniel Chengcf396c952017-12-18 02:46:4243 }
mcasas9eba8ff2017-03-06 02:58:2944
45 NSArray* const features = [detector_ featuresInImage:ci_image];
46
junwei.fu31eb1a52017-06-02 07:35:5647 const int height = bitmap.height();
mcasas9eba8ff2017-03-06 02:58:2948 std::vector<mojom::TextDetectionResultPtr> results;
49 for (CIRectangleFeature* const f in features) {
50 // CIRectangleFeature only has bounding box information.
51 auto result = mojom::TextDetectionResult::New();
52 // In the default Core Graphics coordinate space, the origin is located
53 // in the lower-left corner, and thus |ci_image| is flipped vertically.
54 // We need to adjust |y| coordinate of bounding box before sending it.
55 gfx::RectF boundingbox(f.bounds.origin.x,
56 height - f.bounds.origin.y - f.bounds.size.height,
57 f.bounds.size.width, f.bounds.size.height);
58 result->bounding_box = std::move(boundingbox);
junweifu86bc62c2017-09-15 02:16:5159
60 // Enumerate corner points starting from top-left in clockwise fashion:
Miguel Casas-Sanchezd1a6e592017-10-23 15:20:1761 // https://wicg.github.io/shape-detection-api/text.html#dom-detectedtext-cornerpoints
junweifu86bc62c2017-09-15 02:16:5162 result->corner_points.emplace_back(f.topLeft.x, height - f.topLeft.y);
63 result->corner_points.emplace_back(f.topRight.x, height - f.topRight.y);
64 result->corner_points.emplace_back(f.bottomRight.x,
65 height - f.bottomRight.y);
66 result->corner_points.emplace_back(f.bottomLeft.x, height - f.bottomLeft.y);
67
mcasas9eba8ff2017-03-06 02:58:2968 results.push_back(std::move(result));
69 }
Daniel Chengcf396c952017-12-18 02:46:4270 std::move(callback).Run(std::move(results));
mcasas9eba8ff2017-03-06 02:58:2971}
72
73} // namespace shape_detection