/rs/java/android/renderscript/ScriptIntrinsicHistogram.java

https://github.com/aizuzi/platform_frameworks_base · Java · 186 lines · 83 code · 20 blank · 83 comment · 24 complexity · 3dfeae742a0f3db105568d9e99c33c9c MD5 · raw file

  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. package android.renderscript;
  17. import android.content.Context;
  18. import android.content.res.Resources;
  19. import android.util.Log;
  20. /**
  21. * Intrinsic Histogram filter.
  22. *
  23. *
  24. **/
  25. public final class ScriptIntrinsicHistogram extends ScriptIntrinsic {
  26. private Allocation mOut;
  27. private ScriptIntrinsicHistogram(long id, RenderScript rs) {
  28. super(id, rs);
  29. }
  30. /**
  31. * Create an intrinsic for calculating the histogram of an uchar
  32. * or uchar4 image.
  33. *
  34. * Supported elements types are
  35. * {@link Element#U8_4}, {@link Element#U8_3},
  36. * {@link Element#U8_2}, {@link Element#U8}
  37. *
  38. * @param rs The RenderScript context
  39. * @param e Element type for inputs
  40. *
  41. * @return ScriptIntrinsicHistogram
  42. */
  43. public static ScriptIntrinsicHistogram create(RenderScript rs, Element e) {
  44. if ((!e.isCompatible(Element.U8_4(rs))) &&
  45. (!e.isCompatible(Element.U8_3(rs))) &&
  46. (!e.isCompatible(Element.U8_2(rs))) &&
  47. (!e.isCompatible(Element.U8(rs)))) {
  48. throw new RSIllegalArgumentException("Unsuported element type.");
  49. }
  50. long id = rs.nScriptIntrinsicCreate(9, e.getID(rs));
  51. ScriptIntrinsicHistogram sib = new ScriptIntrinsicHistogram(id, rs);
  52. return sib;
  53. }
  54. /**
  55. * Process an input buffer and place the histogram into the
  56. * output allocation. The output allocation may be a narrower
  57. * vector size than the input. In this case the vector size of
  58. * the output is used to determine how many of the input
  59. * channels are used in the computation. This is useful if you
  60. * have an RGBA input buffer but only want the histogram for
  61. * RGB.
  62. *
  63. * 1D and 2D input allocations are supported.
  64. *
  65. * @param ain The input image
  66. */
  67. public void forEach(Allocation ain) {
  68. if (ain.getType().getElement().getVectorSize() <
  69. mOut.getType().getElement().getVectorSize()) {
  70. throw new RSIllegalArgumentException(
  71. "Input vector size must be >= output vector size.");
  72. }
  73. if (ain.getType().getElement().isCompatible(Element.U8(mRS)) &&
  74. ain.getType().getElement().isCompatible(Element.U8_4(mRS))) {
  75. throw new RSIllegalArgumentException("Output type must be U32 or I32.");
  76. }
  77. forEach(0, ain, null, null);
  78. }
  79. /**
  80. * Set the coefficients used for the RGBA to Luminocity
  81. * calculation. The default is {0.299f, 0.587f, 0.114f, 0.f}.
  82. *
  83. * Coefficients must be >= 0 and sum to 1.0 or less.
  84. *
  85. * @param r Red coefficient
  86. * @param g Green coefficient
  87. * @param b Blue coefficient
  88. * @param a Alpha coefficient
  89. */
  90. public void setDotCoefficients(float r, float g, float b, float a) {
  91. if ((r < 0.f) || (g < 0.f) || (b < 0.f) || (a < 0.f)) {
  92. throw new RSIllegalArgumentException("Coefficient may not be negative.");
  93. }
  94. if ((r + g + b + a) > 1.f) {
  95. throw new RSIllegalArgumentException("Sum of coefficients must be 1.0 or less.");
  96. }
  97. FieldPacker fp = new FieldPacker(16);
  98. fp.addF32(r);
  99. fp.addF32(g);
  100. fp.addF32(b);
  101. fp.addF32(a);
  102. setVar(0, fp);
  103. }
  104. /**
  105. * Set the output of the histogram. 32 bit integer types are
  106. * supported.
  107. *
  108. * @param aout The output allocation
  109. */
  110. public void setOutput(Allocation aout) {
  111. mOut = aout;
  112. if (mOut.getType().getElement() != Element.U32(mRS) &&
  113. mOut.getType().getElement() != Element.U32_2(mRS) &&
  114. mOut.getType().getElement() != Element.U32_3(mRS) &&
  115. mOut.getType().getElement() != Element.U32_4(mRS) &&
  116. mOut.getType().getElement() != Element.I32(mRS) &&
  117. mOut.getType().getElement() != Element.I32_2(mRS) &&
  118. mOut.getType().getElement() != Element.I32_3(mRS) &&
  119. mOut.getType().getElement() != Element.I32_4(mRS)) {
  120. throw new RSIllegalArgumentException("Output type must be U32 or I32.");
  121. }
  122. if ((mOut.getType().getX() != 256) ||
  123. (mOut.getType().getY() != 0) ||
  124. mOut.getType().hasMipmaps() ||
  125. (mOut.getType().getYuv() != 0)) {
  126. throw new RSIllegalArgumentException("Output must be 1D, 256 elements.");
  127. }
  128. setVar(1, aout);
  129. }
  130. /**
  131. * Process an input buffer and place the histogram into the
  132. * output allocation. The dot product of the input channel and
  133. * the coefficients from 'setDotCoefficients' are used to
  134. * calculate the output values.
  135. *
  136. * 1D and 2D input allocations are supported.
  137. *
  138. * @param ain The input image
  139. */
  140. public void forEach_Dot(Allocation ain) {
  141. if (mOut.getType().getElement().getVectorSize() != 1) {
  142. throw new RSIllegalArgumentException("Output vector size must be one.");
  143. }
  144. if (ain.getType().getElement().isCompatible(Element.U8(mRS)) &&
  145. ain.getType().getElement().isCompatible(Element.U8_4(mRS))) {
  146. throw new RSIllegalArgumentException("Output type must be U32 or I32.");
  147. }
  148. forEach(1, ain, null, null);
  149. }
  150. /**
  151. * Get a KernelID for this intrinsic kernel.
  152. *
  153. * @return Script.KernelID The KernelID object.
  154. */
  155. public Script.KernelID getKernelID_Separate() {
  156. return createKernelID(0, 3, null, null);
  157. }
  158. /**
  159. * Get a FieldID for the input field of this intrinsic.
  160. *
  161. * @return Script.FieldID The FieldID object.
  162. */
  163. public Script.FieldID getFieldID_Input() {
  164. return createFieldID(1, null);
  165. }
  166. }