Merge pull request #4791 from atom/nativeimage-as-nsimage
Convert NativeImage instances to OS-specific representations
This commit is contained in:
commit
081ab17e13
4 changed files with 41 additions and 0 deletions
|
@ -184,6 +184,8 @@ mate::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder(
|
||||||
template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate)
|
template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate)
|
||||||
.SetMethod("toPng", &NativeImage::ToPNG)
|
.SetMethod("toPng", &NativeImage::ToPNG)
|
||||||
.SetMethod("toJpeg", &NativeImage::ToJPEG)
|
.SetMethod("toJpeg", &NativeImage::ToJPEG)
|
||||||
|
.SetMethod("getNativeHandle",
|
||||||
|
&NativeImage::GetNativeHandle)
|
||||||
.SetMethod("toDataURL", &NativeImage::ToDataURL)
|
.SetMethod("toDataURL", &NativeImage::ToDataURL)
|
||||||
.SetMethod("toDataUrl", &NativeImage::ToDataURL) // deprecated.
|
.SetMethod("toDataUrl", &NativeImage::ToDataURL) // deprecated.
|
||||||
.SetMethod("isEmpty", &NativeImage::IsEmpty)
|
.SetMethod("isEmpty", &NativeImage::IsEmpty)
|
||||||
|
@ -221,6 +223,23 @@ std::string NativeImage::ToDataURL() {
|
||||||
return data_url;
|
return data_url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Value> NativeImage::GetNativeHandle(
|
||||||
|
v8::Isolate* isolate,
|
||||||
|
mate::Arguments* args) {
|
||||||
|
void* ptr = NULL;
|
||||||
|
#if defined(OS_MACOSX)
|
||||||
|
ptr = reinterpret_cast<void*>(image_.AsNSImage());
|
||||||
|
#else
|
||||||
|
args->ThrowError();
|
||||||
|
return v8::Undefined(isolate);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return node::Buffer::Copy(
|
||||||
|
isolate,
|
||||||
|
reinterpret_cast<char*>(ptr),
|
||||||
|
sizeof(void*)).ToLocalChecked();
|
||||||
|
}
|
||||||
|
|
||||||
bool NativeImage::IsEmpty() {
|
bool NativeImage::IsEmpty() {
|
||||||
return image_.IsEmpty();
|
return image_.IsEmpty();
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,6 +61,9 @@ class NativeImage : public mate::Wrappable {
|
||||||
private:
|
private:
|
||||||
v8::Local<v8::Value> ToPNG(v8::Isolate* isolate);
|
v8::Local<v8::Value> ToPNG(v8::Isolate* isolate);
|
||||||
v8::Local<v8::Value> ToJPEG(v8::Isolate* isolate, int quality);
|
v8::Local<v8::Value> ToJPEG(v8::Isolate* isolate, int quality);
|
||||||
|
v8::Local<v8::Value> GetNativeHandle(
|
||||||
|
v8::Isolate* isolate,
|
||||||
|
mate::Arguments* args);
|
||||||
std::string ToDataURL();
|
std::string ToDataURL();
|
||||||
bool IsEmpty();
|
bool IsEmpty();
|
||||||
gfx::Size GetSize();
|
gfx::Size GetSize();
|
||||||
|
|
|
@ -133,6 +133,12 @@ Returns a [Buffer][buffer] that contains the image's `JPEG` encoded data.
|
||||||
|
|
||||||
Returns the data URL of the image.
|
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()`
|
### `image.isEmpty()`
|
||||||
|
|
||||||
Returns a boolean whether the image is empty.
|
Returns a boolean whether the image is empty.
|
||||||
|
|
|
@ -34,5 +34,18 @@ describe('nativeImage module', () => {
|
||||||
assert.equal(image.getSize().height, 190);
|
assert.equal(image.getSize().height, 190);
|
||||||
assert.equal(image.getSize().width, 538);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue