• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.testapp
18 
19 import android.renderscript.toolkit.BlendingMode
20 import android.renderscript.toolkit.Range2d
21 
22 /**
23  * Reference implementation of a Blend operation.
24  *
25  * See the class Rgba for details of arithmetic operation using that class.
26  */
27 @ExperimentalUnsignedTypes
28 fun referenceBlend(
29     mode: BlendingMode,
30     sourceArray: ByteArray,
31     destArray: ByteArray,
32     sizeX: Int,
33     sizeY: Int,
34     restriction: Range2d?
35 ) {
36     val source = Rgba2dArray(sourceArray, sizeX, sizeY)
37     val dest = Rgba2dArray(destArray, sizeX, sizeY)
38 
39     /**
40      * For each corresponding RGBA value of the source and destination arrays, invoke the blend
41      * function and store the result in the destination array.
42      */
43     fun blendEachPair(blendFunction: (src: Rgba, dst: Rgba) -> Rgba) {
44         dest.forEachCell(restriction) { x, y ->
45             dest[x, y] = blendFunction(source[x, y], dest[x, y])
46         }
47     }
48 
49     when (mode) {
50         BlendingMode.CLEAR -> blendEachPair { _, _ -> Rgba(0, 0, 0, 0) }
51         BlendingMode.SRC -> blendEachPair { src, _ -> src }
52         BlendingMode.DST -> { /* This doesn't do anything. */ }
53         BlendingMode.SRC_OVER -> blendEachPair { src, dst -> blendOver(src, dst) }
54         BlendingMode.DST_OVER -> blendEachPair { src, dst -> blendOver(dst, src) }
55         BlendingMode.SRC_IN -> blendEachPair { src, dst -> blendIn(src, dst) }
56         BlendingMode.DST_IN -> blendEachPair { src, dst -> blendIn(dst, src) }
57         BlendingMode.SRC_OUT -> blendEachPair { src, dst -> blendOut(src, dst) }
58         BlendingMode.DST_OUT -> blendEachPair { src, dst -> blendOut(dst, src) }
59         BlendingMode.SRC_ATOP -> blendEachPair { src, dst -> blendAtop(src, dst) }
60         BlendingMode.DST_ATOP -> blendEachPair { src, dst -> blendAtop(dst, src) }
61         BlendingMode.XOR -> blendEachPair { src, dst -> src xor dst }
62         BlendingMode.MULTIPLY -> blendEachPair { src, dst -> src * dst }
63         BlendingMode.ADD -> blendEachPair { src, dst -> dst + src }
64         BlendingMode.SUBTRACT -> blendEachPair { src, dst -> dst - src }
65     }
66 }
67 
68 @ExperimentalUnsignedTypes
blendOvernull69 private fun blendOver(src: Rgba, dst: Rgba) = src + (dst * (255 - src.a))
70 
71 @ExperimentalUnsignedTypes
72 private fun blendIn(src: Rgba, dst: Rgba) = src * dst.a
73 
74 @ExperimentalUnsignedTypes
75 private fun blendOut(src: Rgba, dst: Rgba) = src * (255 - dst.a)
76 
77 @ExperimentalUnsignedTypes
78 private fun blendAtop(src: Rgba, dst: Rgba): Rgba {
79     val value = src * dst.a + dst * (255 - src.a)
80     value.a = dst.a
81     return value
82 }
83