Improve image re-encoding to prevent quality loss
Describe the Feature
Right now, all images get re-encoded as JPEG on upload: https://github.com/bluesky-social/social-app/blob/a0d4af5c5f7a9a1547e1533ffefba2bd7ed68c4a/src/state/gallery.ts#L242-L250
This comes with a few major downsides:
- Images that fit under the imposed size limit are always re-encoded, leading to unavoidable quality loss (even at 100% quality). In some cases, the re-encoded image is larger than the original, defeating the point of re-encoding in the first place.
- All transparency is lost, even if the CDN and app are capable of displaying transparent images now that images are served in WebP by default.
- Since the image is already lossy from the JPEG re-encoding, the CDN's WebP encoding stacks double compression artifacts. Even more layers of compression/bitrot would be applied if the user was already uploading a lossy image.
I propose that the client should only re-encode if the source image is over the size limit (such that small PNGs are still preserved as PNGs), and that re-encodes should be using another format (probably WebP, which is already supported by expo-image-manipulator).
Note that changing the uploaded format doesn't really impact image downloads; the CDN always serves WebP, so users on the web who right click > Save will always get a WebP regardless of the source format. Saving/sharing images on mobile also goes through the CDN's conversion, but it will re-encode to JPEG clientside, which should probably also be fixed: https://github.com/bluesky-social/social-app/blob/main/src/lib/media/manip.ts
Attachments
No response
Describe Alternatives
#9701 is slightly related to this, but mostly just focuses on skipping re-encoding instead of switching to a newer format.
Additional Context
While image quality has somewhat improved from the CDN switching to WebP, all of the images uploaded to Bluesky from social-app have slight compression artifacts baked into the blobs, so almost every image is lossy (with the exception of other clients and bots).
The lossy conversion is even somewhat visible in my pinned post, originally a lossless PNG from my screenshot tool, which now has compression artifacts burned into it around the text. Here's a screenshot of the source blob (not through the CDN) with the contrast bumped up so it's easier to see:
Source: bluesky-social/social-app