fresco icon indicating copy to clipboard operation
fresco copied to clipboard

Why do Postprocessors modify the source Bitmap?

Open nat-e opened this issue 11 years ago • 11 comments

Hi! I was wondering why you designed the Postprocessors to modify the source Bitmap, instead of returning a new one like Picasso/Glide's Transformations. I find that often it's very hard to modify a bitmap in place, so you end up having to create a new result Bitmap, and then copying all its pixels over to the source one, which seems less efficient. Thanks!

nat-e avatar Apr 01 '15 15:04 nat-e

Because, on Android 4.x, the bitmaps are purgeable - the big pixel data lives in the ashmem heap, not the Java heap. If you use the Bitmap.copy operation this will put all the pixels on the Java heap, the very thing Fresco is designed to avoid.

Internally, Fresco copies the Bitmap for you, so that any changes you make to it don't affect the version in cache. Moreover, Fresco's copy is done safely, keeping the pixel data in ashmem.

Can you tell us what operation you are doing that is hard to do in-place?

tyronen avatar Apr 01 '15 15:04 tyronen

I'm doing some kind of "stack" effect on that bitmap, where the source image is blurred, scaled down a little, and painted again in the output multiple times, if that makes sense. Maybe I'm missing a better way to do this, but because I need to access the source bitmap to paint each "layer", I don't think I can just modify that source bitmap. This processing takes in average ~50ms, is there maybe a way I can change the ImageRequest to cache that postprocessed image in the memory cache instead of the source one?

nat-e avatar Apr 01 '15 17:04 nat-e

I'm doing something similar. A solution to this would be awesome!

REWKyleB avatar Apr 02 '15 16:04 REWKyleB

Hi, we are aware of this and I already proposed that we change that. This should be easy to solve though. We should just pass both source and destination bitmap. And one should not modify the source bitmap. One limitation of that is that the output bitmap would have to be of the same size, but we can change even that by allowing clients to create their own output bitmap. Let me make a solution for that.

Caching is as separate issue, and we will consider implementing that as well.

plamenko avatar Apr 02 '15 18:04 plamenko

I have a fix ready for this.

plamenko avatar Apr 02 '15 21:04 plamenko

Can I get a sample code on how to use Postprocessors in fresco. Any help will be appreciated.

amitshekhariitbhu avatar Apr 11 '15 03:04 amitshekhariitbhu

amitshekhariitbhu: see http://frescolib.org/docs/modifying-image.html

tyronen avatar Apr 12 '15 16:04 tyronen

I refactored postprocessors so that the client has more flexibility. This is soon going to be pushed to maven, and if you are building from source you should extend BasePostprocessor:

https://github.com/facebook/fresco/blob/29080993fce3ccfcdc83a03f1d0a533ee18f72aa/imagepipeline/src/main/java/com/facebook/imagepipeline/request/BasePostprocessor.java

plamenko avatar Apr 16 '15 00:04 plamenko

This is included in Fresco 0.3.0 which has just been released. See the updated documentation.

tyronen avatar Apr 16 '15 16:04 tyronen

@tyronen : thanks

amitshekhariitbhu avatar Apr 16 '15 17:04 amitshekhariitbhu

@plamenko Hi, Apologies for writing on this closed thread. I want to blur the whole image after fresco load it. Could you give a sample code for this?

Write now I am doing this

          Postprocessor blurringPostprocessor = new BasePostprocessor() {
          @Override
          public String getName() {
            return "blurringPostprocessor";
          }

          @Override
          public void process(Bitmap bitmap) {
          //using bluring view to blur the image
          blurringView.invalidate();
          }
        };


ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(parseProfileObject.getParseFile("profilePic").getUrl()))
            .setPostprocessor(blurringPostprocessor)
            .setResizeOptions(new ResizeOptions(512, 334))
            .build();
        PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
            .setOldController(backgroundPhoto.getController())
            .setImageRequest(request)
            .build(); 

PS: Pardon me for my Language.

SendOTP avatar Oct 19 '15 14:10 SendOTP