gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "ImageBackend.h"
 
using namespace v8;
 
ImageBackend::ImageBackend(int width, int height)
    : Backend("image", width, height)
    {}
 
Backend *ImageBackend::construct(int width, int height){
  return new ImageBackend(width, height);
}
 
// This returns an approximate value only, suitable for Nan::AdjustExternalMemory.
// The formats that don't map to intrinsic types (RGB30, A1) round up.
int32_t ImageBackend::approxBytesPerPixel() {
  switch (format) {
  case CAIRO_FORMAT_ARGB32:
  case CAIRO_FORMAT_RGB24:
    return 4;
#ifdef CAIRO_FORMAT_RGB30
  case CAIRO_FORMAT_RGB30:
    return 3;
#endif
  case CAIRO_FORMAT_RGB16_565:
    return 2;
  case CAIRO_FORMAT_A8:
  case CAIRO_FORMAT_A1:
    return 1;
  default:
    return 0;
  }
}
 
cairo_surface_t* ImageBackend::createSurface() {
  assert(!surface);
  surface = cairo_image_surface_create(format, width, height);
  assert(surface);
  Nan::AdjustExternalMemory(approxBytesPerPixel() * width * height);
  return surface;
}
 
void ImageBackend::destroySurface() {
  if (surface) {
    cairo_surface_destroy(surface);
    surface = nullptr;
    Nan::AdjustExternalMemory(-approxBytesPerPixel() * width * height);
  }
}
 
cairo_format_t ImageBackend::getFormat() {
  return format;
}
 
void ImageBackend::setFormat(cairo_format_t _format) {
    this->format = _format;
}
 
Nan::Persistent<FunctionTemplate> ImageBackend::constructor;
 
void ImageBackend::Initialize(Local<Object> target) {
    Nan::HandleScope scope;
 
    Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(ImageBackend::New);
    ImageBackend::constructor.Reset(ctor);
    ctor->InstanceTemplate()->SetInternalFieldCount(1);
    ctor->SetClassName(Nan::New<String>("ImageBackend").ToLocalChecked());
  Nan::Set(target,
           Nan::New<String>("ImageBackend").ToLocalChecked(),
           Nan::GetFunction(ctor).ToLocalChecked()).Check();
}
 
NAN_METHOD(ImageBackend::New) {
  init(info);
}