Turn audio into a shareable video. forked from nypublicradio/audiogram

ImageData.cc 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // ImageData.cc
  3. //
  4. // Copyright (c) 2010 LearnBoost <tj@learnboost.com>
  5. //
  6. #include "ImageData.h"
  7. Nan::Persistent<FunctionTemplate> ImageData::constructor;
  8. /*
  9. * Initialize ImageData.
  10. */
  11. void
  12. ImageData::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
  13. Nan::HandleScope scope;
  14. // Constructor
  15. Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(ImageData::New);
  16. constructor.Reset(ctor);
  17. ctor->InstanceTemplate()->SetInternalFieldCount(1);
  18. ctor->SetClassName(Nan::New("ImageData").ToLocalChecked());
  19. // Prototype
  20. Local<ObjectTemplate> proto = ctor->PrototypeTemplate();
  21. Nan::SetAccessor(proto, Nan::New("width").ToLocalChecked(), GetWidth);
  22. Nan::SetAccessor(proto, Nan::New("height").ToLocalChecked(), GetHeight);
  23. Nan::Set(target, Nan::New("ImageData").ToLocalChecked(), ctor->GetFunction());
  24. }
  25. /*
  26. * Initialize a new ImageData object.
  27. */
  28. NAN_METHOD(ImageData::New) {
  29. if (!info.IsConstructCall()) {
  30. return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
  31. }
  32. #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
  33. Local<v8::Object> clampedArray;
  34. Local<Object> global = Context::GetCurrent()->Global();
  35. #else
  36. Local<Uint8ClampedArray> clampedArray;
  37. #endif
  38. uint32_t width;
  39. uint32_t height;
  40. int length;
  41. if (info[0]->IsUint32() && info[1]->IsUint32()) {
  42. width = info[0]->Uint32Value();
  43. if (width == 0) {
  44. Nan::ThrowRangeError("The source width is zero.");
  45. return;
  46. }
  47. height = info[1]->Uint32Value();
  48. if (height == 0) {
  49. Nan::ThrowRangeError("The source height is zero.");
  50. return;
  51. }
  52. length = width * height * 4;
  53. #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
  54. Local<Int32> sizeHandle = Nan::New(length);
  55. Local<Value> caargv[] = { sizeHandle };
  56. clampedArray = global->Get(Nan::New("Uint8ClampedArray").ToLocalChecked()).As<Function>()->NewInstance(1, caargv);
  57. #else
  58. clampedArray = Uint8ClampedArray::New(ArrayBuffer::New(Isolate::GetCurrent(), length), 0, length);
  59. #endif
  60. #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
  61. } else if (info[0]->ToObject()->GetIndexedPropertiesExternalArrayDataType() == kExternalPixelArray && info[1]->IsUint32()) {
  62. clampedArray = info[0]->ToObject();
  63. length = clampedArray->GetIndexedPropertiesExternalArrayDataLength();
  64. #else
  65. } else if (info[0]->IsUint8ClampedArray() && info[1]->IsUint32()) {
  66. clampedArray = info[0].As<Uint8ClampedArray>();
  67. length = clampedArray->Length();
  68. #endif
  69. if (length == 0) {
  70. Nan::ThrowRangeError("The input data has a zero byte length.");
  71. return;
  72. }
  73. if (length % 4 != 0) {
  74. Nan::ThrowRangeError("The input data byte length is not a multiple of 4.");
  75. return;
  76. }
  77. width = info[1]->Uint32Value();
  78. int size = length / 4;
  79. if (width == 0) {
  80. Nan::ThrowRangeError("The source width is zero.");
  81. return;
  82. }
  83. if (size % width != 0) {
  84. Nan::ThrowRangeError("The input data byte length is not a multiple of (4 * width).");
  85. return;
  86. }
  87. height = size / width;
  88. if (info[2]->IsUint32() && info[2]->Uint32Value() != height) {
  89. Nan::ThrowRangeError("The input data byte length is not equal to (4 * width * height).");
  90. return;
  91. }
  92. } else {
  93. Nan::ThrowTypeError("Expected (Uint8ClampedArray, width[, height]) or (width, height)");
  94. return;
  95. }
  96. Nan::TypedArrayContents<uint8_t> dataPtr(clampedArray);
  97. ImageData *imageData = new ImageData(reinterpret_cast<uint8_t*>(*dataPtr), width, height);
  98. imageData->Wrap(info.This());
  99. info.This()->Set(Nan::New("data").ToLocalChecked(), clampedArray);
  100. info.GetReturnValue().Set(info.This());
  101. }
  102. /*
  103. * Get width.
  104. */
  105. NAN_GETTER(ImageData::GetWidth) {
  106. ImageData *imageData = Nan::ObjectWrap::Unwrap<ImageData>(info.This());
  107. info.GetReturnValue().Set(Nan::New<Number>(imageData->width()));
  108. }
  109. /*
  110. * Get height.
  111. */
  112. NAN_GETTER(ImageData::GetHeight) {
  113. ImageData *imageData = Nan::ObjectWrap::Unwrap<ImageData>(info.This());
  114. info.GetReturnValue().Set(Nan::New<Number>(imageData->height()));
  115. }