blob: 554cd7e5c804f984ae22d38609e3149146a14d40 [file] [log] [blame]
// 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/detection_utils_mac.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_nsobject.h"
#include "base/memory/shared_memory.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "services/shape_detection/barcode_detection_impl.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/skia/include/utils/mac/SkCGUtils.h"
namespace shape_detection {
base::scoped_nsobject<CIImage> CreateCIImageFromSkBitmap(
const SkBitmap& bitmap) {
base::CheckedNumeric<uint32_t> num_pixels =
base::CheckedNumeric<uint32_t>(bitmap.width()) * bitmap.height();
base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4;
if (!num_bytes.IsValid()) {
DLOG(ERROR) << "Data overflow";
return base::scoped_nsobject<CIImage>();
}
// First convert SkBitmap to CGImageRef.
base::ScopedCFTypeRef<CGImageRef> cg_image(
SkCreateCGImageRefWithColorspace(bitmap, NULL));
if (!cg_image) {
DLOG(ERROR) << "Failed to create CGImageRef";
return base::scoped_nsobject<CIImage>();
}
base::scoped_nsobject<CIImage> ci_image(
[[CIImage alloc] initWithCGImage:cg_image]);
if (!ci_image) {
DLOG(ERROR) << "Failed to create CIImage";
return base::scoped_nsobject<CIImage>();
}
return ci_image;
}
} // namespace shape_detection