| /* |
| * Copyright (C) 2021 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.example.testapp |
| |
| import android.graphics.Bitmap |
| import android.renderscript.Allocation |
| import android.renderscript.Element |
| import android.renderscript.RenderScript |
| import android.renderscript.Script |
| import android.renderscript.ScriptIntrinsicBlend |
| import android.renderscript.Type |
| import android.renderscript.toolkit.BlendingMode |
| import android.renderscript.toolkit.Range2d |
| |
| /** |
| * Does a Blend operation using the RenderScript Intrinsics. |
| */ |
| fun intrinsicBlend( |
| context: RenderScript, |
| mode: BlendingMode, |
| sourceArray: ByteArray, |
| destArray: ByteArray, |
| sizeX: Int, |
| sizeY: Int, |
| restriction: Range2d? |
| ) { |
| val scriptBlend = ScriptIntrinsicBlend.create(context, Element.U8_4(context)) |
| val builder = Type.Builder(context, Element.U8_4(context)) |
| builder.setX(sizeX) |
| builder.setY(sizeY) |
| val arrayType = builder.create() |
| val sourceAllocation = Allocation.createTyped(context, arrayType) |
| val destAllocation = Allocation.createTyped(context, arrayType) |
| sourceAllocation.copyFrom(sourceArray) |
| destAllocation.copyFrom(destArray) |
| |
| callBlendForEach(scriptBlend, sourceAllocation, destAllocation, mode, restriction) |
| destAllocation.copyTo(destArray) |
| |
| sourceAllocation.destroy() |
| destAllocation.destroy() |
| arrayType.destroy() |
| scriptBlend.destroy() |
| } |
| |
| fun intrinsicBlend( |
| context: RenderScript, |
| mode: BlendingMode, |
| sourceBitmap: Bitmap, |
| destBitmap: Bitmap, |
| restriction: Range2d? |
| ) { |
| val scriptBlend = ScriptIntrinsicBlend.create(context, Element.U8_4(context)) |
| val sourceAllocation = Allocation.createFromBitmap(context, sourceBitmap) |
| val destAllocation = Allocation.createFromBitmap(context, destBitmap) |
| sourceAllocation.copyFrom(sourceBitmap) |
| destAllocation.copyFrom(destBitmap) |
| |
| callBlendForEach(scriptBlend, sourceAllocation, destAllocation, mode, restriction) |
| destAllocation.copyTo(destBitmap) |
| |
| sourceAllocation.destroy() |
| destAllocation.destroy() |
| scriptBlend.destroy() |
| } |
| |
| private fun callBlendForEach( |
| scriptBlend: ScriptIntrinsicBlend, |
| sourceAllocation: Allocation, |
| destAllocation: Allocation, |
| mode: BlendingMode, |
| restriction: Range2d? |
| ) { |
| if (restriction != null) { |
| val options = Script.LaunchOptions() |
| options.setX(restriction.startX, restriction.endX) |
| options.setY(restriction.startY, restriction.endY) |
| when (mode) { |
| BlendingMode.CLEAR -> scriptBlend.forEachClear( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.SRC -> scriptBlend.forEachSrc( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.DST -> scriptBlend.forEachDst( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.SRC_OVER -> scriptBlend.forEachSrcOver( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.DST_OVER -> scriptBlend.forEachDstOver( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.SRC_IN -> scriptBlend.forEachSrcIn( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.DST_IN -> scriptBlend.forEachDstIn( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.SRC_OUT -> scriptBlend.forEachSrcOut( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.DST_OUT -> scriptBlend.forEachDstOut( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.SRC_ATOP -> scriptBlend.forEachSrcAtop( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.DST_ATOP -> scriptBlend.forEachDstAtop( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.XOR -> scriptBlend.forEachXor( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.MULTIPLY -> scriptBlend.forEachMultiply( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.ADD -> scriptBlend.forEachAdd( |
| sourceAllocation, destAllocation, options |
| ) |
| BlendingMode.SUBTRACT -> scriptBlend.forEachSubtract( |
| sourceAllocation, destAllocation, options |
| ) |
| } |
| } else { |
| when (mode) { |
| BlendingMode.CLEAR -> scriptBlend.forEachClear( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.SRC -> scriptBlend.forEachSrc( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.DST -> scriptBlend.forEachDst( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.SRC_OVER -> scriptBlend.forEachSrcOver( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.DST_OVER -> scriptBlend.forEachDstOver( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.SRC_IN -> scriptBlend.forEachSrcIn( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.DST_IN -> scriptBlend.forEachDstIn( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.SRC_OUT -> scriptBlend.forEachSrcOut( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.DST_OUT -> scriptBlend.forEachDstOut( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.SRC_ATOP -> scriptBlend.forEachSrcAtop( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.DST_ATOP -> scriptBlend.forEachDstAtop( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.XOR -> scriptBlend.forEachXor( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.MULTIPLY -> scriptBlend.forEachMultiply( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.ADD -> scriptBlend.forEachAdd( |
| sourceAllocation, destAllocation |
| ) |
| BlendingMode.SUBTRACT -> scriptBlend.forEachSubtract( |
| sourceAllocation, destAllocation |
| ) |
| } |
| } |
| } |