Skip to content

Commit bcac2ac

Browse files
committed
Added BMP class and buildBMP.
1 parent 03759cb commit bcac2ac

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

src/robotjs.cc

+37-13
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,40 @@ NAN_METHOD(captureScreen)
695695
|____/|_|\__|_| |_| |_|\__,_| .__/
696696
|_|
697697
*/
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+
698732
NAN_METHOD(getColor)
699733
{
700734
MMBitmapRef bitmap;
@@ -704,20 +738,10 @@ NAN_METHOD(getColor)
704738
size_t y = info[1]->Int32Value();
705739

706740
//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());
716742

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

722746
color = MMRGBHexAtPoint(bitmap, x, y);
723747

0 commit comments

Comments
 (0)