0
votes

I'm trying to think of a way to implement a mosaic effect with renderscript as a learning exercise but I can't find enough information to know which of these methods, if any, will work.

  1. Split a bitmap into n x n pixel sub-bitmaps. Run each sub-bitmap through a script that averages the value all pixels, then rejoin the bitmaps back into one in java. I worry that using forEach_root may result in concurrency issues if different pixels are updating the same global variable at the same time. I could use a loop to iterate through each pixel in the sub-bitmap to avoid concurrency issues, but then I would lose the benefit of forEach_root.

  2. Keep the original bitmap intact and run forEach_root. In the script, if I can know the coordinates of the element, I could figure out which square the pixel should be in and then add each pixel to the relative (0,0) for the square in which it resides.

  3. If I can create an allocation that only includes select pixels then I can have one pixel for each mosaic square and add each other pixel in that square to the representative pixel. This will avoid concurrency issues (if they exist), while still allowing multiple simultanious executions

I really like number 3, but I'm not sure if such an allocation can be created. Alternatively, If collisions resulting from different renderscript threads accessing the same variable at the same time are handled gracefully then method one or two are probably easier.

1

1 Answers

2
votes

Here's a bit different idea; say you want to create 4x4 mosaic for 256x256 image;

  1. Create 64x64 allocation to store each mosaic piece color.

  2. Run forEach on this 64x64 allocation and set original 256x256 allocation as parameter for it.

  3. Within each forEach iteration calculate ie. average color from original image 4x4 area this mosaic piece corresponds to.

  4. Run forEach on original 256x256 allocation and give previously calculated 64x64 for it as a parameter.

  5. Within each forEach iteration set color to one from corresponding 64x64 allocation.

I believe this approach should adjust rather easily to different shaped and sized mosaics too. With the overhead of temporary allocation only.