Windows compatibility #19

Hide WebP format and normalise option

Separate test runners for node and iojs
This commit is contained in:
Lovell Fuller 2015-04-21 12:13:19 +01:00
parent 8926ebc56c
commit 1e52c2dbe6
13 changed files with 231 additions and 206 deletions

View File

@ -88,9 +88,12 @@ The _gettext_ dependency of _libvips_ [can lead](https://github.com/lovell/sharp
Requires x86 32-bit Node.js or io.js (use `iojs.exe` rather than `node.exe`). Requires x86 32-bit Node.js or io.js (use `iojs.exe` rather than `node.exe`).
The WebP format is currently unsupported. The WebP format is currently unsupported.
1. [Download](http://www.vips.ecs.soton.ac.uk/supported/current/win32/) and unzip `vips-dev.x.y.z.zip`. 1. Ensure the [node-gyp prerequisites](https://github.com/TooTallNate/node-gyp#installation) are met.
2. Set the `VIPS_HOME` environment variable to the full path of the `vips-dev-x.y.z` directory. 2. [Download](http://www.vips.ecs.soton.ac.uk/supported/current/win32/) and unzip `vips-dev.x.y.z.zip`.
3. Add `vips-dev-x.y.z\bin` to `PATH`. 3. Set the `VIPS_HOME` environment variable to the full path of the `vips-dev-x.y.z` directory.
4. Add `vips-dev-x.y.z\bin` to `PATH`.
Versions of MSVC more recent than 2013 may require the use of `npm install --arch=ia32 --msvs_version=2013`.
### Heroku ### Heroku
@ -678,6 +681,10 @@ A [guide for contributors](https://github.com/lovell/sharp/blob/master/CONTRIBUT
[![Centos 6.5 Build Status](https://snap-ci.com/lovell/sharp/branch/master/build_image)](https://snap-ci.com/lovell/sharp/branch/master) [![Centos 6.5 Build Status](https://snap-ci.com/lovell/sharp/branch/master/build_image)](https://snap-ci.com/lovell/sharp/branch/master)
#### Windows Server 2012
[![Windows Server 2012 Build Status](https://ci.appveyor.com/api/projects/status/pgtul704nkhhg6sg)](https://ci.appveyor.com/project/lovell/sharp)
### Benchmark tests ### Benchmark tests
``` ```

View File

@ -1,17 +1,17 @@
os: Visual Studio 2014 CTP4 os: Visual Studio 2014 CTP4
platform: x86
environment: environment:
VIPS_VERSION_MAJOR_MINOR: 7.42 VIPS_VERSION_MAJOR_MINOR: 7.42
VIPS_VERSION_PATCH: 3 VIPS_VERSION_PATCH: 3
VIPS_WARNING: 0
install: install:
- ps: $env:VIPS_VERSION = "$env:VIPS_VERSION_MAJOR_MINOR.$env:VIPS_VERSION_PATCH" - ps: $env:VIPS_VERSION = "$env:VIPS_VERSION_MAJOR_MINOR.$env:VIPS_VERSION_PATCH"
- ps: Write-Output "VIPS_VERSION=$env:VIPS_VERSION"
- ps: Write-Output "Fetching http://www.vips.ecs.soton.ac.uk/supported/$env:VIPS_VERSION_MAJOR_MINOR/win32/vips-dev-$env:VIPS_VERSION.zip" - ps: Write-Output "Fetching http://www.vips.ecs.soton.ac.uk/supported/$env:VIPS_VERSION_MAJOR_MINOR/win32/vips-dev-$env:VIPS_VERSION.zip"
- ps: Start-FileDownload http://www.vips.ecs.soton.ac.uk/supported/$env:VIPS_VERSION_MAJOR_MINOR/win32/vips-dev-$env:VIPS_VERSION.zip -FileName c:\vips-dev-$env:VIPS_VERSION.zip - ps: Start-FileDownload http://www.vips.ecs.soton.ac.uk/supported/$env:VIPS_VERSION_MAJOR_MINOR/win32/vips-dev-$env:VIPS_VERSION.zip -FileName c:\vips-dev-$env:VIPS_VERSION.zip
- ps: Write-Output "Extracting c:\vips-dev-$env:VIPS_VERSION.zip"
- ps: Invoke-Expression "& 7z -y x c:\vips-dev-$env:VIPS_VERSION.zip -oc:\ | FIND /V `"ing `"" - ps: Invoke-Expression "& 7z -y x c:\vips-dev-$env:VIPS_VERSION.zip -oc:\ | FIND /V `"ing `""
- ps: $env:VIPS_HOME = "c:\vips-dev-$env:VIPS_VERSION" - ps: $env:VIPS_HOME = "c:\vips-dev-$env:VIPS_VERSION"
- ps: $env:PATH = "$env:VIPS_HOME\bin;$env:PATH" - ps: $env:PATH = "$env:VIPS_HOME\bin;$env:PATH"
- ps: Install-Product node 0 x86 - ps: Install-Product node 0 x86
- npm install --msvs_version=2013 - npm install --msvs_version=2013
test_script: test_script:
- npm test - npm run-script test-win32-node

View File

@ -349,10 +349,14 @@ Sharp.prototype.gamma = function(gamma) {
}; };
/* /*
Normalize histogram Enhance output image contrast by stretching its luminance to cover the full dynamic range
*/ */
Sharp.prototype.normalize = function(normalize) { Sharp.prototype.normalize = function(normalize) {
this.options.normalize = (typeof normalize === 'boolean') ? normalize : true; if (process.platform !== 'win32') {
this.options.normalize = (typeof normalize === 'boolean') ? normalize : true;
} else {
console.error('normalize unavailable on win32 platform');
}
return this; return this;
}; };
Sharp.prototype.normalise = Sharp.prototype.normalize; Sharp.prototype.normalise = Sharp.prototype.normalize;

View File

@ -20,7 +20,9 @@
], ],
"description": "High performance Node.js module to resize JPEG, PNG, WebP and TIFF images using the libvips library", "description": "High performance Node.js module to resize JPEG, PNG, WebP and TIFF images using the libvips library",
"scripts": { "scripts": {
"test": "VIPS_WARNING=0 node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --slow=5000 --timeout=10000 ./test/unit/*.js" "test": "VIPS_WARNING=0 node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --slow=5000 --timeout=15000 ./test/unit/*.js",
"test-win32-node": "node ./node_modules/mocha/bin/mocha --slow=5000 --timeout=15000 ./test/unit/*.js",
"test-win32-iojs": "iojs ./node_modules/mocha/bin/mocha --slow=5000 --timeout=15000 ./test/unit/*.js"
}, },
"main": "index.js", "main": "index.js",
"repository": { "repository": {

View File

@ -708,6 +708,7 @@ class ResizeWorker : public NanAsyncWorker {
image = gammaDecoded; image = gammaDecoded;
} }
#ifndef _WIN32
// Apply normalization // Apply normalization
if (baton->normalize) { if (baton->normalize) {
VipsInterpretation typeBeforeNormalize = image->Type; VipsInterpretation typeBeforeNormalize = image->Type;
@ -787,6 +788,7 @@ class ResizeWorker : public NanAsyncWorker {
image = normalized; image = normalized;
} }
} }
#endif
// Convert image to sRGB, if not already // Convert image to sRGB, if not already
if (image->Type != VIPS_INTERPRETATION_sRGB) { if (image->Type != VIPS_INTERPRETATION_sRGB) {

View File

@ -8,6 +8,10 @@
#include "resize.h" #include "resize.h"
#include "utilities.h" #include "utilities.h"
#ifdef _WIN64
#error Windows 64-bit currently unsupported - see https://github.com/lovell/sharp#windows
#endif
extern "C" void init(v8::Handle<v8::Object> target) { extern "C" void init(v8::Handle<v8::Object> target) {
NanScope(); NanScope();
vips_init("sharp"); vips_init("sharp");

View File

@ -31,16 +31,18 @@ describe('Colour space conversion', function() {
.toFile(fixtures.path('output.greyscale-not.jpg'), done); .toFile(fixtures.path('output.greyscale-not.jpg'), done);
}); });
it('From 1-bit TIFF to sRGB WebP [slow]', function(done) { if (sharp.format.webp.output.buffer) {
sharp(fixtures.inputTiff) it('From 1-bit TIFF to sRGB WebP [slow]', function(done) {
.webp() sharp(fixtures.inputTiff)
.toBuffer(function(err, data, info) { .webp()
if (err) throw err; .toBuffer(function(err, data, info) {
assert.strictEqual(true, data.length > 0); if (err) throw err;
assert.strictEqual('webp', info.format); assert.strictEqual(true, data.length > 0);
done(); assert.strictEqual('webp', info.format);
}); done();
}); });
});
}
it('From CMYK to sRGB', function(done) { it('From CMYK to sRGB', function(done) {
sharp(fixtures.inputJpgWithCmykProfile) sharp(fixtures.inputJpgWithCmykProfile)

View File

@ -8,39 +8,42 @@ var cpplint = require('node-cpplint/lib/');
describe('cpplint', function() { describe('cpplint', function() {
// List C++ source files // Ignore cpplint failures, possibly newline-related, on Windows
fs.readdirSync(path.join(__dirname, '..', '..', 'src')).forEach(function (source) { if (process.platform !== 'win32') {
var file = path.join('src', source); // List C++ source files
it(file, function(done) { fs.readdirSync(path.join(__dirname, '..', '..', 'src')).forEach(function (source) {
// Lint each source file var file = path.join('src', source);
cpplint({ it(file, function(done) {
files: [file], // Lint each source file
linelength: 140, cpplint({
filters: { files: [file],
legal: { linelength: 140,
copyright: false filters: {
}, legal: {
build: { copyright: false
include: false, },
include_order: false build: {
}, include: false,
whitespace: { include_order: false
blank_line: false, },
comments: false, whitespace: {
parens: false blank_line: false,
comments: false,
parens: false
}
} }
} }, function(err, report) {
}, function(err, report) { if (err) {
if (err) { throw err;
throw err; }
} var expected = {};
var expected = {}; expected[file] = [];
expected[file] = []; assert.deepEqual(expected, report);
assert.deepEqual(expected, report); done();
done(); });
}); });
});
}); });
}
}); });

View File

@ -28,24 +28,26 @@ describe('Embed', function() {
}); });
}); });
it('JPEG within WebP, to include alpha channel', function(done) { if (sharp.format.webp.output.buffer) {
sharp(fixtures.inputJpg) it('JPEG within WebP, to include alpha channel', function(done) {
.resize(320, 240) sharp(fixtures.inputJpg)
.background({r: 0, g: 0, b: 0, a: 0}) .resize(320, 240)
.embed() .background({r: 0, g: 0, b: 0, a: 0})
.webp() .embed()
.toBuffer(function(err, data, info) { .webp()
if (err) throw err; .toBuffer(function(err, data, info) {
assert.strictEqual(true, data.length > 0);
assert.strictEqual('webp', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
sharp(data).metadata(function(err, metadata) {
if (err) throw err; if (err) throw err;
assert.strictEqual(4, metadata.channels); assert.strictEqual(true, data.length > 0);
done(); assert.strictEqual('webp', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
sharp(data).metadata(function(err, metadata) {
if (err) throw err;
assert.strictEqual(4, metadata.channels);
done();
});
}); });
}); });
}); }
}); });

View File

@ -31,16 +31,18 @@ describe('Partial image extraction', function() {
}); });
}); });
it('WebP', function(done) { if (sharp.format.webp.output.file) {
sharp(fixtures.inputWebP) it('WebP', function(done) {
.extract(50, 100, 125, 200) sharp(fixtures.inputWebP)
.toFile(fixtures.path('output.extract.webp'), function(err, info) { .extract(50, 100, 125, 200)
if (err) throw err; .toFile(fixtures.path('output.extract.webp'), function(err, info) {
assert.strictEqual(125, info.width); if (err) throw err;
assert.strictEqual(200, info.height); assert.strictEqual(125, info.width);
done(); assert.strictEqual(200, info.height);
}); done();
}); });
});
}
it('TIFF', function(done) { it('TIFF', function(done) {
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)

View File

@ -806,8 +806,10 @@ describe('Input/output', function() {
.toBuffer(function(err) { .toBuffer(function(err) {
sharp.queue.removeListener('change', queueListener); sharp.queue.removeListener('change', queueListener);
if (err) throw err; if (err) throw err;
assert.strictEqual(2, eventCounter); process.nextTick(function() {
done(); assert.strictEqual(2, eventCounter);
done();
});
}); });
}); });

View File

@ -82,19 +82,21 @@ describe('Image metadata', function() {
}); });
}); });
it('WebP', function(done) { if (sharp.format.webp.input.file) {
sharp(fixtures.inputWebP).metadata(function(err, metadata) { it('WebP', function(done) {
if (err) throw err; sharp(fixtures.inputWebP).metadata(function(err, metadata) {
assert.strictEqual('webp', metadata.format); if (err) throw err;
assert.strictEqual(1024, metadata.width); assert.strictEqual('webp', metadata.format);
assert.strictEqual(772, metadata.height); assert.strictEqual(1024, metadata.width);
assert.strictEqual('srgb', metadata.space); assert.strictEqual(772, metadata.height);
assert.strictEqual(3, metadata.channels); assert.strictEqual('srgb', metadata.space);
assert.strictEqual(false, metadata.hasProfile); assert.strictEqual(3, metadata.channels);
assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual(false, metadata.hasProfile);
done(); assert.strictEqual(false, metadata.hasAlpha);
done();
});
}); });
}); }
it('GIF via libmagick', function(done) { it('GIF via libmagick', function(done) {
sharp(fixtures.inputGif).metadata(function(err, metadata) { sharp(fixtures.inputGif).metadata(function(err, metadata) {

View File

@ -13,124 +13,117 @@ describe('Normalization', function () {
assert.strictEqual(sharp.prototype.normalize, sharp.prototype.normalise); assert.strictEqual(sharp.prototype.normalize, sharp.prototype.normalise);
}); });
it('spreads rgb image values between 0 and 255', function(done) { // Normalize is currently unavailable on Windows
sharp(fixtures.inputJpgWithLowContrast) if (process.platform !== 'win32') {
.normalize()
.raw()
.toBuffer(function (err, data, info) {
if (err) throw err;
var min = 255, max = 0, i;
for (i = 0; i < data.length; i += 3) {
min = Math.min(min, data[i], data[i + 1], data[i + 2]);
max = Math.max(max, data[i], data[i + 1], data[i + 2]);
}
assert.strictEqual(0, min);
assert.strictEqual(255, max);
return done();
});
});
it('spreads grayscaled image values between 0 and 255', function(done) { it('spreads rgb image values between 0 and 255', function(done) {
sharp(fixtures.inputJpgWithLowContrast) sharp(fixtures.inputJpgWithLowContrast)
.gamma() .normalize()
.greyscale() .raw()
.normalize(true) .toBuffer(function (err, data, info) {
.raw() if (err) throw err;
.toBuffer(function (err, data, info) { var min = 255, max = 0, i;
if (err) throw err; for (i = 0; i < data.length; i += 3) {
var min = 255, max = 0, i; min = Math.min(min, data[i], data[i + 1], data[i + 2]);
for (i = 0; i < data.length; i++) { max = Math.max(max, data[i], data[i + 1], data[i + 2]);
min = Math.min(min, data[i]); }
max = Math.max(max, data[i]); assert.strictEqual(0, min);
} assert.strictEqual(255, max);
assert.strictEqual(0, min); return done();
assert.strictEqual(255, max); });
return done(); });
});
});
it('stretches greyscale images with alpha channel', function (done) { it('spreads grayscaled image values between 0 and 255', function(done) {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputJpgWithLowContrast)
.normalize() .gamma()
.raw() .greyscale()
.toBuffer(function (err, data, info) { .normalize(true)
// raw toBuffer does not return the alpha channel (yet?) .raw()
var min = 255, max = 0, i; .toBuffer(function (err, data, info) {
for (i = 0; i < data.length; i++) { if (err) throw err;
min = Math.min(min, data[i]); var min = 255, max = 0, i;
max = Math.max(max, data[i]); for (i = 0; i < data.length; i++) {
} min = Math.min(min, data[i]);
assert.strictEqual(0, min); max = Math.max(max, data[i]);
assert.strictEqual(255, max); }
return done(); assert.strictEqual(0, min);
}); assert.strictEqual(255, max);
}); return done();
});
});
it('keeps an existing alpha channel', function (done) { it('stretches greyscale images with alpha channel', function (done) {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithGreyAlpha)
.normalize() .normalize()
.toBuffer(function (err, data, info) { .raw()
sharp(data) .toBuffer(function (err, data, info) {
.metadata() var min = 255, max = 0, i;
.then(function (metadata) { for (i = 0; i < data.length; i++) {
assert.strictEqual(4, metadata.channels); min = Math.min(min, data[i]);
assert.strictEqual(true, metadata.hasAlpha); max = Math.max(max, data[i]);
assert.strictEqual('srgb', metadata.space); }
}) assert.strictEqual(0, min);
.finally(done); assert.strictEqual(255, max);
}); return done();
}); });
});
it('keeps the alpha channel of greyscale images intact', function (done) { it('keeps an existing alpha channel', function (done) {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithTransparency)
.normalize() .normalize()
.toBuffer(function (err, data, info) { .toBuffer(function (err, data, info) {
sharp(data) sharp(data)
.metadata() .metadata()
.then(function (metadata) { .then(function (metadata) {
assert.strictEqual(true, metadata.hasAlpha); assert.strictEqual(4, metadata.channels);
// because of complications with greyscale assert.strictEqual(true, metadata.hasAlpha);
// we return everything in srgb for now. assert.strictEqual('srgb', metadata.space);
// })
// assert.strictEqual(2, metadata.channels); .finally(done);
// assert.strictEqual('b-w', metadata.space); });
assert.strictEqual(4, metadata.channels); });
assert.strictEqual('srgb', metadata.space);
})
.finally(done);
});
});
it('returns a black image for images with only one color', function (done) { it('keeps the alpha channel of greyscale images intact', function (done) {
sharp(fixtures.inputPngWithOneColor) sharp(fixtures.inputPngWithGreyAlpha)
.normalize() .normalize()
.toBuffer() .toBuffer(function (err, data, info) {
.bind({}) sharp(data)
.then(function (imageData) { .metadata()
this.imageData = imageData; .then(function (metadata) {
return sharp(imageData) assert.strictEqual(true, metadata.hasAlpha);
.metadata(); assert.strictEqual(4, metadata.channels);
}) assert.strictEqual('srgb', metadata.space);
.then(function (metadata) { })
assert.strictEqual(false, metadata.hasAlpha); .finally(done);
// because of complications with greyscale });
// we return everything in srgb for now. });
//
// assert.strictEqual(1, metadata.channels); it('returns a black image for images with only one color', function (done) {
// assert.strictEqual('b-w', metadata.space); sharp(fixtures.inputPngWithOneColor)
assert.strictEqual(3, metadata.channels); .normalize()
assert.strictEqual('srgb', metadata.space); .toBuffer()
}) .bind({})
.then(function () { .then(function (imageData) {
return sharp(this.imageData) this.imageData = imageData;
.raw() return sharp(imageData)
.toBuffer(); .metadata();
}) })
.then(function (rawData) { .then(function (metadata) {
// var blackBuffer = new Buffer([0,0,0,0]); assert.strictEqual(false, metadata.hasAlpha);
var blackBuffer = new Buffer([0,0,0, 0,0,0, 0,0,0, 0,0,0]); assert.strictEqual(3, metadata.channels);
assert.strictEqual(blackBuffer.toString(), rawData.toString()); assert.strictEqual('srgb', metadata.space);
}) })
.finally(done); .then(function () {
}); return sharp(this.imageData)
.raw()
.toBuffer();
})
.then(function (rawData) {
var blackBuffer = new Buffer([0,0,0, 0,0,0, 0,0,0, 0,0,0]);
assert.strictEqual(blackBuffer.toString(), rawData.toString());
})
.finally(done);
});
}
}); });