summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/gallery3d/exif/Util.java
blob: 0e51fd7bffc99bf8cae357f6da92283685394811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.exif;

import java.io.Closeable;

class Util {
    public static boolean equals(Object a, Object b) {
        return (a == b) || (a == null ? false : a.equals(b));
    }

    public static void closeSilently(Closeable c) {
        if (c == null) return;
        try {
            c.close();
        } catch (Throwable t) {
            // do nothing
        }
    }

    /**
     * Tags that are not defined in the spec.
     */
    static final short TAG_XP_TITLE = (short) 0x9c9b;
    static final short TAG_XP_COMMENT = (short) 0x9c9c;
    static final short TAG_XP_AUTHOR = (short) 0x9c9d;
    static final short TAG_XP_KEYWORDS = (short) 0x9c9e;
    static final short TAG_XP_SUBJECT = (short) 0x9c9f;

    private static String tagUndefinedTypeValueToString(ExifTag tag) {
        StringBuilder sbuilder = new StringBuilder();
        byte[] buf = new byte[tag.getComponentCount()];
        tag.getBytes(buf);
        switch (tag.getTagId()) {
            case ExifTag.TAG_COMPONENTS_CONFIGURATION:
                for(int i = 0, n = tag.getComponentCount(); i < n; i++) {
                    if(i != 0) sbuilder.append(" ");
                    sbuilder.append(buf[i]);
                }
                break;
            default:
                if (buf.length == 1) {
                    sbuilder.append(buf[0]);
                } else {
                    for (int i = 0, n = buf.length; i < n; i++) {
                        byte code = buf[i];
                        if (code == 0) continue;
                        if (code > 31 && code < 127) {
                            sbuilder.append((char) code);
                        } else {
                            sbuilder.append('.');
                        }
                    }
                }
        }
        return sbuilder.toString();
    }

    /**
     * Returns a string representation of the value of this tag.
     */
    public static String tagValueToString(ExifTag tag) {
        StringBuilder sbuilder = new StringBuilder();
        short id = tag.getTagId();
        switch (tag.getDataType()) {
            case ExifTag.TYPE_UNDEFINED:
                sbuilder.append(tagUndefinedTypeValueToString(tag));
                break;
            case ExifTag.TYPE_UNSIGNED_BYTE:
                if (id == ExifTag.TAG_MAKER_NOTE || id == TAG_XP_TITLE ||
                        id == TAG_XP_COMMENT || id == TAG_XP_AUTHOR ||
                        id == TAG_XP_KEYWORDS || id == TAG_XP_SUBJECT) {
                    sbuilder.append(tagUndefinedTypeValueToString(tag));
                } else {
                    byte[] buf = new byte[tag.getComponentCount()];
                    tag.getBytes(buf);
                    for(int i = 0, n = tag.getComponentCount(); i < n; i++) {
                        if(i != 0) sbuilder.append(" ");
                        sbuilder.append(buf[i]);
                    }
                }
                break;
            case ExifTag.TYPE_ASCII:
                byte[] buf = tag.getStringByte();
                for (int i = 0, n = buf.length; i < n; i++) {
                    byte code = buf[i];
                    if (code == 0) {
                        // Treat some tag as undefined type data.
                        if (id == ExifTag.TAG_COPYRIGHT || id == ExifTag.TAG_GPS_DATE_STAMP) {
                            continue;
                        } else {
                            break;
                        }
                    }
                    if (code > 31 && code < 127) {
                        sbuilder.append((char) code);
                    } else {
                        sbuilder.append('.');
                    }
                }
                break;
            case ExifTag.TYPE_UNSIGNED_LONG:
                for(int i = 0, n = tag.getComponentCount(); i < n; i++) {
                    if(i != 0) sbuilder.append(" ");
                    sbuilder.append(tag.getValueAt(i));
                }
                break;
            case ExifTag.TYPE_RATIONAL:
            case ExifTag.TYPE_UNSIGNED_RATIONAL:
                for(int i = 0, n = tag.getComponentCount(); i < n; i++) {
                    Rational r = tag.getRational(i);
                    if(i != 0) sbuilder.append(" ");
                    sbuilder.append(r.getNominator()).append("/").append(r.getDenominator());
                }
                break;
            case ExifTag.TYPE_UNSIGNED_SHORT:
                for(int i = 0, n = tag.getComponentCount(); i < n; i++) {
                    if(i != 0) sbuilder.append(" ");
                    sbuilder.append((int) tag.getValueAt(i));
                }
                break;
            case ExifTag.TYPE_LONG:
                for(int i = 0, n = tag.getComponentCount(); i < n; i++) {
                    if(i != 0) sbuilder.append(" ");
                    sbuilder.append((int) tag.getValueAt(i));
                }
                break;
        }
        return sbuilder.toString();
    }
}