10
10
#include " MMBitmap.h"
11
11
#include " snprintf.h"
12
12
#include " microsleep.h"
13
+ #include " io.h"
14
+ #include " bmp_io.h"
13
15
14
16
using namespace v8 ;
15
17
@@ -695,6 +697,40 @@ NAN_METHOD(captureScreen)
695
697
|____/|_|\__|_| |_| |_|\__,_| .__/
696
698
|_|
697
699
*/
700
+
701
+ class BMP
702
+ {
703
+ public:
704
+ size_t width;
705
+ size_t height;
706
+ size_t byteWidth;
707
+ uint8_t bitsPerPixel;
708
+ uint8_t bytesPerPixel;
709
+ uint8_t *image;
710
+ };
711
+
712
+ // Convert object from Javascript to a C++ class (BMP).
713
+ BMP buildBMP (Local<Object> info)
714
+ {
715
+ Local<Object> obj = Nan::To<v8::Object>(info).ToLocalChecked ();
716
+
717
+ BMP img;
718
+
719
+ img.width = obj->Get (Nan::New (" width" ).ToLocalChecked ())->Uint32Value ();
720
+ img.height = obj->Get (Nan::New (" height" ).ToLocalChecked ())->Uint32Value ();
721
+ img.byteWidth = obj->Get (Nan::New (" byteWidth" ).ToLocalChecked ())->Uint32Value ();
722
+ img.bitsPerPixel = obj->Get (Nan::New (" bitsPerPixel" ).ToLocalChecked ())->Uint32Value ();
723
+ img.bytesPerPixel = obj->Get (Nan::New (" bytesPerPixel" ).ToLocalChecked ())->Uint32Value ();
724
+
725
+ char * buf = node::Buffer::Data (obj->Get (Nan::New (" image" ).ToLocalChecked ()));
726
+
727
+ // Convert the buffer to a uint8_t which createMMBitmap requires.
728
+ img.image = (uint8_t *)malloc (img.byteWidth * img.height );
729
+ memcpy (img.image , buf, img.byteWidth * img.height );
730
+
731
+ return img;
732
+ }
733
+
698
734
NAN_METHOD (getColor)
699
735
{
700
736
MMBitmapRef bitmap;
@@ -704,23 +740,13 @@ NAN_METHOD(getColor)
704
740
size_t y = info[1 ]->Int32Value ();
705
741
706
742
// Get our image object from JavaScript.
707
- Local<Object> obj = Nan::To<v8::Object>(info[0 ]).ToLocalChecked ();
743
+ BMP img = buildBMP ( Nan::To<v8::Object>(info[0 ]).ToLocalChecked () );
708
744
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 ()));
745
+ // Create the bitmap.
746
+ bitmap = createMMBitmap (img.image , img.width , img.height , img.byteWidth , img.bitsPerPixel , img.bytesPerPixel );
716
747
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);
721
-
722
748
color = MMRGBHexAtPoint (bitmap, x, y);
723
-
749
+
724
750
char hex[7 ];
725
751
726
752
padHex (color, hex);
@@ -731,6 +757,32 @@ NAN_METHOD(getColor)
731
757
732
758
}
733
759
760
+ NAN_METHOD (saveBitmap)
761
+ {
762
+ MMBitmapRef bitmap;
763
+ MMImageType type = kBMPImageType ;
764
+
765
+ // Get our image object from JavaScript.
766
+ BMP img = buildBMP (Nan::To<v8::Object>(info[0 ]).ToLocalChecked ());
767
+
768
+ char *path;
769
+ Nan::Utf8String string (info[1 ]);
770
+
771
+ path = *string;
772
+
773
+ // Create the bitmap.
774
+ bitmap = createMMBitmap (img.image , img.width , img.height , img.byteWidth , img.bitsPerPixel , img.bytesPerPixel );
775
+
776
+ if (saveMMBitmapToFile (bitmap, path, type) != 0 ) {
777
+ return Nan::ThrowError (" Could not save image to file." );
778
+ }
779
+
780
+ destroyMMBitmap (bitmap);
781
+
782
+ info.GetReturnValue ().Set (Nan::New (1 ));
783
+
784
+ }
785
+
734
786
NAN_MODULE_INIT (InitAll)
735
787
{
736
788
Nan::Set (target, Nan::New (" dragMouse" ).ToLocalChecked (),
@@ -780,6 +832,9 @@ NAN_MODULE_INIT(InitAll)
780
832
781
833
Nan::Set (target, Nan::New (" getColor" ).ToLocalChecked (),
782
834
Nan::GetFunction (Nan::New<FunctionTemplate>(getColor)).ToLocalChecked ());
835
+
836
+ Nan::Set (target, Nan::New (" saveBitmap" ).ToLocalChecked (),
837
+ Nan::GetFunction (Nan::New<FunctionTemplate>(saveBitmap)).ToLocalChecked ());
783
838
}
784
839
785
840
NODE_MODULE (robotjs, InitAll)
0 commit comments