Compare commits

...

2 Commits

Author SHA1 Message Date
Lovell Fuller
d5d85a8697 Expose vips internal cache settings and status 2014-02-25 23:31:33 +00:00
Lovell Fuller
ae9a8b0f57 Remove unnecessary temporary copy of input buffer. 2014-02-23 10:52:40 +00:00
6 changed files with 46 additions and 14 deletions

View File

@@ -51,7 +51,7 @@ Scale and crop to `width` x `height` calling `callback` when complete.
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant image data when a Buffer is requested.
### Examples
#### Examples
```javascript
sharp.resize("input.jpg", "output.jpg", 300, 200, function(err) {
@@ -101,6 +101,20 @@ sharp.resize("input.jpg", sharp.buffer.webp, 200, 300, {canvas: sharp.canvas.emb
});
```
### cache([limit])
If `limit` is provided, set the `vips` internal cache limit to this value in MB. The default value is 100.
Always returns cache statistics, namely current usage, high water mark and maximum limit.
The high water mark may be higher than the maximum limit.
```javascript
var stats = sharp.cache(); // { current: 98, high: 115, limit: 100 }
sharp.cache(200); // { current: 98, high: 115, limit: 200 }
sharp.cache(50); // { current: 49, high: 115, limit: 50 }
```
## Testing
npm test

View File

@@ -53,6 +53,13 @@ module.exports.resize = function(input, output, width, height, options, callback
sharp.resize(options.inFile, options.inBuffer, output, width, height, canvas, sharpen, progessive, sequentialRead, callback);
};
module.exports.cache = function(limit) {
if (Number.isNaN(limit)) {
limit = null;
}
return sharp.cache(limit);
}
/* Deprecated v0.0.x methods */
module.exports.crop = function(input, output, width, height, sharpen, callback) {
sharp.resize(input, output, width, height, {canvas: "c", sharpen: true}, callback);

View File

@@ -1,6 +1,6 @@
{
"name": "sharp",
"version": "0.1.6",
"version": "0.1.7",
"author": "Lovell Fuller",
"description": "High performance module to resize JPEG, PNG, WebP and TIFF images using the libvips image processing library",
"scripts": {

View File

@@ -268,11 +268,6 @@ void resize_async_after(uv_work_t *work, int status) {
resize_baton *baton = static_cast<resize_baton*>(work->data);
// Free temporary copy of input buffer
if (baton->buffer_in_len > 0) {
g_free(baton->buffer_in);
}
Handle<Value> argv[2] = { Null(), Null() };
if (!baton->err.empty()) {
// Error
@@ -301,12 +296,8 @@ Handle<Value> resize(const Arguments& args) {
baton->file_in = *String::Utf8Value(args[0]->ToString());
if (args[1]->IsObject()) {
Local<Object> buffer = args[1]->ToObject();
// Take temporary copy of input buffer
if (Buffer::Length(buffer) > 0) {
baton->buffer_in_len = Buffer::Length(buffer);
baton->buffer_in = g_malloc(baton->buffer_in_len);
memcpy(baton->buffer_in, Buffer::Data(buffer), baton->buffer_in_len);
}
baton->buffer_in_len = Buffer::Length(buffer);
baton->buffer_in = Buffer::Data(buffer);
}
baton->file_out = *String::Utf8Value(args[2]->ToString());
baton->width = args[3]->Int32Value();
@@ -332,6 +323,22 @@ Handle<Value> resize(const Arguments& args) {
return scope.Close(Undefined());
}
Handle<Value> cache(const Arguments& args) {
HandleScope scope;
// Set cache limit
if (args[0]->IsInt32()) {
vips_cache_set_max_mem(args[0]->Int32Value() * 1048576);
}
// Get cache statistics
Local<Object> cache = Object::New();
cache->Set(String::NewSymbol("current"), Number::New(vips_tracked_get_mem() / 1048576));
cache->Set(String::NewSymbol("high"), Number::New(vips_tracked_get_mem_highwater() / 1048576));
cache->Set(String::NewSymbol("limit"), Number::New(vips_cache_get_max_mem() / 1048576));
return scope.Close(cache);
}
static void at_exit(void* arg) {
HandleScope scope;
vips_shutdown();
@@ -342,6 +349,7 @@ extern "C" void init(Handle<Object> target) {
vips_init("");
AtExit(at_exit);
NODE_SET_METHOD(target, "resize", resize);
NODE_SET_METHOD(target, "cache", cache);
}
NODE_MODULE(sharp, init)

View File

@@ -26,4 +26,6 @@ async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) {
next();
}
);
}, function() {});
}, function() {
console.dir(sharp.cache());
});

View File

@@ -420,4 +420,5 @@ async.series({
Object.keys(results).forEach(function(format) {
assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format);
});
console.dir(sharp.cache());
});