mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
0.11 compat using nan
This commit is contained in:
parent
6a2816e917
commit
e96fd8b9de
@ -10,7 +10,8 @@
|
|||||||
'/usr/local/lib/glib-2.0/include',
|
'/usr/local/lib/glib-2.0/include',
|
||||||
'/usr/include/glib-2.0',
|
'/usr/include/glib-2.0',
|
||||||
'/usr/lib/glib-2.0/include',
|
'/usr/lib/glib-2.0/include',
|
||||||
'/usr/lib/x86_64-linux-gnu/glib-2.0/include'
|
'/usr/lib/x86_64-linux-gnu/glib-2.0/include',
|
||||||
|
'<!(node -e "require(\'nan\')")'
|
||||||
],
|
],
|
||||||
'cflags': ['-fexceptions', '-pedantic', '-Wall', '-O3'],
|
'cflags': ['-fexceptions', '-pedantic', '-Wall', '-O3'],
|
||||||
'cflags_cc': ['-fexceptions', '-pedantic', '-Wall', '-O3']
|
'cflags_cc': ['-fexceptions', '-pedantic', '-Wall', '-O3']
|
||||||
|
@ -29,12 +29,14 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"imagemagick": "*",
|
"imagemagick": "*",
|
||||||
"gm": "*",
|
"gm": "*",
|
||||||
"epeg": "*",
|
|
||||||
"async": "*",
|
"async": "*",
|
||||||
"benchmark": "*"
|
"benchmark": "*"
|
||||||
},
|
},
|
||||||
"license": "Apache 2.0",
|
"license": "Apache 2.0",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.8"
|
"node": ">=0.8"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"nan": "^0.8.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
452
src/sharp.cc
452
src/sharp.cc
@ -5,6 +5,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <vips/vips.h>
|
#include <vips/vips.h>
|
||||||
|
|
||||||
|
#include "nan.h"
|
||||||
|
|
||||||
using namespace v8;
|
using namespace v8;
|
||||||
using namespace node;
|
using namespace node;
|
||||||
|
|
||||||
@ -23,7 +25,6 @@ struct resize_baton {
|
|||||||
bool progessive;
|
bool progessive;
|
||||||
VipsAccess access_method;
|
VipsAccess access_method;
|
||||||
std::string err;
|
std::string err;
|
||||||
Persistent<Function> callback;
|
|
||||||
|
|
||||||
resize_baton(): buffer_in_len(0), buffer_out_len(0) {}
|
resize_baton(): buffer_in_len(0), buffer_out_len(0) {}
|
||||||
};
|
};
|
||||||
@ -67,253 +68,251 @@ void resize_error(resize_baton *baton, VipsImage *unref) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void resize_async(uv_work_t *work) {
|
class ResizeWorker : public NanAsyncWorker {
|
||||||
resize_baton* baton = static_cast<resize_baton*>(work->data);
|
public:
|
||||||
|
ResizeWorker(NanCallback *callback, resize_baton *baton)
|
||||||
|
: NanAsyncWorker(callback), baton(baton) {}
|
||||||
|
~ResizeWorker() {}
|
||||||
|
|
||||||
// Input
|
void Execute () {
|
||||||
ImageType inputImageType = JPEG;
|
// Input
|
||||||
VipsImage *in = vips_image_new();
|
ImageType inputImageType = JPEG;
|
||||||
if (baton->buffer_in_len > 1) {
|
VipsImage *in = vips_image_new();
|
||||||
if (memcmp(MARKER_JPEG, baton->buffer_in, 2) == 0) {
|
if (baton->buffer_in_len > 1) {
|
||||||
if (vips_jpegload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "access", baton->access_method, NULL)) {
|
if (memcmp(MARKER_JPEG, baton->buffer_in, 2) == 0) {
|
||||||
|
if (vips_jpegload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "access", baton->access_method, NULL)) {
|
||||||
|
return resize_error(baton, in);
|
||||||
|
}
|
||||||
|
} else if(memcmp(MARKER_PNG, baton->buffer_in, 2) == 0) {
|
||||||
|
inputImageType = PNG;
|
||||||
|
if (vips_pngload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "access", baton->access_method, NULL)) {
|
||||||
|
return resize_error(baton, in);
|
||||||
|
}
|
||||||
|
} else if(memcmp(MARKER_WEBP, baton->buffer_in, 2) == 0) {
|
||||||
|
inputImageType = WEBP;
|
||||||
|
if (vips_webpload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "access", baton->access_method, NULL)) {
|
||||||
|
return resize_error(baton, in);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resize_error(baton, in);
|
||||||
|
(baton->err).append("Unsupported input buffer");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if (is_jpeg(baton->file_in)) {
|
||||||
|
if (vips_jpegload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
||||||
return resize_error(baton, in);
|
return resize_error(baton, in);
|
||||||
}
|
}
|
||||||
} else if(memcmp(MARKER_PNG, baton->buffer_in, 2) == 0) {
|
} else if (is_png(baton->file_in)) {
|
||||||
inputImageType = PNG;
|
inputImageType = PNG;
|
||||||
if (vips_pngload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "access", baton->access_method, NULL)) {
|
if (vips_pngload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
||||||
return resize_error(baton, in);
|
return resize_error(baton, in);
|
||||||
}
|
}
|
||||||
} else if(memcmp(MARKER_WEBP, baton->buffer_in, 2) == 0) {
|
} else if (is_webp(baton->file_in)) {
|
||||||
inputImageType = WEBP;
|
inputImageType = WEBP;
|
||||||
if (vips_webpload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "access", baton->access_method, NULL)) {
|
if (vips_webpload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
||||||
|
return resize_error(baton, in);
|
||||||
|
}
|
||||||
|
} else if (is_tiff(baton->file_in)) {
|
||||||
|
inputImageType = TIFF;
|
||||||
|
if (vips_tiffload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
||||||
return resize_error(baton, in);
|
return resize_error(baton, in);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
resize_error(baton, in);
|
resize_error(baton, in);
|
||||||
(baton->err).append("Unsupported input buffer");
|
(baton->err).append("Unsupported input file " + baton->file_in);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (is_jpeg(baton->file_in)) {
|
|
||||||
if (vips_jpegload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
|
||||||
return resize_error(baton, in);
|
|
||||||
}
|
|
||||||
} else if (is_png(baton->file_in)) {
|
|
||||||
inputImageType = PNG;
|
|
||||||
if (vips_pngload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
|
||||||
return resize_error(baton, in);
|
|
||||||
}
|
|
||||||
} else if (is_webp(baton->file_in)) {
|
|
||||||
inputImageType = WEBP;
|
|
||||||
if (vips_webpload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
|
||||||
return resize_error(baton, in);
|
|
||||||
}
|
|
||||||
} else if (is_tiff(baton->file_in)) {
|
|
||||||
inputImageType = TIFF;
|
|
||||||
if (vips_tiffload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
|
||||||
return resize_error(baton, in);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
resize_error(baton, in);
|
|
||||||
(baton->err).append("Unsupported input file " + baton->file_in);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scaling calculations
|
// Scaling calculations
|
||||||
double factor;
|
double factor;
|
||||||
if (baton->width > 0 && baton->height > 0) {
|
if (baton->width > 0 && baton->height > 0) {
|
||||||
// Fixed width and height
|
// Fixed width and height
|
||||||
double xfactor = (double)(in->Xsize) / (double)(baton->width);
|
double xfactor = (double)(in->Xsize) / (double)(baton->width);
|
||||||
double yfactor = (double)(in->Ysize) / (double)(baton->height);
|
double yfactor = (double)(in->Ysize) / (double)(baton->height);
|
||||||
factor = baton->crop ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor);
|
factor = baton->crop ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor);
|
||||||
} else if (baton->width > 0) {
|
} else if (baton->width > 0) {
|
||||||
// Fixed width, auto height
|
// Fixed width, auto height
|
||||||
factor = (double)(in->Xsize) / (double)(baton->width);
|
factor = (double)(in->Xsize) / (double)(baton->width);
|
||||||
baton->height = floor((double)(in->Ysize) / factor);
|
baton->height = floor((double)(in->Ysize) / factor);
|
||||||
} else if (baton->height > 0) {
|
} else if (baton->height > 0) {
|
||||||
// Fixed height, auto width
|
// Fixed height, auto width
|
||||||
factor = (double)(in->Ysize) / (double)(baton->height);
|
factor = (double)(in->Ysize) / (double)(baton->height);
|
||||||
baton->width = floor((double)(in->Xsize) / factor);
|
baton->width = floor((double)(in->Xsize) / factor);
|
||||||
} else {
|
} else {
|
||||||
// Identity transform
|
// Identity transform
|
||||||
factor = 1;
|
factor = 1;
|
||||||
baton->width = in->Xsize;
|
baton->width = in->Xsize;
|
||||||
baton->height = in->Ysize;
|
baton->height = in->Ysize;
|
||||||
}
|
|
||||||
int shrink = floor(factor);
|
|
||||||
if (shrink < 1) {
|
|
||||||
shrink = 1;
|
|
||||||
}
|
|
||||||
double residual = shrink / (double)factor;
|
|
||||||
|
|
||||||
// Try to use libjpeg shrink-on-load
|
|
||||||
int shrink_on_load = 1;
|
|
||||||
if (inputImageType == JPEG) {
|
|
||||||
if (shrink >= 8) {
|
|
||||||
factor = factor / 8;
|
|
||||||
shrink_on_load = 8;
|
|
||||||
} else if (shrink >= 4) {
|
|
||||||
factor = factor / 4;
|
|
||||||
shrink_on_load = 4;
|
|
||||||
} else if (shrink >= 2) {
|
|
||||||
factor = factor / 2;
|
|
||||||
shrink_on_load = 2;
|
|
||||||
}
|
}
|
||||||
}
|
int shrink = floor(factor);
|
||||||
VipsImage *shrunk_on_load = vips_image_new();
|
if (shrink < 1) {
|
||||||
if (shrink_on_load > 1) {
|
shrink = 1;
|
||||||
// Recalculate integral shrink and double residual
|
}
|
||||||
factor = std::max(factor, 1.0);
|
double residual = shrink / (double)factor;
|
||||||
shrink = floor(factor);
|
|
||||||
residual = shrink / factor;
|
// Try to use libjpeg shrink-on-load
|
||||||
// Reload input using shrink-on-load
|
int shrink_on_load = 1;
|
||||||
if (baton->buffer_in_len > 1) {
|
if (inputImageType == JPEG) {
|
||||||
if (vips_jpegload_buffer(baton->buffer_in, baton->buffer_in_len, &shrunk_on_load, "shrink", shrink_on_load, NULL)) {
|
if (shrink >= 8) {
|
||||||
return resize_error(baton, in);
|
factor = factor / 8;
|
||||||
|
shrink_on_load = 8;
|
||||||
|
} else if (shrink >= 4) {
|
||||||
|
factor = factor / 4;
|
||||||
|
shrink_on_load = 4;
|
||||||
|
} else if (shrink >= 2) {
|
||||||
|
factor = factor / 2;
|
||||||
|
shrink_on_load = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VipsImage *shrunk_on_load = vips_image_new();
|
||||||
|
if (shrink_on_load > 1) {
|
||||||
|
// Recalculate integral shrink and double residual
|
||||||
|
factor = std::max(factor, 1.0);
|
||||||
|
shrink = floor(factor);
|
||||||
|
residual = shrink / factor;
|
||||||
|
// Reload input using shrink-on-load
|
||||||
|
if (baton->buffer_in_len > 1) {
|
||||||
|
if (vips_jpegload_buffer(baton->buffer_in, baton->buffer_in_len, &shrunk_on_load, "shrink", shrink_on_load, NULL)) {
|
||||||
|
return resize_error(baton, in);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (vips_jpegload((baton->file_in).c_str(), &shrunk_on_load, "shrink", shrink_on_load, NULL)) {
|
||||||
|
return resize_error(baton, in);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (vips_jpegload((baton->file_in).c_str(), &shrunk_on_load, "shrink", shrink_on_load, NULL)) {
|
vips_copy(in, &shrunk_on_load, NULL);
|
||||||
return resize_error(baton, in);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
g_object_unref(in);
|
||||||
vips_copy(in, &shrunk_on_load, NULL);
|
|
||||||
}
|
|
||||||
g_object_unref(in);
|
|
||||||
|
|
||||||
VipsImage *shrunk = vips_image_new();
|
VipsImage *shrunk = vips_image_new();
|
||||||
if (shrink > 1) {
|
if (shrink > 1) {
|
||||||
// Use vips_shrink with the integral reduction
|
// Use vips_shrink with the integral reduction
|
||||||
if (vips_shrink(shrunk_on_load, &shrunk, shrink, shrink, NULL)) {
|
if (vips_shrink(shrunk_on_load, &shrunk, shrink, shrink, NULL)) {
|
||||||
return resize_error(baton, shrunk_on_load);
|
return resize_error(baton, shrunk_on_load);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
vips_copy(shrunk_on_load, &shrunk, NULL);
|
|
||||||
}
|
|
||||||
g_object_unref(shrunk_on_load);
|
|
||||||
|
|
||||||
// Use vips_affine with the remaining float part using bilinear interpolation
|
|
||||||
VipsImage *affined = vips_image_new();
|
|
||||||
if (residual != 0) {
|
|
||||||
if (vips_affine(shrunk, &affined, residual, 0, 0, residual, "interpolate", vips_interpolate_bilinear_static(), NULL)) {
|
|
||||||
return resize_error(baton, shrunk);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
vips_copy(shrunk, &affined, NULL);
|
|
||||||
}
|
|
||||||
g_object_unref(shrunk);
|
|
||||||
|
|
||||||
// Crop/embed
|
|
||||||
VipsImage *canvased = vips_image_new();
|
|
||||||
if (affined->Xsize != baton->width || affined->Ysize != baton->height) {
|
|
||||||
if (baton->crop) {
|
|
||||||
// Crop
|
|
||||||
int width = std::min(affined->Xsize, baton->width);
|
|
||||||
int height = std::min(affined->Ysize, baton->height);
|
|
||||||
int left = (affined->Xsize - width + 1) / 2;
|
|
||||||
int top = (affined->Ysize - height + 1) / 2;
|
|
||||||
if (vips_extract_area(affined, &canvased, left, top, width, height, NULL)) {
|
|
||||||
return resize_error(baton, affined);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Embed
|
vips_copy(shrunk_on_load, &shrunk, NULL);
|
||||||
int left = (baton->width - affined->Xsize) / 2;
|
}
|
||||||
int top = (baton->height - affined->Ysize) / 2;
|
g_object_unref(shrunk_on_load);
|
||||||
if (vips_embed(affined, &canvased, left, top, baton->width, baton->height, "extend", baton->extend, NULL)) {
|
|
||||||
return resize_error(baton, affined);
|
// Use vips_affine with the remaining float part using bilinear interpolation
|
||||||
|
VipsImage *affined = vips_image_new();
|
||||||
|
if (residual != 0) {
|
||||||
|
if (vips_affine(shrunk, &affined, residual, 0, 0, residual, "interpolate", vips_interpolate_bilinear_static(), NULL)) {
|
||||||
|
return resize_error(baton, shrunk);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
vips_copy(shrunk, &affined, NULL);
|
||||||
}
|
}
|
||||||
} else {
|
g_object_unref(shrunk);
|
||||||
vips_copy(affined, &canvased, NULL);
|
|
||||||
}
|
|
||||||
g_object_unref(affined);
|
|
||||||
|
|
||||||
// Mild sharpen
|
// Crop/embed
|
||||||
VipsImage *sharpened = vips_image_new();
|
VipsImage *canvased = vips_image_new();
|
||||||
if (baton->sharpen) {
|
if (affined->Xsize != baton->width || affined->Ysize != baton->height) {
|
||||||
INTMASK* sharpen = im_create_imaskv("sharpen", 3, 3,
|
if (baton->crop) {
|
||||||
-1, -1, -1,
|
// Crop
|
||||||
-1, 32, -1,
|
int width = std::min(affined->Xsize, baton->width);
|
||||||
-1, -1, -1);
|
int height = std::min(affined->Ysize, baton->height);
|
||||||
sharpen->scale = 24;
|
int left = (affined->Xsize - width + 1) / 2;
|
||||||
if (im_conv(canvased, sharpened, sharpen)) {
|
int top = (affined->Ysize - height + 1) / 2;
|
||||||
return resize_error(baton, canvased);
|
if (vips_extract_area(affined, &canvased, left, top, width, height, NULL)) {
|
||||||
|
return resize_error(baton, affined);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Embed
|
||||||
|
int left = (baton->width - affined->Xsize) / 2;
|
||||||
|
int top = (baton->height - affined->Ysize) / 2;
|
||||||
|
if (vips_embed(affined, &canvased, left, top, baton->width, baton->height, "extend", baton->extend, NULL)) {
|
||||||
|
return resize_error(baton, affined);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
vips_copy(affined, &canvased, NULL);
|
||||||
}
|
}
|
||||||
} else {
|
g_object_unref(affined);
|
||||||
vips_copy(canvased, &sharpened, NULL);
|
|
||||||
}
|
|
||||||
g_object_unref(canvased);
|
|
||||||
|
|
||||||
// Output
|
// Mild sharpen
|
||||||
if (baton->file_out == "__jpeg" || (baton->file_out == "__input" && inputImageType == JPEG)) {
|
VipsImage *sharpened = vips_image_new();
|
||||||
// Write JPEG to buffer
|
if (baton->sharpen) {
|
||||||
if (vips_jpegsave_buffer(sharpened, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "Q", 80, "optimize_coding", TRUE, "interlace", baton->progessive, NULL)) {
|
INTMASK* sharpen = im_create_imaskv("sharpen", 3, 3,
|
||||||
return resize_error(baton, sharpened);
|
-1, -1, -1,
|
||||||
|
-1, 32, -1,
|
||||||
|
-1, -1, -1);
|
||||||
|
sharpen->scale = 24;
|
||||||
|
if (im_conv(canvased, sharpened, sharpen)) {
|
||||||
|
return resize_error(baton, canvased);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
vips_copy(canvased, &sharpened, NULL);
|
||||||
}
|
}
|
||||||
} else if (baton->file_out == "__png" || (baton->file_out == "__input" && inputImageType == PNG)) {
|
g_object_unref(canvased);
|
||||||
// Write PNG to buffer
|
|
||||||
if (vips_pngsave_buffer(sharpened, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "compression", 6, "interlace", baton->progessive, NULL)) {
|
|
||||||
return resize_error(baton, sharpened);
|
|
||||||
}
|
|
||||||
} else if (baton->file_out == "__webp" || (baton->file_out == "__input" && inputImageType == WEBP)) {
|
|
||||||
// Write WEBP to buffer
|
|
||||||
if (vips_webpsave_buffer(sharpened, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "Q", 80, NULL)) {
|
|
||||||
return resize_error(baton, sharpened);
|
|
||||||
}
|
|
||||||
} else if (is_jpeg(baton->file_out)) {
|
|
||||||
// Write JPEG to file
|
|
||||||
if (vips_jpegsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "Q", 80, "optimize_coding", TRUE, "interlace", baton->progessive, NULL)) {
|
|
||||||
return resize_error(baton, sharpened);
|
|
||||||
}
|
|
||||||
} else if (is_png(baton->file_out)) {
|
|
||||||
// Write PNG to file
|
|
||||||
if (vips_pngsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "compression", 6, "interlace", baton->progessive, NULL)) {
|
|
||||||
return resize_error(baton, sharpened);
|
|
||||||
}
|
|
||||||
} else if (is_webp(baton->file_out)) {
|
|
||||||
// Write WEBP to file
|
|
||||||
if (vips_webpsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "Q", 80, NULL)) {
|
|
||||||
return resize_error(baton, sharpened);
|
|
||||||
}
|
|
||||||
} else if (is_tiff(baton->file_out)) {
|
|
||||||
// Write TIFF to file
|
|
||||||
if (vips_tiffsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "compression", VIPS_FOREIGN_TIFF_COMPRESSION_JPEG, "Q", 80, NULL)) {
|
|
||||||
return resize_error(baton, sharpened);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
(baton->err).append("Unsupported output " + baton->file_out);
|
|
||||||
}
|
|
||||||
g_object_unref(sharpened);
|
|
||||||
vips_thread_shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
void resize_async_after(uv_work_t *work, int status) {
|
// Output
|
||||||
HandleScope scope;
|
if (baton->file_out == "__jpeg" || (baton->file_out == "__input" && inputImageType == JPEG)) {
|
||||||
|
// Write JPEG to buffer
|
||||||
resize_baton *baton = static_cast<resize_baton*>(work->data);
|
if (vips_jpegsave_buffer(sharpened, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "Q", 80, "optimize_coding", TRUE, "interlace", baton->progessive, NULL)) {
|
||||||
|
return resize_error(baton, sharpened);
|
||||||
Handle<Value> argv[2] = { Null(), Null() };
|
}
|
||||||
if (!baton->err.empty()) {
|
} else if (baton->file_out == "__png" || (baton->file_out == "__input" && inputImageType == PNG)) {
|
||||||
// Error
|
// Write PNG to buffer
|
||||||
argv[0] = scope.Close(String::New(baton->err.data(), baton->err.size()));
|
if (vips_pngsave_buffer(sharpened, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "compression", 6, "interlace", baton->progessive, NULL)) {
|
||||||
} else if (baton->buffer_out_len > 0) {
|
return resize_error(baton, sharpened);
|
||||||
// Buffer
|
}
|
||||||
Buffer *slowBuffer = Buffer::New(baton->buffer_out_len);
|
} else if (baton->file_out == "__webp" || (baton->file_out == "__input" && inputImageType == WEBP)) {
|
||||||
memcpy(Buffer::Data(slowBuffer), baton->buffer_out, baton->buffer_out_len);
|
// Write WEBP to buffer
|
||||||
Local<Object> globalObj = Context::GetCurrent()->Global();
|
if (vips_webpsave_buffer(sharpened, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "Q", 80, NULL)) {
|
||||||
Local<Function> bufferConstructor = Local<Function>::Cast(globalObj->Get(String::New("Buffer")));
|
return resize_error(baton, sharpened);
|
||||||
Handle<Value> constructorArgs[3] = { slowBuffer->handle_, v8::Integer::New(baton->buffer_out_len), v8::Integer::New(0) };
|
}
|
||||||
argv[1] = scope.Close(bufferConstructor->NewInstance(3, constructorArgs));
|
} else if (is_jpeg(baton->file_out)) {
|
||||||
g_free(baton->buffer_out);
|
// Write JPEG to file
|
||||||
|
if (vips_jpegsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "Q", 80, "optimize_coding", TRUE, "interlace", baton->progessive, NULL)) {
|
||||||
|
return resize_error(baton, sharpened);
|
||||||
|
}
|
||||||
|
} else if (is_png(baton->file_out)) {
|
||||||
|
// Write PNG to file
|
||||||
|
if (vips_pngsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "compression", 6, "interlace", baton->progessive, NULL)) {
|
||||||
|
return resize_error(baton, sharpened);
|
||||||
|
}
|
||||||
|
} else if (is_webp(baton->file_out)) {
|
||||||
|
// Write WEBP to file
|
||||||
|
if (vips_webpsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "Q", 80, NULL)) {
|
||||||
|
return resize_error(baton, sharpened);
|
||||||
|
}
|
||||||
|
} else if (is_tiff(baton->file_out)) {
|
||||||
|
// Write TIFF to file
|
||||||
|
if (vips_tiffsave(sharpened, baton->file_out.c_str(), "strip", TRUE, "compression", VIPS_FOREIGN_TIFF_COMPRESSION_JPEG, "Q", 80, NULL)) {
|
||||||
|
return resize_error(baton, sharpened);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(baton->err).append("Unsupported output " + baton->file_out);
|
||||||
|
}
|
||||||
|
g_object_unref(sharpened);
|
||||||
|
vips_thread_shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
|
void HandleOKCallback () {
|
||||||
baton->callback.Dispose();
|
NanScope();
|
||||||
delete baton;
|
|
||||||
delete work;
|
|
||||||
}
|
|
||||||
|
|
||||||
Handle<Value> resize(const Arguments& args) {
|
Handle<Value> argv[2] = { Null(), Null() };
|
||||||
HandleScope scope;
|
if (!baton->err.empty()) {
|
||||||
|
// Error
|
||||||
|
argv[0] = String::New(baton->err.data(), baton->err.size());
|
||||||
|
} else if (baton->buffer_out_len > 0) {
|
||||||
|
// Buffer
|
||||||
|
argv[1] = NanNewBufferHandle((char *)baton->buffer_out, baton->buffer_out_len);
|
||||||
|
g_free(baton->buffer_out);
|
||||||
|
}
|
||||||
|
delete baton;
|
||||||
|
callback->Call(2, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
resize_baton* baton;
|
||||||
|
};
|
||||||
|
|
||||||
|
NAN_METHOD(resize) {
|
||||||
|
NanScope();
|
||||||
|
|
||||||
resize_baton *baton = new resize_baton;
|
resize_baton *baton = new resize_baton;
|
||||||
baton->file_in = *String::Utf8Value(args[0]->ToString());
|
baton->file_in = *String::Utf8Value(args[0]->ToString());
|
||||||
@ -338,16 +337,15 @@ Handle<Value> resize(const Arguments& args) {
|
|||||||
baton->sharpen = args[6]->BooleanValue();
|
baton->sharpen = args[6]->BooleanValue();
|
||||||
baton->progessive = args[7]->BooleanValue();
|
baton->progessive = args[7]->BooleanValue();
|
||||||
baton->access_method = args[8]->BooleanValue() ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM;
|
baton->access_method = args[8]->BooleanValue() ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM;
|
||||||
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[9]));
|
|
||||||
|
|
||||||
uv_work_t *work = new uv_work_t;
|
NanCallback *callback = new NanCallback(args[9].As<v8::Function>());
|
||||||
work->data = baton;
|
|
||||||
uv_queue_work(uv_default_loop(), work, resize_async, (uv_after_work_cb)resize_async_after);
|
NanAsyncQueueWorker(new ResizeWorker(callback, baton));
|
||||||
return scope.Close(Undefined());
|
NanReturnUndefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Handle<Value> cache(const Arguments& args) {
|
NAN_METHOD(cache) {
|
||||||
HandleScope scope;
|
NanScope();
|
||||||
|
|
||||||
// Set cache limit
|
// Set cache limit
|
||||||
if (args[0]->IsInt32()) {
|
if (args[0]->IsInt32()) {
|
||||||
@ -359,16 +357,16 @@ Handle<Value> cache(const Arguments& args) {
|
|||||||
cache->Set(String::NewSymbol("current"), Number::New(vips_tracked_get_mem() / 1048576));
|
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("high"), Number::New(vips_tracked_get_mem_highwater() / 1048576));
|
||||||
cache->Set(String::NewSymbol("limit"), Number::New(vips_cache_get_max_mem() / 1048576));
|
cache->Set(String::NewSymbol("limit"), Number::New(vips_cache_get_max_mem() / 1048576));
|
||||||
return scope.Close(cache);
|
NanReturnValue(cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void at_exit(void* arg) {
|
static void at_exit(void* arg) {
|
||||||
HandleScope scope;
|
NanScope();
|
||||||
vips_shutdown();
|
vips_shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void init(Handle<Object> target) {
|
extern "C" void init(Handle<Object> target) {
|
||||||
HandleScope scope;
|
NanScope();
|
||||||
vips_init("");
|
vips_init("");
|
||||||
AtExit(at_exit);
|
AtExit(at_exit);
|
||||||
NODE_SET_METHOD(target, "resize", resize);
|
NODE_SET_METHOD(target, "resize", resize);
|
||||||
|
@ -2,7 +2,7 @@ var sharp = require("../index");
|
|||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var imagemagick = require("imagemagick");
|
var imagemagick = require("imagemagick");
|
||||||
var gm = require("gm");
|
var gm = require("gm");
|
||||||
var epeg = require("epeg");
|
//var epeg = require("epeg");
|
||||||
var async = require("async");
|
var async = require("async");
|
||||||
var assert = require("assert");
|
var assert = require("assert");
|
||||||
var Benchmark = require("benchmark");
|
var Benchmark = require("benchmark");
|
||||||
@ -65,7 +65,8 @@ async.series({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}).add("epeg-file-file", {
|
})
|
||||||
|
/*.add("epeg-file-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
var image = new epeg.Image({path: inputJpg});
|
var image = new epeg.Image({path: inputJpg});
|
||||||
@ -80,7 +81,8 @@ async.series({
|
|||||||
assert.notStrictEqual(null, buffer);
|
assert.notStrictEqual(null, buffer);
|
||||||
deferred.resolve();
|
deferred.resolve();
|
||||||
}
|
}
|
||||||
}).add("sharp-buffer-file", {
|
})*/
|
||||||
|
.add("sharp-buffer-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp(inputJpgBuffer).resize(width, height).write(outputJpg, function(err) {
|
sharp(inputJpgBuffer).resize(width, height).write(outputJpg, function(err) {
|
||||||
|
@ -2,7 +2,7 @@ var sharp = require("../index");
|
|||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var imagemagick = require("imagemagick");
|
var imagemagick = require("imagemagick");
|
||||||
var gm = require("gm");
|
var gm = require("gm");
|
||||||
var epeg = require("epeg");
|
//var epeg = require("epeg");
|
||||||
var async = require("async");
|
var async = require("async");
|
||||||
var assert = require("assert");
|
var assert = require("assert");
|
||||||
var Benchmark = require("benchmark");
|
var Benchmark = require("benchmark");
|
||||||
@ -46,7 +46,7 @@ new Benchmark.Suite("random").add("imagemagick", {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}).add("epeg", {
|
})/*.add("epeg", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
var image = new epeg.Image({path: inputJpg});
|
var image = new epeg.Image({path: inputJpg});
|
||||||
@ -54,7 +54,7 @@ new Benchmark.Suite("random").add("imagemagick", {
|
|||||||
assert.notStrictEqual(null, buffer);
|
assert.notStrictEqual(null, buffer);
|
||||||
deferred.resolve();
|
deferred.resolve();
|
||||||
}
|
}
|
||||||
}).add("sharp", {
|
})*/.add("sharp", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp(inputJpg).resize(randomDimension(), randomDimension()).toBuffer(function(err, buffer) {
|
sharp(inputJpg).resize(randomDimension(), randomDimension()).toBuffer(function(err, buffer) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user