diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index 69ead0464585..81db35715bcd 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -184,6 +184,8 @@ mate::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder( template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate) .SetMethod("toPng", &NativeImage::ToPNG) .SetMethod("toJpeg", &NativeImage::ToJPEG) + .SetMethod("getNativeHandle", + &NativeImage::GetNativeHandle) .SetMethod("toDataURL", &NativeImage::ToDataURL) .SetMethod("toDataUrl", &NativeImage::ToDataURL) // deprecated. .SetMethod("isEmpty", &NativeImage::IsEmpty) @@ -221,6 +223,23 @@ std::string NativeImage::ToDataURL() { return data_url; } +v8::Local NativeImage::GetNativeHandle( + v8::Isolate* isolate, + mate::Arguments* args) { +void* ptr = NULL; +#if defined(OS_MACOSX) + ptr = reinterpret_cast(image_.AsNSImage()); +#else + args->ThrowError(); + return v8::Undefined(isolate); +#endif + + return node::Buffer::Copy( + isolate, + reinterpret_cast(ptr), + sizeof(void*)).ToLocalChecked(); +} + bool NativeImage::IsEmpty() { return image_.IsEmpty(); } diff --git a/atom/common/api/atom_api_native_image.h b/atom/common/api/atom_api_native_image.h index 1f0fe946ba51..145f5ff1dcdc 100644 --- a/atom/common/api/atom_api_native_image.h +++ b/atom/common/api/atom_api_native_image.h @@ -61,6 +61,9 @@ class NativeImage : public mate::Wrappable { private: v8::Local ToPNG(v8::Isolate* isolate); v8::Local ToJPEG(v8::Isolate* isolate, int quality); + v8::Local GetNativeHandle( + v8::Isolate* isolate, + mate::Arguments* args); std::string ToDataURL(); bool IsEmpty(); gfx::Size GetSize(); diff --git a/docs/api/native-image.md b/docs/api/native-image.md index 515e89fba1a4..c33f35c6fe51 100644 --- a/docs/api/native-image.md +++ b/docs/api/native-image.md @@ -133,6 +133,12 @@ Returns a [Buffer][buffer] that contains the image's `JPEG` encoded data. Returns the data URL of the image. +### `image.getNativeHandle()` + +Returns a pointer to an underlying native type (encoded as a [Buffer][buffer]) which can be used with native APIs. Note that in many cases, this pointer is a weak pointer to the underlying native image not a copy, so you _must_ ensure that the associated `nativeImage` instance is kept around. + +Returns a [Buffer][buffer] that represents a pointer to a native type - on OS X, this type is an NSImage object. + ### `image.isEmpty()` Returns a boolean whether the image is empty. diff --git a/spec/api-native-image-spec.js b/spec/api-native-image-spec.js index 043a914578c0..a7ee8be007ed 100644 --- a/spec/api-native-image-spec.js +++ b/spec/api-native-image-spec.js @@ -34,5 +34,18 @@ describe('nativeImage module', () => { assert.equal(image.getSize().height, 190); assert.equal(image.getSize().width, 538); }); + + it('Gets an NSImage pointer on OS X', () => { + if (process.platform !== 'darwin') return; + + const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`; + const image = nativeImage.createFromPath(imagePath); + const nsimage = image.getNativeHandle(); + + assert.equal(nsimage.length, 8); + + // If all bytes are null, that's Bad + assert.equal(nsimage.reduce((acc,x) => acc || (x != 0), false), true); + }); }); });