Skip to content

Commit

Permalink
chore: simplify alpha premultiplication, now supported by sharp (#1073)
Browse files Browse the repository at this point in the history
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:
lovell/sharp#1599 (comment)

Feature developped here by @mnutt:
lovell/sharp#2685

Signed-off-by: Martin d'Allens <martin.dallens@liberty-rider.com>
  • Loading branch information
Caerbannog committed Nov 23, 2023
1 parent 526766c commit b25a642
Showing 1 changed file with 1 addition and 16 deletions.
17 changes: 1 addition & 16 deletions src/serve_rendered.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b25a642

Please sign in to comment.