chore: simplify alpha premultiplication, now supported by sharp

Maplibre-native outputs premultiplied pixels values. The sharp library did not
support it so we added code to cancel the alpha premultiplication.
Note that this can only visible onr raster tiles (and probably static maps).

The sharp library now supports premultiplied pixels with the right config.
Let's use it: it should be faster and easie to maintain.

Feature announced here:
https://github.com/lovell/sharp/issues/1599#issuecomment-837004081

Feature developped here by @mnutt:
https://github.com/lovell/sharp/pull/2685

Signed-off-by: Martin d'Allens <martin.dallens@liberty-rider.com>
This commit is contained in:
Martin d'Allens 2023-11-20 17:25:59 +01:00
parent 526766c8f4
commit 26f59ff7af

View file

@ -428,24 +428,9 @@ const respondImage = (
return res.status(500).header('Content-Type', 'text/plain').send(err); return res.status(500).header('Content-Type', 'text/plain').send(err);
} }
// Fix semi-transparent outlines on raw, premultiplied input
// https://github.com/maptiler/tileserver-gl/issues/350#issuecomment-477857040
for (let i = 0; i < data.length; i += 4) {
const alpha = data[i + 3];
const norm = alpha / 255;
if (alpha === 0) {
data[i] = 0;
data[i + 1] = 0;
data[i + 2] = 0;
} else {
data[i] = data[i] / norm;
data[i + 1] = data[i + 1] / norm;
data[i + 2] = data[i + 2] / norm;
}
}
const image = sharp(data, { const image = sharp(data, {
raw: { raw: {
premultiplied: true,
width: params.width * scale, width: params.width * scale,
height: params.height * scale, height: params.height * scale,
channels: 4, channels: 4,