summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/gallery3d/exif/ExifParserTest.java
blob: c93e09a4c5306faa416c7d93583ccd3e3e720675 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * 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 android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class ExifParserTest extends ExifXmlDataTestCase {
    private static final String TAG = "ExifParserTest";

    public ExifParserTest(int imgRes, int xmlRes) {
        super(imgRes, xmlRes);
    }

    public ExifParserTest(String imgPath, String xmlPath) {
        super(imgPath, xmlPath);
    }

    private List<Map<Short, Set<String>>> mGroundTruth;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        mGroundTruth = ExifXmlReader.readXml(getXmlParser());
    }

    public void testParse() throws Exception {
        try {
            ExifParser parser = ExifParser.parse(getImageInputStream());
            int event = parser.next();
            while (event != ExifParser.EVENT_END) {
                switch (event) {
                    case ExifParser.EVENT_START_OF_IFD:
                        break;
                    case ExifParser.EVENT_NEW_TAG:
                        ExifTag tag = parser.getTag();
                        if (!tag.hasValue()) {
                            parser.registerForTagValue(tag);
                        } else {
                            checkTag(tag);
                        }
                        break;
                    case ExifParser.EVENT_VALUE_OF_REGISTERED_TAG:
                        tag = parser.getTag();
                        if (tag.getDataType() == ExifTag.TYPE_UNDEFINED) {
                            byte[] buf = new byte[tag.getComponentCount()];
                            parser.read(buf);
                            tag.setValue(buf);
                        }
                        checkTag(tag);
                        break;
                }
                event = parser.next();
            }
        } catch (Exception e) {
            throw new Exception(getImageTitle(), e);
        }
    }

    private void checkTag(ExifTag tag) {
        // Ignore offset tags since the ground-truth from exiftool doesn't have it.
        // We can verify it by examining the sub-IFD or thumbnail itself.
        if (ExifTag.isSubIfdOffsetTag(tag.getTagId())) return;

        // TODO: Test MakerNote and UserComment
        if (tag.getTagId() == ExifTag.TAG_MAKER_NOTE
                || tag.getTagId() == ExifTag.TAG_USER_COMMENT) return;

        Set<String> truth = mGroundTruth.get(tag.getIfd()).get(tag.getTagId());

        if (truth == null) {
            fail(String.format("Unknown Tag %02x", tag.getTagId()) + ", " + getImageTitle());
        }

        String dataString = tag.valueToString().trim();
        assertTrue(String.format("Tag %02x", tag.getTagId()) + ", " + getImageTitle()
                + ": " + dataString,
                truth.contains(dataString));
    }

    private void parseOneIfd(int ifd, int options) throws Exception {
        try {
            Map<Short, Set<String>> expectedResult = mGroundTruth.get(ifd);
            int numOfTag = 0;
            ExifParser parser = ExifParser.parse(getImageInputStream(), options);
            int event = parser.next();
            while(event != ExifParser.EVENT_END) {
                switch (event) {
                    case ExifParser.EVENT_START_OF_IFD:
                        assertEquals(getImageTitle(), ifd, parser.getCurrentIfd());
                        break;
                    case ExifParser.EVENT_NEW_TAG:
                        ExifTag tag = parser.getTag();
                        // The exiftool doesn't provide MakerNote tag
                        if (!ExifTag.isSubIfdOffsetTag(tag.getTagId())
                                && tag.getTagId() != ExifTag.TAG_MAKER_NOTE) numOfTag++;
                        if (tag.hasValue()) {
                            checkTag(tag);
                        } else {
                            parser.registerForTagValue(tag);
                        }
                        break;
                    case ExifParser.EVENT_VALUE_OF_REGISTERED_TAG:
                        tag = parser.getTag();
                        if (tag.getDataType() == ExifTag.TYPE_UNDEFINED) {
                            byte[] buf = new byte[tag.getComponentCount()];
                            parser.read(buf);
                            tag.setValue(buf);
                        }
                        checkTag(tag);
                        break;
                    case ExifParser.EVENT_COMPRESSED_IMAGE:
                    case ExifParser.EVENT_UNCOMPRESSED_STRIP:
                        fail("Invalid Event type: " + event + ", " + getImageTitle());
                        break;
                }
                event = parser.next();
            }
            assertEquals(expectedResult.size(), numOfTag);
        } catch (Exception e) {
            throw new Exception(getImageTitle(), e);
        }
    }

    public void testOnlyExifIfd() throws Exception {
        parseOneIfd(IfdId.TYPE_IFD_EXIF, ExifParser.OPTION_IFD_EXIF);
    }

    public void testOnlyIfd0() throws Exception {
        parseOneIfd(IfdId.TYPE_IFD_0, ExifParser.OPTION_IFD_0);
    }

    public void testOnlyIfd1() throws Exception {
        parseOneIfd(IfdId.TYPE_IFD_1, ExifParser.OPTION_IFD_1);
    }

    public void testOnlyInteroperabilityIfd() throws Exception {
        parseOneIfd(IfdId.TYPE_IFD_INTEROPERABILITY, ExifParser.OPTION_IFD_INTEROPERABILITY);
    }

    public void testOnlyReadSomeTag() throws Exception {
        try {
            ExifParser parser = ExifParser.parse(getImageInputStream(), ExifParser.OPTION_IFD_0);
            int event = parser.next();
            boolean isTagFound = false;
            while (event != ExifParser.EVENT_END) {
                switch (event) {
                    case ExifParser.EVENT_START_OF_IFD:
                        assertEquals(getImageTitle(), IfdId.TYPE_IFD_0, parser.getCurrentIfd());
                        break;
                    case ExifParser.EVENT_NEW_TAG:
                        ExifTag tag = parser.getTag();
                        if (tag.getTagId() == ExifTag.TAG_MODEL) {
                            if (tag.hasValue()) {
                                isTagFound = true;
                                checkTag(tag);
                            } else {
                                parser.registerForTagValue(tag);
                            }
                            parser.skipRemainingTagsInCurrentIfd();
                        }
                        break;
                    case ExifParser.EVENT_VALUE_OF_REGISTERED_TAG:
                        tag = parser.getTag();
                        assertEquals(getImageTitle(), ExifTag.TAG_MODEL, tag.getTagId());
                        checkTag(tag);
                        isTagFound = true;
                        break;
                }
                event = parser.next();
            }
            assertTrue(getImageTitle(), isTagFound);
        } catch (Exception e) {
            throw new Exception(getImageTitle(), e);
        }
    }

    public void testReadThumbnail() throws Exception {
        try {
            ExifParser parser = ExifParser.parse(getImageInputStream(),
                    ExifParser.OPTION_IFD_1 | ExifParser.OPTION_THUMBNAIL);

            int event = parser.next();
            Bitmap bmp = null;
            boolean mIsContainCompressedImage = false;
            while (event != ExifParser.EVENT_END) {
                switch (event) {
                    case ExifParser.EVENT_NEW_TAG:
                        ExifTag tag = parser.getTag();
                        if (tag.getTagId() == ExifTag.TAG_COMPRESSION) {
                            if (tag.getUnsignedShort(0) == ExifTag.Compression.JPEG) {
                                mIsContainCompressedImage = true;
                            }
                        }
                        break;
                    case ExifParser.EVENT_COMPRESSED_IMAGE:
                        int imageSize = parser.getCompressedImageSize();
                        byte buf[] = new byte[imageSize];
                        parser.read(buf);
                        bmp = BitmapFactory.decodeByteArray(buf, 0, imageSize);
                        break;
                }
                event = parser.next();
            }
            if (mIsContainCompressedImage) {
                assertNotNull(getImageTitle(), bmp);
            }
        } catch (Exception e) {
            throw new Exception(getImageTitle(), e);
        }
    }
}