mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 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" |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 11 | #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 | |
| 15 | namespace shape_detection { |
| 16 | |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 17 | // static |
Ben Goodger | 21ada1e | 2017-07-19 14:53:01 | [diff] [blame] | 18 | void TextDetectionImpl::Create(mojom::TextDetectionRequest request) { |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 19 | // Text detection needs at least MAC OS X 10.11. |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 20 | if (@available(macOS 10.11, *)) { |
Jeremy Roman | 83533d7a | 2017-11-15 23:39:06 | [diff] [blame] | 21 | mojo::MakeStrongBinding(std::make_unique<TextDetectionImplMac>(), |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 22 | std::move(request)); |
| 23 | } |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | TextDetectionImplMac::TextDetectionImplMac() { |
| 27 | NSDictionary* const opts = @{CIDetectorAccuracy : CIDetectorAccuracyHigh}; |
erikchen | d2b3c76 | 2017-07-18 00:12:48 | [diff] [blame] | 28 | detector_.reset( |
| 29 | [[CIDetector detectorOfType:CIDetectorTypeText context:nil options:opts] |
| 30 | retain]); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TextDetectionImplMac::~TextDetectionImplMac() {} |
| 34 | |
junwei.fu | 31eb1a5 | 2017-06-02 07:35:56 | [diff] [blame] | 35 | void TextDetectionImplMac::Detect(const SkBitmap& bitmap, |
Reilly Grant | 006dafc | 2017-07-07 15:18:42 | [diff] [blame] | 36 | DetectCallback callback) { |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 37 | DCHECK(base::mac::IsAtLeastOS10_11()); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 38 | |
junwei.fu | 31eb1a5 | 2017-06-02 07:35:56 | [diff] [blame] | 39 | base::scoped_nsobject<CIImage> ci_image = CreateCIImageFromSkBitmap(bitmap); |
Daniel Cheng | cf396c95 | 2017-12-18 02:46:42 | [diff] [blame] | 40 | if (!ci_image) { |
| 41 | std::move(callback).Run({}); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 42 | return; |
Daniel Cheng | cf396c95 | 2017-12-18 02:46:42 | [diff] [blame] | 43 | } |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 44 | |
| 45 | NSArray* const features = [detector_ featuresInImage:ci_image]; |
| 46 | |
junwei.fu | 31eb1a5 | 2017-06-02 07:35:56 | [diff] [blame] | 47 | const int height = bitmap.height(); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 48 | 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); |
junweifu | 86bc62c | 2017-09-15 02:16:51 | [diff] [blame] | 59 | |
| 60 | // Enumerate corner points starting from top-left in clockwise fashion: |
Miguel Casas-Sanchez | d1a6e59 | 2017-10-23 15:20:17 | [diff] [blame] | 61 | // https://wicg.github.io/shape-detection-api/text.html#dom-detectedtext-cornerpoints |
junweifu | 86bc62c | 2017-09-15 02:16:51 | [diff] [blame] | 62 | 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 | |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 68 | results.push_back(std::move(result)); |
| 69 | } |
Daniel Cheng | cf396c95 | 2017-12-18 02:46:42 | [diff] [blame] | 70 | std::move(callback).Run(std::move(results)); |
mcasas | 9eba8ff | 2017-03-06 02:58:29 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | } // namespace shape_detection |