Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: support premultiplied raw pixel input #1599

Closed
justewg opened this issue Mar 5, 2019 · 8 comments
Closed

Enhancement: support premultiplied raw pixel input #1599

justewg opened this issue Mar 5, 2019 · 8 comments

Comments

@justewg
Copy link

justewg commented Mar 5, 2019

Thanks for utility, great work.
I use the utility inside the map-tile-server-gl node-app. Server configuration when rendering raster tiles allows you to set the color of the lines in rgba. But as a result, PNG-images enclose a gray or black substrate under the translucent white color. Result on the attached screenshot.
Images on the server are created as follows:

var image = sharp(data, {
          raw: {
            width: params.width * scale,
            height: params.height * scale,
            channels: 4
          }
        })
...
image.png()

I tried using .embed (), .ensureAlpha (), passing the create parameter to the constructor, pointing to background with transparent white, but it didn’t work.
How do I get a white backing in white translucent color, or do I get a 24-bit PNG supporting translucent colors?
image

@lovell
Copy link
Owner

lovell commented Mar 5, 2019

Hello, what is the source of data? Could it have been premultiplied?

What does ... refer to in this code sample? Are you able to provide a standalone code sample and input image data that exhibits this behaviour?

@lovell lovell added the question label Mar 5, 2019
@justewg
Copy link
Author

justewg commented Mar 5, 2019

@lovell
Copy link
Owner

lovell commented Mar 5, 2019

mapbox/mapbox-gl-native#9124

"The Node binding however, returns the premultiplied image buffer as is"

@justewg
Copy link
Author

justewg commented Mar 5, 2019

Sorry, I didn't quite understand what it was about in this post.
The point is that in any case, the image will be with a gray stroke under the translucent pixels?

@lovell
Copy link
Owner

lovell commented Mar 5, 2019

It looks like tileserver-gl uses mapbox-gl-native to render, which returns RGBA data that has been premultiplied. This is what's causing the artifacts you're seeing on (semi-)transparent pixels.

The quick fix would probably be to modify tileserver-gl to unpremultiply pixel values before passing them to sharp - there's an example of what's required to do this in consbio/mbgl-renderer#25

@lovell lovell changed the title Semi-transparent outlines Semi-transparent outlines on raw, premultiplied input Mar 5, 2019
@justewg
Copy link
Author

justewg commented Mar 6, 2019

Thanks for help, I've add the issue to server's author, because haven't found the solution myself. The are no using mbgl-renderer as javascript-library, I see only mbgl/renderer c-sources and can't move futher, unfortunately

@lovell
Copy link
Owner

lovell commented Apr 1, 2019

Let's leave this open to track a future possible enhancement to flag premultiplied raw pixel data input via an additional property on the raw object.

@lovell lovell changed the title Semi-transparent outlines on raw, premultiplied input Enhancement: support premultiplied raw pixel input Apr 1, 2019
@lovell lovell added this to the v0.28.2 milestone May 3, 2021
@lovell
Copy link
Owner

lovell commented May 10, 2021

v0.28.2 now available with support for this via the following API:

sharp(input, { raw: { premultiplied: true, ... } })...

@lovell lovell closed this as completed May 10, 2021
Caerbannog added a commit to liberty-rider/tileserver-gl that referenced this issue Nov 20, 2023
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>
Caerbannog added a commit to liberty-rider/tileserver-gl that referenced this issue Nov 23, 2023
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>
acalcutt pushed a commit to maptiler/tileserver-gl that referenced this issue Nov 23, 2023
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants