Browse Source

Improve how _data_len is managed and use to adjust memory, hide more of mime API behind cairo version conditional.

v1.x
c-spencer 13 years ago
parent
commit
cc994d2d8d
  1. 19
      src/Image.cc
  2. 2
      src/Image.h

19
src/Image.cc

@ -269,6 +269,11 @@ Image::SetOnerror(Local<String>, Local<Value> val, const AccessorInfo &info) {
Image::Image() { Image::Image() {
filename = NULL; filename = NULL;
_data = NULL; _data = NULL;
_data_len = 0;
#if CAIRO_VERSION_MINOR >= 10
_mime_data = NULL;
_mime_data_len = 0;
#endif
_surface = NULL; _surface = NULL;
width = height = 0; width = height = 0;
state = DEFAULT; state = DEFAULT;
@ -280,15 +285,19 @@ Image::Image() {
Image::~Image() { Image::~Image() {
if (_surface) { if (_surface) {
V8::AdjustAmountOfExternalAllocatedMemory(-4 * width * height); V8::AdjustAmountOfExternalAllocatedMemory(-_data_len);
cairo_surface_destroy(_surface); cairo_surface_destroy(_surface);
} }
if (_data) free(_data); free(_data);
#if CAIRO_VERSION_MINOR >= 10
if (_mime_data) { if (_mime_data) {
V8::AdjustAmountOfExternalAllocatedMemory(-_mime_data_len); V8::AdjustAmountOfExternalAllocatedMemory(-_mime_data_len);
free(_mime_data); free(_mime_data);
} }
#endif
free(filename); free(filename);
} }
@ -316,8 +325,9 @@ Image::loaded() {
width = cairo_image_surface_get_width(_surface); width = cairo_image_surface_get_width(_surface);
height = cairo_image_surface_get_height(_surface); height = cairo_image_surface_get_height(_surface);
_data_len = height * cairo_image_surface_get_stride(_surface);
// TODO: adjust accordingly when re-assigned src // TODO: adjust accordingly when re-assigned src
V8::AdjustAmountOfExternalAllocatedMemory(4 * width * height); V8::AdjustAmountOfExternalAllocatedMemory(_data_len);
if (!onload.IsEmpty()) { if (!onload.IsEmpty()) {
TryCatch try_catch; TryCatch try_catch;
@ -580,6 +590,7 @@ Image::loadGIFFromBuffer(uint8_t *buf, unsigned len) {
} }
_data = data; _data = data;
return CAIRO_STATUS_SUCCESS; return CAIRO_STATUS_SUCCESS;
} }
#endif /* HAVE_GIF */ #endif /* HAVE_GIF */
@ -690,7 +701,6 @@ Image::decodeJPEGIntoSurface(jpeg_decompress_struct *info) {
free(src); free(src);
_data = data; _data = data;
_data_len = width * height * 4;
return CAIRO_STATUS_SUCCESS; return CAIRO_STATUS_SUCCESS;
} }
@ -743,7 +753,6 @@ Image::decodeJPEGBufferIntoMimeSurface(uint8_t *buf, unsigned len) {
} }
_data = data; _data = data;
_data_len = buf_size;
return assignDataAsMime(buf, len, CAIRO_MIME_TYPE_JPEG); return assignDataAsMime(buf, len, CAIRO_MIME_TYPE_JPEG);
} }

2
src/Image.h

@ -88,8 +88,10 @@ class Image: public node::ObjectWrap {
cairo_surface_t *_surface; cairo_surface_t *_surface;
uint8_t *_data; uint8_t *_data;
int _data_len; int _data_len;
#if CAIRO_VERSION_MINOR >= 10
uint8_t *_mime_data; uint8_t *_mime_data;
int _mime_data_len; int _mime_data_len;
#endif
~Image(); ~Image();
}; };

Loading…
Cancel
Save