Cleanup the code of getBitmap
authorCheng Zhao <zcbenz@gmail.com>
Fri, 5 Aug 2016 08:55:57 +0000 (17:55 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 5 Aug 2016 09:05:44 +0000 (18:05 +0900)
atom/common/api/atom_api_native_image.cc
docs/api/native-image.md

index 6aeb2fb..b08c9a9 100644 (file)
@@ -172,6 +172,9 @@ bool ReadImageSkiaFromICO(gfx::ImageSkia* image, HICON icon) {
 }
 #endif
 
+void Noop(char*, void*) {
+}
+
 }  // namespace
 
 NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
@@ -228,18 +231,6 @@ v8::Local<v8::Value> NativeImage::ToBitmap(v8::Isolate* isolate) {
                             bitmap->getSafeSize()).ToLocalChecked();
 }
 
-void noop(char*, void*) {}
-
-v8::Local<v8::Value> NativeImage::GetBitmap(v8::Isolate* isolate) {
-  const SkBitmap* bitmap = image_.ToSkBitmap();
-  SkPixelRef* ref = bitmap->pixelRef();
-  return node::Buffer::New(isolate,
-                            reinterpret_cast<char*>(ref->pixels()),
-                            bitmap->getSafeSize(),
-                            &noop,
-                            nullptr).ToLocalChecked();
-}
-
 v8::Local<v8::Value> NativeImage::ToJPEG(v8::Isolate* isolate, int quality) {
   std::vector<unsigned char> output;
   gfx::JPEG1xEncodedDataFromImage(image_, quality, &output);
@@ -258,6 +249,16 @@ std::string NativeImage::ToDataURL() {
   return data_url;
 }
 
+v8::Local<v8::Value> NativeImage::GetBitmap(v8::Isolate* isolate) {
+  const SkBitmap* bitmap = image_.ToSkBitmap();
+  SkPixelRef* ref = bitmap->pixelRef();
+  return node::Buffer::New(isolate,
+                           reinterpret_cast<char*>(ref->pixels()),
+                           bitmap->getSafeSize(),
+                           &Noop,
+                           nullptr).ToLocalChecked();
+}
+
 v8::Local<v8::Value> NativeImage::GetNativeHandle(v8::Isolate* isolate,
                                                   mate::Arguments* args) {
 #if defined(OS_MACOSX)
index c78b1d6..947a5b7 100644 (file)
@@ -163,7 +163,8 @@ Returns a [Buffer][buffer] that contains the image's `JPEG` encoded data.
 
 #### `image.toBitmap()`
 
-Returns a [Buffer][buffer] that contains a copy of the image's raw pixel data.
+Returns a [Buffer][buffer] that contains a copy of the image's raw bitmap pixel
+data.
 
 #### `image.toDataURL()`
 
@@ -171,8 +172,11 @@ Returns the data URL of the image.
 
 #### `image.getBitmap()`
 
-Returns a [Buffer][buffer] that contains the image's raw pixel data. The pixel 
-data is not owned by the `Buffer` object.
+Returns a [Buffer][buffer] that contains the image's raw bitmap pixel data.
+
+The difference between `getBitmap()` and `toBitmap()` is, `getBitmap()` does not
+copy the bitmap data, so you have to use the returned Buffer immediately in
+current event loop tick, otherwise the data might be changed or destroyed.
 
 #### `image.getNativeHandle()` _macOS_