mirror of
https://github.com/lovell/sharp.git
synced 2026-02-04 13:46:19 +01:00
Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1b47ef419 | ||
|
|
0954ca6adf | ||
|
|
c9d7f43bd9 | ||
|
|
481741315d | ||
|
|
cae1dbdb89 | ||
|
|
200d5a9312 | ||
|
|
e9ca25cb45 | ||
|
|
33f24d41e7 | ||
|
|
45d5f12a63 | ||
|
|
8785ca4331 | ||
|
|
1ecdf97bdb | ||
|
|
3703ee41aa | ||
|
|
c8f023d8ba | ||
|
|
fe773733cd | ||
|
|
19bec9346e | ||
|
|
9bd335079f | ||
|
|
b6dc179551 | ||
|
|
e96fd8b9de | ||
|
|
6a2816e917 | ||
|
|
06d88de5a2 | ||
|
|
a292e1fe8e | ||
|
|
08bb35e7af | ||
|
|
0e89a5bbf2 | ||
|
|
2a0d79a78b | ||
|
|
764b57022b | ||
|
|
5f61331d1a | ||
|
|
d0e6a4c0f3 | ||
|
|
f99e42d447 | ||
|
|
9b4387be97 | ||
|
|
9c3631ecb7 | ||
|
|
31ca68fb14 | ||
|
|
d5d85a8697 | ||
|
|
ae9a8b0f57 | ||
|
|
0899252a72 | ||
|
|
16551bc058 | ||
|
|
e9d196f696 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -12,7 +12,6 @@ logs
|
|||||||
results
|
results
|
||||||
build
|
build
|
||||||
node_modules
|
node_modules
|
||||||
tests/output.jpg
|
tests/fixtures/output.*
|
||||||
tests/output.png
|
|
||||||
|
|
||||||
npm-debug.log
|
npm-debug.log
|
||||||
|
|||||||
16
.travis.yml
Normal file
16
.travis.yml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
language: node_js
|
||||||
|
node_js:
|
||||||
|
- "0.10"
|
||||||
|
before_install:
|
||||||
|
- sudo add-apt-repository ppa:lyrasis/precise-backports -y
|
||||||
|
- sudo apt-get update -qq
|
||||||
|
- sudo apt-get install -qq automake gobject-introspection gtk-doc-tools libfftw3-dev libglib2.0-dev libjpeg-turbo8-dev libpng12-dev libwebp-dev libtiff4-dev liborc-0.4-dev libxml2-dev swig graphicsmagick libmagick++-dev
|
||||||
|
- git clone https://github.com/jcupitt/libvips.git
|
||||||
|
- cd libvips
|
||||||
|
- git checkout 7.38
|
||||||
|
- ./bootstrap.sh
|
||||||
|
- ./configure --enable-debug=no --enable-cxx=no --without-python
|
||||||
|
- make
|
||||||
|
- sudo make install
|
||||||
|
- sudo ldconfig
|
||||||
|
- cd $TRAVIS_BUILD_DIR
|
||||||
250
README.md
250
README.md
@@ -1,58 +1,67 @@
|
|||||||
# sharp
|
# sharp
|
||||||
|
|
||||||
_adj_
|
* [Installation](https://github.com/lovell/sharp#installation)
|
||||||
|
* [Usage examples](https://github.com/lovell/sharp#usage-examples)
|
||||||
|
* [API](https://github.com/lovell/sharp#api)
|
||||||
|
* [Testing](https://github.com/lovell/sharp#testing)
|
||||||
|
* [Performance](https://github.com/lovell/sharp#performance)
|
||||||
|
* [Licence](https://github.com/lovell/sharp#licence)
|
||||||
|
|
||||||
1. clearly defined; distinct: a sharp photographic image.
|
The typical use case for this high speed Node.js module is to convert large images of many formats to smaller, web-friendly JPEG, PNG and WebP images of varying dimensions.
|
||||||
2. quick, brisk, or spirited.
|
|
||||||
3. shrewd or astute: a sharp bargainer.
|
|
||||||
4. (Informal.) very stylish: a sharp dresser; a sharp jacket.
|
|
||||||
|
|
||||||
The typical use case for this high speed Node.js module is to convert large JPEG and PNG images to smaller JPEG and PNG images of varying dimensions.
|
The performance of JPEG resizing is typically 8x faster than ImageMagick and GraphicsMagick, based mainly on the number of CPU cores available. Everything remains non-blocking thanks to _libuv_.
|
||||||
|
|
||||||
The performance of JPEG resizing is typically 15x-25x faster than ImageMagick and GraphicsMagick, based mainly on the number of CPU cores available.
|
This module supports reading and writing images of JPEG, PNG and WebP to and from both Buffer objects and the filesystem. It also supports reading images of many other types from the filesystem via libmagick++ or libgraphicsmagick++ if present.
|
||||||
|
|
||||||
Under the hood you'll find the blazingly fast [libvips](https://github.com/jcupitt/libvips) image processing library, originally created in 1989 at Birkbeck College and currently maintained by John Cupitt.
|
When generating JPEG output all metadata is removed and Huffman tables optimised without having to use separate command line tools like [jpegoptim](https://github.com/tjko/jpegoptim) and [jpegtran](http://jpegclub.org/jpegtran/).
|
||||||
|
|
||||||
## Prerequisites
|
Anyone who has used the Node.js bindings for [GraphicsMagick](https://github.com/aheckmann/gm) will find the API similarly fluent.
|
||||||
|
|
||||||
* Node.js v0.8+
|
This module is powered by the blazingly fast [libvips](https://github.com/jcupitt/libvips) image processing library, originally created in 1989 at Birkbeck College and currently maintained by John Cupitt.
|
||||||
* [libvips](https://github.com/jcupitt/libvips) v7.38+
|
|
||||||
|
|
||||||
For the sharpest results, please compile libvips from source.
|
## Installation
|
||||||
|
|
||||||
## Install
|
|
||||||
|
|
||||||
npm install sharp
|
npm install sharp
|
||||||
|
|
||||||
## Usage
|
### Prerequisites
|
||||||
|
|
||||||
var sharp = require("sharp");
|
* Node.js v0.10+
|
||||||
|
* [libvips](https://github.com/jcupitt/libvips) v7.38.5+
|
||||||
|
|
||||||
### resize(input, output, width, height, [options], callback)
|
### Install libvips on Mac OS
|
||||||
|
|
||||||
Scale and crop to `width` x `height` calling `callback` when complete.
|
brew install homebrew/science/vips --with-webp --with-graphicsmagick
|
||||||
|
|
||||||
`input` can either be a filename String or a Buffer. When using a filename libvips will `mmap` the file for improved performance.
|
The _gettext_ dependency of _libvips_ [can lead](https://github.com/lovell/sharp/issues/9) to a `library not found for -lintl` error. If so, please try:
|
||||||
|
|
||||||
`output` can either be a filename String or one of `sharp.buffer.jpeg` or `sharp.buffer.png` to pass a Buffer containing image data to `callback`.
|
brew link gettext --force
|
||||||
|
|
||||||
`width` is the Number of pixels wide the resultant image should be.
|
### Install libvips on Ubuntu/Debian Linux
|
||||||
|
|
||||||
`height` is the Number of pixels high the resultant image should be.
|
sudo apt-get install automake build-essential git gobject-introspection gtk-doc-tools libfftw3-dev libglib2.0-dev libjpeg-turbo8-dev libpng12-dev libwebp-dev libtiff5-dev liborc-0.4-dev libxml2-dev swig
|
||||||
|
git clone https://github.com/jcupitt/libvips.git
|
||||||
|
cd libvips
|
||||||
|
git checkout 7.38
|
||||||
|
./bootstrap.sh
|
||||||
|
./configure --enable-debug=no --enable-cxx=no --without-python
|
||||||
|
make
|
||||||
|
sudo make install
|
||||||
|
sudo ldconfig
|
||||||
|
|
||||||
`options` is optional, and can contain one or more of:
|
Ubuntu 12.04 requires `libtiff4-dev` instead of `libtiff5-dev` and has [a bug](https://bugs.launchpad.net/ubuntu/+source/libwebp/+bug/1108731) in the libwebp package. Work around these problems by running these command first:
|
||||||
|
|
||||||
* `canvas` can be one of `sharp.canvas.crop`, `sharp.canvas.embedWhite` or `sharp.canvas.embedBlack`. Defaults to `sharp.canvas.crop`.
|
sudo add-apt-repository ppa:lyrasis/precise-backports
|
||||||
* `sharpen` when set to true will perform a mild sharpen of the resultant image. This typically reduces performance by 30%.
|
sudo apt-get update
|
||||||
* `progressive` when set will use progressive (interlace) scan for the output. This typically reduces performance by 30%.
|
sudo apt-get install libtiff4-dev
|
||||||
* `sequentialRead` is an advanced setting that, when set, switches the libvips access method to `VIPS_ACCESS_SEQUENTIAL`. This will reduce memory usage and can improve performance on some systems.
|
|
||||||
|
|
||||||
`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.
|
## Usage examples
|
||||||
|
|
||||||
### Examples
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
sharp.resize("input.jpg", "output.jpg", 300, 200, function(err) {
|
var sharp = require('sharp');
|
||||||
|
```
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
sharp('input.jpg').resize(300, 200).write('output.jpg', function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
@@ -62,96 +71,183 @@ sharp.resize("input.jpg", "output.jpg", 300, 200, function(err) {
|
|||||||
```
|
```
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
sharp.resize("input.jpg", sharp.buffer.jpeg, 300, 200, {progressive: true}, function(err, buffer) {
|
sharp('input.jpg').resize(null, 200).progressive().toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
// buffer contains progressive JPEG image data
|
// buffer contains progressive JPEG image data, 200 pixels high
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
sharp.resize("input.jpg", sharp.buffer.png, 300, 200, {sharpen: true}, function(err, buffer) {
|
sharp('input.png').resize(300).sharpen().webp(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
// buffer contains sharpened PNG image data (converted from JPEG)
|
// buffer contains sharpened WebP image data (converted from PNG), 300 pixels wide
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
sharp.resize(buffer, "output.jpg", 200, 300, {canvas: sharp.canvas.embedWhite}, function(err) {
|
sharp(buffer).resize(200, 300).embedWhite().write('output.tiff', function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
// output.jpg is a 200 pixels wide and 300 pixels high image containing a scaled version
|
// output.tiff is a 200 pixels wide and 300 pixels high image containing a scaled
|
||||||
// of the image data contained in buffer embedded on a white canvas
|
// version, embedded on a white canvas, of the image data in buffer
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
sharp.resize("input.jpg", sharp.buffer.jpeg, 200, 300, {canvas: sharp.canvas.embedBlack}, function(err, buffer) {
|
sharp('input.gif').resize(200, 300).embedBlack().webp(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
// buffer contains JPEG image data of a 200 pixels wide and 300 pixels high image
|
// buffer contains WebP image data of a 200 pixels wide and 300 pixels high image
|
||||||
// containing a scaled version of input.png embedded on a black canvas
|
// containing a scaled version, embedded on a black canvas, of input.gif
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### sharp(input)
|
||||||
|
|
||||||
|
Constructor to which further methods are chained. `input` can be one of:
|
||||||
|
|
||||||
|
* Buffer containing JPEG, PNG or WebP image data, or
|
||||||
|
* String containing the filename of an image, with most major formats supported.
|
||||||
|
|
||||||
|
### resize(width, [height])
|
||||||
|
|
||||||
|
Scale to `width` x `height`. By default, the resized image is cropped to the exact size specified.
|
||||||
|
|
||||||
|
`width` is the Number of pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height.
|
||||||
|
|
||||||
|
`height` is the Number of pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width.
|
||||||
|
|
||||||
|
### crop()
|
||||||
|
|
||||||
|
Crop the resized image to the exact size specified, the default behaviour.
|
||||||
|
|
||||||
|
### embedWhite()
|
||||||
|
|
||||||
|
Embed the resized image on a white background of the exact size specified.
|
||||||
|
|
||||||
|
### embedBlack()
|
||||||
|
|
||||||
|
Embed the resized image on a black background of the exact size specified.
|
||||||
|
|
||||||
|
### sharpen()
|
||||||
|
|
||||||
|
Perform a mild sharpen of the resultant image. This typically reduces performance by 30%.
|
||||||
|
|
||||||
|
### progressive()
|
||||||
|
|
||||||
|
Use progressive (interlace) scan for JPEG and PNG output. This typically reduces compression performance by 30% but results in an image that can be rendered sooner when decompressed.
|
||||||
|
|
||||||
|
### sequentialRead()
|
||||||
|
|
||||||
|
An advanced setting that switches the libvips access method to `VIPS_ACCESS_SEQUENTIAL`. This will reduce memory usage and can improve performance on some systems.
|
||||||
|
|
||||||
|
### write(filename, callback)
|
||||||
|
|
||||||
|
`filename` is a String containing the filename to write the image data to. The format is inferred from the extension, with JPEG, PNG, WebP and TIFF supported.
|
||||||
|
|
||||||
|
`callback` is called with a single argument `(err)` containing an error message, if any.
|
||||||
|
|
||||||
|
### jpeg(callback)
|
||||||
|
|
||||||
|
Write JPEG image data to a Buffer.
|
||||||
|
|
||||||
|
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant JPEG image data.
|
||||||
|
|
||||||
|
### png(callback)
|
||||||
|
|
||||||
|
Write PNG image data to a Buffer.
|
||||||
|
|
||||||
|
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant PNG image data.
|
||||||
|
|
||||||
|
### webp(callback)
|
||||||
|
|
||||||
|
Write WebP image data to a Buffer.
|
||||||
|
|
||||||
|
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant WebP image data.
|
||||||
|
|
||||||
|
### toBuffer(callback)
|
||||||
|
|
||||||
|
Write image data to a Buffer, the format of which will match the input image. JPEG, PNG and WebP are supported.
|
||||||
|
|
||||||
|
`callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant image data.
|
||||||
|
|
||||||
|
### sharp.cache([limit])
|
||||||
|
|
||||||
|
If `limit` is provided, set the (soft) limit of _libvips_ working/cache memory to this value in MB. The default value is 100.
|
||||||
|
|
||||||
|
This method always returns cache statistics, useful for determining how much working memory is required for a particular task.
|
||||||
|
|
||||||
|
Warnings such as _Application transferred too many scanlines_ are a good indicator you've set this value too low.
|
||||||
|
|
||||||
|
```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
|
## Testing
|
||||||
|
|
||||||
|
[](https://travis-ci.org/lovell/sharp)
|
||||||
|
|
||||||
npm test
|
npm test
|
||||||
|
|
||||||
|
Running the tests requires both ImageMagick and GraphicsMagick plus one of either libmagick++-dev or libgraphicsmagick++.
|
||||||
|
|
||||||
|
brew install imagemagick
|
||||||
|
brew install graphicsmagick
|
||||||
|
|
||||||
|
sudo apt-get install imagemagick graphicsmagick libmagick++-dev
|
||||||
|
|
||||||
## Performance
|
## Performance
|
||||||
|
|
||||||
Test environment:
|
### Test environment
|
||||||
|
|
||||||
* AMD Athlon 4 core 3.3GHz 512KB L2 CPU 1333 DDR3
|
* Intel Xeon [L5520](http://ark.intel.com/products/40201/Intel-Xeon-Processor-L5520-8M-Cache-2_26-GHz-5_86-GTs-Intel-QPI) 2.27GHz 8MB cache
|
||||||
* libvips 7.37
|
* Ubuntu 13.10
|
||||||
* libjpeg-turbo8 1.3.0
|
* libvips 7.38.5
|
||||||
* libpng 1.6.6
|
|
||||||
* zlib1g 1.2.7
|
|
||||||
|
|
||||||
`-file-buffer` indicates read from file and write to buffer, `-buffer-file` indicates read from buffer and write to file etc.
|
### The contenders
|
||||||
|
|
||||||
`-sharpen`, `-progressive` etc. demonstrate the negative effect of options on performance.
|
* [imagemagick-native](https://github.com/mash/node-imagemagick-native) - Supports Buffers only and blocks main V8 thread whilst processing.
|
||||||
|
* [imagemagick](https://github.com/rsms/node-imagemagick) - Supports filesystem only and "has been unmaintained for a long time".
|
||||||
|
* [gm](https://github.com/aheckmann/gm) - Fully featured wrapper around GraphicsMagick.
|
||||||
|
* sharp - Caching within libvips disabled to ensure a fair comparison.
|
||||||
|
|
||||||
### JPEG
|
### The task
|
||||||
|
|
||||||
* imagemagick x 5.53 ops/sec ±0.62% (31 runs sampled)
|
Decompress a 2725x2225 JPEG image, resize and crop to 720x480, then compress to JPEG.
|
||||||
* gm-file-file x 4.10 ops/sec ±0.41% (25 runs sampled)
|
|
||||||
* gm-file-buffer x 4.10 ops/sec ±0.36% (25 runs sampled)
|
|
||||||
* epeg-file-file x 23.82 ops/sec ±0.18% (60 runs sampled)
|
|
||||||
* epeg-file-buffer x 23.98 ops/sec ±0.16% (61 runs sampled)
|
|
||||||
|
|
||||||
* sharp-buffer-file x 20.76 ops/sec ±0.55% (54 runs sampled)
|
### Results
|
||||||
* sharp-buffer-buffer x 20.90 ops/sec ±0.26% (54 runs sampled)
|
|
||||||
* sharp-file-file x 91.78 ops/sec ±0.38% (88 runs sampled)
|
|
||||||
* sharp-file-buffer x __93.05 ops/sec__ ±0.61% (76 runs sampled)
|
|
||||||
|
|
||||||
* sharp-file-buffer-sharpen x 63.09 ops/sec ±5.58% (63 runs sampled)
|
| Module | Input | Output | Ops/sec | Speed-up |
|
||||||
* sharp-file-buffer-progressive x 61.68 ops/sec ±0.53% (76 runs sampled)
|
| :-------------------- | :----- | :----- | ------: | -------: |
|
||||||
* sharp-file-buffer-sequentialRead x 60.66 ops/sec ±0.38% (75 runs sampled)
|
| imagemagick-native | buffer | buffer | 0.97 | 1 |
|
||||||
|
| imagemagick | file | file | 2.49 | 2.6 |
|
||||||
|
| gm | buffer | file | 3.72 | 3.8 |
|
||||||
|
| gm | buffer | buffer | 3.80 | 3.9 |
|
||||||
|
| gm | file | file | 3.67 | 3.8 |
|
||||||
|
| gm | file | buffer | 3.67 | 3.8 |
|
||||||
|
| sharp | buffer | file | 13.62 | 14.0 |
|
||||||
|
| sharp | buffer | buffer | 12.43 | 12.8 |
|
||||||
|
| sharp | file | file | 13.02 | 13.4 |
|
||||||
|
| sharp | file | buffer | 11.15 | 11.5 |
|
||||||
|
| sharp +sharpen | file | buffer | 10.26 | 10.6 |
|
||||||
|
| sharp +progressive | file | buffer | 9.44 | 9.7 |
|
||||||
|
| sharp +sequentialRead | file | buffer | 11.94 | 12.3 |
|
||||||
|
|
||||||
### PNG
|
You can expect much greater performance with caching enabled (default) and using 16+ core machines.
|
||||||
|
|
||||||
* imagemagick x 4.27 ops/sec ±0.21% (25 runs sampled)
|
|
||||||
* gm-file-file x 8.33 ops/sec ±0.19% (44 runs sampled)
|
|
||||||
* gm-file-buffer x 7.45 ops/sec ±0.16% (40 runs sampled)
|
|
||||||
|
|
||||||
* sharp-buffer-file x 4.94 ops/sec ±118.46% (26 runs sampled)
|
|
||||||
* sharp-buffer-buffer x 12.59 ops/sec ±0.55% (64 runs sampled)
|
|
||||||
* sharp-file-file x 44.06 ops/sec ±6.86% (75 runs sampled)
|
|
||||||
* sharp-file-buffer x __46.29 ops/sec__ ±0.38% (76 runs sampled)
|
|
||||||
|
|
||||||
* sharp-file-buffer-sharpen x 38.86 ops/sec ±0.22% (65 runs sampled)
|
|
||||||
* sharp-file-buffer-progressive x 46.35 ops/sec ±0.20% (76 runs sampled)
|
|
||||||
* sharp-file-buffer-sequentialRead x 29.02 ops/sec ±0.62% (72 runs sampled)
|
|
||||||
|
|
||||||
## Licence
|
## Licence
|
||||||
|
|
||||||
Copyright 2013, 2014 Lovell Fuller
|
Copyright 2013, 2014 Lovell Fuller and Pierre Inglebert
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
|||||||
10
binding.gyp
10
binding.gyp
@@ -3,17 +3,17 @@
|
|||||||
'target_name': 'sharp',
|
'target_name': 'sharp',
|
||||||
'sources': ['src/sharp.cc'],
|
'sources': ['src/sharp.cc'],
|
||||||
'libraries': [
|
'libraries': [
|
||||||
'<!@(PKG_CONFIG_PATH="/usr/local/lib/pkgconfig" pkg-config --libs vips)',
|
'<!@(PKG_CONFIG_PATH="/usr/local/Library/ENV/pkgconfig/10.8:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig" pkg-config --libs vips)'
|
||||||
'<!@(PKG_CONFIG_PATH="/usr/lib/pkgconfig" pkg-config --libs vips)'
|
|
||||||
],
|
],
|
||||||
'include_dirs': [
|
'include_dirs': [
|
||||||
'/usr/local/include/glib-2.0',
|
'/usr/local/include/glib-2.0',
|
||||||
'/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', '-O3'],
|
'cflags': ['-fexceptions', '-pedantic', '-Wall', '-O3'],
|
||||||
'cflags_cc': ['-fexceptions', '-O3']
|
'cflags_cc': ['-fexceptions', '-pedantic', '-Wall', '-O3']
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|||||||
168
index.js
168
index.js
@@ -1,64 +1,128 @@
|
|||||||
var sharp = require("./build/Release/sharp");
|
/*jslint node: true */
|
||||||
|
'use strict';
|
||||||
|
|
||||||
module.exports.buffer = {
|
var sharp = require('./build/Release/sharp');
|
||||||
jpeg: "__jpeg",
|
|
||||||
png: "__png"
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.canvas = {
|
var Sharp = function(input) {
|
||||||
crop: "c",
|
if (!(this instanceof Sharp)) {
|
||||||
embedWhite: "w",
|
return new Sharp(input);
|
||||||
embedBlack: "b"
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.resize = function(input, output, width, height, options, callback) {
|
|
||||||
"use strict";
|
|
||||||
if (typeof options === 'function') {
|
|
||||||
callback = options;
|
|
||||||
options = {};
|
|
||||||
} else {
|
|
||||||
options = options || {};
|
|
||||||
}
|
}
|
||||||
|
this.options = {
|
||||||
|
width: -1,
|
||||||
|
height: -1,
|
||||||
|
canvas: 'c',
|
||||||
|
sharpen: false,
|
||||||
|
progressive: false,
|
||||||
|
sequentialRead: false,
|
||||||
|
output: '__jpeg'
|
||||||
|
};
|
||||||
if (typeof input === 'string') {
|
if (typeof input === 'string') {
|
||||||
options.inFile = input;
|
this.options.inFile = input;
|
||||||
} else if (typeof input ==='object' && input instanceof Buffer) {
|
} else if (typeof input ==='object' && input instanceof Buffer) {
|
||||||
options.inBuffer = input;
|
this.options.inBuffer = input;
|
||||||
} else {
|
} else {
|
||||||
callback("Unsupported input " + typeof input);
|
throw 'Unsupported input ' + typeof input;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (!output || output.length === 0) {
|
return this;
|
||||||
callback("Invalid output");
|
};
|
||||||
return;
|
module.exports = Sharp;
|
||||||
}
|
|
||||||
var outWidth = Number(width);
|
Sharp.prototype.crop = function() {
|
||||||
if (Number.isNaN(outWidth)) {
|
this.options.canvas = 'c';
|
||||||
callback("Invalid width " + width);
|
return this;
|
||||||
return;
|
|
||||||
}
|
|
||||||
var outHeight = Number(height);
|
|
||||||
if (Number.isNaN(outHeight)) {
|
|
||||||
callback("Invalid height " + height);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var canvas = options.canvas || "c";
|
|
||||||
if (canvas.length !== 1 || "cwb".indexOf(canvas) === -1) {
|
|
||||||
callback("Invalid canvas " + canvas);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var sharpen = !!options.sharpen;
|
|
||||||
var progessive = !!options.progessive;
|
|
||||||
var sequentialRead = !!options.sequentialRead;
|
|
||||||
sharp.resize(options.inFile, options.inBuffer, output, width, height, canvas, sharpen, progessive, sequentialRead, callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Deprecated v0.0.x methods */
|
Sharp.prototype.embedWhite = function() {
|
||||||
module.exports.crop = function(input, output, width, height, sharpen, callback) {
|
this.options.canvas = 'w';
|
||||||
sharp.resize(input, output, width, height, {canvas: "c", sharpen: true}, callback);
|
return this;
|
||||||
};
|
};
|
||||||
module.exports.embedWhite = function(input, output, width, height, callback) {
|
|
||||||
sharp.resize(input, output, width, height, {canvas: "w", sharpen: true}, callback);
|
Sharp.prototype.embedBlack = function() {
|
||||||
|
this.options.canvas = 'b';
|
||||||
|
return this;
|
||||||
};
|
};
|
||||||
module.exports.embedBlack = function(input, output, width, height, callback) {
|
|
||||||
sharp.resize(input, output, width, height, {canvas: "b", sharpen: true}, callback);
|
Sharp.prototype.sharpen = function(sharpen) {
|
||||||
|
this.options.sharpen = (typeof sharpen === 'boolean') ? sharpen : true;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Sharp.prototype.progressive = function(progressive) {
|
||||||
|
this.options.progressive = (typeof progressive === 'boolean') ? progressive : true;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Sharp.prototype.sequentialRead = function(sequentialRead) {
|
||||||
|
this.options.sequentialRead = (typeof sequentialRead === 'boolean') ? sequentialRead : true;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Sharp.prototype.resize = function(width, height) {
|
||||||
|
if (!width) {
|
||||||
|
this.options.width = -1;
|
||||||
|
} else {
|
||||||
|
if (!Number.isNaN(width)) {
|
||||||
|
this.options.width = width;
|
||||||
|
} else {
|
||||||
|
throw 'Invalid width ' + width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!height) {
|
||||||
|
this.options.height = -1;
|
||||||
|
} else {
|
||||||
|
if (!Number.isNaN(height)) {
|
||||||
|
this.options.height = height;
|
||||||
|
} else {
|
||||||
|
throw 'Invalid height ' + height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Sharp.prototype.write = function(output, callback) {
|
||||||
|
if (!output || output.length === 0) {
|
||||||
|
throw 'Invalid output';
|
||||||
|
} else {
|
||||||
|
this._sharp(output, callback);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Sharp.prototype.toBuffer = function(callback) {
|
||||||
|
return this._sharp('__input', callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Sharp.prototype.jpeg = function(callback) {
|
||||||
|
return this._sharp('__jpeg', callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Sharp.prototype.png = function(callback) {
|
||||||
|
return this._sharp('__png', callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Sharp.prototype.webp = function(callback) {
|
||||||
|
return this._sharp('__webp', callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Sharp.prototype._sharp = function(output, callback) {
|
||||||
|
sharp.resize(
|
||||||
|
this.options.inFile,
|
||||||
|
this.options.inBuffer,
|
||||||
|
output,
|
||||||
|
this.options.width,
|
||||||
|
this.options.height,
|
||||||
|
this.options.canvas,
|
||||||
|
this.options.sharpen,
|
||||||
|
this.options.progressive,
|
||||||
|
this.options.sequentialRead,
|
||||||
|
callback
|
||||||
|
);
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.cache = function(limit) {
|
||||||
|
if (Number.isNaN(limit)) {
|
||||||
|
limit = null;
|
||||||
|
}
|
||||||
|
return sharp.cache(limit);
|
||||||
};
|
};
|
||||||
|
|||||||
32
package.json
32
package.json
@@ -1,10 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "sharp",
|
"name": "sharp",
|
||||||
"version": "0.1.3",
|
"version": "0.3.0",
|
||||||
"author": "Lovell Fuller",
|
"author": "Lovell Fuller <npm@lovell.info>",
|
||||||
"description": "High performance module to resize JPEG and PNG images using the libvips image processing library",
|
"contributors": [
|
||||||
|
"Pierre Inglebert <pierre.inglebert@gmail.com>"
|
||||||
|
],
|
||||||
|
"description": "High performance Node.js module to resize JPEG, PNG and WebP images using the libvips library",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node tests/perf.js"
|
"test": "node tests/unit && node tests/perf"
|
||||||
},
|
},
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -14,24 +17,31 @@
|
|||||||
"keywords": [
|
"keywords": [
|
||||||
"jpeg",
|
"jpeg",
|
||||||
"png",
|
"png",
|
||||||
|
"webp",
|
||||||
|
"tiff",
|
||||||
|
"gif",
|
||||||
"resize",
|
"resize",
|
||||||
"thumbnail",
|
"thumbnail",
|
||||||
"sharpen",
|
"sharpen",
|
||||||
"crop",
|
"crop",
|
||||||
"embed",
|
"embed",
|
||||||
"libvips",
|
"libvips",
|
||||||
|
"vips",
|
||||||
"fast",
|
"fast",
|
||||||
"buffer"
|
"buffer"
|
||||||
],
|
],
|
||||||
|
"dependencies": {
|
||||||
|
"nan": "^0.8.0"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"imagemagick": "*",
|
"imagemagick": "^0.1.3",
|
||||||
"gm": "*",
|
"imagemagick-native": "^0.2.9",
|
||||||
"epeg": "*",
|
"gm": "^1.14.2",
|
||||||
"async": "*",
|
"async": "^0.6.2",
|
||||||
"benchmark": "*"
|
"benchmark": "^1.0.0"
|
||||||
},
|
},
|
||||||
"license": "Apache 2.0",
|
"license": "Apache 2.0",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.8"
|
"node": ">=0.10"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
427
src/sharp.cc
427
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;
|
||||||
|
|
||||||
@@ -15,37 +17,48 @@ struct resize_baton {
|
|||||||
std::string file_out;
|
std::string file_out;
|
||||||
void* buffer_out;
|
void* buffer_out;
|
||||||
size_t buffer_out_len;
|
size_t buffer_out_len;
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
bool crop;
|
bool crop;
|
||||||
VipsExtend extend;
|
VipsExtend extend;
|
||||||
bool sharpen;
|
bool sharpen;
|
||||||
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) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
JPEG,
|
JPEG,
|
||||||
PNG
|
PNG,
|
||||||
|
WEBP,
|
||||||
|
TIFF,
|
||||||
|
MAGICK
|
||||||
} ImageType;
|
} ImageType;
|
||||||
|
|
||||||
unsigned char MARKER_JPEG[] = {0xff, 0xd8};
|
unsigned char MARKER_JPEG[] = {0xff, 0xd8};
|
||||||
unsigned char MARKER_PNG[] = {0x89, 0x50};
|
unsigned char MARKER_PNG[] = {0x89, 0x50};
|
||||||
|
unsigned char MARKER_WEBP[] = {0x52, 0x49};
|
||||||
|
|
||||||
bool ends_with(std::string const &str, std::string const &end) {
|
bool ends_with(std::string const &str, std::string const &end) {
|
||||||
return str.length() >= end.length() && 0 == str.compare(str.length() - end.length(), end.length(), end);
|
return str.length() >= end.length() && 0 == str.compare(str.length() - end.length(), end.length(), end);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_jpeg(std::string const &str) {
|
bool is_jpeg(std::string const &str) {
|
||||||
return ends_with(str, ".jpg") || ends_with(str, ".jpeg");
|
return ends_with(str, ".jpg") || ends_with(str, ".jpeg") || ends_with(str, ".JPG") || ends_with(str, ".JPEG");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_png(std::string const &str) {
|
bool is_png(std::string const &str) {
|
||||||
return ends_with(str, ".png");
|
return ends_with(str, ".png") || ends_with(str, ".PNG");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_webp(std::string const &str) {
|
||||||
|
return ends_with(str, ".webp") || ends_with(str, ".WEBP");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_tiff(std::string const &str) {
|
||||||
|
return ends_with(str, ".tif") || ends_with(str, ".tiff") || ends_with(str, ".TIF") || ends_with(str, ".TIFF");
|
||||||
}
|
}
|
||||||
|
|
||||||
void resize_error(resize_baton *baton, VipsImage *unref) {
|
void resize_error(resize_baton *baton, VipsImage *unref) {
|
||||||
@@ -56,201 +69,263 @@ 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() {}
|
||||||
|
|
||||||
ImageType inputImageType = JPEG;
|
void Execute () {
|
||||||
VipsImage *in = vips_image_new();
|
// Input
|
||||||
if (baton->buffer_in_len > 1) {
|
ImageType inputImageType = JPEG;
|
||||||
if (memcmp(MARKER_JPEG, baton->buffer_in, 2) == 0) {
|
VipsImage *in = vips_image_new();
|
||||||
if (vips_jpegload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "access", baton->access_method, NULL)) {
|
if (baton->buffer_in_len > 1) {
|
||||||
|
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 (vips_foreign_is_a("jpegload", baton->file_in.c_str())) {
|
||||||
|
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 (vips_foreign_is_a("pngload", baton->file_in.c_str())) {
|
||||||
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 (vips_foreign_is_a("webpload", baton->file_in.c_str())) {
|
||||||
|
inputImageType = WEBP;
|
||||||
|
if (vips_webpload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
||||||
|
return resize_error(baton, in);
|
||||||
|
}
|
||||||
|
} else if (vips_foreign_is_a("tiffload", baton->file_in.c_str())) {
|
||||||
|
inputImageType = TIFF;
|
||||||
|
if (vips_tiffload((baton->file_in).c_str(), &in, "access", baton->access_method, NULL)) {
|
||||||
|
return resize_error(baton, in);
|
||||||
|
}
|
||||||
|
} else if(vips_foreign_is_a("magickload", (baton->file_in).c_str())) {
|
||||||
|
inputImageType = MAGICK;
|
||||||
|
if (vips_magickload((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;
|
||||||
}
|
}
|
||||||
} 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 {
|
|
||||||
resize_error(baton, in);
|
|
||||||
(baton->err).append("Unsupported input " + baton->file_in);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
double xfactor = static_cast<double>(in->Xsize) / std::max(baton->width, 1);
|
// Scaling calculations
|
||||||
double yfactor = static_cast<double>(in->Ysize) / std::max(baton->height, 1);
|
double factor;
|
||||||
double factor = baton->crop ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor);
|
if (baton->width > 0 && baton->height > 0) {
|
||||||
factor = std::max(factor, 1.0);
|
// Fixed width and height
|
||||||
int shrink = floor(factor);
|
double xfactor = (double)(in->Xsize) / (double)(baton->width);
|
||||||
double residual = shrink / factor;
|
double yfactor = (double)(in->Ysize) / (double)(baton->height);
|
||||||
|
factor = baton->crop ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor);
|
||||||
// Try to use libjpeg shrink-on-load
|
} else if (baton->width > 0) {
|
||||||
int shrink_on_load = 1;
|
// Fixed width, auto height
|
||||||
if (inputImageType == JPEG) {
|
factor = (double)(in->Xsize) / (double)(baton->width);
|
||||||
if (shrink >= 8) {
|
baton->height = floor((double)(in->Ysize) / factor);
|
||||||
factor = factor / 8;
|
} else if (baton->height > 0) {
|
||||||
shrink_on_load = 8;
|
// Fixed height, auto width
|
||||||
} else if (shrink >= 4) {
|
factor = (double)(in->Ysize) / (double)(baton->height);
|
||||||
factor = factor / 4;
|
baton->width = floor((double)(in->Xsize) / factor);
|
||||||
shrink_on_load = 4;
|
} else {
|
||||||
} else if (shrink >= 2) {
|
// Identity transform
|
||||||
factor = factor / 2;
|
factor = 1;
|
||||||
shrink_on_load = 2;
|
baton->width = in->Xsize;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VipsImage *shrunk_on_load = vips_image_new();
|
||||||
if (shrink_on_load > 1) {
|
if (shrink_on_load > 1) {
|
||||||
// Recalculate integral shrink and double residual
|
// Recalculate integral shrink and double residual
|
||||||
factor = std::max(factor, 1.0);
|
factor = std::max(factor, 1.0);
|
||||||
shrink = floor(factor);
|
shrink = floor(factor);
|
||||||
residual = shrink / factor;
|
residual = shrink / factor;
|
||||||
// Reload input using shrink-on-load
|
// Reload input using shrink-on-load
|
||||||
g_object_unref(in);
|
|
||||||
in = vips_image_new();
|
|
||||||
if (baton->buffer_in_len > 1) {
|
if (baton->buffer_in_len > 1) {
|
||||||
if (vips_jpegload_buffer(baton->buffer_in, baton->buffer_in_len, &in, "shrink", shrink_on_load, NULL)) {
|
if (vips_jpegload_buffer(baton->buffer_in, baton->buffer_in_len, &shrunk_on_load, "shrink", shrink_on_load, NULL)) {
|
||||||
return resize_error(baton, in);
|
return resize_error(baton, in);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (vips_jpegload((baton->file_in).c_str(), &in, "shrink", shrink_on_load, NULL)) {
|
if (vips_jpegload((baton->file_in).c_str(), &shrunk_on_load, "shrink", shrink_on_load, NULL)) {
|
||||||
return resize_error(baton, in);
|
return resize_error(baton, in);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
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(in, &shrunk, shrink, shrink, NULL)) {
|
if (vips_shrink(shrunk_on_load, &shrunk, shrink, shrink, NULL)) {
|
||||||
return resize_error(baton, in);
|
return resize_error(baton, shrunk_on_load);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
vips_copy(in, &shrunk, NULL);
|
|
||||||
}
|
|
||||||
g_object_unref(in);
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (baton->file_out == "__jpeg") {
|
// Mild sharpen
|
||||||
// Write JPEG to buffer
|
VipsImage *sharpened = vips_image_new();
|
||||||
if (vips_jpegsave_buffer(canvased, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "Q", 80, "optimize_coding", TRUE, "interlace", baton->progessive, NULL)) {
|
if (baton->sharpen) {
|
||||||
return resize_error(baton, canvased);
|
INTMASK* sharpen = im_create_imaskv("sharpen", 3, 3,
|
||||||
|
-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") {
|
g_object_unref(canvased);
|
||||||
// Write PNG to buffer
|
|
||||||
if (vips_pngsave_buffer(canvased, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "compression", 6, "interlace", baton->progessive, NULL)) {
|
|
||||||
return resize_error(baton, canvased);
|
|
||||||
}
|
|
||||||
} else if (is_jpeg(baton->file_out)) {
|
|
||||||
// Write JPEG to file
|
|
||||||
if (vips_jpegsave(canvased, baton->file_out.c_str(), "strip", TRUE, "Q", 80, "optimize_coding", TRUE, "interlace", baton->progessive, NULL)) {
|
|
||||||
return resize_error(baton, canvased);
|
|
||||||
}
|
|
||||||
} else if (is_png(baton->file_out)) {
|
|
||||||
// Write PNG to file
|
|
||||||
if (vips_pngsave(canvased, baton->file_out.c_str(), "strip", TRUE, "compression", 6, "interlace", baton->progessive, NULL)) {
|
|
||||||
return resize_error(baton, canvased);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
(baton->err).append("Unsupported output " + baton->file_out);
|
|
||||||
}
|
|
||||||
g_object_unref(canvased);
|
|
||||||
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] = 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] = 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());
|
||||||
if (args[1]->IsObject()) {
|
if (args[1]->IsObject()) {
|
||||||
Local<Object> buffer = args[1]->ToObject();
|
Local<Object> buffer = args[1]->ToObject();
|
||||||
baton->buffer_in = Buffer::Data(buffer);
|
|
||||||
baton->buffer_in_len = Buffer::Length(buffer);
|
baton->buffer_in_len = Buffer::Length(buffer);
|
||||||
|
baton->buffer_in = Buffer::Data(buffer);
|
||||||
}
|
}
|
||||||
baton->file_out = *String::Utf8Value(args[2]->ToString());
|
baton->file_out = *String::Utf8Value(args[2]->ToString());
|
||||||
baton->width = args[3]->Int32Value();
|
baton->width = args[3]->Int32Value();
|
||||||
@@ -268,24 +343,40 @@ 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
NAN_METHOD(cache) {
|
||||||
|
NanScope();
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
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);
|
||||||
};
|
NODE_SET_METHOD(target, "cache", cache);
|
||||||
|
}
|
||||||
|
|
||||||
NODE_MODULE(sharp, init);
|
NODE_MODULE(sharp, init)
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 813 KiB After Width: | Height: | Size: 813 KiB |
BIN
tests/fixtures/4.webp
vendored
Normal file
BIN
tests/fixtures/4.webp
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 173 KiB |
|
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 84 KiB |
BIN
tests/fixtures/Crash_test.gif
vendored
Normal file
BIN
tests/fixtures/Crash_test.gif
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 278 KiB |
BIN
tests/fixtures/G31D.TIF
vendored
Normal file
BIN
tests/fixtures/G31D.TIF
vendored
Normal file
Binary file not shown.
@@ -1,9 +1,10 @@
|
|||||||
var sharp = require("../index");
|
var sharp = require("../index");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
var path = require("path");
|
||||||
var assert = require("assert");
|
var assert = require("assert");
|
||||||
var async = require("async");
|
var async = require("async");
|
||||||
|
|
||||||
var inputJpg = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/
|
var inputJpg = path.join(__dirname, "fixtures/2569067123_aca715a2ee_o.jpg"); // http://www.flickr.com/photos/grizdave/2569067123/
|
||||||
var width = 720;
|
var width = 720;
|
||||||
var height = 480;
|
var height = 480;
|
||||||
|
|
||||||
@@ -11,7 +12,7 @@ async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) {
|
|||||||
var start = new Date().getTime();
|
var start = new Date().getTime();
|
||||||
async.times(parallelism,
|
async.times(parallelism,
|
||||||
function(id, callback) {
|
function(id, callback) {
|
||||||
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, function(err, buffer) {
|
sharp(inputJpg).resize(width, height).toBuffer(function(err, buffer) {
|
||||||
buffer = null;
|
buffer = null;
|
||||||
callback(err, new Date().getTime() - start);
|
callback(err, new Date().getTime() - start);
|
||||||
});
|
});
|
||||||
@@ -26,4 +27,6 @@ async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) {
|
|||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}, function() {});
|
}, function() {
|
||||||
|
console.dir(sharp.cache());
|
||||||
|
});
|
||||||
|
|||||||
282
tests/perf.js
282
tests/perf.js
@@ -1,25 +1,39 @@
|
|||||||
var sharp = require("../index");
|
var sharp = require("../index");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
var path = require("path");
|
||||||
var imagemagick = require("imagemagick");
|
var imagemagick = require("imagemagick");
|
||||||
|
var imagemagickNative = require("imagemagick-native");
|
||||||
var gm = require("gm");
|
var gm = require("gm");
|
||||||
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");
|
||||||
|
|
||||||
var inputJpg = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/
|
var fixturesPath = path.join(__dirname, "fixtures");
|
||||||
var outputJpg = __dirname + "/output.jpg";
|
|
||||||
|
|
||||||
var inputPng = __dirname + "/50020484-00001.png"; // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png
|
var inputJpg = path.join(fixturesPath, "2569067123_aca715a2ee_o.jpg"); // http://www.flickr.com/photos/grizdave/2569067123/
|
||||||
var outputPng = __dirname + "/output.png";
|
var outputJpg = path.join(fixturesPath, "output.jpg");
|
||||||
|
|
||||||
|
var inputPng = path.join(fixturesPath, "50020484-00001.png"); // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png
|
||||||
|
var outputPng = path.join(fixturesPath, "output.png");
|
||||||
|
|
||||||
|
var inputWebp = path.join(fixturesPath, "4.webp"); // http://www.gstatic.com/webp/gallery/4.webp
|
||||||
|
var outputWebp = path.join(fixturesPath, "output.webp");
|
||||||
|
|
||||||
|
var inputTiff = path.join(fixturesPath, "G31D.TIF"); // http://www.fileformat.info/format/tiff/sample/e6c9a6e5253348f4aef6d17b534360ab/index.htm
|
||||||
|
var outputTiff = path.join(fixturesPath, "output.tiff");
|
||||||
|
|
||||||
|
var inputGif = path.join(fixturesPath, "Crash_test.gif"); // http://upload.wikimedia.org/wikipedia/commons/e/e3/Crash_test.gif
|
||||||
|
|
||||||
var width = 720;
|
var width = 720;
|
||||||
var height = 480;
|
var height = 480;
|
||||||
|
|
||||||
|
// Disable libvips cache to ensure tests are as fair as they can be
|
||||||
|
sharp.cache(0);
|
||||||
|
|
||||||
async.series({
|
async.series({
|
||||||
jpeg: function(callback) {
|
jpeg: function(callback) {
|
||||||
var inputJpgBuffer = fs.readFileSync(inputJpg);
|
var inputJpgBuffer = fs.readFileSync(inputJpg);
|
||||||
(new Benchmark.Suite("jpeg")).add("imagemagick", {
|
(new Benchmark.Suite("jpeg")).add("imagemagick-file-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
imagemagick.resize({
|
imagemagick.resize({
|
||||||
@@ -36,6 +50,41 @@ async.series({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}).add("imagemagick-native-buffer-buffer", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
imagemagickNative.convert({
|
||||||
|
srcData: inputJpgBuffer,
|
||||||
|
quality: 80,
|
||||||
|
width: width,
|
||||||
|
height: height,
|
||||||
|
format: 'JPEG'
|
||||||
|
});
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
}).add("gm-buffer-file", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
gm(inputJpgBuffer).resize(width, height).quality(80).write(outputJpg, function (err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("gm-buffer-buffer", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
gm(inputJpgBuffer).resize(width, height).quality(80).toBuffer(function (err, buffer) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
assert.notStrictEqual(null, buffer);
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}).add("gm-file-file", {
|
}).add("gm-file-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
@@ -59,25 +108,10 @@ async.series({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}).add("epeg-file-file", {
|
|
||||||
defer: true,
|
|
||||||
fn: function(deferred) {
|
|
||||||
var image = new epeg.Image({path: inputJpg});
|
|
||||||
image.downsize(width, height, 80).saveTo(outputJpg);
|
|
||||||
deferred.resolve();
|
|
||||||
}
|
|
||||||
}).add("epeg-file-buffer", {
|
|
||||||
defer: true,
|
|
||||||
fn: function(deferred) {
|
|
||||||
var image = new epeg.Image({path: inputJpg});
|
|
||||||
var buffer = image.downsize(width, height, 80).process();
|
|
||||||
assert.notStrictEqual(null, buffer);
|
|
||||||
deferred.resolve();
|
|
||||||
}
|
|
||||||
}).add("sharp-buffer-file", {
|
}).add("sharp-buffer-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputJpgBuffer, outputJpg, width, height, function(err) {
|
sharp(inputJpgBuffer).resize(width, height).write(outputJpg, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -88,7 +122,7 @@ async.series({
|
|||||||
}).add("sharp-buffer-buffer", {
|
}).add("sharp-buffer-buffer", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputJpgBuffer, sharp.buffer.jpeg, width, height, function(err, buffer) {
|
sharp(inputJpgBuffer).resize(width, height).toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -100,7 +134,7 @@ async.series({
|
|||||||
}).add("sharp-file-file", {
|
}).add("sharp-file-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputJpg, outputJpg, width, height, function(err) {
|
sharp(inputJpg).resize(width, height).write(outputJpg, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -111,7 +145,7 @@ async.series({
|
|||||||
}).add("sharp-file-buffer", {
|
}).add("sharp-file-buffer", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, function(err, buffer) {
|
sharp(inputJpg).resize(width, height).toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -123,7 +157,7 @@ async.series({
|
|||||||
}).add("sharp-file-buffer-sharpen", {
|
}).add("sharp-file-buffer-sharpen", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, {sharpen: true}, function(err, buffer) {
|
sharp(inputJpg).resize(width, height).sharpen().toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -135,7 +169,7 @@ async.series({
|
|||||||
}).add("sharp-file-buffer-progressive", {
|
}).add("sharp-file-buffer-progressive", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, {progressive: true}, function(err, buffer) {
|
sharp(inputJpg).resize(width, height).progressive().toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -147,7 +181,7 @@ async.series({
|
|||||||
}).add("sharp-file-buffer-sequentialRead", {
|
}).add("sharp-file-buffer-sequentialRead", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputJpg, sharp.buffer.jpeg, width, height, {sequentialRead: true}, function(err, buffer) {
|
sharp(inputJpg).resize(width, height).sequentialRead().toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -164,7 +198,7 @@ async.series({
|
|||||||
},
|
},
|
||||||
png: function(callback) {
|
png: function(callback) {
|
||||||
var inputPngBuffer = fs.readFileSync(inputPng);
|
var inputPngBuffer = fs.readFileSync(inputPng);
|
||||||
(new Benchmark.Suite("png")).add("imagemagick", {
|
(new Benchmark.Suite("png")).add("imagemagick-file-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
imagemagick.resize({
|
imagemagick.resize({
|
||||||
@@ -180,6 +214,17 @@ async.series({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}).add("imagemagick-native-buffer-buffer", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
imagemagickNative.convert({
|
||||||
|
srcData: inputPngBuffer,
|
||||||
|
width: width,
|
||||||
|
height: height,
|
||||||
|
format: 'PNG'
|
||||||
|
});
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
}).add("gm-file-file", {
|
}).add("gm-file-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
@@ -206,7 +251,7 @@ async.series({
|
|||||||
}).add("sharp-buffer-file", {
|
}).add("sharp-buffer-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputPngBuffer, outputPng, width, height, function(err) {
|
sharp(inputPngBuffer).resize(width, height).write(outputPng, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -217,7 +262,7 @@ async.series({
|
|||||||
}).add("sharp-buffer-buffer", {
|
}).add("sharp-buffer-buffer", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputPngBuffer, sharp.buffer.png, width, height, function(err, buffer) {
|
sharp(inputPngBuffer).resize(width, height).toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -229,7 +274,7 @@ async.series({
|
|||||||
}).add("sharp-file-file", {
|
}).add("sharp-file-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputPng, outputPng, width, height, function(err) {
|
sharp(inputPng).resize(width, height).write(outputPng, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -240,7 +285,7 @@ async.series({
|
|||||||
}).add("sharp-file-buffer", {
|
}).add("sharp-file-buffer", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputPng, sharp.buffer.png, width, height, function(err, buffer) {
|
sharp(inputPng).resize(width, height).toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -252,7 +297,7 @@ async.series({
|
|||||||
}).add("sharp-file-buffer-sharpen", {
|
}).add("sharp-file-buffer-sharpen", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputPng, sharp.buffer.png, width, height, {sharpen: true}, function(err, buffer) {
|
sharp(inputPng).resize(width, height).sharpen().toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -264,7 +309,7 @@ async.series({
|
|||||||
}).add("sharp-file-buffer-progressive", {
|
}).add("sharp-file-buffer-progressive", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputPng, sharp.buffer.png, width, height, {progressive: true}, function(err, buffer) {
|
sharp(inputPng).resize(width, height).progressive().toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -276,7 +321,7 @@ async.series({
|
|||||||
}).add("sharp-file-buffer-sequentialRead", {
|
}).add("sharp-file-buffer-sequentialRead", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
sharp.resize(inputPng, sharp.buffer.png, width, height, {sequentialRead: true}, function(err, buffer) {
|
sharp(inputPng).sequentialRead().resize(width, height).toBuffer(function(err, buffer) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
} else {
|
||||||
@@ -290,10 +335,171 @@ async.series({
|
|||||||
}).on("complete", function() {
|
}).on("complete", function() {
|
||||||
callback(null, this.filter("fastest").pluck("name"));
|
callback(null, this.filter("fastest").pluck("name"));
|
||||||
}).run();
|
}).run();
|
||||||
}
|
},
|
||||||
|
webp: function(callback) {
|
||||||
|
var inputWebpBuffer = fs.readFileSync(inputWebp);
|
||||||
|
(new Benchmark.Suite("webp")).add("sharp-buffer-file", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputWebpBuffer).resize(width, height).write(outputWebp, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("sharp-buffer-buffer", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputWebpBuffer).resize(width, height).toBuffer(function(err, buffer) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
assert.notStrictEqual(null, buffer);
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("sharp-file-file", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputWebp).resize(width, height).write(outputWebp, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("sharp-file-buffer", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputWebp).resize(width, height).toBuffer(function(err, buffer) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
assert.notStrictEqual(null, buffer);
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("sharp-file-buffer-sharpen", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputWebp).resize(width, height).sharpen().toBuffer(function(err, buffer) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
assert.notStrictEqual(null, buffer);
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("sharp-file-buffer-sequentialRead", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputWebp).sequentialRead().resize(width, height).toBuffer(function(err, buffer) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
assert.notStrictEqual(null, buffer);
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).on("cycle", function(event) {
|
||||||
|
console.log("webp " + String(event.target));
|
||||||
|
}).on("complete", function() {
|
||||||
|
callback(null, this.filter("fastest").pluck("name"));
|
||||||
|
}).run();
|
||||||
|
},
|
||||||
|
tiff: function(callback) {
|
||||||
|
(new Benchmark.Suite("tiff")).add("sharp-file-file", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputTiff).resize(width, height).write(outputTiff, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("sharp-file-file-sharpen", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputTiff).resize(width, height).sharpen().write(outputTiff, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("sharp-file-file-sequentialRead", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputTiff).sequentialRead().resize(width, height).write(outputTiff, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).on("cycle", function(event) {
|
||||||
|
console.log("tiff " + String(event.target));
|
||||||
|
}).on("complete", function() {
|
||||||
|
callback(null, this.filter("fastest").pluck("name"));
|
||||||
|
}).run();
|
||||||
|
},
|
||||||
|
gif: function(callback) {
|
||||||
|
(new Benchmark.Suite("gif")).add("sharp-file-file", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputGif).resize(width, height).write(outputTiff, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("sharp-file-file-sharpen", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputGif).resize(width, height).sharpen().write(outputTiff, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("sharp-file-file-sequentialRead", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputGif).sequentialRead().resize(width, height).write(outputTiff, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).on("cycle", function(event) {
|
||||||
|
console.log("gif " + String(event.target));
|
||||||
|
}).on("complete", function() {
|
||||||
|
callback(null, this.filter("fastest").pluck("name"));
|
||||||
|
}).run();
|
||||||
|
}
|
||||||
}, function(err, results) {
|
}, function(err, results) {
|
||||||
assert(!err, err);
|
assert(!err, err);
|
||||||
Object.keys(results).forEach(function(format) {
|
Object.keys(results).forEach(function(format) {
|
||||||
assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format);
|
if (results[format].toString().substr(0, 5) !== "sharp") {
|
||||||
|
console.log("sharp was slower than " + results[format] + " for " + format);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
console.dir(sharp.cache());
|
||||||
});
|
});
|
||||||
|
|||||||
68
tests/random.js
Executable file
68
tests/random.js
Executable file
@@ -0,0 +1,68 @@
|
|||||||
|
var sharp = require("../index");
|
||||||
|
var fs = require("fs");
|
||||||
|
var path = require("path");
|
||||||
|
var imagemagick = require("imagemagick");
|
||||||
|
var gm = require("gm");
|
||||||
|
var async = require("async");
|
||||||
|
var assert = require("assert");
|
||||||
|
var Benchmark = require("benchmark");
|
||||||
|
|
||||||
|
var fixturesPath = path.join(__dirname, "fixtures");
|
||||||
|
var inputJpg = path.join(fixturesPath, "2569067123_aca715a2ee_o.jpg"); // http://www.flickr.com/photos/grizdave/2569067123/
|
||||||
|
var outputJpg = path.join(fixturesPath, "output.jpg");
|
||||||
|
|
||||||
|
var min = 320;
|
||||||
|
var max = 960;
|
||||||
|
|
||||||
|
var randomDimension = function() {
|
||||||
|
return Math.random() * (max - min) + min;
|
||||||
|
};
|
||||||
|
|
||||||
|
new Benchmark.Suite("random").add("imagemagick", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
imagemagick.resize({
|
||||||
|
srcPath: inputJpg,
|
||||||
|
dstPath: outputJpg,
|
||||||
|
quality: 0.8,
|
||||||
|
width: randomDimension(),
|
||||||
|
height: randomDimension()
|
||||||
|
}, function(err) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("gm", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
gm(inputJpg).resize(randomDimension(), randomDimension()).quality(80).toBuffer(function (err, buffer) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
assert.notStrictEqual(null, buffer);
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).add("sharp", {
|
||||||
|
defer: true,
|
||||||
|
fn: function(deferred) {
|
||||||
|
sharp(inputJpg).resize(randomDimension(), randomDimension()).toBuffer(function(err, buffer) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
} else {
|
||||||
|
assert.notStrictEqual(null, buffer);
|
||||||
|
deferred.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}).on("cycle", function(event) {
|
||||||
|
console.log(String(event.target));
|
||||||
|
}).on("complete", function() {
|
||||||
|
var winner = this.filter("fastest").pluck("name");
|
||||||
|
assert.strictEqual("sharp", String(winner), "sharp was slower than " + winner);
|
||||||
|
console.dir(sharp.cache());
|
||||||
|
}).run();
|
||||||
73
tests/unit.js
Executable file
73
tests/unit.js
Executable file
@@ -0,0 +1,73 @@
|
|||||||
|
var sharp = require("../index");
|
||||||
|
var path = require("path");
|
||||||
|
var imagemagick = require("imagemagick");
|
||||||
|
var assert = require("assert");
|
||||||
|
var async = require("async");
|
||||||
|
|
||||||
|
var fixturesPath = path.join(__dirname, "fixtures");
|
||||||
|
|
||||||
|
var inputJpg = path.join(fixturesPath, "2569067123_aca715a2ee_o.jpg"); // http://www.flickr.com/photos/grizdave/2569067123/
|
||||||
|
var outputJpg = path.join(fixturesPath, "output.jpg");
|
||||||
|
|
||||||
|
async.series([
|
||||||
|
// Resize with exact crop
|
||||||
|
function(done) {
|
||||||
|
sharp(inputJpg).resize(320, 240).write(outputJpg, function(err) {
|
||||||
|
if (err) throw err;
|
||||||
|
imagemagick.identify(outputJpg, function(err, features) {
|
||||||
|
if (err) throw err;
|
||||||
|
assert.strictEqual(320, features.width);
|
||||||
|
assert.strictEqual(240, features.height);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
// Resize to fixed width
|
||||||
|
function(done) {
|
||||||
|
sharp(inputJpg).resize(320).write(outputJpg, function(err) {
|
||||||
|
if (err) throw err;
|
||||||
|
imagemagick.identify(outputJpg, function(err, features) {
|
||||||
|
if (err) throw err;
|
||||||
|
assert.strictEqual(320, features.width);
|
||||||
|
assert.strictEqual(261, features.height);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
// Resize to fixed height
|
||||||
|
function(done) {
|
||||||
|
sharp(inputJpg).resize(null, 320).write(outputJpg, function(err) {
|
||||||
|
if (err) throw err;
|
||||||
|
imagemagick.identify(outputJpg, function(err, features) {
|
||||||
|
if (err) throw err;
|
||||||
|
assert.strictEqual(391, features.width);
|
||||||
|
assert.strictEqual(320, features.height);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
// Identity transform
|
||||||
|
function(done) {
|
||||||
|
sharp(inputJpg).write(outputJpg, function(err) {
|
||||||
|
if (err) throw err;
|
||||||
|
imagemagick.identify(outputJpg, function(err, features) {
|
||||||
|
if (err) throw err;
|
||||||
|
assert.strictEqual(2725, features.width);
|
||||||
|
assert.strictEqual(2225, features.height);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
// Upscale
|
||||||
|
function(done) {
|
||||||
|
sharp(inputJpg).resize(3000).write(outputJpg, function(err) {
|
||||||
|
if (err) throw err;
|
||||||
|
imagemagick.identify(outputJpg, function(err, features) {
|
||||||
|
if (err) throw err;
|
||||||
|
assert.strictEqual(3000, features.width);
|
||||||
|
assert.strictEqual(2449, features.height);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
]);
|
||||||
Reference in New Issue
Block a user