/rs/java/android/renderscript/Byte2.java

https://github.com/aizuzi/platform_frameworks_base · Java · 394 lines · 163 code · 48 blank · 183 comment · 3 complexity · 2a63a7ba52fa98bd5420db2779e3d69f MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 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 java.lang.Math;
  18. import android.util.Log;
  19. /**
  20. * Class for exposing the native RenderScript byte2 type back to the Android system.
  21. *
  22. **/
  23. public class Byte2 {
  24. public byte x;
  25. public byte y;
  26. public Byte2() {
  27. }
  28. public Byte2(byte initX, byte initY) {
  29. x = initX;
  30. y = initY;
  31. }
  32. /** @hide */
  33. public Byte2(Byte2 source) {
  34. this.x = source.x;
  35. this.y = source.y;
  36. }
  37. /** @hide
  38. * Vector add
  39. *
  40. * @param a
  41. */
  42. public void add(Byte2 a) {
  43. this.x += a.x;
  44. this.y += a.y;
  45. }
  46. /** @hide
  47. * Vector add
  48. *
  49. * @param a
  50. * @param b
  51. * @return
  52. */
  53. public static Byte2 add(Byte2 a, Byte2 b) {
  54. Byte2 result = new Byte2();
  55. result.x = (byte)(a.x + b.x);
  56. result.y = (byte)(a.y + b.y);
  57. return result;
  58. }
  59. /** @hide
  60. * Vector add
  61. *
  62. * @param value
  63. */
  64. public void add(byte value) {
  65. x += value;
  66. y += value;
  67. }
  68. /** @hide
  69. * Vector add
  70. *
  71. * @param a
  72. * @param b
  73. * @return
  74. */
  75. public static Byte2 add(Byte2 a, byte b) {
  76. Byte2 result = new Byte2();
  77. result.x = (byte)(a.x + b);
  78. result.y = (byte)(a.y + b);
  79. return result;
  80. }
  81. /** @hide
  82. * Vector subtraction
  83. *
  84. * @param a
  85. */
  86. public void sub(Byte2 a) {
  87. this.x -= a.x;
  88. this.y -= a.y;
  89. }
  90. /** @hide
  91. * Vector subtraction
  92. *
  93. * @param a
  94. * @param b
  95. * @return
  96. */
  97. public static Byte2 sub(Byte2 a, Byte2 b) {
  98. Byte2 result = new Byte2();
  99. result.x = (byte)(a.x - b.x);
  100. result.y = (byte)(a.y - b.y);
  101. return result;
  102. }
  103. /** @hide
  104. * Vector subtraction
  105. *
  106. * @param value
  107. */
  108. public void sub(byte value) {
  109. x -= value;
  110. y -= value;
  111. }
  112. /** @hide
  113. * Vector subtraction
  114. *
  115. * @param a
  116. * @param b
  117. * @return
  118. */
  119. public static Byte2 sub(Byte2 a, byte b) {
  120. Byte2 result = new Byte2();
  121. result.x = (byte)(a.x - b);
  122. result.y = (byte)(a.y - b);
  123. return result;
  124. }
  125. /** @hide
  126. * Vector multiplication
  127. *
  128. * @param a
  129. */
  130. public void mul(Byte2 a) {
  131. this.x *= a.x;
  132. this.y *= a.y;
  133. }
  134. /** @hide
  135. * Vector multiplication
  136. *
  137. * @param a
  138. * @param b
  139. * @return
  140. */
  141. public static Byte2 mul(Byte2 a, Byte2 b) {
  142. Byte2 result = new Byte2();
  143. result.x = (byte)(a.x * b.x);
  144. result.y = (byte)(a.y * b.y);
  145. return result;
  146. }
  147. /** @hide
  148. * Vector multiplication
  149. *
  150. * @param value
  151. */
  152. public void mul(byte value) {
  153. x *= value;
  154. y *= value;
  155. }
  156. /** @hide
  157. * Vector multiplication
  158. *
  159. * @param a
  160. * @param b
  161. * @return
  162. */
  163. public static Byte2 mul(Byte2 a, byte b) {
  164. Byte2 result = new Byte2();
  165. result.x = (byte)(a.x * b);
  166. result.y = (byte)(a.y * b);
  167. return result;
  168. }
  169. /** @hide
  170. * Vector division
  171. *
  172. * @param a
  173. */
  174. public void div(Byte2 a) {
  175. this.x /= a.x;
  176. this.y /= a.y;
  177. }
  178. /** @hide
  179. * Vector division
  180. *
  181. * @param a
  182. * @param b
  183. * @return
  184. */
  185. public static Byte2 div(Byte2 a, Byte2 b) {
  186. Byte2 result = new Byte2();
  187. result.x = (byte)(a.x / b.x);
  188. result.y = (byte)(a.y / b.y);
  189. return result;
  190. }
  191. /** @hide
  192. * Vector division
  193. *
  194. * @param value
  195. */
  196. public void div(byte value) {
  197. x /= value;
  198. y /= value;
  199. }
  200. /** @hide
  201. * Vector division
  202. *
  203. * @param a
  204. * @param b
  205. * @return
  206. */
  207. public static Byte2 div(Byte2 a, byte b) {
  208. Byte2 result = new Byte2();
  209. result.x = (byte)(a.x / b);
  210. result.y = (byte)(a.y / b);
  211. return result;
  212. }
  213. /** @hide
  214. * get vector length
  215. *
  216. * @return
  217. */
  218. public byte length() {
  219. return 2;
  220. }
  221. /** @hide
  222. * set vector negate
  223. */
  224. public void negate() {
  225. this.x = (byte)(-x);
  226. this.y = (byte)(-y);
  227. }
  228. /** @hide
  229. * Vector dot Product
  230. *
  231. * @param a
  232. * @return
  233. */
  234. public byte dotProduct(Byte2 a) {
  235. return (byte)((x * a.x) + (y * a.y));
  236. }
  237. /** @hide
  238. * Vector dot Product
  239. *
  240. * @param a
  241. * @param b
  242. * @return
  243. */
  244. public static byte dotProduct(Byte2 a, Byte2 b) {
  245. return (byte)((b.x * a.x) + (b.y * a.y));
  246. }
  247. /** @hide
  248. * Vector add Multiple
  249. *
  250. * @param a
  251. * @param factor
  252. */
  253. public void addMultiple(Byte2 a, byte factor) {
  254. x += a.x * factor;
  255. y += a.y * factor;
  256. }
  257. /** @hide
  258. * set vector value by Byte2
  259. *
  260. * @param a
  261. */
  262. public void set(Byte2 a) {
  263. this.x = a.x;
  264. this.y = a.y;
  265. }
  266. /** @hide
  267. * set the vector field value by Char
  268. *
  269. * @param a
  270. * @param b
  271. */
  272. public void setValues(byte a, byte b) {
  273. this.x = a;
  274. this.y = b;
  275. }
  276. /** @hide
  277. * return the element sum of vector
  278. *
  279. * @return
  280. */
  281. public byte elementSum() {
  282. return (byte)(x + y);
  283. }
  284. /** @hide
  285. * get the vector field value by index
  286. *
  287. * @param i
  288. * @return
  289. */
  290. public byte get(int i) {
  291. switch (i) {
  292. case 0:
  293. return x;
  294. case 1:
  295. return y;
  296. default:
  297. throw new IndexOutOfBoundsException("Index: i");
  298. }
  299. }
  300. /** @hide
  301. * set the vector field value by index
  302. *
  303. * @param i
  304. * @param value
  305. */
  306. public void setAt(int i, byte value) {
  307. switch (i) {
  308. case 0:
  309. x = value;
  310. return;
  311. case 1:
  312. y = value;
  313. return;
  314. default:
  315. throw new IndexOutOfBoundsException("Index: i");
  316. }
  317. }
  318. /** @hide
  319. * add the vector field value by index
  320. *
  321. * @param i
  322. * @param value
  323. */
  324. public void addAt(int i, byte value) {
  325. switch (i) {
  326. case 0:
  327. x += value;
  328. return;
  329. case 1:
  330. y += value;
  331. return;
  332. default:
  333. throw new IndexOutOfBoundsException("Index: i");
  334. }
  335. }
  336. /** @hide
  337. * copy the vector to Char array
  338. *
  339. * @param data
  340. * @param offset
  341. */
  342. public void copyTo(byte[] data, int offset) {
  343. data[offset] = x;
  344. data[offset + 1] = y;
  345. }
  346. }