Home>
1.

I want to speed up the process of composite composition.
We have achieved this, but I want to shorten the processing time.

2. Source code
// composite composition
    private fun composite(srcImg1 :Bitmap ,srcImg2 :Bitmap) :Bitmap (
        // image
        var dstImg1 :Bitmap = Bitmap.createBitmap(srcImg1.width, srcImg1.height, Bitmap.Config.ARGB_8888)
        // height width
        val width1 :Int = dstImg1.width
        val height1 :Int = dstImg1.height
        // illuminance
        var rgb1 :Int
        var gray1 :Int
        var rgb2: Int
        var gray2 :Int
        // composite composite adopt brighter one
        for(j in 0..height1-1 step 1) {
            for (i in 0..width1-1 step 1) {
                rgb1 = srcImg1.getPixel(i, j)
                rgb2 = srcImg2.getPixel(i, j)
                gray1 = (Color.red(rgb1) * 0.3 + Color.green(rgb1) * 0.59 + Color.blue(rgb1) * 0.11).toInt()
                gray2 = (Color.red(rgb2) * 0.3 + Color.green(rgb2) * 0.59 + Color.blue(rgb2) * 0.11).toInt()
                if (gray1 >= gray2){
                    dstImg1.setPixel(i, j, Color.rgb(Color.red(rgb1), Color.green(rgb1), Color.blue(rgb1)))
                }else{
                    dstImg1.setPixel(i, j, Color.rgb(Color.red(rgb2), Color.green(rgb2), Color.blue(rgb2)))
                }
            }
        }
        return dstImg1
    }

3. Processing time

Terminal 1: P10
Image Size: 6048 * 3402 = 20575296
Processing time: 17984msec About 17 seconds *Measure the time with the above function in between
Roughly 1 pixel processing time: 0.874msec

Terminal 2: oppo
Image Size: 4608 * 2592 = 11943936
Processing time: 14119msec About 14 seconds *Measure the time with the above function in between
Roughly 1 pixel processing time: 1.18msec

Calculated by image size/processing time

4. Question

Since the height direction and the width direction are turned with for, it will take considerably time if the image size becomes large.
I would like to reduce this time to 1/10 if possible, but is there any other way?

5. What I tried

I couldn't think of any other way, so I searched for another way on the net, but couldn't find it,
I have a question on this site.

6.information:(FW/tool ​​version, etc.)

Android Studio 3.6.3
Language: kotlin
Based on: Camera2Basic (kotlin)
Terminal 1: P10
Terminal 2: oppo A5 2020

Finally

It would be very helpful if you have any information or would like to try this and give us a comment.

  • Answer # 1

    This is the content of this composite composition.
    I hope it will be helpful.

      private fun composite2(srcImg1 :Bitmap ,srcImg2 :Bitmap) :Bitmap (
            // array for the output image
            var dstImg1 :Bitmap = Bitmap.createBitmap(srcImg1.width, srcImg1.height, Bitmap.Config.ARGB_8888)
            // image height, width, pixels
            val width = dstImg1.width
            val height = dstImg1.height
            val psize = (width * height)
            val pixels1 = IntArray(psize) //Important LongArray element makes width1 * height1 array
            val pixels2 = IntArray(psize) //Important LongArray element makes width1 * height1 array
            srcImg1.getPixels(pixels1,0,width,0,0,width,height)
            srcImg2.getPixels(pixels2,0,width,0,0,width,height)
            // composite composition
            for(i in 0..(psize-1) step 1) (
                var r_1 = (pixels1[i] shr 16) and 0xFF // right BIT shift shr
                var g_1 = (pixels1[i] shr 8) and 0xFF // right BIT shift shr
                var b_1 = (pixels1[i]) and 0xFF // right BIT shift shr
                var r_2 = (pixels2[i] shr 16) and 0xFF // right BIT shift shr
                var g_2 = (pixels2[i] shr 8) and 0xFF // right BIT shift shr
                var b_2 = (pixels2[i]) and 0xFF // right BIT shift shr
                var gray1 = ((r_1 * 0.3) + (g_1 * 0.59) + (b_1 * 0.11)).toInt()
                var gray2 = ((r_2 * 0.3) + (g_2 * 0.59) + (b_2 * 0.11)).toInt()
                if (gray1 >= gray2){
                    pixels1[i]= pixels1[i]
                }else{
                    pixels1[i]= pixels2[i]
                }
            }
            dstImg1.setPixels(pixels1,0,width,0,0,width,height)
            return dstImg1
        }