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 17 package android.renderscript; 18 19 20 /** 21 * Class for exposing the native RenderScript byte4 type back to the Android system. 22 * 23 **/ 24 public class Byte4 { 25 public byte x; 26 public byte y; 27 public byte z; 28 public byte w; 29 30 public Byte4() { 31 } 32 33 public Byte4(byte initX, byte initY, byte initZ, byte initW) { 34 x = initX; 35 y = initY; 36 z = initZ; 37 w = initW; 38 } 39 /** @hide */ 40 public Byte4(Byte4 source) { 41 this.x = source.x; 42 this.y = source.y; 43 this.z = source.z; 44 this.w = source.w; 45 } 46 47 /** @hide 48 * Vector add 49 * 50 * @param a 51 */ 52 public void add(Byte4 a) { 53 this.x += a.x; 54 this.y += a.y; 55 this.z += a.z; 56 this.w += a.w; 57 } 58 59 /** @hide 60 * Vector add 61 * 62 * @param a 63 * @param b 64 * @return 65 */ 66 public static Byte4 add(Byte4 a, Byte4 b) { 67 Byte4 result = new Byte4(); 68 result.x = (byte)(a.x + b.x); 69 result.y = (byte)(a.y + b.y); 70 result.z = (byte)(a.z + b.z); 71 result.w = (byte)(a.w + b.w); 72 73 return result; 74 } 75 76 /** @hide 77 * Vector add 78 * 79 * @param value 80 */ 81 public void add(byte value) { 82 x += value; 83 y += value; 84 z += value; 85 w += value; 86 } 87 88 /** @hide 89 * Vector add 90 * 91 * @param a 92 * @param b 93 * @return 94 */ 95 public static Byte4 add(Byte4 a, byte b) { 96 Byte4 result = new Byte4(); 97 result.x = (byte)(a.x + b); 98 result.y = (byte)(a.y + b); 99 result.z = (byte)(a.z + b); 100 result.w = (byte)(a.w + b); 101 102 return result; 103 } 104 105 /** @hide 106 * Vector subtraction 107 * 108 * @param a 109 */ 110 public void sub(Byte4 a) { 111 this.x -= a.x; 112 this.y -= a.y; 113 this.z -= a.z; 114 this.w -= a.w; 115 } 116 117 /** @hide 118 * Vector subtraction 119 * 120 * @param a 121 * @param b 122 * @return 123 */ 124 public static Byte4 sub(Byte4 a, Byte4 b) { 125 Byte4 result = new Byte4(); 126 result.x = (byte)(a.x - b.x); 127 result.y = (byte)(a.y - b.y); 128 result.z = (byte)(a.z - b.z); 129 result.w = (byte)(a.w - b.w); 130 131 return result; 132 } 133 134 /** @hide 135 * Vector subtraction 136 * 137 * @param value 138 */ 139 public void sub(byte value) { 140 x -= value; 141 y -= value; 142 z -= value; 143 w -= value; 144 } 145 146 /** @hide 147 * Vector subtraction 148 * 149 * @param a 150 * @param b 151 * @return 152 */ 153 public static Byte4 sub(Byte4 a, byte b) { 154 Byte4 result = new Byte4(); 155 result.x = (byte)(a.x - b); 156 result.y = (byte)(a.y - b); 157 result.z = (byte)(a.z - b); 158 result.w = (byte)(a.w - b); 159 160 return result; 161 } 162 163 /** @hide 164 * Vector multiplication 165 * 166 * @param a 167 */ 168 public void mul(Byte4 a) { 169 this.x *= a.x; 170 this.y *= a.y; 171 this.z *= a.z; 172 this.w *= a.w; 173 } 174 175 /** @hide 176 * Vector multiplication 177 * 178 * @param a 179 * @param b 180 * @return 181 */ 182 public static Byte4 mul(Byte4 a, Byte4 b) { 183 Byte4 result = new Byte4(); 184 result.x = (byte)(a.x * b.x); 185 result.y = (byte)(a.y * b.y); 186 result.z = (byte)(a.z * b.z); 187 result.w = (byte)(a.w * b.w); 188 189 return result; 190 } 191 192 /** @hide 193 * Vector multiplication 194 * 195 * @param value 196 */ 197 public void mul(byte value) { 198 x *= value; 199 y *= value; 200 z *= value; 201 w *= value; 202 } 203 204 /** @hide 205 * Vector multiplication 206 * 207 * @param a 208 * @param b 209 * @return 210 */ 211 public static Byte4 mul(Byte4 a, byte b) { 212 Byte4 result = new Byte4(); 213 result.x = (byte)(a.x * b); 214 result.y = (byte)(a.y * b); 215 result.z = (byte)(a.z * b); 216 result.w = (byte)(a.w * b); 217 218 return result; 219 } 220 221 /** @hide 222 * Vector division 223 * 224 * @param a 225 */ 226 public void div(Byte4 a) { 227 this.x /= a.x; 228 this.y /= a.y; 229 this.z /= a.z; 230 this.w /= a.w; 231 } 232 233 /** @hide 234 * Vector division 235 * 236 * @param a 237 * @param b 238 * @return 239 */ 240 public static Byte4 div(Byte4 a, Byte4 b) { 241 Byte4 result = new Byte4(); 242 result.x = (byte)(a.x / b.x); 243 result.y = (byte)(a.y / b.y); 244 result.z = (byte)(a.z / b.z); 245 result.w = (byte)(a.w / b.w); 246 247 return result; 248 } 249 250 /** @hide 251 * Vector division 252 * 253 * @param value 254 */ 255 public void div(byte value) { 256 x /= value; 257 y /= value; 258 z /= value; 259 w /= value; 260 } 261 262 /** @hide 263 * Vector division 264 * 265 * @param a 266 * @param b 267 * @return 268 */ 269 public static Byte4 div(Byte4 a, byte b) { 270 Byte4 result = new Byte4(); 271 result.x = (byte)(a.x / b); 272 result.y = (byte)(a.y / b); 273 result.z = (byte)(a.z / b); 274 result.w = (byte)(a.w / b); 275 276 return result; 277 } 278 279 /** @hide 280 * get vector length 281 * 282 * @return 283 */ 284 public byte length() { 285 return 4; 286 } 287 288 /** @hide 289 * set vector negate 290 */ 291 public void negate() { 292 this.x = (byte)(-x); 293 this.y = (byte)(-y); 294 this.z = (byte)(-z); 295 this.w = (byte)(-w); 296 } 297 298 /** @hide 299 * Vector dot Product 300 * 301 * @param a 302 * @return 303 */ 304 public byte dotProduct(Byte4 a) { 305 return (byte)((x * a.x) + (y * a.y) + (z * a.z) + (w * a.w)); 306 } 307 308 /** @hide 309 * Vector dot Product 310 * 311 * @param a 312 * @param b 313 * @return 314 */ 315 public static byte dotProduct(Byte4 a, Byte4 b) { 316 return (byte)((b.x * a.x) + (b.y * a.y) + (b.z * a.z) + (b.w * a.w)); 317 } 318 319 /** @hide 320 * Vector add Multiple 321 * 322 * @param a 323 * @param factor 324 */ 325 public void addMultiple(Byte4 a, byte factor) { 326 x += a.x * factor; 327 y += a.y * factor; 328 z += a.z * factor; 329 w += a.w * factor; 330 } 331 332 /** @hide 333 * set vector value by Byte4 334 * 335 * @param a 336 */ 337 public void set(Byte4 a) { 338 this.x = a.x; 339 this.y = a.y; 340 this.z = a.z; 341 this.w = a.w; 342 } 343 344 /** @hide 345 * set the vector field values 346 * 347 * @param a 348 * @param b 349 * @param c 350 * @param d 351 */ 352 public void setValues(byte a, byte b, byte c, byte d) { 353 this.x = a; 354 this.y = b; 355 this.z = c; 356 this.w = d; 357 } 358 359 /** @hide 360 * return the element sum of vector 361 * 362 * @return 363 */ 364 public byte elementSum() { 365 return (byte)(x + y + z + w); 366 } 367 368 /** @hide 369 * get the vector field value by index 370 * 371 * @param i 372 * @return 373 */ 374 public byte get(int i) { 375 switch (i) { 376 case 0: 377 return x; 378 case 1: 379 return y; 380 case 2: 381 return z; 382 case 3: 383 return w; 384 default: 385 throw new IndexOutOfBoundsException("Index: i"); 386 } 387 } 388 389 /** @hide 390 * set the vector field value by index 391 * 392 * @param i 393 * @param value 394 */ 395 public void setAt(int i, byte value) { 396 switch (i) { 397 case 0: 398 x = value; 399 return; 400 case 1: 401 y = value; 402 return; 403 case 2: 404 z = value; 405 return; 406 case 3: 407 w = value; 408 return; 409 default: 410 throw new IndexOutOfBoundsException("Index: i"); 411 } 412 } 413 414 /** @hide 415 * add the vector field value by index 416 * 417 * @param i 418 * @param value 419 */ 420 public void addAt(int i, byte value) { 421 switch (i) { 422 case 0: 423 x += value; 424 return; 425 case 1: 426 y += value; 427 return; 428 case 2: 429 z += value; 430 return; 431 case 3: 432 w += value; 433 return; 434 default: 435 throw new IndexOutOfBoundsException("Index: i"); 436 } 437 } 438 439 /** @hide 440 * copy the vector to Char array 441 * 442 * @param data 443 * @param offset 444 */ 445 public void copyTo(byte[] data, int offset) { 446 data[offset] = x; 447 data[offset + 1] = y; 448 data[offset + 2] = z; 449 data[offset + 3] = w; 450 } 451 } 452 453 454 455