From b25a6420dda4000d6512929930cf9d2608cd148a Mon Sep 17 00:00:00 2001 From: Martin d'Allens Date: Thu, 23 Nov 2023 18:16:31 +0100 Subject: [PATCH] chore: simplify alpha premultiplication, now supported by sharp (#1073) 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 --- src/serve_rendered.js | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/serve_rendered.js b/src/serve_rendered.js index fbdd1f0..0beb383 100644 --- a/src/serve_rendered.js +++ b/src/serve_rendered.js @@ -428,24 +428,9 @@ const respondImage = ( 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, { raw: { + premultiplied: true, width: params.width * scale, height: params.height * scale, channels: 4,