Add support for WebP images - #6311
Conversation
1916e43 to
0915660
Compare
|
(force push to fix formatting using |
0915660 to
7f8db8d
Compare
|
(force push to fix casing of WebP in docs) |
|
Does this increase binary size? Webp is kind of a dead end that's being replaced by other formats. In particular jxl and avif. It can also not be natively embedded in any of typst's targets, with the exception of the experimental HTML |
Before: 35M (34,640,352 bytes) Not noticeably, just by 0.3%.
I would love to use any image format that gives us better compression, but at the moment we use these images in a number of places, including the web & in React Native. Currently, support for JPEG XL is only in Safari. AVIF could potentially be something we could start adopting soon, but it would require us shipping native code in our apps since React Native currently doesn't support it. Now, this is just my use case, and is of course something that you can say is outside the scope of this project. I'm just trying to describe why WebP matters to us. Also, we have a lot of files uploaded in lossy WebP source format from our clients. Switching our current files would probably cause inflation in file size, or drop in quality.
I'm not exactly sure how the output works today, but creating two documents using In any case, this approach doesn't seem to be any worse than converting the images to PNG before loading them into Typst. Again, speaking for my own use case, we are using the PNG rasterizer to render the output as an image. We do want PDF export as well, but as long as the same pixels shows up inside the PDF, which seems to be the case, this is good enough for us. I'm guessing that the potential issue is that the file size might be bigger inside the PDF? |
|
The PDF output will be the same as with a PNG as it is reencoded. The main reason against WebP support in Typst was compile time and dependency count, which is a bit of a moot point given our current dependencies. @reknih What do you think? |
Yes, they will be converted to png. Edit: Or potentially jpg, depending how this was implemented. |
|
@laurmaedje asked me (the resident file format enjoyer at Typst) to comment on this: We have been somewhat apprehensive about adding WebP in the past. Let me lay out our rationale here: Reasons to add WebP
Reasons against WebP
Despite these negatives, I cautiously approve of this PR since including the encoder for SVGs only seems dumb. We're incurring the binary size, security, and maintenance burden anyways and including WebPs is feasible for determined users, it's just harder than it needs to be. With that being said, I am not opposed to completely removing this dependency instead in the upcoming or a future release. |
Not saying that this isn't a concern, but something that I think makes this easier to swallow is that the WebP encoder/decoder is maintained by the same organization (
This is true, but if you already have a WebP file, that is something that needs to happen if you want to put it in a PDF, whichever tool you use. E.g. for my use case the workaround would be to convert the images to PNG myself, which would have the exact same end result.
Today I learned 😁 But it seems like the complaint was that when people tried to save images from websites they got a WebP file, which then their programs couldn't open? I guess this isn't as much of a problem today when tools support WebP, and even then, I think that the tradeoff less bandwidth for every visitor vs. some people have to jump through hoops to save images is an easy choice for most websites...
This could be something to include though. For the HTML export, it seems like it would be better to convert both GIF and PNG to WebP, instead of PNG. Since "WebP lossless images are 26% smaller in size compared to PNGs", and all browsers supports it.
I think that I agree with this. In a design workflow I imagine that you always want to work with the raw data and only perform lossless transformations until the absolute last step, where you compress it in order to deliver the final product. I have no expertise to make me try and comment on the perceptual visual differences vs file size but at least the WebP team has a study linked that backs up their claim that WebP has smaller sizes when the end result looks the same: https://developers.google.com/speed/webp/docs/webp_study
This makes me very happy to hear! 🙌 Let me know if there is anything else I can do to help move this forward! 🚀 |
7f8db8d to
3ecc692
Compare
|
Adressed comments and rebased on latest |
|
Thanks! |
This adds support for loading WebP images, the same way that any other images is loaded.
I'd be happy to add a test for this, but I think that we first need to add a WebP image to the assets package, similar to how it's done with "rhino.png" or "tiger.jpg". Also, I couldn't find any test for GIF files, so maybe the same should be done for a GIF file as well?
As can be seen in the diff over
Cargo.lock, this doesn't add any new dependencies!image-webpis already included in the build (viakrilla).Some background on why I'm submitting this can be found here: #2925 (comment)
I have also made a version that gates this behind a feature flag, it can be found here: main...LinusU:typst:webp