/rs/java/android/renderscript/ScriptIntrinsicConvolve3x3.java

https://github.com/aizuzi/platform_frameworks_base · Java · 133 lines · 47 code · 13 blank · 73 comment · 2 complexity · 0438cb6d49a4a541f03161e518e48250 MD5 · raw file

  1. /*
  2. * Copyright (C) 2012 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.util.Log;
  18. /**
  19. * Intrinsic for applying a 3x3 convolve to an allocation.
  20. *
  21. **/
  22. public final class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic {
  23. private final float[] mValues = new float[9];
  24. private Allocation mInput;
  25. private ScriptIntrinsicConvolve3x3(long id, RenderScript rs) {
  26. super(id, rs);
  27. }
  28. /**
  29. * Supported elements types are {@link Element#U8}, {@link
  30. * Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4},
  31. * {@link Element#F32}, {@link Element#F32_2}, {@link
  32. * Element#F32_3}, and {@link Element#F32_4}
  33. *
  34. * The default coefficients are.
  35. *
  36. * <code>
  37. * <p> [ 0, 0, 0 ]
  38. * <p> [ 0, 1, 0 ]
  39. * <p> [ 0, 0, 0 ]
  40. * </code>
  41. *
  42. * @param rs The RenderScript context
  43. * @param e Element type for intputs and outputs
  44. *
  45. * @return ScriptIntrinsicConvolve3x3
  46. */
  47. public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) {
  48. float f[] = { 0, 0, 0, 0, 1, 0, 0, 0, 0};
  49. if (!e.isCompatible(Element.U8(rs)) &&
  50. !e.isCompatible(Element.U8_2(rs)) &&
  51. !e.isCompatible(Element.U8_3(rs)) &&
  52. !e.isCompatible(Element.U8_4(rs)) &&
  53. !e.isCompatible(Element.F32(rs)) &&
  54. !e.isCompatible(Element.F32_2(rs)) &&
  55. !e.isCompatible(Element.F32_3(rs)) &&
  56. !e.isCompatible(Element.F32_4(rs))) {
  57. throw new RSIllegalArgumentException("Unsuported element type.");
  58. }
  59. long id = rs.nScriptIntrinsicCreate(1, e.getID(rs));
  60. ScriptIntrinsicConvolve3x3 si = new ScriptIntrinsicConvolve3x3(id, rs);
  61. si.setCoefficients(f);
  62. return si;
  63. }
  64. /**
  65. * Set the input of the blur.
  66. * Must match the element type supplied during create.
  67. *
  68. * @param ain The input allocation.
  69. */
  70. public void setInput(Allocation ain) {
  71. mInput = ain;
  72. setVar(1, ain);
  73. }
  74. /**
  75. * Set the coefficients for the convolve.
  76. *
  77. * The convolve layout is
  78. * <code>
  79. * <p> [ 0, 1, 2 ]
  80. * <p> [ 3, 4, 5 ]
  81. * <p> [ 6, 7, 8 ]
  82. * </code>
  83. *
  84. * @param v The array of coefficients to set
  85. */
  86. public void setCoefficients(float v[]) {
  87. FieldPacker fp = new FieldPacker(9*4);
  88. for (int ct=0; ct < mValues.length; ct++) {
  89. mValues[ct] = v[ct];
  90. fp.addF32(mValues[ct]);
  91. }
  92. setVar(0, fp);
  93. }
  94. /**
  95. * Apply the filter to the input and save to the specified
  96. * allocation.
  97. *
  98. * @param aout Output allocation. Must match creation element
  99. * type.
  100. */
  101. public void forEach(Allocation aout) {
  102. forEach(0, null, aout, null);
  103. }
  104. /**
  105. * Get a KernelID for this intrinsic kernel.
  106. *
  107. * @return Script.KernelID The KernelID object.
  108. */
  109. public Script.KernelID getKernelID() {
  110. return createKernelID(0, 2, null, null);
  111. }
  112. /**
  113. * Get a FieldID for the input field of this intrinsic.
  114. *
  115. * @return Script.FieldID The FieldID object.
  116. */
  117. public Script.FieldID getFieldID_Input() {
  118. return createFieldID(1, null);
  119. }
  120. }