Add failing tests for constructor auto-orientation

This commit is contained in:
Don Denton 2024-07-12 22:59:55 -04:00
parent bc42109382
commit b8c9372fae

View File

@ -9,72 +9,20 @@ const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Rotation', function () { describe('Rotation', function () {
['Landscape', 'Portrait'].forEach(function (orientation) { ['rotate', 'autoOrient', 'constructor'].forEach(function (rotateMethod) {
[1, 2, 3, 4, 5, 6, 7, 8].forEach(function (exifTag) { describe(`Auto orientation via ${rotateMethod}:`, () => {
const input = fixtures[`inputJpgWith${orientation}Exif${exifTag}`]; const options = rotateMethod === 'constructor' ? { autoOrient: true } : {};
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}-out.jpg`);
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate`, function (done) {
const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
sharp(input)
.rotate()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight);
fixtures.assertSimilar(expectedOutput, data, done);
});
});
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then resize`, function (done) {
const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [320, 240] : [320, 427];
sharp(input)
.rotate()
.resize({ width: 320 })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight);
fixtures.assertSimilar(expectedOutput, data, done);
});
});
it(`${orientation} image with EXIF Orientation ${exifTag}: Resize then auto-rotate`, function (done) {
const [expectedWidth, expectedHeight] = orientation === 'Landscape'
? (exifTag < 5) ? [320, 240] : [320, 240]
: [320, 427];
sharp(input)
.resize({ width: 320 })
.rotate()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight);
fixtures.assertSimilar(expectedOutput, data, done);
});
});
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-orient (alias of .rotate())`, function (done) { ['Landscape', 'Portrait'].forEach(function (orientation) {
const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; [1, 2, 3, 4, 5, 6, 7, 8].forEach(function (exifTag) {
sharp(input) const input = fixtures[`inputJpgWith${orientation}Exif${exifTag}`];
.autoOrient() const expectedOutput = fixtures.expected(`${orientation}_${exifTag}-out.jpg`);
.toBuffer(function (err, data, info) { it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate`, function (done) {
if (err) throw err; const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight);
fixtures.assertSimilar(expectedOutput, data, done);
});
});
[true, false].forEach((doResize) => { const img = sharp(input, options);
[90, 180, 270, 45].forEach(function (angle) { rotateMethod === 'rotate' && img.rotate();
const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; rotateMethod === 'autoOrient' && img.autoOrient();
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_rotate${angle}-out.jpg`);
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then rotate ${angle} ${doResize ? 'and resize' : ''}`, function (done) {
const [width, height] = (angle === 45 ? [742, 742] : [inputWidth, inputHeight]).map((x) => doResize ? Math.floor(x / 1.875) : x);
const [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width];
const img = sharp(input)
.rotate()
.rotate(angle);
doResize && img.resize(expectedWidth);
img.toBuffer(function (err, data, info) { img.toBuffer(function (err, data, info) {
if (err) throw err; if (err) throw err;
@ -83,25 +31,90 @@ describe('Rotation', function () {
fixtures.assertSimilar(expectedOutput, data, done); fixtures.assertSimilar(expectedOutput, data, done);
}); });
}); });
});
[[true, true], [true, false], [false, true]].forEach(function ([flip, flop]) { it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then resize`, function (done) {
const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [320, 240] : [320, 427];
const flipFlopFileName = [flip && 'flip', flop && 'flop'].filter(Boolean).join('_');
const flipFlopTestName = [flip && 'flip', flop && 'flop'].filter(Boolean).join(' & ');
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then ${flipFlopTestName} ${doResize ? 'and resize' : ''}`, function (done) {
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_${flipFlopFileName}-out.jpg`);
const img = sharp(input).rotate(); const img = sharp(input, options);
flip && img.flip(); rotateMethod === 'rotate' && img.rotate();
flop && img.flop(); rotateMethod === 'autoOrient' && img.autoOrient();
doResize && img.resize(orientation === 'Landscape' ? 320 : 240);
img.toBuffer(function (err, data, info) { img.resize({ width: 320 })
if (err) throw err; .toBuffer(function (err, data, info) {
assert.strictEqual(info.width, inputWidth / (doResize ? 1.875 : 1)); if (err) throw err;
assert.strictEqual(info.height, inputHeight / (doResize ? 1.875 : 1)); assert.strictEqual(info.width, expectedWidth);
fixtures.assertSimilar(expectedOutput, data, done); assert.strictEqual(info.height, expectedHeight);
fixtures.assertSimilar(expectedOutput, data, done);
});
});
if (rotateMethod !== 'constructor') {
it(`${orientation} image with EXIF Orientation ${exifTag}: Resize then auto-rotate`, function (done) {
const [expectedWidth, expectedHeight] = orientation === 'Landscape'
? (exifTag < 5) ? [320, 240] : [320, 240]
: [320, 427];
const img = sharp(input, options)
.resize({ width: 320 });
rotateMethod === 'rotate' && img.rotate();
rotateMethod === 'autoOrient' && img.autoOrient();
img.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight);
fixtures.assertSimilar(expectedOutput, data, done);
});
});
}
[true, false].forEach((doResize) => {
[90, 180, 270, 45].forEach(function (angle) {
const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_rotate${angle}-out.jpg`);
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then rotate ${angle} ${doResize ? 'and resize' : ''}`, function (done) {
const [width, height] = (angle === 45 ? [742, 742] : [inputWidth, inputHeight]).map((x) => doResize ? Math.floor(x / 1.875) : x);
const [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width];
const img = sharp(input, options);
rotateMethod === 'rotate' && img.rotate();
rotateMethod === 'autoOrient' && img.autoOrient();
img.rotate(angle);
doResize && img.resize(expectedWidth);
img.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight);
fixtures.assertSimilar(expectedOutput, data, done);
});
});
});
[[true, true], [true, false], [false, true]].forEach(function ([flip, flop]) {
const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
const flipFlopFileName = [flip && 'flip', flop && 'flop'].filter(Boolean).join('_');
const flipFlopTestName = [flip && 'flip', flop && 'flop'].filter(Boolean).join(' & ');
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then ${flipFlopTestName} ${doResize ? 'and resize' : ''}`, function (done) {
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_${flipFlopFileName}-out.jpg`);
const img = sharp(input, options);
rotateMethod === 'rotate' && img.rotate();
rotateMethod === 'autoOrient' && img.autoOrient();
flip && img.flip();
flop && img.flop();
doResize && img.resize(orientation === 'Landscape' ? 320 : 240);
img.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(info.width, inputWidth / (doResize ? 1.875 : 1));
assert.strictEqual(info.height, inputHeight / (doResize ? 1.875 : 1));
fixtures.assertSimilar(expectedOutput, data, done);
});
});
}); });
}); });
}); });