sharp/test/unit/rotate.js
Nicolas Coden 99810c0311 Add support for any rotation angle (#791)
Allow to provide any positive or negative multiple of 90 to `.rotate(...)`.
Negative angles and angles above 360 are converted to valid 0/90/180/270
rotations (0 rotations are still ignored).

Changes:
- [Node] Add `useExifOrientation` internal variable to know if the Exif
  orientation must be used instead of the provided angle. This allows to save a
  negative angle in the `angle` option, because the `-1` special case is not
  needed.

- [Node] Change check for planed-rotation in extract, to prepare a
  rotation before extraction: check with both `angle` and `useExifOrientation`
  options.
  I think this check contains a bit too much logics on rotation options. Maybe
  we could move this condition to a dedicated function.

- [C++] Separate `CalculateRotationAndFlip` into two generic functions:
  - `CalculateExifRotationAndFlip`: Calculate the angle of rotation and
    need-to-flip for the given Exif orientation.
  - `CalculateAngleRotation`: Calculate the rotation for the given angle.

  One or the other function is used to calculate the rotation, depending on
  wether the Exif orientation tag or the provided angle must be used.

- Add unit tests for `-3690`, `-450`, `-90`, `90`, `450`, `3690` and `-3780`,
  `-540`, `0`, `180`, `540`, `3780` rotations
- Add `320x240` fixture image for tests.

Unrelated changes (squashed):
- Add ncoden to the list of contributors
2017-05-22 11:08:33 +01:00

257 lines
8.5 KiB
JavaScript

'use strict';
const assert = require('assert');
const sharp = require('../../');
const fixtures = require('../fixtures');
describe('Rotation', function () {
['Landscape', 'Portrait'].forEach(function (orientation) {
[1, 2, 3, 4, 5, 6, 7, 8].forEach(function (exifTag) {
it('Input image has Orientation EXIF tag value of (' + exifTag + '), auto-rotate', function (done) {
sharp(fixtures['inputJpgWith' + orientation + 'Exif' + exifTag])
.rotate()
.resize(320)
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(orientation === 'Landscape' ? 240 : 427, info.height);
fixtures.assertSimilar(fixtures.expected(orientation + '_' + exifTag + '-out.jpg'), data, done);
});
});
});
});
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();
});
});
[-3690, -450, -90, 90, 450, 3690].forEach(function (angle) {
it('Rotate by any 90-multiple angle (' + angle + 'deg)', function (done) {
sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(240, info.width);
assert.strictEqual(320, info.height);
done();
});
});
});
[-3780, -540, 0, 180, 540, 3780].forEach(function (angle) {
it('Rotate by any 180-multiple angle (' + angle + 'deg)', function (done) {
sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
done();
});
});
});
it('Rotate by 270 degrees, square output ignoring aspect ratio', function (done) {
sharp(fixtures.inputJpg)
.resize(240, 240)
.ignoreAspectRatio()
.rotate(270)
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(240, info.width);
assert.strictEqual(240, info.height);
sharp(data).metadata(function (err, metadata) {
if (err) throw err;
assert.strictEqual(240, metadata.width);
assert.strictEqual(240, metadata.height);
done();
});
});
});
it('Rotate by 270 degrees, rectangular output ignoring aspect ratio', function (done) {
sharp(fixtures.inputJpg)
.resize(320, 240)
.ignoreAspectRatio()
.rotate(270)
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
sharp(data).metadata(function (err, metadata) {
if (err) throw err;
assert.strictEqual(320, metadata.width);
assert.strictEqual(240, metadata.height);
done();
});
});
});
it('Input image has Orientation EXIF tag but do not rotate output', function (done) {
sharp(fixtures.inputJpgWithExif)
.resize(320)
.withMetadata()
.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(427, info.height);
sharp(data).metadata(function (err, metadata) {
if (err) throw err;
assert.strictEqual(8, metadata.orientation);
done();
});
});
});
it('Input image has Orientation EXIF tag value of 8 (270 degrees), auto-rotate', function (done) {
sharp(fixtures.inputJpgWithExif)
.rotate()
.resize(320)
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('exif-8.jpg'), data, done);
});
});
it('Override EXIF Orientation tag metadata after auto-rotate', function (done) {
sharp(fixtures.inputJpgWithExif)
.rotate()
.resize(320)
.withMetadata({orientation: 3})
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
sharp(data).metadata(function (err, metadata) {
if (err) throw err;
assert.strictEqual(3, metadata.orientation);
fixtures.assertSimilar(fixtures.expected('exif-8.jpg'), data, done);
});
});
});
it('Input image has Orientation EXIF tag value of 5 (270 degrees + flip), auto-rotate', function (done) {
sharp(fixtures.inputJpgWithExifMirroring)
.rotate()
.resize(320)
.withMetadata()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
sharp(data).metadata(function (err, metadata) {
if (err) throw err;
assert.strictEqual(1, metadata.orientation);
fixtures.assertSimilar(fixtures.expected('exif-5.jpg'), data, done);
});
});
});
it('Attempt to auto-rotate using image that has no EXIF', function (done) {
sharp(fixtures.inputJpg).rotate().resize(320).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(261, info.height);
done();
});
});
it('Attempt to auto-rotate image format without EXIF support', function (done) {
sharp(fixtures.inputPng)
.rotate()
.resize(320)
.jpeg()
.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(236, info.height);
done();
});
});
it('Rotate to an invalid angle, should fail', function () {
assert.throws(function () {
sharp(fixtures.inputJpg).rotate(1);
});
});
it('Flip - vertical', function (done) {
sharp(fixtures.inputJpg)
.resize(320)
.flip()
.withMetadata()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height);
sharp(data).metadata(function (err, metadata) {
if (err) throw err;
assert.strictEqual(1, metadata.orientation);
fixtures.assertSimilar(fixtures.expected('flip.jpg'), data, done);
});
});
});
it('Flop - horizontal', function (done) {
sharp(fixtures.inputJpg)
.resize(320)
.flop()
.withMetadata()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height);
sharp(data).metadata(function (err, metadata) {
if (err) throw err;
assert.strictEqual(1, metadata.orientation);
fixtures.assertSimilar(fixtures.expected('flop.jpg'), data, done);
});
});
});
it('Flip and flop', function (done) {
sharp(fixtures.inputJpg)
.resize(320)
.flop()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height);
fixtures.assertSimilar(fixtures.expected('flip-and-flop.jpg'), data, done);
});
});
it('Neither flip nor flop', function (done) {
sharp(fixtures.inputJpg)
.resize(320)
.flip(false)
.flop(false)
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(261, info.height);
fixtures.assertSimilar(fixtures.inputJpg, data, done);
});
});
});