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');
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) {
[1, 2, 3, 4, 5, 6, 7, 8].forEach(function (exifTag) {
const input = fixtures[`inputJpgWith${orientation}Exif${exifTag}`];
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) {
const img = sharp(input, options);
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);
});
});
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()
const img = sharp(input, options);
rotateMethod === 'rotate' && img.rotate();
rotateMethod === 'autoOrient' && img.autoOrient();
img.resize({ width: 320 })
.toBuffer(function (err, data, info) {
if (err) throw err;
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) {
const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600];
sharp(input)
.autoOrient()
.toBuffer(function (err, data, info) {
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) {
@ -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 [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width];
const img = sharp(input)
.rotate()
.rotate(angle);
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) {
@ -92,7 +99,11 @@ describe('Rotation', function () {
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);
rotateMethod === 'rotate' && img.rotate();
rotateMethod === 'autoOrient' && img.autoOrient();
flip && img.flip();
flop && img.flop();
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) {
sharp(fixtures.inputJpg)