Ensure op ordering is respected where possible #3319

Emit warnings when previous ops might be ignored
Flip and flop now occur before rotate, if any
This commit is contained in:
Lovell Fuller
2022-08-18 13:57:13 +01:00
parent e547eaa180
commit 212a6e7519
13 changed files with 168 additions and 50 deletions

View File

@@ -138,7 +138,20 @@ describe('Partial image extraction', function () {
if (err) throw err;
assert.strictEqual(280, info.width);
assert.strictEqual(380, info.height);
fixtures.assertSimilar(fixtures.expected('rotate-extract.jpg'), data, { threshold: 7 }, done);
fixtures.assertSimilar(fixtures.expected('rotate-extract.jpg'), data, done);
});
});
it('Extract then rotate then extract', function (done) {
sharp(fixtures.inputPngWithGreyAlpha)
.extract({ left: 20, top: 10, width: 180, height: 280 })
.rotate(90)
.extract({ left: 20, top: 10, width: 200, height: 100 })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(200, info.width);
assert.strictEqual(100, info.height);
fixtures.assertSimilar(fixtures.expected('extract-rotate-extract.jpg'), data, done);
});
});
@@ -164,7 +177,7 @@ describe('Partial image extraction', function () {
if (err) throw err;
assert.strictEqual(380, info.width);
assert.strictEqual(280, info.height);
fixtures.assertSimilar(fixtures.expected('rotate-extract-45.jpg'), data, { threshold: 7 }, done);
fixtures.assertSimilar(fixtures.expected('rotate-extract-45.jpg'), data, done);
});
});
@@ -281,5 +294,27 @@ describe('Partial image extraction', function () {
done();
});
});
it('Multiple extract emits warning', () => {
let warningMessage = '';
const s = sharp();
s.on('warning', function (msg) { warningMessage = msg; });
const options = { top: 0, left: 0, width: 1, height: 1 };
s.extract(options);
assert.strictEqual(warningMessage, '');
s.extract(options);
assert.strictEqual(warningMessage, 'ignoring previous extract options');
});
it('Multiple rotate+extract emits warning', () => {
let warningMessage = '';
const s = sharp().rotate();
s.on('warning', function (msg) { warningMessage = msg; });
const options = { top: 0, left: 0, width: 1, height: 1 };
s.extract(options);
assert.strictEqual(warningMessage, '');
s.extract(options);
assert.strictEqual(warningMessage, 'ignoring previous extract options');
});
});
});

View File

@@ -791,4 +791,14 @@ describe('Resize dimensions', function () {
sharp().resize(null, null, { position: 'unknown' });
});
});
it('Multiple resize emits warning', () => {
let warningMessage = '';
const s = sharp();
s.on('warning', function (msg) { warningMessage = msg; });
s.resize(1);
assert.strictEqual(warningMessage, '');
s.resize(2);
assert.strictEqual(warningMessage, 'ignoring previous resize options');
});
});

View File

@@ -25,8 +25,8 @@ describe('Rotation', function () {
it('Rotate by 30 degrees with semi-transparent background', function (done) {
sharp(fixtures.inputJpg)
.rotate(30, { background: { r: 255, g: 0, b: 0, alpha: 0.5 } })
.resize(320)
.rotate(30, { background: { r: 255, g: 0, b: 0, alpha: 0.5 } })
.png()
.toBuffer(function (err, data, info) {
if (err) throw err;
@@ -39,8 +39,8 @@ describe('Rotation', function () {
it('Rotate by 30 degrees with solid background', function (done) {
sharp(fixtures.inputJpg)
.rotate(30, { background: { r: 255, g: 0, b: 0, alpha: 0.5 } })
.resize(320)
.rotate(30, { background: { r: 255, g: 0, b: 0, alpha: 0.5 } })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
@@ -51,25 +51,31 @@ describe('Rotation', function () {
});
it('Rotate by 90 degrees, respecting output input size', function (done) {
sharp(fixtures.inputJpg).rotate(90).resize(320, 240).toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
done();
});
sharp(fixtures.inputJpg)
.rotate(90)
.resize(320, 240)
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
done();
});
});
it('Rotate by 30 degrees, respecting output input size', function (done) {
sharp(fixtures.inputJpg).rotate(30).resize(320, 240).toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(397, info.width);
assert.strictEqual(368, info.height);
done();
});
it('Resize then rotate by 30 degrees, respecting output input size', function (done) {
sharp(fixtures.inputJpg)
.resize(320, 240)
.rotate(30)
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('jpeg', info.format);
assert.strictEqual(397, info.width);
assert.strictEqual(368, info.height);
done();
});
});
[-3690, -450, -90, 90, 450, 3690].forEach(function (angle) {
@@ -141,8 +147,8 @@ describe('Rotation', function () {
it('Rotate by 270 degrees, rectangular output ignoring aspect ratio', function (done) {
sharp(fixtures.inputJpg)
.resize(320, 240, { fit: sharp.fit.fill })
.rotate(270)
.resize(320, 240, { fit: sharp.fit.fill })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
@@ -300,6 +306,16 @@ describe('Rotation', function () {
)
);
it('Multiple rotate emits warning', () => {
let warningMessage = '';
const s = sharp();
s.on('warning', function (msg) { warningMessage = msg; });
s.rotate();
assert.strictEqual(warningMessage, '');
s.rotate();
assert.strictEqual(warningMessage, 'ignoring previous rotate options');
});
it('Flip - vertical', function (done) {
sharp(fixtures.inputJpg)
.resize(320)