fresco icon indicating copy to clipboard operation
fresco copied to clipboard

AutoRotateEnabled is ignored when Postprocessor is set with request

Open maxchursin opened this issue 10 years ago • 10 comments

This is maybe expected behavior, but I can't find where this documented then, I'm trying to implement custom resizing logic for image before sending file out over network (I use Pipeline directly with imagePipeline.fetchDecodedImage(imageRequest, ...) and sending bitmap over on the DataSource callback), I expect image rotation happens on the Decoding step before postprocessor takes that decoded sourceBitmap, but it is not rotated as exif info suggest. On the other hand if I take postprocessor out and add setResizingOptions() instead, the bitmap is rotated correctly on the DataSource callback. Am I missing something? Is there better way to do it?

        ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(contentURI)
                .setAutoRotateEnabled(true)
                .setPostprocessor(new BasePostprocessor() {
                    @Override
                    public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) {
                        float maxPixels = 1080 * 1080;
                        int originalWidth = sourceBitmap.getWidth();
                        int originalHeight = sourceBitmap.getHeight();
                        float scaleFactor = (Math.min(maxPixels / (originalWidth * originalHeight), 1));
                        CloseableReference<Bitmap> bitmapRef = bitmapFactory.createBitmap(
                                ((int) (originalWidth * scaleFactor)),
                                ((int) (originalHeight * scaleFactor)));
                        try {
                            Bitmap destBitmap = bitmapRef.get();
                            for (int x = 0; x < destBitmap.getWidth(); x++) {
                                for (int y = 0; y < destBitmap.getHeight(); y++) {
                                    destBitmap.setPixel(x, y,
                                            sourceBitmap.getPixel((int) (x / scaleFactor), (int) (y / scaleFactor)));
                                }
                            }
                            return CloseableReference.cloneOrNull(bitmapRef);
                        } finally {
                            CloseableReference.closeSafely(bitmapRef);
                        }
                    }
                })
                .build();

maxchursin avatar Jan 28 '16 08:01 maxchursin

May I see the code in your datasource callback?

Btw, Instead of doing for-loop and setPixel/getPixel, it is much more efficient (and the scaling quality will be much better) if you just do something like this:

Canvas canvas = new Canvas(destBitmap);
Rect destRect = new Rect(0,0,destBitmap.width(), destBitmap.height());
canvas.drawBitmap(sourceBitmap, null, destRect, paint);

plamenko avatar Jan 31 '16 12:01 plamenko

Thanks for this canvas trick, I've been trying to find a better way to transform that bitmap without using android's BitmapFactory, and haven't thought to look in canvas. And here is my datasource callback:

        new BaseBitmapDataSubscriber() {
            @Override
            protected void onNewResultImpl(@Nullable Bitmap bitmap) {
                if (bitmap == null) {
                    return;
                }
                ByteArrayOutputStream stream = null;
                try {
                    stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, AttachmentUtils.QUALITY, stream);
                    byteArray = stream.toByteArray();
                } finally {
                    if (stream != null) {
                        try {
                            stream.close();
                        } catch (IOException e) {
                            Crash.logException(e);
                        }
                    }
                }
            }

            @Override
            protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
            }}, new DefaultExecutorSupplier(3).forBackgroundTasks()

maxchursin avatar Feb 02 '16 22:02 maxchursin

It actually doesn't work with or without postprocessor, the rotation is lost in either case

maxchursin avatar Feb 16 '16 20:02 maxchursin

I think you need to set ResizeOptions in order for image to get rotated. Can you try that? Anyhow, this looks like a bug and we need to fix it.

plamenko avatar Feb 17 '16 13:02 plamenko

Have you enabled downsampling in your ImagePipelineConfig? I've been trying to reproduce your problem and the only way I've managed to is by doing this.

The reason is that if downsampling is used instead of resizing, rotation is not automatically done at that early stage but the rotation angle is retained and passed along to Drawee so that the GPU can do the rotation at draw time.

The problem in this case though is that the rotation angle isn't provided to either the post processor or the subscriber.

What you could do is ensure downsampling is off and simply replace your post-processor with .setResizeOptions(new ResizeOptions(1080, 1080)) which will reduce the image if necessary and fix the rotation.

kirwan avatar Feb 18 '16 15:02 kirwan

@kirwan I do have downsampling enabled and I'll give it a try without, but the thing is - I want postprocessor to do resizing, and in my opinion this is what it meant for - to implement custom image mutation not provided by the lib out of the box. This is a true - the rotation angle is not provided in BaseBitmapDataSubscriber but it is in BaseDataSubscriber, so what I currently end up doing is use the later subscriber and do rotation manually there, this may not be the best solution from memory consumption point of view, since I ended up creating new bitmap. The expected behaviour for me here is: bitmap provided in BaseBitmapDataSubscriber as well as one in PostProcessor should be already rotated if setAutoRotateEnabled(true) Does this sounds reasonable for you?

maxchursin avatar Feb 18 '16 23:02 maxchursin

You're absolutely right. The current behaviour wasn't intended. We've discussed a solution internally without wanting to create any breaking changes to the API. I can't say right now when we'll fix this though.

kirwan avatar Feb 23 '16 14:02 kirwan

I Have same problem~

public void setImageUri(Uri uri, int width, int height) { ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri). setResizeOptions(new ResizeOptions(width, height)). setAutoRotateEnabled(true).build();

    ImagePipeline imagePipeline = Fresco.getImagePipeline();
    final DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, this);

    DraweeController controller = Fresco.newDraweeControllerBuilder().
            setTapToRetryEnabled(true).
            setOldController(mDraweeHolder.getController()).
            setImageRequest(imageRequest).setControllerListener(new BaseControllerListener<ImageInfo>() {

        @Override public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) {
            super.onFinalImageSet(id, imageInfo, animatable);

            try {
                imageReference = dataSource.getResult();
                if (imageReference != null) {
                    CloseableImage image = imageReference.get();
                    if (image != null && image instanceof CloseableStaticBitmap) {
                        CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) image;
                        Bitmap bitmap = closeableStaticBitmap.getUnderlyingBitmap();
                        if (bitmap != null) {

                            //rotate bitmap
                            setImageBitmap(bitmap);
                        }
                    }
                }
            } finally {
                dataSource.close();
                CloseableReference.closeSafely(imageReference);
            }
        }
    }).build();
    mDraweeHolder.setController(controller);
}

this is code is PhotoView + fresco for load Sdcard images

ImagePipelineConfig setDownsampleEnabled(true);

but setAutoRotate..... is Invalid ~

@kirwan Link Code

ikkyu0319 avatar Mar 18 '16 03:03 ikkyu0319

I've just completed a series of changes to be included in the next Fresco release.

This adds a RotationOptions class which can be specified when building an image request. As well as having a new API, several fixes have been made with respect to rotation when downscaling is enabled, when a postprocessor is attached and other cases we were aware of.

The default behaviour will now be to fix the rotation in the pipeline so that by the time a post-processor is triggered, the image is fixed.

kirwan avatar Sep 14 '16 14:09 kirwan

The initial commit for these changes is 36dd783deed7299ecf16b37a2d56cc66d07fb637.

kirwan avatar Sep 14 '16 14:09 kirwan