diff --git a/lib/output.js b/lib/output.js index 65281123..a5194bff 100644 --- a/lib/output.js +++ b/lib/output.js @@ -25,7 +25,9 @@ const formats = new Map([ ['j2c', 'jp2'] ]); -const errJp2Save = new Error('JP2 output requires libvips with support for OpenJPEG'); +const jp2Regex = /\.jp[2x]|j2[kc]$/i; + +const errJp2Save = () => new Error('JP2 output requires libvips with support for OpenJPEG'); const bitdepthFromColourCount = (colours) => 1 << 31 - Math.clz32(Math.ceil(Math.log2(colours))); @@ -68,6 +70,8 @@ function toFile (fileOut, callback) { err = new Error('Missing output file path'); } else if (is.string(this.options.input.file) && path.resolve(this.options.input.file) === path.resolve(fileOut)) { err = new Error('Cannot use same file for input and output'); + } else if (jp2Regex.test(fileOut) && !this.constructor.format.jp2k.output.file) { + err = errJp2Save(); } if (err) { if (is.fn(callback)) { @@ -630,7 +634,7 @@ function gif (options) { /* istanbul ignore next */ function jp2 (options) { if (!this.constructor.format.jp2k.output.buffer) { - throw errJp2Save; + throw errJp2Save(); } if (is.object(options)) { if (is.defined(options.quality)) { diff --git a/test/unit/jp2.js b/test/unit/jp2.js index fd305f77..4b7dcf4c 100644 --- a/test/unit/jp2.js +++ b/test/unit/jp2.js @@ -8,20 +8,20 @@ const fixtures = require('../fixtures'); describe('JP2 output', () => { if (!sharp.format.jp2k.input.buffer) { - it('JP2 output should fail due to missing OpenJPEG', () => { - assert.rejects(() => + it('JP2 output should fail due to missing OpenJPEG', () => + assert.rejects(async () => sharp(fixtures.inputJpg) .jp2() .toBuffer(), /JP2 output requires libvips with support for OpenJPEG/ - ); - }); + ) + ); - it('JP2 file output should fail due to missing OpenJPEG', () => { - assert.rejects(async () => await sharp().toFile('test.jp2'), + it('JP2 file output should fail due to missing OpenJPEG', () => + assert.rejects(async () => sharp(fixtures.inputJpg).toFile('test.jp2'), /JP2 output requires libvips with support for OpenJPEG/ - ); - }); + ) + ); } else { it('JP2 Buffer to PNG Buffer', () => { sharp(fs.readFileSync(fixtures.inputJp2))