Skip to content

Commit b58962f

Browse files
committed
Starting to implement bitmap sharing.
1 parent 03759cb commit b58962f

File tree

5 files changed

+95
-15
lines changed

5 files changed

+95
-15
lines changed

binding.gyp

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
'src/screen.c',
6161
'src/screengrab.c',
6262
'src/snprintf.c',
63-
'src/MMBitmap.c'
63+
'src/MMBitmap.c',
64+
'src/io.c',
65+
'src/bmp_io.c'
6466
]
6567
}]
6668
}

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ function bitmap(width, height, byteWidth, bitsPerPixel, bytesPerPixel, image)
1717
{
1818
return robotjs.getColor(this, x, y);
1919
};
20+
21+
this.save = function(path)
22+
{
23+
return robotjs.saveBitmap(this, path);
24+
};
2025
}
2126

2227
module.exports.screen.capture = function(x, y, width, height)

src/bmp_io.h

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#include "MMBitmap.h"
66
#include "io.h"
77

8+
#ifdef __cplusplus
9+
extern "C"
10+
{
11+
#endif
12+
813
enum _BMPReadError {
914
kBMPGenericError = 0,
1015
kBMPAccessError,
@@ -51,4 +56,8 @@ int saveMMBitmapAsBMP(MMBitmapRef bitmap, const char *path);
5156
*/
5257
void flipBitmapData(void *data, size_t width, size_t height, size_t bytewidth);
5358

59+
#ifdef __cplusplus
60+
}
61+
#endif
62+
5463
#endif /* BMP_IO_H */

src/io.h

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include <stddef.h>
77
#include <stdint.h>
88

9+
#ifdef __cplusplus
10+
extern "C"
11+
{
12+
#endif
913

1014
enum _MMImageType {
1115
kInvalidImageType = 0,
@@ -43,4 +47,9 @@ int saveMMBitmapToFile(MMBitmapRef bitmap, const char *path, MMImageType type);
4347
* Returned string is constant and hence should not be freed. */
4448
const char *MMIOErrorString(MMImageType type, MMIOError error);
4549

50+
#ifdef __cplusplus
51+
}
52+
#endif
53+
54+
4655
#endif /* IO_H */

src/robotjs.cc

+69-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "MMBitmap.h"
1111
#include "snprintf.h"
1212
#include "microsleep.h"
13+
#include "io.h"
14+
#include "bmp_io.h"
1315

1416
using namespace v8;
1517

@@ -695,6 +697,40 @@ NAN_METHOD(captureScreen)
695697
|____/|_|\__|_| |_| |_|\__,_| .__/
696698
|_|
697699
*/
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+
698734
NAN_METHOD(getColor)
699735
{
700736
MMBitmapRef bitmap;
@@ -704,23 +740,13 @@ NAN_METHOD(getColor)
704740
size_t y = info[1]->Int32Value();
705741

706742
//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());
708744

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);
716747

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-
722748
color = MMRGBHexAtPoint(bitmap, x, y);
723-
749+
724750
char hex[7];
725751

726752
padHex(color, hex);
@@ -731,6 +757,32 @@ NAN_METHOD(getColor)
731757

732758
}
733759

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+
734786
NAN_MODULE_INIT(InitAll)
735787
{
736788
Nan::Set(target, Nan::New("dragMouse").ToLocalChecked(),
@@ -780,6 +832,9 @@ NAN_MODULE_INIT(InitAll)
780832

781833
Nan::Set(target, Nan::New("getColor").ToLocalChecked(),
782834
Nan::GetFunction(Nan::New<FunctionTemplate>(getColor)).ToLocalChecked());
835+
836+
Nan::Set(target, Nan::New("saveBitmap").ToLocalChecked(),
837+
Nan::GetFunction(Nan::New<FunctionTemplate>(saveBitmap)).ToLocalChecked());
783838
}
784839

785840
NODE_MODULE(robotjs, InitAll)

0 commit comments

Comments
 (0)