@@ -695,6 +695,40 @@ NAN_METHOD(captureScreen)
695
695
|____/|_|\__|_| |_| |_|\__,_| .__/
696
696
|_|
697
697
*/
698
+
699
+ class BMP
700
+ {
701
+ public:
702
+ size_t width;
703
+ size_t height;
704
+ size_t byteWidth;
705
+ uint8_t bitsPerPixel;
706
+ uint8_t bytesPerPixel;
707
+ uint8_t *image;
708
+ };
709
+
710
+ // Convert object from Javascript to a C++ class (BMP).
711
+ BMP buildBMP (Local<Object> info)
712
+ {
713
+ Local<Object> obj = Nan::To<v8::Object>(info).ToLocalChecked ();
714
+
715
+ BMP img;
716
+
717
+ img.width = obj->Get (Nan::New (" width" ).ToLocalChecked ())->Uint32Value ();
718
+ img.height = obj->Get (Nan::New (" height" ).ToLocalChecked ())->Uint32Value ();
719
+ img.byteWidth = obj->Get (Nan::New (" byteWidth" ).ToLocalChecked ())->Uint32Value ();
720
+ img.bitsPerPixel = obj->Get (Nan::New (" bitsPerPixel" ).ToLocalChecked ())->Uint32Value ();
721
+ img.bytesPerPixel = obj->Get (Nan::New (" bytesPerPixel" ).ToLocalChecked ())->Uint32Value ();
722
+
723
+ char * buf = node::Buffer::Data (obj->Get (Nan::New (" image" ).ToLocalChecked ()));
724
+
725
+ // Convert the buffer to a uint8_t which createMMBitmap requires.
726
+ img.image = (uint8_t *)malloc (img.byteWidth * img.height );
727
+ memcpy (img.image , buf, img.byteWidth * img.height );
728
+
729
+ return img;
730
+ }
731
+
698
732
NAN_METHOD (getColor)
699
733
{
700
734
MMBitmapRef bitmap;
@@ -704,20 +738,10 @@ NAN_METHOD(getColor)
704
738
size_t y = info[1 ]->Int32Value ();
705
739
706
740
// Get our image object from JavaScript.
707
- Local<Object> obj = Nan::To<v8::Object>(info[0 ]).ToLocalChecked ();
708
-
709
- size_t width = obj->Get (Nan::New (" width" ).ToLocalChecked ())->Uint32Value ();
710
- size_t height = obj->Get (Nan::New (" height" ).ToLocalChecked ())->Uint32Value ();
711
- size_t byteWidth = obj->Get (Nan::New (" byteWidth" ).ToLocalChecked ())->Uint32Value ();
712
- uint8_t bitsPerPixel = obj->Get (Nan::New (" bitsPerPixel" ).ToLocalChecked ())->Uint32Value ();
713
- uint8_t bytesPerPixel = obj->Get (Nan::New (" bytesPerPixel" ).ToLocalChecked ())->Uint32Value ();
714
-
715
- char * buf = node::Buffer::Data (obj->Get (Nan::New (" image" ).ToLocalChecked ()));
741
+ BMP img = buildBMP (Nan::To<v8::Object>(info[0 ]).ToLocalChecked ());
716
742
717
- uint8_t *data = (uint8_t *)malloc (byteWidth * height);
718
- memcpy (data, buf, byteWidth * height);
719
-
720
- bitmap = createMMBitmap (data, width, height, byteWidth, bitsPerPixel, bytesPerPixel);
743
+ // Create the bitmap.
744
+ bitmap = createMMBitmap (img.image , img.width , img.height , img.byteWidth , img.bitsPerPixel , img.bytesPerPixel );
721
745
722
746
color = MMRGBHexAtPoint (bitmap, x, y);
723
747
0 commit comments