1
votes

I'm writing an image manipulation app (e.g. operations include blurring, rotating, scaling, merging, flood filling) and I'm worried about memory use.

The problem I'm having is that I need to make use of Bitmap+Canvas so that I can take advantage of existing features to manipulate images and I also need to put copies of the image in int arrays to modify the pixels as it's too slow to modify Bitmaps using the set/getPixel function for some operations like blurring and flood fill.

My current memory usage is:

  • A Bitmap object for the image itself.
  • An offscreen Bitmap object that I use to perform image manipulation tasks using Canvas.
  • An alpha-only bitmap used for masking.
  • Two int arrays that each have enough pixels to contain a copy of the main image.

For example, I use the two int arrays to perform the flood fill operation: I copy the main image to one array, put a grayscale version of the main image in the other array (where the grayscale representation is created using the offscreen Bitamp and Canvas first), use the grayscale image when making color comparisons to perform the flood fill operation in the first array and then copy the final image back to the main image Bitmap. I could perhaps eliminate the offscreen bitmap for this operation, but e.g. I use the offscreen bitmap for performing painting operations.

Are there any tricks I can use to reduce my memory usage? For example, are there any quick ways to modify pixels that don't involve making int array copies of Bitmap objects? Can I use a Canvas object to directly modify an int array?

I want to add layers functionality (i.e. the main image would be composed of several bitmaps) but I'm already getting short on memory.

1
You can perform some (all?) of those operations by using matrices and colormatrices/color filters. This is what I would consider a pretty advanced topic though, so be prepared to spend days/weeks learning how to use it well.user432209
Could you give me some more tips? I know about colormatrices already and I don't see how these could help here.rbcc

1 Answers

0
votes

Depending on what you are using your two int arrays for, you could consider making them byte arrays. Again, depending on what your use is, you could store multiple bit flags in each byte using binary masks. And, depending on how you want boundary detection to work, you could decrease the bits per pixel on the image you use for the flood fill operation. HTH