Ensure 16-bit input images can be normalised

This commit is contained in:
Lovell Fuller 2016-02-03 19:33:34 +00:00
parent e380576da2
commit 322aa60891
2 changed files with 94 additions and 92 deletions

View File

@ -20,7 +20,8 @@
[#315](https://github.com/lovell/sharp/issues/315) [#315](https://github.com/lovell/sharp/issues/315)
[@impomezia](https://github.com/impomezia) [@impomezia](https://github.com/impomezia)
* Ensure 16-bit input images can be embedded onto a transparent background. * Ensure 16-bit input images can be normalised and embedded onto transparent backgrounds.
[#339](https://github.com/lovell/sharp/issues/339)
[#340](https://github.com/lovell/sharp/issues/340) [#340](https://github.com/lovell/sharp/issues/340)
[@janaz](https://github.com/janaz) [@janaz](https://github.com/janaz)

View File

@ -5,107 +5,108 @@ var assert = require('assert');
var sharp = require('../../index'); var sharp = require('../../index');
var fixtures = require('../fixtures'); var fixtures = require('../fixtures');
var assertNormalized = function(data) {
var min = 255;
var max = 0;
for (var i = 0; i < data.length; i++) {
min = Math.min(min, data[i]);
max = Math.max(max, data[i]);
}
assert.strictEqual(0, min);
assert.strictEqual(255, max);
};
describe('Normalization', function () { describe('Normalization', function () {
it('uses the same prototype for both spellings', function () { it('uses the same prototype for both spellings', function() {
assert.strictEqual(sharp.prototype.normalize, sharp.prototype.normalise); assert.strictEqual(sharp.prototype.normalize, sharp.prototype.normalise);
}); });
// Normalize is currently unavailable on Windows it('spreads rgb image values between 0 and 255', function(done) {
if (process.platform !== 'win32') { sharp(fixtures.inputJpgWithLowContrast)
.normalize()
.raw()
.toBuffer(function (err, data, info) {
if (err) throw err;
assertNormalized(data);
done();
});
});
it('spreads rgb image values between 0 and 255', function(done) { it('spreads grayscaled image values between 0 and 255', function(done) {
sharp(fixtures.inputJpgWithLowContrast) sharp(fixtures.inputJpgWithLowContrast)
.normalize() .gamma()
.raw() .greyscale()
.toBuffer(function (err, data, info) { .normalize(true)
if (err) throw err; .raw()
var min = 255, max = 0, i; .toBuffer(function (err, data, info) {
for (i = 0; i < data.length; i += 3) { if (err) throw err;
min = Math.min(min, data[i], data[i + 1], data[i + 2]); assertNormalized(data);
max = Math.max(max, data[i], data[i + 1], data[i + 2]); done();
} });
assert.strictEqual(0, min); });
assert.strictEqual(255, max);
done();
});
});
it('spreads grayscaled image values between 0 and 255', function(done) { it('stretches greyscale images with alpha channel', function(done) {
sharp(fixtures.inputJpgWithLowContrast) sharp(fixtures.inputPngWithGreyAlpha)
.gamma() .normalize()
.greyscale() .raw()
.normalize(true) .toBuffer(function (err, data, info) {
.raw() if (err) throw err;
.toBuffer(function (err, data, info) { assertNormalized(data);
if (err) throw err; done();
var min = 255, max = 0, i; });
for (i = 0; i < data.length; i++) { });
min = Math.min(min, data[i]);
max = Math.max(max, data[i]);
}
assert.strictEqual(0, min);
assert.strictEqual(255, max);
done();
});
});
it('stretches greyscale images with alpha channel', function (done) { it('keeps an existing alpha channel', function(done) {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithTransparency)
.normalize() .normalize()
.raw() .toBuffer(function (err, data) {
.toBuffer(function (err, data, info) { if (err) throw err;
var min = 255, max = 0, i; sharp(data).metadata(function(err, metadata) {
for (i = 0; i < data.length; i++) {
min = Math.min(min, data[i]);
max = Math.max(max, data[i]);
}
assert.strictEqual(0, min);
assert.strictEqual(255, max);
done();
});
});
it('keeps an existing alpha channel', function (done) {
sharp(fixtures.inputPngWithTransparency)
.normalize()
.toBuffer(function (err, data) {
if (err) return done(err); if (err) return done(err);
sharp(data).metadata(function(err, metadata) { assert.strictEqual(4, metadata.channels);
if (err) return done(err); assert.strictEqual(true, metadata.hasAlpha);
assert.strictEqual(4, metadata.channels); assert.strictEqual('srgb', metadata.space);
assert.strictEqual(true, metadata.hasAlpha);
assert.strictEqual('srgb', metadata.space);
done();
});
});
});
it('keeps the alpha channel of greyscale images intact', function (done) {
sharp(fixtures.inputPngWithGreyAlpha)
.normalize()
.toBuffer(function (err, data) {
if (err) return done(err);
sharp(data).metadata(function(err, metadata) {
if (err) return done(err);
assert.strictEqual(true, metadata.hasAlpha);
assert.strictEqual(4, metadata.channels);
assert.strictEqual('srgb', metadata.space);
done();
});
});
});
it('does not alter images with only one color', function (done) {
var output = fixtures.path('output.unmodified-png-with-one-color.png');
sharp(fixtures.inputPngWithOneColor)
.normalize()
.toFile(output, function(err, info) {
if (err) done(err);
fixtures.assertMaxColourDistance(output, fixtures.inputPngWithOneColor, 0);
done(); done();
}); });
}); });
});
it('keeps the alpha channel of greyscale images intact', function(done) {
sharp(fixtures.inputPngWithGreyAlpha)
.normalize()
.toBuffer(function (err, data) {
if (err) throw err;
sharp(data).metadata(function(err, metadata) {
if (err) return done(err);
assert.strictEqual(true, metadata.hasAlpha);
assert.strictEqual(4, metadata.channels);
assert.strictEqual('srgb', metadata.space);
done();
});
});
});
it('does not alter images with only one color', function(done) {
var output = fixtures.path('output.unmodified-png-with-one-color.png');
sharp(fixtures.inputPngWithOneColor)
.normalize()
.toFile(output, function(err, info) {
if (err) done(err);
fixtures.assertMaxColourDistance(output, fixtures.inputPngWithOneColor, 0);
done();
});
});
it('works with 16-bit RGBA images', function(done) {
sharp(fixtures.inputPngWithTransparency16bit)
.normalize()
.raw()
.toBuffer(function (err, data, info) {
if (err) throw err;
assertNormalized(data);
done();
});
});
}
}); });