1 /*
2  * Copyright (C) 2016 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 com.android.dialer.callcomposer.camera.exif;
18 
19 /**
20  * This class stores the EXIF header in IFDs according to the JPEG specification. It is the result
21  * produced by {@link ExifReader}.
22  *
23  * @see ExifReader
24  * @see IfdData
25  */
26 public class ExifData {
27 
28   private final IfdData[] ifdDatas = new IfdData[IfdId.TYPE_IFD_COUNT];
29 
30   /**
31    * Adds IFD data. If IFD data of the same type already exists, it will be replaced by the new
32    * data.
33    */
addIfdData(IfdData data)34   void addIfdData(IfdData data) {
35     ifdDatas[data.getId()] = data;
36   }
37 
38   /** Returns the {@link IfdData} object corresponding to a given IFD if it exists or null. */
getIfdData(int ifdId)39   IfdData getIfdData(int ifdId) {
40     if (ExifTag.isValidIfd(ifdId)) {
41       return ifdDatas[ifdId];
42     }
43     return null;
44   }
45 
46   /**
47    * Returns the tag with a given TID in the given IFD if the tag exists. Otherwise returns null.
48    */
getTag(short tag, int ifd)49   protected ExifTag getTag(short tag, int ifd) {
50     IfdData ifdData = ifdDatas[ifd];
51     return (ifdData == null) ? null : ifdData.getTag(tag);
52   }
53 
54   /**
55    * Adds the given ExifTag to its default IFD and returns an existing ExifTag with the same TID or
56    * null if none exist.
57    */
addTag(ExifTag tag)58   ExifTag addTag(ExifTag tag) {
59     if (tag != null) {
60       int ifd = tag.getIfd();
61       return addTag(tag, ifd);
62     }
63     return null;
64   }
65 
66   /**
67    * Adds the given ExifTag to the given IFD and returns an existing ExifTag with the same TID or
68    * null if none exist.
69    */
addTag(ExifTag tag, int ifdId)70   private ExifTag addTag(ExifTag tag, int ifdId) {
71     if (tag != null && ExifTag.isValidIfd(ifdId)) {
72       IfdData ifdData = getOrCreateIfdData(ifdId);
73       return ifdData.setTag(tag);
74     }
75     return null;
76   }
77 
78   /**
79    * Returns the {@link IfdData} object corresponding to a given IFD or generates one if none exist.
80    */
getOrCreateIfdData(int ifdId)81   private IfdData getOrCreateIfdData(int ifdId) {
82     IfdData ifdData = ifdDatas[ifdId];
83     if (ifdData == null) {
84       ifdData = new IfdData(ifdId);
85       ifdDatas[ifdId] = ifdData;
86     }
87     return ifdData;
88   }
89 }
90