| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2013 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 android.renderscript; | 
|  | 18 |  | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 19 | /** | 
|  | 20 | * Intrinsic Histogram filter. | 
|  | 21 | * | 
| Xusong Wang | 1f8dc65 | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 22 | * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a | 
|  | 23 | * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration | 
|  | 24 | * guide</a> for the proposed alternatives. | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 25 | **/ | 
| Xusong Wang | 1f8dc65 | 2021-01-05 10:09:52 -0800 | [diff] [blame] | 26 | @Deprecated | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 27 | public final class ScriptIntrinsicHistogram extends ScriptIntrinsic { | 
|  | 28 | private Allocation mOut; | 
|  | 29 |  | 
| Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 30 | private ScriptIntrinsicHistogram(long id, RenderScript rs) { | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 31 | super(id, rs); | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | /** | 
|  | 35 | * Create an intrinsic for calculating the histogram of an uchar | 
|  | 36 | * or uchar4 image. | 
|  | 37 | * | 
| Jason Sams | 9274384 | 2013-06-18 18:52:42 -0700 | [diff] [blame] | 38 | * Supported elements types are | 
|  | 39 | * {@link Element#U8_4}, {@link Element#U8_3}, | 
|  | 40 | * {@link Element#U8_2}, {@link Element#U8} | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 41 | * | 
|  | 42 | * @param rs The RenderScript context | 
| Jason Sams | 1a3edb0 | 2013-06-18 18:23:37 -0700 | [diff] [blame] | 43 | * @param e Element type for inputs | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 44 | * | 
|  | 45 | * @return ScriptIntrinsicHistogram | 
|  | 46 | */ | 
|  | 47 | public static ScriptIntrinsicHistogram create(RenderScript rs, Element e) { | 
| Jason Sams | 1a3edb0 | 2013-06-18 18:23:37 -0700 | [diff] [blame] | 48 | if ((!e.isCompatible(Element.U8_4(rs))) && | 
|  | 49 | (!e.isCompatible(Element.U8_3(rs))) && | 
|  | 50 | (!e.isCompatible(Element.U8_2(rs))) && | 
|  | 51 | (!e.isCompatible(Element.U8(rs)))) { | 
| Stephen Hines | ad57e33 | 2016-04-11 13:05:55 -0700 | [diff] [blame] | 52 | throw new RSIllegalArgumentException("Unsupported element type."); | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 53 | } | 
| Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 54 | long id = rs.nScriptIntrinsicCreate(9, e.getID(rs)); | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 55 | ScriptIntrinsicHistogram sib = new ScriptIntrinsicHistogram(id, rs); | 
|  | 56 | return sib; | 
|  | 57 | } | 
|  | 58 |  | 
| Jason Sams | 1a3edb0 | 2013-06-18 18:23:37 -0700 | [diff] [blame] | 59 | /** | 
|  | 60 | * Process an input buffer and place the histogram into the | 
|  | 61 | * output allocation. The output allocation may be a narrower | 
|  | 62 | * vector size than the input. In this case the vector size of | 
|  | 63 | * the output is used to determine how many of the input | 
|  | 64 | * channels are used in the computation. This is useful if you | 
|  | 65 | * have an RGBA input buffer but only want the histogram for | 
|  | 66 | * RGB. | 
|  | 67 | * | 
|  | 68 | * 1D and 2D input allocations are supported. | 
|  | 69 | * | 
|  | 70 | * @param ain The input image | 
|  | 71 | */ | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 72 | public void forEach(Allocation ain) { | 
| Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 73 | forEach(ain, null); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | /** | 
|  | 77 | * Process an input buffer and place the histogram into the | 
|  | 78 | * output allocation. The output allocation may be a narrower | 
|  | 79 | * vector size than the input. In this case the vector size of | 
|  | 80 | * the output is used to determine how many of the input | 
|  | 81 | * channels are used in the computation. This is useful if you | 
|  | 82 | * have an RGBA input buffer but only want the histogram for | 
|  | 83 | * RGB. | 
|  | 84 | * | 
|  | 85 | * 1D and 2D input allocations are supported. | 
|  | 86 | * | 
|  | 87 | * @param ain The input image | 
|  | 88 | * @param opt LaunchOptions for clipping | 
|  | 89 | */ | 
|  | 90 | public void forEach(Allocation ain, Script.LaunchOptions opt) { | 
| Jason Sams | 8ace2ac | 2013-06-18 17:34:34 -0700 | [diff] [blame] | 91 | if (ain.getType().getElement().getVectorSize() < | 
|  | 92 | mOut.getType().getElement().getVectorSize()) { | 
|  | 93 |  | 
|  | 94 | throw new RSIllegalArgumentException( | 
| Jason Sams | 1a3edb0 | 2013-06-18 18:23:37 -0700 | [diff] [blame] | 95 | "Input vector size must be >= output vector size."); | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 96 | } | 
| Miao Wang | b590b35 | 2015-01-15 11:09:23 -0800 | [diff] [blame] | 97 | if (!ain.getType().getElement().isCompatible(Element.U8(mRS)) && | 
| Miao Wang | 8a12653 | 2015-01-23 09:41:04 -0800 | [diff] [blame] | 98 | !ain.getType().getElement().isCompatible(Element.U8_2(mRS)) && | 
|  | 99 | !ain.getType().getElement().isCompatible(Element.U8_3(mRS)) && | 
| Miao Wang | b590b35 | 2015-01-15 11:09:23 -0800 | [diff] [blame] | 100 | !ain.getType().getElement().isCompatible(Element.U8_4(mRS))) { | 
| Miao Wang | 8a12653 | 2015-01-23 09:41:04 -0800 | [diff] [blame] | 101 | throw new RSIllegalArgumentException("Input type must be U8, U8_1, U8_2 or U8_4."); | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 104 | forEach(0, ain, null, null, opt); | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 107 |  | 
|  | 108 |  | 
| Jason Sams | 1a3edb0 | 2013-06-18 18:23:37 -0700 | [diff] [blame] | 109 | /** | 
|  | 110 | * Set the coefficients used for the RGBA to Luminocity | 
|  | 111 | * calculation. The default is {0.299f, 0.587f, 0.114f, 0.f}. | 
|  | 112 | * | 
|  | 113 | * Coefficients must be >= 0 and sum to 1.0 or less. | 
|  | 114 | * | 
|  | 115 | * @param r Red coefficient | 
|  | 116 | * @param g Green coefficient | 
|  | 117 | * @param b Blue coefficient | 
|  | 118 | * @param a Alpha coefficient | 
|  | 119 | */ | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 120 | public void setDotCoefficients(float r, float g, float b, float a) { | 
|  | 121 | if ((r < 0.f) || (g < 0.f) || (b < 0.f) || (a < 0.f)) { | 
|  | 122 | throw new RSIllegalArgumentException("Coefficient may not be negative."); | 
|  | 123 | } | 
|  | 124 | if ((r + g + b + a) > 1.f) { | 
|  | 125 | throw new RSIllegalArgumentException("Sum of coefficients must be 1.0 or less."); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | FieldPacker fp = new FieldPacker(16); | 
|  | 129 | fp.addF32(r); | 
|  | 130 | fp.addF32(g); | 
|  | 131 | fp.addF32(b); | 
|  | 132 | fp.addF32(a); | 
|  | 133 | setVar(0, fp); | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | /** | 
| Jason Sams | 1a3edb0 | 2013-06-18 18:23:37 -0700 | [diff] [blame] | 137 | * Set the output of the histogram.  32 bit integer types are | 
|  | 138 | * supported. | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 139 | * | 
| Jason Sams | 1a3edb0 | 2013-06-18 18:23:37 -0700 | [diff] [blame] | 140 | * @param aout The output allocation | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 141 | */ | 
|  | 142 | public void setOutput(Allocation aout) { | 
|  | 143 | mOut = aout; | 
|  | 144 | if (mOut.getType().getElement() != Element.U32(mRS) && | 
| Jason Sams | 8ace2ac | 2013-06-18 17:34:34 -0700 | [diff] [blame] | 145 | mOut.getType().getElement() != Element.U32_2(mRS) && | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 146 | mOut.getType().getElement() != Element.U32_3(mRS) && | 
|  | 147 | mOut.getType().getElement() != Element.U32_4(mRS) && | 
|  | 148 | mOut.getType().getElement() != Element.I32(mRS) && | 
| Jason Sams | 8ace2ac | 2013-06-18 17:34:34 -0700 | [diff] [blame] | 149 | mOut.getType().getElement() != Element.I32_2(mRS) && | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 150 | mOut.getType().getElement() != Element.I32_3(mRS) && | 
|  | 151 | mOut.getType().getElement() != Element.I32_4(mRS)) { | 
|  | 152 |  | 
|  | 153 | throw new RSIllegalArgumentException("Output type must be U32 or I32."); | 
|  | 154 | } | 
|  | 155 | if ((mOut.getType().getX() != 256) || | 
|  | 156 | (mOut.getType().getY() != 0) || | 
|  | 157 | mOut.getType().hasMipmaps() || | 
|  | 158 | (mOut.getType().getYuv() != 0)) { | 
|  | 159 |  | 
|  | 160 | throw new RSIllegalArgumentException("Output must be 1D, 256 elements."); | 
|  | 161 | } | 
|  | 162 | setVar(1, aout); | 
|  | 163 | } | 
|  | 164 |  | 
| Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 165 |  | 
| Jason Sams | 1a3edb0 | 2013-06-18 18:23:37 -0700 | [diff] [blame] | 166 | /** | 
|  | 167 | * Process an input buffer and place the histogram into the | 
|  | 168 | * output allocation. The dot product of the input channel and | 
|  | 169 | * the coefficients from 'setDotCoefficients' are used to | 
|  | 170 | * calculate the output values. | 
|  | 171 | * | 
|  | 172 | * 1D and 2D input allocations are supported. | 
|  | 173 | * | 
|  | 174 | * @param ain The input image | 
|  | 175 | */ | 
| Jason Sams | a49e89d | 2013-08-29 17:00:37 -0700 | [diff] [blame] | 176 | public void forEach_Dot(Allocation ain) { | 
| Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 177 | forEach_Dot(ain, null); | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | /** | 
|  | 181 | * Process an input buffer and place the histogram into the | 
|  | 182 | * output allocation. The dot product of the input channel and | 
|  | 183 | * the coefficients from 'setDotCoefficients' are used to | 
|  | 184 | * calculate the output values. | 
|  | 185 | * | 
|  | 186 | * 1D and 2D input allocations are supported. | 
|  | 187 | * | 
|  | 188 | * @param ain The input image | 
|  | 189 | * @param opt LaunchOptions for clipping | 
|  | 190 | */ | 
|  | 191 | public void forEach_Dot(Allocation ain, Script.LaunchOptions opt) { | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 192 | if (mOut.getType().getElement().getVectorSize() != 1) { | 
|  | 193 | throw new RSIllegalArgumentException("Output vector size must be one."); | 
|  | 194 | } | 
| Miao Wang | b590b35 | 2015-01-15 11:09:23 -0800 | [diff] [blame] | 195 | if (!ain.getType().getElement().isCompatible(Element.U8(mRS)) && | 
| Miao Wang | 8a12653 | 2015-01-23 09:41:04 -0800 | [diff] [blame] | 196 | !ain.getType().getElement().isCompatible(Element.U8_2(mRS)) && | 
|  | 197 | !ain.getType().getElement().isCompatible(Element.U8_3(mRS)) && | 
| Miao Wang | b590b35 | 2015-01-15 11:09:23 -0800 | [diff] [blame] | 198 | !ain.getType().getElement().isCompatible(Element.U8_4(mRS))) { | 
| Miao Wang | 8a12653 | 2015-01-23 09:41:04 -0800 | [diff] [blame] | 199 | throw new RSIllegalArgumentException("Input type must be U8, U8_1, U8_2 or U8_4."); | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 200 | } | 
|  | 201 |  | 
| Tim Murray | 6f842ac | 2014-01-13 11:47:53 -0800 | [diff] [blame] | 202 | forEach(1, ain, null, null, opt); | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 203 | } | 
|  | 204 |  | 
|  | 205 |  | 
|  | 206 |  | 
|  | 207 | /** | 
|  | 208 | * Get a KernelID for this intrinsic kernel. | 
|  | 209 | * | 
|  | 210 | * @return Script.KernelID The KernelID object. | 
|  | 211 | */ | 
| Jason Sams | a49e89d | 2013-08-29 17:00:37 -0700 | [diff] [blame] | 212 | public Script.KernelID getKernelID_Separate() { | 
| Jason Sams | 01e9f90 | 2013-06-18 11:53:03 -0700 | [diff] [blame] | 213 | return createKernelID(0, 3, null, null); | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | /** | 
|  | 217 | * Get a FieldID for the input field of this intrinsic. | 
|  | 218 | * | 
|  | 219 | * @return Script.FieldID The FieldID object. | 
|  | 220 | */ | 
|  | 221 | public Script.FieldID getFieldID_Input() { | 
|  | 222 | return createFieldID(1, null); | 
|  | 223 | } | 
|  | 224 | } | 
|  | 225 |  |