facebookincubator / spectrum

A client-side image transcoding library.
https://libspectrum.io
MIT License
1.99k stars 166 forks source link

lossless jpg rotation by modifying exif-orientation? #193

Closed k3b closed 3 years ago

k3b commented 4 years ago

In my android app https://github.com/k3b/LosslessJpgCrop/ I use sprectrum to do lossless cropping/rotation of jpg images

I have observed that rotating a jpg image without cropping the file size changes it-s file size and rotation is done throuh image manipulation.

I would have expected that only the exif-orientation flag is updated.

If i rotate an image with exif-orientation=90 degrees by 180 degrees i expect the same image but with orientation 270 degrees.

The current code does an image-rotation transformation and result exif-orientation is always 0

Although rotating changes image-file size, is it still loss-less?

I am using this code:

public void crop(InputStream inputStream, OutputStream outputStream, int left, int top, int right, int bottom, int relativeRotationInDegrees) throws IOException {
    final EncodeRequirement encoding =
            new EncodeRequirement(EncodedImageFormat.JPEG, 80, EncodeRequirement.Mode.LOSSLESS);
    try {
        final TranscodeOptions.Builder optionsBuilder = TranscodeOptions
                .Builder(encoding)
                .cropAbsoluteToOrigin(left, top, right, bottom, false);
        if (relativeRotationInDegrees != 0) {
            optionsBuilder.rotate(relativeRotationInDegrees);
        }
        mSpectrum.transcode(
                EncodedImageSource.from(inputStream),
                EncodedImageSink.from(outputStream),
                optionsBuilder.build(),
                "my_callsite_identifier");
    } catch (Exception ex) {
        throw new IOException("Cannot Transcode from " + inputStream + " to " + outputStream + " : " + ex.getMessage(), ex);
    }
}
cuva commented 4 years ago

Hi @k3b

Thanks for your question. What you can do is check the ruleName member of the SpectrumResult returned by the transcode method to check which rule has been used.

libjpeg_lossless_rotate_and_crop is the lossless rule. This should confirm wether spectrum is doing a lossless or lossy operation.