1/*
2 * Copyright (C) 2011 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 com.android.camera.panorama;
18
19import java.text.SimpleDateFormat;
20import java.util.Date;
21
22public class PanoUtil {
23    public static String createName(String format, long dateTaken) {
24        Date date = new Date(dateTaken);
25        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
26        return dateFormat.format(date);
27    }
28
29    // TODO: Add comments about the range of these two arguments.
30    public static double calculateDifferenceBetweenAngles(double firstAngle,
31            double secondAngle) {
32        double difference1 = (secondAngle - firstAngle) % 360;
33        if (difference1 < 0) {
34            difference1 += 360;
35        }
36
37        double difference2 = (firstAngle - secondAngle) % 360;
38        if (difference2 < 0) {
39            difference2 += 360;
40        }
41
42        return Math.min(difference1, difference2);
43    }
44
45    public static void decodeYUV420SPQuarterRes(int[] rgb, byte[] yuv420sp, int width, int height) {
46        final int frameSize = width * height;
47
48        for (int j = 0, ypd = 0; j < height; j += 4) {
49            int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
50            for (int i = 0; i < width; i += 4, ypd++) {
51                int y = (0xff & ((int) yuv420sp[j * width + i])) - 16;
52                if (y < 0) {
53                    y = 0;
54                }
55                if ((i & 1) == 0) {
56                    v = (0xff & yuv420sp[uvp++]) - 128;
57                    u = (0xff & yuv420sp[uvp++]) - 128;
58                    uvp += 2;  // Skip the UV values for the 4 pixels skipped in between
59                }
60                int y1192 = 1192 * y;
61                int r = (y1192 + 1634 * v);
62                int g = (y1192 - 833 * v - 400 * u);
63                int b = (y1192 + 2066 * u);
64
65                if (r < 0) {
66                    r = 0;
67                } else if (r > 262143) {
68                    r = 262143;
69                }
70                if (g < 0) {
71                    g = 0;
72                } else if (g > 262143) {
73                    g = 262143;
74                }
75                if (b < 0) {
76                    b = 0;
77                } else if (b > 262143) {
78                    b = 262143;
79                }
80
81                rgb[ypd] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) |
82                        ((b >> 10) & 0xff);
83            }
84        }
85    }
86}
87