blob: a9e5ad451903b6b8128a9614f40732aa216a3882 [file] [log] [blame]
Jason Sams01e9f902013-06-18 11:53:03 -07001/*
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
17package android.renderscript;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.util.Log;
22
23/**
24 * Intrinsic Histogram filter.
25 *
26 *
27 **/
28public 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) {
Jason Sams8ace2ac2013-06-18 17:34:34 -070057 if (ain.getType().getElement().getVectorSize() <
58 mOut.getType().getElement().getVectorSize()) {
59
60 throw new RSIllegalArgumentException(
61 "Input vector sizse must be >= output vector size.");
Jason Sams01e9f902013-06-18 11:53:03 -070062 }
63 if (ain.getType().getElement().isCompatible(Element.U8(mRS)) &&
64 ain.getType().getElement().isCompatible(Element.U8_4(mRS))) {
65 throw new RSIllegalArgumentException("Output type must be U32 or I32.");
66 }
67
68 forEach(0, ain, null, null);
69 }
70
71 public void setDotCoefficients(float r, float g, float b, float a) {
72 if ((r < 0.f) || (g < 0.f) || (b < 0.f) || (a < 0.f)) {
73 throw new RSIllegalArgumentException("Coefficient may not be negative.");
74 }
75 if ((r + g + b + a) > 1.f) {
76 throw new RSIllegalArgumentException("Sum of coefficients must be 1.0 or less.");
77 }
78
79 FieldPacker fp = new FieldPacker(16);
80 fp.addF32(r);
81 fp.addF32(g);
82 fp.addF32(b);
83 fp.addF32(a);
84 setVar(0, fp);
85 }
86
87 /**
88 * Set the output of the histogram.
89 *
90 * @param ain The input allocation
91 */
92 public void setOutput(Allocation aout) {
93 mOut = aout;
94 if (mOut.getType().getElement() != Element.U32(mRS) &&
Jason Sams8ace2ac2013-06-18 17:34:34 -070095 mOut.getType().getElement() != Element.U32_2(mRS) &&
Jason Sams01e9f902013-06-18 11:53:03 -070096 mOut.getType().getElement() != Element.U32_3(mRS) &&
97 mOut.getType().getElement() != Element.U32_4(mRS) &&
98 mOut.getType().getElement() != Element.I32(mRS) &&
Jason Sams8ace2ac2013-06-18 17:34:34 -070099 mOut.getType().getElement() != Element.I32_2(mRS) &&
Jason Sams01e9f902013-06-18 11:53:03 -0700100 mOut.getType().getElement() != Element.I32_3(mRS) &&
101 mOut.getType().getElement() != Element.I32_4(mRS)) {
102
103 throw new RSIllegalArgumentException("Output type must be U32 or I32.");
104 }
105 if ((mOut.getType().getX() != 256) ||
106 (mOut.getType().getY() != 0) ||
107 mOut.getType().hasMipmaps() ||
108 (mOut.getType().getYuv() != 0)) {
109
110 throw new RSIllegalArgumentException("Output must be 1D, 256 elements.");
111 }
112 setVar(1, aout);
113 }
114
115 public void forEach_dot(Allocation ain) {
116 if (mOut.getType().getElement().getVectorSize() != 1) {
117 throw new RSIllegalArgumentException("Output vector size must be one.");
118 }
119 if (ain.getType().getElement().isCompatible(Element.U8(mRS)) &&
120 ain.getType().getElement().isCompatible(Element.U8_4(mRS))) {
121 throw new RSIllegalArgumentException("Output type must be U32 or I32.");
122 }
123
124 forEach(1, ain, null, null);
125 }
126
127
128
129 /**
130 * Get a KernelID for this intrinsic kernel.
131 *
132 * @return Script.KernelID The KernelID object.
133 */
134 public Script.KernelID getKernelID_seperate() {
135 return createKernelID(0, 3, null, null);
136 }
137
138 /**
139 * Get a FieldID for the input field of this intrinsic.
140 *
141 * @return Script.FieldID The FieldID object.
142 */
143 public Script.FieldID getFieldID_Input() {
144 return createFieldID(1, null);
145 }
146}
147