Files

395 lines
12 KiB
JavaScript

/*!
Copyright 2013 Lovell Fuller and others.
SPDX-License-Identifier: Apache-2.0
*/
const { suite, test } = require('node:test');
const sharp = require('../../');
const fixtures = require('../fixtures');
suite('Raw pixel data', () => {
suite('Raw pixel input', () => {
test('Empty data', (t) => {
t.plan(4);
t.assert.throws(() => {
sharp(Buffer.from(''));
}, /empty/);
t.assert.throws(() => {
sharp(new ArrayBuffer(0));
}, /empty/);
t.assert.throws(() => {
sharp(new Uint8Array(0));
}, /empty/);
t.assert.throws(() => {
sharp(new Uint8ClampedArray(0));
}, /empty/);
});
test('Missing options', (t) => {
t.plan(1);
t.assert.throws(() => {
sharp({ raw: {} });
});
});
test('Incomplete options', (t) => {
t.plan(1);
t.assert.throws(() => {
sharp({ raw: { width: 1, height: 1 } });
});
});
test('Invalid channels', (t) => {
t.plan(1);
t.assert.throws(() => {
sharp({ raw: { width: 1, height: 1, channels: 5 } });
});
});
test('Invalid height', (t) => {
t.plan(1);
t.assert.throws(() => {
sharp({ raw: { width: 1, height: 0, channels: 4 } });
});
});
test('Invalid width', (t) => {
t.plan(1);
t.assert.throws(() => {
sharp({ raw: { width: 'zoinks', height: 1, channels: 4 } });
});
});
test('Width beyond pixel limit', (t) => {
t.plan(1);
t.assert.throws(() => {
sharp({ raw: { width: 100000001, height: 1, channels: 4 } });
});
});
test('Height beyond pixel limit', (t) => {
t.plan(1);
t.assert.throws(() => {
sharp({ raw: { width: 1, height: 100000001, channels: 4 } });
});
});
test('Invalid premultiplied', (t) => {
t.plan(1);
t.assert.throws(
() => sharp({ raw: { width: 1, height: 1, channels: 4, premultiplied: 'zoinks' } }),
/Expected boolean for raw\.premultiplied but received zoinks of type string/
);
});
test('Invalid pageHeight', (t) => {
t.plan(4);
const width = 8;
const height = 8;
const channels = 4;
t.assert.throws(
() => sharp({ raw: { width, height, channels, pageHeight: 'zoinks' } }),
/Expected positive integer for raw\.pageHeight but received zoinks of type string/
);
t.assert.throws(
() => sharp({ raw: { width, height, channels, pageHeight: -1 } }),
/Expected positive integer for raw\.pageHeight but received -1 of type number/
);
t.assert.throws(
() => sharp({ raw: { width, height, channels, pageHeight: 9 } }),
/Expected positive integer for raw\.pageHeight but received 9 of type number/
);
t.assert.throws(
() => sharp({ raw: { width, height, channels, pageHeight: 3 } }),
/Expected raw\.height 8 to be a multiple of raw\.pageHeight 3/
);
});
test('RGB', async (t) => {
t.plan(6);
// Convert to raw pixel data
const raw = await sharp(fixtures.inputJpg)
.resize(256)
.raw()
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual(256, raw.info.width);
t.assert.strictEqual(209, raw.info.height);
t.assert.strictEqual(3, raw.info.channels);
// Convert back to JPEG
const jpeg = await sharp(raw.data, {
raw: {
width: raw.info.width,
height: raw.info.height,
channels: raw.info.channels
}
})
.jpeg()
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual(256, jpeg.info.width);
t.assert.strictEqual(209, jpeg.info.height);
t.assert.strictEqual(3, jpeg.info.channels);
await fixtures.assertSimilar(fixtures.inputJpg, jpeg.data);
});
test('RGBA', async (t) => {
t.plan(6);
// Convert to raw pixel data
const raw = await sharp(fixtures.inputPngOverlayLayer1)
.resize(256)
.raw()
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual(256, raw.info.width);
t.assert.strictEqual(192, raw.info.height);
t.assert.strictEqual(4, raw.info.channels);
// Convert back to PNG
const png = await sharp(raw.data, {
raw: {
width: raw.info.width,
height: raw.info.height,
channels: raw.info.channels
}
})
.png()
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual(256, png.info.width);
t.assert.strictEqual(192, png.info.height);
t.assert.strictEqual(4, png.info.channels);
await fixtures.assertSimilar(fixtures.inputPngOverlayLayer1, png.data, { threshold: 7 });
});
test('RGBA premultiplied', async (t) => {
t.plan(7);
// Convert to raw pixel data
const raw = await sharp(fixtures.inputPngSolidAlpha)
.resize(256)
.raw()
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual(256, raw.info.width);
t.assert.strictEqual(192, raw.info.height);
t.assert.strictEqual(4, raw.info.channels);
const originalData = Buffer.from(raw.data);
// Premultiply image data
for (let i = 0; i < raw.data.length; i += 4) {
const alpha = raw.data[i + 3];
const norm = alpha / 255;
if (alpha < 255) {
raw.data[i] = Math.round(raw.data[i] * norm);
raw.data[i + 1] = Math.round(raw.data[i + 1] * norm);
raw.data[i + 2] = Math.round(raw.data[i + 2] * norm);
}
}
// Convert back to PNG
const unpremultiplied = await sharp(raw.data, {
raw: {
width: raw.info.width,
height: raw.info.height,
channels: raw.info.channels,
premultiplied: true
}
})
.raw()
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual(256, unpremultiplied.info.width);
t.assert.strictEqual(192, unpremultiplied.info.height);
t.assert.strictEqual(4, unpremultiplied.info.channels);
t.assert.equal(unpremultiplied.data.compare(originalData), 0, 'output buffer matches unpremultiplied input buffer');
});
test('JPEG to raw Stream and back again', async (t) => {
t.plan(3);
const width = 32;
const height = 24;
const writable = sharp({
raw: {
width,
height,
channels: 3
}
});
const finished = new Promise((resolve, reject) => {
writable
.jpeg()
.toBuffer((err, _data, info) => {
if (err) {
reject(err);
return;
}
t.assert.strictEqual('jpeg', info.format);
t.assert.strictEqual(32, info.width);
t.assert.strictEqual(24, info.height);
resolve();
});
});
sharp(fixtures.inputJpg)
.resize(width, height)
.raw()
.pipe(writable);
await finished;
});
});
suite('Output raw, uncompressed image data', () => {
test('1 channel greyscale image', async (t) => {
t.plan(6);
const { data, info } = await sharp(fixtures.inputJpg)
.greyscale()
.resize(32, 24)
.raw()
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual(32 * 24 * 1, info.size);
t.assert.strictEqual(data.length, info.size);
t.assert.strictEqual('raw', info.format);
t.assert.strictEqual(32, info.width);
t.assert.strictEqual(24, info.height);
t.assert.strictEqual(1, info.channels);
});
test('3 channel colour image without transparency', async (t) => {
t.plan(5);
const { data, info } = await sharp(fixtures.inputJpg)
.resize(32, 24)
.toFormat('raw')
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual(32 * 24 * 3, info.size);
t.assert.strictEqual(data.length, info.size);
t.assert.strictEqual('raw', info.format);
t.assert.strictEqual(32, info.width);
t.assert.strictEqual(24, info.height);
});
test('4 channel colour image with transparency', async (t) => {
t.plan(5);
const { data, info } = await sharp(fixtures.inputPngWithTransparency)
.resize(32, 24)
.toFormat(sharp.format.raw)
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual(32 * 24 * 4, info.size);
t.assert.strictEqual(data.length, info.size);
t.assert.strictEqual('raw', info.format);
t.assert.strictEqual(32, info.width);
t.assert.strictEqual(24, info.height);
});
test('Extract A from RGBA', async (t) => {
t.plan(3);
const { info } = await sharp(fixtures.inputPngWithTransparency)
.resize(32, 24)
.extractChannel(3)
.toColourspace('b-w')
.raw()
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual('raw', info.format);
t.assert.strictEqual(1, info.channels);
t.assert.strictEqual(32 * 24, info.size);
});
});
suite('Raw pixel depths', () => {
test('Invalid depth', (t) => {
t.plan(1);
t.assert.throws(() => {
sharp(Buffer.alloc(3), { raw: { width: 1, height: 1, channels: 3 } })
.raw({ depth: 'zoinks' });
});
});
for (const { type, depth, bits } of [
{ type: Uint8Array, depth: undefined, bits: 8 },
{ type: Uint8Array, depth: 'uchar', bits: 8 },
{ type: Uint8ClampedArray, depth: 'uchar', bits: 8 },
{ type: Int8Array, depth: 'char', bits: 8 },
{ type: Uint16Array, depth: 'ushort', bits: 16 },
{ type: Int16Array, depth: 'short', bits: 16 },
{ type: Uint32Array, depth: 'uint', bits: 32 },
{ type: Int32Array, depth: 'int', bits: 32 },
{ type: Float32Array, depth: 'float', bits: 32 },
{ type: Float64Array, depth: 'double', bits: 64 }
]) {
test(type.name, async (t) => {
t.plan(depth === undefined ? 4 : 5);
const { data, info } = await sharp(new type(3), { raw: { width: 1, height: 1, channels: 3 } })
.raw({ depth })
.toBuffer({ resolveWithObject: true });
t.assert.strictEqual(1, info.width);
t.assert.strictEqual(1, info.height);
t.assert.strictEqual(3, info.channels);
if (depth !== undefined) {
t.assert.strictEqual(depth, info.depth);
}
t.assert.strictEqual(data.length / 3, bits / 8);
});
}
});
test('Animated', async (t) => {
t.plan(4);
const gif = await sharp(
Buffer.alloc(8),
{ raw: { width: 1, height: 2, channels: 4, pageHeight: 1 }, animated: true }
)
.gif({ keepDuplicateFrames: true })
.toBuffer();
const { width, height, pages, delay } = await sharp(gif).metadata();
t.assert.strictEqual(width, 1);
t.assert.strictEqual(height, 1);
t.assert.strictEqual(pages, 2);
t.assert.strictEqual(delay.length, 2);
});
suite('16-bit roundtrip', () => {
test('grey', async (t) => {
t.plan(1);
const grey = 42000;
const png = await sharp(
Uint16Array.from([grey]),
{ raw: { width: 1, height: 1, channels: 1 } }
)
.toColourspace('grey16')
.png({ compressionLevel: 0 })
.toBuffer();
const raw = await sharp(png)
.toColourspace('grey16')
.raw({ depth: 'ushort' })
.toBuffer();
if (fixtures.isLittleEndian) {
t.assert.strictEqual(raw.readUint16LE(0), grey);
} else {
t.assert.strictEqual(raw.readUint16BE(0), grey);
}
});
test('RGB', async (t) => {
t.plan(3);
const rgb = [10946, 28657, 46368];
const png = await sharp(
Uint16Array.from(rgb),
{ raw: { width: 1, height: 1, channels: 3 } }
)
.toColourspace('rgb16')
.png({ compressionLevel: 0 })
.toBuffer();
const raw = await sharp(png)
.toColourspace('rgb16')
.raw({ depth: 'ushort' })
.toBuffer();
if (fixtures.isLittleEndian) {
t.assert.strictEqual(raw.readUint16LE(0), rgb[0]);
t.assert.strictEqual(raw.readUint16LE(2), rgb[1]);
t.assert.strictEqual(raw.readUint16LE(4), rgb[2]);
} else {
t.assert.strictEqual(raw.readUint16BE(0), rgb[0]);
t.assert.strictEqual(raw.readUint16BE(2), rgb[1]);
t.assert.strictEqual(raw.readUint16BE(4), rgb[2]);
}
});
});
});