| 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 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | /** |
| 24 | * Intrinsic Histogram filter. |
| 25 | * |
| 26 | * |
| 27 | **/ |
| 28 | public final class ScriptIntrinsicHistogram extends ScriptIntrinsic { |
| 29 | private Allocation mOut; |
| 30 | |
| 31 | private ScriptIntrinsicHistogram(int id, RenderScript rs) { |
| 32 | super(id, rs); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Create an intrinsic for calculating the histogram of an uchar |
| 37 | * or uchar4 image. |
| 38 | * |
| 39 | * Supported elements types are {@link Element#U8_4, @link |
| 40 | * Element#U8} |
| 41 | * |
| 42 | * @param rs The RenderScript context |
| 43 | * @param e Element type for inputs and outputs |
| 44 | * |
| 45 | * @return ScriptIntrinsicHistogram |
| 46 | */ |
| 47 | public static ScriptIntrinsicHistogram create(RenderScript rs, Element e) { |
| 48 | if ((!e.isCompatible(Element.U8_4(rs))) && (!e.isCompatible(Element.U8(rs)))) { |
| 49 | throw new RSIllegalArgumentException("Unsuported element type."); |
| 50 | } |
| 51 | int id = rs.nScriptIntrinsicCreate(9, e.getID(rs)); |
| 52 | ScriptIntrinsicHistogram sib = new ScriptIntrinsicHistogram(id, rs); |
| 53 | return sib; |
| 54 | } |
| 55 | |
| 56 | public void forEach(Allocation ain) { |
| 57 | int vecSize = mOut.getType().getElement().getVectorSize(); |
| 58 | if (ain.getType().getElement().getVectorSize() != vecSize) { |
| 59 | throw new RSIllegalArgumentException("Vector sizes must match."); |
| 60 | } |
| 61 | if (ain.getType().getElement().isCompatible(Element.U8(mRS)) && |
| 62 | ain.getType().getElement().isCompatible(Element.U8_4(mRS))) { |
| 63 | throw new RSIllegalArgumentException("Output type must be U32 or I32."); |
| 64 | } |
| 65 | |
| 66 | forEach(0, ain, null, null); |
| 67 | } |
| 68 | |
| 69 | public void setDotCoefficients(float r, float g, float b, float a) { |
| 70 | if ((r < 0.f) || (g < 0.f) || (b < 0.f) || (a < 0.f)) { |
| 71 | throw new RSIllegalArgumentException("Coefficient may not be negative."); |
| 72 | } |
| 73 | if ((r + g + b + a) > 1.f) { |
| 74 | throw new RSIllegalArgumentException("Sum of coefficients must be 1.0 or less."); |
| 75 | } |
| 76 | |
| 77 | FieldPacker fp = new FieldPacker(16); |
| 78 | fp.addF32(r); |
| 79 | fp.addF32(g); |
| 80 | fp.addF32(b); |
| 81 | fp.addF32(a); |
| 82 | setVar(0, fp); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Set the output of the histogram. |
| 87 | * |
| 88 | * @param ain The input allocation |
| 89 | */ |
| 90 | public void setOutput(Allocation aout) { |
| 91 | mOut = aout; |
| 92 | if (mOut.getType().getElement() != Element.U32(mRS) && |
| 93 | mOut.getType().getElement() != Element.U32_3(mRS) && |
| 94 | mOut.getType().getElement() != Element.U32_4(mRS) && |
| 95 | mOut.getType().getElement() != Element.I32(mRS) && |
| 96 | mOut.getType().getElement() != Element.I32_3(mRS) && |
| 97 | mOut.getType().getElement() != Element.I32_4(mRS)) { |
| 98 | |
| 99 | throw new RSIllegalArgumentException("Output type must be U32 or I32."); |
| 100 | } |
| 101 | if ((mOut.getType().getX() != 256) || |
| 102 | (mOut.getType().getY() != 0) || |
| 103 | mOut.getType().hasMipmaps() || |
| 104 | (mOut.getType().getYuv() != 0)) { |
| 105 | |
| 106 | throw new RSIllegalArgumentException("Output must be 1D, 256 elements."); |
| 107 | } |
| 108 | setVar(1, aout); |
| 109 | } |
| 110 | |
| 111 | public void forEach_dot(Allocation ain) { |
| 112 | if (mOut.getType().getElement().getVectorSize() != 1) { |
| 113 | throw new RSIllegalArgumentException("Output vector size must be one."); |
| 114 | } |
| 115 | if (ain.getType().getElement().isCompatible(Element.U8(mRS)) && |
| 116 | ain.getType().getElement().isCompatible(Element.U8_4(mRS))) { |
| 117 | throw new RSIllegalArgumentException("Output type must be U32 or I32."); |
| 118 | } |
| 119 | |
| 120 | forEach(1, ain, null, null); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Get a KernelID for this intrinsic kernel. |
| 127 | * |
| 128 | * @return Script.KernelID The KernelID object. |
| 129 | */ |
| 130 | public Script.KernelID getKernelID_seperate() { |
| 131 | return createKernelID(0, 3, null, null); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get a FieldID for the input field of this intrinsic. |
| 136 | * |
| 137 | * @return Script.FieldID The FieldID object. |
| 138 | */ |
| 139 | public Script.FieldID getFieldID_Input() { |
| 140 | return createFieldID(1, null); |
| 141 | } |
| 142 | } |
| 143 | |