简体   繁体   中英

Allocating big bitmap for renderscript (android)

I try to make simple image filter in android renderscript, and it does work for small images. However I get out of memory error for a picture as big as picture taken with camera for example (everything works fine for small images though). I know my code is kinda lousy (mostly copied from here ), so any tip on how to make renderscript computation on big bitmap is appreciated.. Here's the code for calling rs:

RenderScript rs = RenderScript.create(this);
        Allocation allocIn = Allocation.createFromBitmap(rs, bmp);
        Allocation allocOut = Allocation.createTyped(rs,  allocIn.getType());

        ScriptC_sample sc = new ScriptC_sample(rs, getResources(), R.raw.sample);

        sc.set_in(allocIn);
        sc.set_out(allocOut);

        sc.forEach_root(allocIn, allocOut);

        Bitmap bmpOut = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
        allocOut.copyTo(bmpOut);
        imgView.setImageBitmap(bmpOut);     

And here's what i believe is relevant from rendescript file itself:

rs_allocation in;

void root(const uchar4* v_in, uchar4* v_out, const void* usrData, 
uint32_t x, uint32_t y) {

    float4 curPixel = rsUnpackColor8888(*v_in);

    // ... computations 

    *v_out = rsPackColorTo8888(curPixel.r, curPixel.g, curPixel.b, curPixel.a);
} 

I do understand that I can't really load such a big bitmap to memory (I can load the file into imageView though?), and I've previously used inJustDecodeBounds to handle it.. But here I have no idea how and where to use it, and I don't want to resize the bitmap (I'd like to process the original file and save modified file of the same size)

If you are running on devices with Android 2.3.3 or later then you can use BitmapRegionDecoder to read in only part of an image file. Therefore you can read in a region, process it and save the results, then repeat until the entire image has been processed.

Depending on what image processing you are doing, you may need to overlap the regions to correctly process their edges.

In the Android framework I do not believe there is an equivalent of BitmapRegionDecoder for saving out large images in sections so you will have to use an external library to achieve that. Something like PNGJ allows line by line load and save of PNG files (I have not used PNGJ so cannot comment on the library itself).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM