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,40 +9,37 @@ const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('Rotation', function () { describe('Rotation', function () {
['rotate', 'autoOrient', 'constructor'].forEach(function (rotateMethod) {
describe(`Auto orientation via ${rotateMethod}:`, () => {
const options = rotateMethod === 'constructor' ? { autoOrient: true } : {};
['Landscape', 'Portrait'].forEach(function (orientation) { ['Landscape', 'Portrait'].forEach(function (orientation) {
[1, 2, 3, 4, 5, 6, 7, 8].forEach(function (exifTag) { [1, 2, 3, 4, 5, 6, 7, 8].forEach(function (exifTag) {
const input = fixtures[`inputJpgWith${orientation}Exif${exifTag}`]; const input = fixtures[`inputJpgWith${orientation}Exif${exifTag}`];
const expectedOutput = fixtures.expected(`${orientation}_${exifTag}-out.jpg`); const expectedOutput = fixtures.expected(`${orientation}_${exifTag}-out.jpg`);
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate`, function (done) { it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate`, function (done) {
const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
sharp(input)
.rotate() const img = sharp(input, options);
.toBuffer(function (err, data, info) { rotateMethod === 'rotate' && img.rotate();
rotateMethod === 'autoOrient' && img.autoOrient();
img.toBuffer(function (err, data, info) {
if (err) throw err; if (err) throw err;
assert.strictEqual(info.width, expectedWidth); assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight); assert.strictEqual(info.height, expectedHeight);
fixtures.assertSimilar(expectedOutput, data, done); fixtures.assertSimilar(expectedOutput, data, done);
}); });
}); });
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then resize`, function (done) { it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then resize`, function (done) {
const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [320, 240] : [320, 427]; const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [320, 240] : [320, 427];
sharp(input)
.rotate() const img = sharp(input, options);
.resize({ width: 320 }) rotateMethod === 'rotate' && img.rotate();
.toBuffer(function (err, data, info) { rotateMethod === 'autoOrient' && img.autoOrient();
if (err) throw err;
assert.strictEqual(info.width, expectedWidth); img.resize({ width: 320 })
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) { .toBuffer(function (err, data, info) {
if (err) throw err; if (err) throw err;
assert.strictEqual(info.width, expectedWidth); assert.strictEqual(info.width, expectedWidth);
@ -51,17 +48,25 @@ describe('Rotation', function () {
}); });
}); });
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-orient (alias of .rotate())`, function (done) { if (rotateMethod !== 'constructor') {
const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; it(`${orientation} image with EXIF Orientation ${exifTag}: Resize then auto-rotate`, function (done) {
sharp(input) const [expectedWidth, expectedHeight] = orientation === 'Landscape'
.autoOrient() ? (exifTag < 5) ? [320, 240] : [320, 240]
.toBuffer(function (err, data, info) { : [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; if (err) throw err;
assert.strictEqual(info.width, expectedWidth); assert.strictEqual(info.width, expectedWidth);
assert.strictEqual(info.height, expectedHeight); assert.strictEqual(info.height, expectedHeight);
fixtures.assertSimilar(expectedOutput, data, done); fixtures.assertSimilar(expectedOutput, data, done);
}); });
}); });
}
[true, false].forEach((doResize) => { [true, false].forEach((doResize) => {
[90, 180, 270, 45].forEach(function (angle) { [90, 180, 270, 45].forEach(function (angle) {
@ -71,9 +76,11 @@ describe('Rotation', function () {
const [width, height] = (angle === 45 ? [742, 742] : [inputWidth, inputHeight]).map((x) => doResize ? Math.floor(x / 1.875) : x); 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 [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width];
const img = sharp(input) const img = sharp(input, options);
.rotate() rotateMethod === 'rotate' && img.rotate();
.rotate(angle); rotateMethod === 'autoOrient' && img.autoOrient();
img.rotate(angle);
doResize && img.resize(expectedWidth); doResize && img.resize(expectedWidth);
img.toBuffer(function (err, data, info) { img.toBuffer(function (err, data, info) {
@ -92,7 +99,11 @@ describe('Rotation', function () {
it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then ${flipFlopTestName} ${doResize ? 'and resize' : ''}`, function (done) { 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 expectedOutput = fixtures.expected(`${orientation}_${exifTag}_${flipFlopFileName}-out.jpg`);
const img = sharp(input).rotate(); const img = sharp(input, options);
rotateMethod === 'rotate' && img.rotate();
rotateMethod === 'autoOrient' && img.autoOrient();
flip && img.flip(); flip && img.flip();
flop && img.flop(); flop && img.flop();
doResize && img.resize(orientation === 'Landscape' ? 320 : 240); doResize && img.resize(orientation === 'Landscape' ? 320 : 240);
@ -108,6 +119,8 @@ describe('Rotation', function () {
}); });
}); });
}); });
});
});
it('Rotate by 30 degrees with semi-transparent background', function (done) { it('Rotate by 30 degrees with semi-transparent background', function (done) {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)