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