You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(Could also be relevant for some other formats if typst currently does not do so)
For JPEG, JPEG2000, non-interlaced PNG, TIFF images with CCITT Group 4 encoded data, and JBIG2 with single-page generic coding (e.g. using jbig2enc), img2pdf directly embeds the image data into the PDF without re-encoding it. It thus treats the PDF format merely as a container format for the image data. In these cases, img2pdf only increases the filesize by the size of the PDF container (typically around 500 to 700 bytes). Since data is only copied and not re-encoded, img2pdf is also typically faster than other solutions for these input formats.
This was considered as not possible for JPEG because the file can be invalid. Without decoding it, it would not be possible to validate if "file.jpg" or "file.png" are expected image files.
As mentioned in the linked issue, currently an inconsistent "hack" exists to already allow what you request: embed the file in a PDF, then all next times embed the PDF instead of the PNG/JPG. For PDF embedding, the file contents are currently not checked and just copied instead, even if the PDF is invalid.
Related to this (because not many people know this): PNG and JPG have the same API (iteration by scanlines). A very efficient implementation could subsample an image (for cropped images, or even scaled images) without decoding it completely. This is done by skipping each Y scanlines and each X columns. The problem is that only the C implementations support this. The rust image people all have ignored this. As a result, decoding JPG and PNG in Rust always requires a full decompression, I think even to RGBA even if it's greyscale or monochrome, even if we could crop and/or scale an image from a big 50 MB image with only constant memory requirement.
Yes, this is what is done with JPG. But decoding the image is required. Reencoding is not. Your issue asked for "without decoding".
I mentioned both, but the no reencoding would be by far the most important. Encoding is generally much slower than decoding. Also, crucially, we wouldn't undo the effort of png optimizers.
I'd like to move this along, especially with LaurenzV/krilla#415 merged, but krilla has not made a release with all the changes yet. @LaurenzV do you have any release plans to unblock this?
It is "complete" for opaque PNG images. You might be able to get some savings with certain types of transparent PNGs as well, but all the low-hanging fruits (and the most important cases) are already implemented.
@TechPizzaDev Thanks for the reminder, I didn't notice the version bump either! I already have some integration code from before, will put up a PR in a bit.
Activity
jqwv commentedon Jan 18, 2026
This was considered as not possible for JPEG because the file can be invalid. Without decoding it, it would not be possible to validate if "file.jpg" or "file.png" are expected image files.
As mentioned in the linked issue, currently an inconsistent "hack" exists to already allow what you request: embed the file in a PDF, then all next times embed the PDF instead of the PNG/JPG. For PDF embedding, the file contents are currently not checked and just copied instead, even if the PDF is invalid.
Related to this (because not many people know this): PNG and JPG have the same API (iteration by scanlines). A very efficient implementation could subsample an image (for cropped images, or even scaled images) without decoding it completely. This is done by skipping each Y scanlines and each X columns. The problem is that only the C implementations support this. The rust image people all have ignored this. As a result, decoding JPG and PNG in Rust always requires a full decompression, I think even to RGBA even if it's greyscale or monochrome, even if we could crop and/or scale an image from a big 50 MB image with only constant memory requirement.
Enivex commentedon Jan 18, 2026
Even if you need to verify that an image is valid by decoding, you can still embed after doing that instead of reencoding.
jqwv commentedon Jan 18, 2026
Yes, this is what is done with JPG. But decoding the image is required. Reencoding is not.
Your issue asked for "without decoding".
Enivex commentedon Jan 18, 2026
I mentioned both, but the no reencoding would be by far the most important. Encoding is generally much slower than decoding. Also, crucially, we wouldn't undo the effort of png optimizers.
[-]Inclusion of png in pdf without reencoding[/-][+]Inclusion of PNG in PDF without reencoding[/+]andylizi commentedon Feb 2, 2026
Most of the work would be done in krilla. I've made an attempt at LaurenzV/krilla#331
TechPizzaDev commentedon Sep 26, 2026
I'd like to move this along, especially with LaurenzV/krilla#415 merged, but krilla has not made a release with all the changes yet.
@LaurenzV do you have any release plans to unblock this?
LaurenzV commentedon Sep 26, 2026
I believe typst already uses a git revision that contains the change, so shouldn't be blocked on a new release.
TechPizzaDev commentedon Sep 26, 2026
Oh yeah, it's pointing at 7772dbe, from July 31st. That makes it easier, sorry about the ping!
Enivex commentedon Sep 26, 2026
Is the implementation in krilla complete?
Ideally this should also happen for jpg if it doesn't already.
LaurenzV commentedon Sep 26, 2026
Pretty sure JPEG already works this way.
It is "complete" for opaque PNG images. You might be able to get some savings with certain types of transparent PNGs as well, but all the low-hanging fruits (and the most important cases) are already implemented.
andylizi commentedon Sep 26, 2026
@TechPizzaDev Thanks for the reminder, I didn't notice the version bump either! I already have some integration code from before, will put up a PR in a bit.