|
6 | 6 | const { suite, test } = require('node:test'); |
7 | 7 |
|
8 | 8 | const sharp = require('../../'); |
| 9 | +const fixtures = require('../fixtures'); |
9 | 10 |
|
10 | 11 | suite('JXL', () => { |
11 | 12 | test('called without options does not throw an error', (t) => { |
@@ -114,4 +115,93 @@ suite('JXL', () => { |
114 | 115 | sharp().jxl({ effort: 'fail' }); |
115 | 116 | }); |
116 | 117 | }); |
| 118 | + test('invalid loop throws', (t) => { |
| 119 | + t.plan(2); |
| 120 | + t.assert.throws(() => { |
| 121 | + sharp().jxl({ loop: -1 }); |
| 122 | + }); |
| 123 | + t.assert.throws(() => { |
| 124 | + sharp().jxl({ loop: 65536 }); |
| 125 | + }); |
| 126 | + }); |
| 127 | + test('invalid delay throws', (t) => { |
| 128 | + t.plan(2); |
| 129 | + t.assert.throws(() => { |
| 130 | + sharp().jxl({ delay: -1 }); |
| 131 | + }); |
| 132 | + t.assert.throws(() => { |
| 133 | + sharp().jxl({ delay: [65536] }); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + suite('animated', { skip: !sharp.format.jxl.input.buffer || !sharp.format.jxl.output.buffer }, () => { |
| 138 | + test('should read animated image', async (t) => { |
| 139 | + t.plan(5); |
| 140 | + const { format, pages, pageHeight, loop, delay } = await sharp(fixtures.inputJxlAnimated, { pages: -1 }).metadata(); |
| 141 | + t.assert.strictEqual(format, 'jxl'); |
| 142 | + t.assert.strictEqual(pages, 30); |
| 143 | + t.assert.strictEqual(pageHeight, 80); |
| 144 | + t.assert.strictEqual(loop, 0); |
| 145 | + t.assert.deepStrictEqual(delay, Array(30).fill(30)); |
| 146 | + }); |
| 147 | + test('should repeat a single delay for all frames', async (t) => { |
| 148 | + t.plan(1); |
| 149 | + const data = await sharp(fixtures.inputJxlAnimated, { pages: -1 }) |
| 150 | + .jxl({ delay: 100 }) |
| 151 | + .toBuffer(); |
| 152 | + const updated = await sharp(data, { pages: -1 }).metadata(); |
| 153 | + |
| 154 | + t.assert.deepStrictEqual(updated.delay, Array(updated.pages).fill(100)); |
| 155 | + }); |
| 156 | + test('should limit animation loop', async (t) => { |
| 157 | + t.plan(1); |
| 158 | + const data = await sharp(fixtures.inputJxlAnimated, { pages: -1 }) |
| 159 | + .jxl({ loop: 3 }) |
| 160 | + .toBuffer(); |
| 161 | + const updated = await sharp(data, { pages: -1 }).metadata(); |
| 162 | + |
| 163 | + t.assert.strictEqual(updated.loop, 3); |
| 164 | + }); |
| 165 | + test('should change delay between frames', async (t) => { |
| 166 | + t.plan(1); |
| 167 | + const original = await sharp(fixtures.inputJxlAnimated, { pages: -1 }).metadata(); |
| 168 | + |
| 169 | + const expectedDelay = [...Array(original.pages).fill(40)]; |
| 170 | + const data = await sharp(fixtures.inputJxlAnimated, { pages: -1 }) |
| 171 | + .jxl({ delay: expectedDelay }) |
| 172 | + .toBuffer(); |
| 173 | + const updated = await sharp(data, { pages: -1 }).metadata(); |
| 174 | + |
| 175 | + t.assert.deepStrictEqual(updated.delay, expectedDelay); |
| 176 | + }); |
| 177 | + test('should preserve delay between frames', async (t) => { |
| 178 | + t.plan(1); |
| 179 | + const data = await sharp(fixtures.inputJxlAnimated, { pages: -1 }) |
| 180 | + .jxl() |
| 181 | + .toBuffer(); |
| 182 | + const updated = await sharp(data, { pages: -1 }).metadata(); |
| 183 | + |
| 184 | + t.assert.deepStrictEqual(updated.delay, Array(30).fill(30)); |
| 185 | + }); |
| 186 | + test('should round trip to file', async (t) => { |
| 187 | + t.plan(3); |
| 188 | + const output = fixtures.path('output.animated.jxl'); |
| 189 | + await sharp(fixtures.inputJxlAnimated, { pages: -1 }).jxl({ loop: 3 }).toFile(output); |
| 190 | + const { pages, loop, delay } = await sharp(output, { pages: -1 }).metadata(); |
| 191 | + t.assert.strictEqual(pages, 30); |
| 192 | + t.assert.strictEqual(loop, 3); |
| 193 | + t.assert.deepStrictEqual(delay, Array(30).fill(30)); |
| 194 | + }); |
| 195 | + test('should resize animated image to page height', async (t) => { |
| 196 | + t.plan(2); |
| 197 | + const data = await sharp(fixtures.inputJxlAnimated, { pages: -1 }) |
| 198 | + .resize({ height: 40 }) |
| 199 | + .jxl({ effort: 1 }) |
| 200 | + .toBuffer(); |
| 201 | + const updated = await sharp(data, { pages: -1 }).metadata(); |
| 202 | + |
| 203 | + t.assert.strictEqual(updated.height, 40 * 30); |
| 204 | + t.assert.strictEqual(updated.pageHeight, 40); |
| 205 | + }); |
| 206 | + }); |
117 | 207 | }); |
0 commit comments