OSDN Git Service

Merge "Add many contextual actions to new gallery" into gb-ub-photos-bryce
[android-x86/packages-apps-Gallery2.git] / gallerycommon / src / com / android / gallery3d / exif / ExifTag.java
1 /*
2  * Copyright (C) 2012 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.gallery3d.exif;
18
19 import java.nio.charset.Charset;
20 import java.text.SimpleDateFormat;
21 import java.util.Arrays;
22 import java.util.Date;
23
24 /**
25  * This class stores information of an EXIF tag. For more information about
26  * defined EXIF tags, please read the Jeita EXIF 2.2 standard. Tags should be
27  * instantiated using {@link ExifInterface#buildTag}.
28  *
29  * @see ExifInterface
30  */
31 public class ExifTag {
32     /**
33      * The BYTE type in the EXIF standard. An 8-bit unsigned integer.
34      */
35     public static final short TYPE_UNSIGNED_BYTE = 1;
36     /**
37      * The ASCII type in the EXIF standard. An 8-bit byte containing one 7-bit
38      * ASCII code. The final byte is terminated with NULL.
39      */
40     public static final short TYPE_ASCII = 2;
41     /**
42      * The SHORT type in the EXIF standard. A 16-bit (2-byte) unsigned integer
43      */
44     public static final short TYPE_UNSIGNED_SHORT = 3;
45     /**
46      * The LONG type in the EXIF standard. A 32-bit (4-byte) unsigned integer
47      */
48     public static final short TYPE_UNSIGNED_LONG = 4;
49     /**
50      * The RATIONAL type of EXIF standard. It consists of two LONGs. The first
51      * one is the numerator and the second one expresses the denominator.
52      */
53     public static final short TYPE_UNSIGNED_RATIONAL = 5;
54     /**
55      * The UNDEFINED type in the EXIF standard. An 8-bit byte that can take any
56      * value depending on the field definition.
57      */
58     public static final short TYPE_UNDEFINED = 7;
59     /**
60      * The SLONG type in the EXIF standard. A 32-bit (4-byte) signed integer
61      * (2's complement notation).
62      */
63     public static final short TYPE_LONG = 9;
64     /**
65      * The SRATIONAL type of EXIF standard. It consists of two SLONGs. The first
66      * one is the numerator and the second one is the denominator.
67      */
68     public static final short TYPE_RATIONAL = 10;
69
70     private static Charset US_ASCII = Charset.forName("US-ASCII");
71     private static final int TYPE_TO_SIZE_MAP[] = new int[11];
72     private static final int UNSIGNED_SHORT_MAX = 65535;
73     private static final long UNSIGNED_LONG_MAX = 4294967295L;
74     private static final long LONG_MAX = Integer.MAX_VALUE;
75     private static final long LONG_MIN = Integer.MIN_VALUE;
76
77     static {
78         TYPE_TO_SIZE_MAP[TYPE_UNSIGNED_BYTE] = 1;
79         TYPE_TO_SIZE_MAP[TYPE_ASCII] = 1;
80         TYPE_TO_SIZE_MAP[TYPE_UNSIGNED_SHORT] = 2;
81         TYPE_TO_SIZE_MAP[TYPE_UNSIGNED_LONG] = 4;
82         TYPE_TO_SIZE_MAP[TYPE_UNSIGNED_RATIONAL] = 8;
83         TYPE_TO_SIZE_MAP[TYPE_UNDEFINED] = 1;
84         TYPE_TO_SIZE_MAP[TYPE_LONG] = 4;
85         TYPE_TO_SIZE_MAP[TYPE_RATIONAL] = 8;
86     }
87
88     static final int SIZE_UNDEFINED = 0;
89
90     // Exif TagId
91     private final short mTagId;
92     // Exif Tag Type
93     private final short mDataType;
94     // If tag has defined count
95     private boolean mHasDefinedDefaultComponentCount;
96     // Actual data count in tag (should be number of elements in value array)
97     private int mComponentCountActual;
98     // The ifd that this tag should be put in
99     private int mIfd;
100     // The value (array of elements of type Tag Type)
101     private Object mValue;
102     // Value offset in exif header.
103     private int mOffset;
104
105     private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("yyyy:MM:dd kk:mm:ss");
106
107     /**
108      * Returns true if the given IFD is a valid IFD.
109      */
110     public static boolean isValidIfd(int ifdId) {
111         return ifdId == IfdId.TYPE_IFD_0 || ifdId == IfdId.TYPE_IFD_1
112                 || ifdId == IfdId.TYPE_IFD_EXIF || ifdId == IfdId.TYPE_IFD_INTEROPERABILITY
113                 || ifdId == IfdId.TYPE_IFD_GPS;
114     }
115
116     /**
117      * Returns true if a given type is a valid tag type.
118      */
119     public static boolean isValidType(short type) {
120         return type == TYPE_UNSIGNED_BYTE || type == TYPE_ASCII ||
121                 type == TYPE_UNSIGNED_SHORT || type == TYPE_UNSIGNED_LONG ||
122                 type == TYPE_UNSIGNED_RATIONAL || type == TYPE_UNDEFINED ||
123                 type == TYPE_LONG || type == TYPE_RATIONAL;
124     }
125
126     // Use builtTag in ExifInterface instead of constructor.
127     ExifTag(short tagId, short type, int componentCount, int ifd,
128             boolean hasDefinedComponentCount) {
129         mTagId = tagId;
130         mDataType = type;
131         mComponentCountActual = componentCount;
132         mHasDefinedDefaultComponentCount = hasDefinedComponentCount;
133         mIfd = ifd;
134         mValue = null;
135     }
136
137     /**
138      * Gets the element size of the given data type in bytes.
139      *
140      * @see #TYPE_ASCII
141      * @see #TYPE_LONG
142      * @see #TYPE_RATIONAL
143      * @see #TYPE_UNDEFINED
144      * @see #TYPE_UNSIGNED_BYTE
145      * @see #TYPE_UNSIGNED_LONG
146      * @see #TYPE_UNSIGNED_RATIONAL
147      * @see #TYPE_UNSIGNED_SHORT
148      */
149     public static int getElementSize(short type) {
150         return TYPE_TO_SIZE_MAP[type];
151     }
152
153     /**
154      * Returns the ID of the IFD this tag belongs to.
155      *
156      * @see IfdId#TYPE_IFD_0
157      * @see IfdId#TYPE_IFD_1
158      * @see IfdId#TYPE_IFD_EXIF
159      * @see IfdId#TYPE_IFD_GPS
160      * @see IfdId#TYPE_IFD_INTEROPERABILITY
161      */
162     public int getIfd() {
163         return mIfd;
164     }
165
166     protected void setIfd(int ifdId) {
167         mIfd = ifdId;
168     }
169
170     /**
171      * Gets the TID of this tag.
172      */
173     public short getTagId() {
174         return mTagId;
175     }
176
177     /**
178      * Gets the data type of this tag
179      *
180      * @see #TYPE_ASCII
181      * @see #TYPE_LONG
182      * @see #TYPE_RATIONAL
183      * @see #TYPE_UNDEFINED
184      * @see #TYPE_UNSIGNED_BYTE
185      * @see #TYPE_UNSIGNED_LONG
186      * @see #TYPE_UNSIGNED_RATIONAL
187      * @see #TYPE_UNSIGNED_SHORT
188      */
189     public short getDataType() {
190         return mDataType;
191     }
192
193     /**
194      * Gets the total data size in bytes of the value of this tag.
195      */
196     public int getDataSize() {
197         return getComponentCount() * getElementSize(getDataType());
198     }
199
200     /**
201      * Gets the component count of this tag.
202      */
203
204     // TODO: fix integer overflows with this
205     public int getComponentCount() {
206         return mComponentCountActual;
207     }
208
209     /**
210      * Sets the component count of this tag. Call this function before
211      * setValue() if the length of value does not match the component count.
212      */
213     protected void forceSetComponentCount(int count) {
214         mComponentCountActual = count;
215     }
216
217     /**
218      * Returns true if this ExifTag contains value; otherwise, this tag will
219      * contain an offset value that is determined when the tag is written.
220      */
221     public boolean hasValue() {
222         return mValue != null;
223     }
224
225     /**
226      * Sets integer values into this tag. This method should be used for tags of
227      * type {@link #TYPE_UNSIGNED_SHORT}. This method will fail if:
228      * <ul>
229      * <li>The component type of this tag is not {@link #TYPE_UNSIGNED_SHORT},
230      * {@link #TYPE_UNSIGNED_LONG}, or {@link #TYPE_LONG}.</li>
231      * <li>The value overflows.</li>
232      * <li>The value.length does NOT match the component count in the definition
233      * for this tag.</li>
234      * </ul>
235      */
236     public boolean setValue(int[] value) {
237         if (checkBadComponentCount(value.length)) {
238             return false;
239         }
240         if (mDataType != TYPE_UNSIGNED_SHORT && mDataType != TYPE_LONG &&
241                 mDataType != TYPE_UNSIGNED_LONG) {
242             return false;
243         }
244         if (mDataType == TYPE_UNSIGNED_SHORT && checkOverflowForUnsignedShort(value)) {
245             return false;
246         } else if (mDataType == TYPE_UNSIGNED_LONG && checkOverflowForUnsignedLong(value)) {
247             return false;
248         }
249
250         long[] data = new long[value.length];
251         for (int i = 0; i < value.length; i++) {
252             data[i] = value[i];
253         }
254         mValue = data;
255         mComponentCountActual = value.length;
256         return true;
257     }
258
259     /**
260      * Sets integer value into this tag. This method should be used for tags of
261      * type {@link #TYPE_UNSIGNED_SHORT}, or {@link #TYPE_LONG}. This method
262      * will fail if:
263      * <ul>
264      * <li>The component type of this tag is not {@link #TYPE_UNSIGNED_SHORT},
265      * {@link #TYPE_UNSIGNED_LONG}, or {@link #TYPE_LONG}.</li>
266      * <li>The value overflows.</li>
267      * <li>The component count in the definition of this tag is not 1.</li>
268      * </ul>
269      */
270     public boolean setValue(int value) {
271         return setValue(new int[] {
272                 value
273         });
274     }
275
276     /**
277      * Sets long values into this tag. This method should be used for tags of
278      * type {@link #TYPE_UNSIGNED_LONG}. This method will fail if:
279      * <ul>
280      * <li>The component type of this tag is not {@link #TYPE_UNSIGNED_LONG}.</li>
281      * <li>The value overflows.</li>
282      * <li>The value.length does NOT match the component count in the definition
283      * for this tag.</li>
284      * </ul>
285      */
286     public boolean setValue(long[] value) {
287         if (checkBadComponentCount(value.length) || mDataType != TYPE_UNSIGNED_LONG) {
288             return false;
289         }
290         if (checkOverflowForUnsignedLong(value)) {
291             return false;
292         }
293         mValue = value;
294         mComponentCountActual = value.length;
295         return true;
296     }
297
298     /**
299      * Sets long values into this tag. This method should be used for tags of
300      * type {@link #TYPE_UNSIGNED_LONG}. This method will fail if:
301      * <ul>
302      * <li>The component type of this tag is not {@link #TYPE_UNSIGNED_LONG}.</li>
303      * <li>The value overflows.</li>
304      * <li>The component count in the definition for this tag is not 1.</li>
305      * </ul>
306      */
307     public boolean setValue(long value) {
308         return setValue(new long[] {
309                 value
310         });
311     }
312
313     /**
314      * Sets a string value into this tag. This method should be used for tags of
315      * type {@link #TYPE_ASCII}. The string is converted to an ASCII string.
316      * Characters that cannot be converted are replaced with '?'. The length of
317      * the string must be equal to either (component count -1) or (component
318      * count). The final byte will be set to the string null terminator '\0',
319      * overwriting the last character in the string if the value.length is equal
320      * to the component count. This method will fail if:
321      * <ul>
322      * <li>The data type is not {@link #TYPE_ASCII} or {@link #TYPE_UNDEFINED}.</li>
323      * <li>The length of the string is not equal to (component count -1) or
324      * (component count) in the definition for this tag.</li>
325      * </ul>
326      */
327     public boolean setValue(String value) {
328         if (mDataType != TYPE_ASCII && mDataType != TYPE_UNDEFINED) {
329             return false;
330         }
331
332         byte[] buf = value.getBytes(US_ASCII);
333         byte[] finalBuf = (buf[buf.length - 1] == 0 || mDataType == TYPE_UNDEFINED) ? buf : Arrays
334                 .copyOf(buf, buf.length + 1);
335         int count = finalBuf.length;
336         if (checkBadComponentCount(count)) {
337             return false;
338         }
339         mComponentCountActual = count;
340         mValue = finalBuf;
341         return true;
342     }
343
344     /**
345      * Sets Rational values into this tag. This method should be used for tags
346      * of type {@link #TYPE_UNSIGNED_RATIONAL}, or {@link #TYPE_RATIONAL}. This
347      * method will fail if:
348      * <ul>
349      * <li>The component type of this tag is not {@link #TYPE_UNSIGNED_RATIONAL}
350      * or {@link #TYPE_RATIONAL}.</li>
351      * <li>The value overflows.</li>
352      * <li>The value.length does NOT match the component count in the definition
353      * for this tag.</li>
354      * </ul>
355      *
356      * @see Rational
357      */
358     public boolean setValue(Rational[] value) {
359         if (checkBadComponentCount(value.length)) {
360             return false;
361         }
362         if (mDataType != TYPE_UNSIGNED_RATIONAL && mDataType != TYPE_RATIONAL) {
363             return false;
364         }
365         if (mDataType == TYPE_UNSIGNED_RATIONAL && checkOverflowForUnsignedRational(value)) {
366             return false;
367         } else if (mDataType == TYPE_RATIONAL && checkOverflowForRational(value)) {
368             return false;
369         }
370
371         mValue = value;
372         mComponentCountActual = value.length;
373         return true;
374     }
375
376     /**
377      * Sets a Rational value into this tag. This method should be used for tags
378      * of type {@link #TYPE_UNSIGNED_RATIONAL}, or {@link #TYPE_RATIONAL}. This
379      * method will fail if:
380      * <ul>
381      * <li>The component type of this tag is not {@link #TYPE_UNSIGNED_RATIONAL}
382      * or {@link #TYPE_RATIONAL}.</li>
383      * <li>The value overflows.</li>
384      * <li>The component count in the definition for this tag is not 1.</li>
385      * </ul>
386      *
387      * @see Rational
388      */
389     public boolean setValue(Rational value) {
390         return setValue(new Rational[] {
391                 value
392         });
393     }
394
395     /**
396      * Sets byte values into this tag. This method should be used for tags of
397      * type {@link #TYPE_UNSIGNED_BYTE} or {@link #TYPE_UNDEFINED}. This method
398      * will fail if:
399      * <ul>
400      * <li>The component type of this tag is not {@link #TYPE_UNSIGNED_BYTE} or
401      * {@link #TYPE_UNDEFINED} .</li>
402      * <li>The length does NOT match the component count in the definition for
403      * this tag.</li>
404      * </ul>
405      */
406     public boolean setValue(byte[] value, int offset, int length) {
407         if (checkBadComponentCount(length)) {
408             return false;
409         }
410         if (mDataType != TYPE_UNSIGNED_BYTE && mDataType != TYPE_UNDEFINED) {
411             return false;
412         }
413         mValue = new byte[length];
414         System.arraycopy(value, offset, mValue, 0, length);
415         mComponentCountActual = length;
416         return true;
417     }
418
419     /**
420      * Equivalent to setValue(value, 0, value.length).
421      */
422     public boolean setValue(byte[] value) {
423         return setValue(value, 0, value.length);
424     }
425
426     /**
427      * Sets byte value into this tag. This method should be used for tags of
428      * type {@link #TYPE_UNSIGNED_BYTE} or {@link #TYPE_UNDEFINED}. This method
429      * will fail if:
430      * <ul>
431      * <li>The component type of this tag is not {@link #TYPE_UNSIGNED_BYTE} or
432      * {@link #TYPE_UNDEFINED} .</li>
433      * <li>The component count in the definition for this tag is not 1.</li>
434      * </ul>
435      */
436     public boolean setValue(byte value) {
437         return setValue(new byte[] {
438                 value
439         });
440     }
441
442     /**
443      * Sets the value for this tag using an appropriate setValue method for the
444      * given object. This method will fail if:
445      * <ul>
446      * <li>The corresponding setValue method for the class of the object passed
447      * in would fail.</li>
448      * <li>There is no obvious way to cast the object passed in into an EXIF tag
449      * type.</li>
450      * </ul>
451      */
452     public boolean setValue(Object obj) {
453         if (obj == null) {
454             return false;
455         } else if (obj instanceof Short) {
456             return setValue(((Short) obj).shortValue() & 0x0ffff);
457         } else if (obj instanceof String) {
458             return setValue((String) obj);
459         } else if (obj instanceof int[]) {
460             return setValue((int[]) obj);
461         } else if (obj instanceof long[]) {
462             return setValue((long[]) obj);
463         } else if (obj instanceof Rational) {
464             return setValue((Rational) obj);
465         } else if (obj instanceof Rational[]) {
466             return setValue((Rational[]) obj);
467         } else if (obj instanceof byte[]) {
468             return setValue((byte[]) obj);
469         } else if (obj instanceof Integer) {
470             return setValue(((Integer) obj).intValue());
471         } else if (obj instanceof Long) {
472             return setValue(((Long) obj).longValue());
473         } else if (obj instanceof Byte) {
474             return setValue(((Byte) obj).byteValue());
475         } else if (obj instanceof Short[]) {
476             // Nulls in this array are treated as zeroes.
477             Short[] arr = (Short[]) obj;
478             int[] fin = new int[arr.length];
479             for (int i = 0; i < arr.length; i++) {
480                 fin[i] = (arr[i] == null) ? 0 : arr[i].shortValue() & 0x0ffff;
481             }
482             return setValue(fin);
483         } else if (obj instanceof Integer[]) {
484             // Nulls in this array are treated as zeroes.
485             Integer[] arr = (Integer[]) obj;
486             int[] fin = new int[arr.length];
487             for (int i = 0; i < arr.length; i++) {
488                 fin[i] = (arr[i] == null) ? 0 : arr[i].intValue();
489             }
490             return setValue(fin);
491         } else if (obj instanceof Long[]) {
492             // Nulls in this array are treated as zeroes.
493             Long[] arr = (Long[]) obj;
494             long[] fin = new long[arr.length];
495             for (int i = 0; i < arr.length; i++) {
496                 fin[i] = (arr[i] == null) ? 0 : arr[i].longValue();
497             }
498             return setValue(fin);
499         } else if (obj instanceof Byte[]) {
500             // Nulls in this array are treated as zeroes.
501             Byte[] arr = (Byte[]) obj;
502             byte[] fin = new byte[arr.length];
503             for (int i = 0; i < arr.length; i++) {
504                 fin[i] = (arr[i] == null) ? 0 : arr[i].byteValue();
505             }
506             return setValue(fin);
507         } else {
508             return false;
509         }
510     }
511
512     /**
513      * Sets a timestamp to this tag. The method converts the timestamp with the
514      * format of "yyyy:MM:dd kk:mm:ss" and calls {@link #setValue(String)}. This
515      * method will fail if the data type is not {@link #TYPE_ASCII} or the
516      * component count of this tag is not 20 or undefined.
517      *
518      * @param time the number of milliseconds since Jan. 1, 1970 GMT
519      * @return true on success
520      */
521     public boolean setTimeValue(long time) {
522         // synchronized on TIME_FORMAT as SimpleDateFormat is not thread safe
523         synchronized (TIME_FORMAT) {
524             return setValue(TIME_FORMAT.format(new Date(time)));
525         }
526     }
527
528     /**
529      * Gets the value as a String. This method should be used for tags of type
530      * {@link #TYPE_ASCII}.
531      *
532      * @return the value as a String, or null if the tag's value does not exist
533      *         or cannot be converted to a String.
534      */
535     public String getValueAsString() {
536         if (mValue == null) {
537             return null;
538         } else if (mValue instanceof String) {
539             return (String) mValue;
540         } else if (mValue instanceof byte[]) {
541             return new String((byte[]) mValue, US_ASCII);
542         }
543         return null;
544     }
545
546     /**
547      * Gets the value as a String. This method should be used for tags of type
548      * {@link #TYPE_ASCII}.
549      *
550      * @param defaultValue the String to return if the tag's value does not
551      *            exist or cannot be converted to a String.
552      * @return the tag's value as a String, or the defaultValue.
553      */
554     public String getValueAsString(String defaultValue) {
555         String s = getValueAsString();
556         if (s == null) {
557             return defaultValue;
558         }
559         return s;
560     }
561
562     /**
563      * Gets the value as a byte array. This method should be used for tags of
564      * type {@link #TYPE_UNDEFINED} or {@link #TYPE_UNSIGNED_BYTE}.
565      *
566      * @return the value as a byte array, or null if the tag's value does not
567      *         exist or cannot be converted to a byte array.
568      */
569     public byte[] getValueAsBytes() {
570         if (mValue instanceof byte[]) {
571             return (byte[]) mValue;
572         }
573         return null;
574     }
575
576     /**
577      * Gets the value as a byte. If there are more than 1 bytes in this value,
578      * gets the first byte. This method should be used for tags of type
579      * {@link #TYPE_UNDEFINED} or {@link #TYPE_UNSIGNED_BYTE}.
580      *
581      * @param defaultValue the byte to return if tag's value does not exist or
582      *            cannot be converted to a byte.
583      * @return the tag's value as a byte, or the defaultValue.
584      */
585     public byte getValueAsByte(byte defaultValue) {
586         byte[] b = getValueAsBytes();
587         if (b == null || b.length < 1) {
588             return defaultValue;
589         }
590         return b[0];
591     }
592
593     /**
594      * Gets the value as an array of Rationals. This method should be used for
595      * tags of type {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}.
596      *
597      * @return the value as as an array of Rationals, or null if the tag's value
598      *         does not exist or cannot be converted to an array of Rationals.
599      */
600     public Rational[] getValueAsRationals() {
601         if (mValue instanceof Rational[]) {
602             return (Rational[]) mValue;
603         }
604         return null;
605     }
606
607     /**
608      * Gets the value as a Rational. If there are more than 1 Rationals in this
609      * value, gets the first one. This method should be used for tags of type
610      * {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}.
611      *
612      * @param defaultValue the Rational to return if tag's value does not exist
613      *            or cannot be converted to a Rational.
614      * @return the tag's value as a Rational, or the defaultValue.
615      */
616     public Rational getValueAsRational(Rational defaultValue) {
617         Rational[] r = getValueAsRationals();
618         if (r == null || r.length < 1) {
619             return defaultValue;
620         }
621         return r[0];
622     }
623
624     /**
625      * Gets the value as a Rational. If there are more than 1 Rationals in this
626      * value, gets the first one. This method should be used for tags of type
627      * {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}.
628      *
629      * @param defaultValue the numerator of the Rational to return if tag's
630      *            value does not exist or cannot be converted to a Rational (the
631      *            denominator will be 1).
632      * @return the tag's value as a Rational, or the defaultValue.
633      */
634     public Rational getValueAsRational(long defaultValue) {
635         Rational defaultVal = new Rational(defaultValue, 1);
636         return getValueAsRational(defaultVal);
637     }
638
639     /**
640      * Gets the value as an array of ints. This method should be used for tags
641      * of type {@link #TYPE_UNSIGNED_SHORT}, {@link #TYPE_UNSIGNED_LONG}.
642      *
643      * @return the value as as an array of ints, or null if the tag's value does
644      *         not exist or cannot be converted to an array of ints.
645      */
646     public int[] getValueAsInts() {
647         if (mValue == null) {
648             return null;
649         } else if (mValue instanceof long[]) {
650             long[] val = (long[]) mValue;
651             int[] arr = new int[val.length];
652             for (int i = 0; i < val.length; i++) {
653                 arr[i] = (int) val[i]; // Truncates
654             }
655             return arr;
656         }
657         return null;
658     }
659
660     /**
661      * Gets the value as an int. If there are more than 1 ints in this value,
662      * gets the first one. This method should be used for tags of type
663      * {@link #TYPE_UNSIGNED_SHORT}, {@link #TYPE_UNSIGNED_LONG}.
664      *
665      * @param defaultValue the int to return if tag's value does not exist or
666      *            cannot be converted to an int.
667      * @return the tag's value as a int, or the defaultValue.
668      */
669     public int getValueAsInt(int defaultValue) {
670         int[] i = getValueAsInts();
671         if (i == null || i.length < 1) {
672             return defaultValue;
673         }
674         return i[0];
675     }
676
677     /**
678      * Gets the value as an array of longs. This method should be used for tags
679      * of type {@link #TYPE_UNSIGNED_LONG}.
680      *
681      * @return the value as as an array of longs, or null if the tag's value
682      *         does not exist or cannot be converted to an array of longs.
683      */
684     public long[] getValueAsLongs() {
685         if (mValue instanceof long[]) {
686             return (long[]) mValue;
687         }
688         return null;
689     }
690
691     /**
692      * Gets the value or null if none exists. If there are more than 1 longs in
693      * this value, gets the first one. This method should be used for tags of
694      * type {@link #TYPE_UNSIGNED_LONG}.
695      *
696      * @param defaultValue the long to return if tag's value does not exist or
697      *            cannot be converted to a long.
698      * @return the tag's value as a long, or the defaultValue.
699      */
700     public long getValueAsLong(long defaultValue) {
701         long[] l = getValueAsLongs();
702         if (l == null || l.length < 1) {
703             return defaultValue;
704         }
705         return l[0];
706     }
707
708     /**
709      * Gets the tag's value or null if none exists.
710      */
711     public Object getValue() {
712         return mValue;
713     }
714
715     /**
716      * Gets a long representation of the value.
717      *
718      * @param defaultValue value to return if there is no value or value is a
719      *            rational with a denominator of 0.
720      * @return the tag's value as a long, or defaultValue if no representation
721      *         exists.
722      */
723     public long forceGetValueAsLong(long defaultValue) {
724         long[] l = getValueAsLongs();
725         if (l != null && l.length >= 1) {
726             return l[0];
727         }
728         byte[] b = getValueAsBytes();
729         if (b != null && b.length >= 1) {
730             return b[0];
731         }
732         Rational[] r = getValueAsRationals();
733         if (r != null && r.length >= 1 && r[0].getDenominator() != 0) {
734             return (long) r[0].toDouble();
735         }
736         return defaultValue;
737     }
738
739     /**
740      * Gets a string representation of the value.
741      */
742     public String forceGetValueAsString() {
743         if (mValue == null) {
744             return "";
745         } else if (mValue instanceof byte[]) {
746             if (mDataType == TYPE_ASCII) {
747                 return new String((byte[]) mValue, US_ASCII);
748             } else {
749                 return Arrays.toString((byte[]) mValue);
750             }
751         } else if (mValue instanceof long[]) {
752             if (((long[]) mValue).length == 1) {
753                 return String.valueOf(((long[]) mValue)[0]);
754             } else {
755                 return Arrays.toString((long[]) mValue);
756             }
757         } else if (mValue instanceof Object[]) {
758             if (((Object[]) mValue).length == 1) {
759                 Object val = ((Object[]) mValue)[0];
760                 if (val == null) {
761                     return "";
762                 } else {
763                     return val.toString();
764                 }
765             } else {
766                 return Arrays.toString((Object[]) mValue);
767             }
768         } else {
769             return mValue.toString();
770         }
771     }
772
773     /**
774      * Gets the value for type {@link #TYPE_ASCII}, {@link #TYPE_LONG},
775      * {@link #TYPE_UNDEFINED}, {@link #TYPE_UNSIGNED_BYTE},
776      * {@link #TYPE_UNSIGNED_LONG}, or {@link #TYPE_UNSIGNED_SHORT}. For
777      * {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}, call
778      * {@link #getRational(int)} instead.
779      *
780      * @exception IllegalArgumentException if the data type is
781      *                {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}.
782      */
783     protected long getValueAt(int index) {
784         if (mValue instanceof long[]) {
785             return ((long[]) mValue)[index];
786         } else if (mValue instanceof byte[]) {
787             return ((byte[]) mValue)[index];
788         }
789         throw new IllegalArgumentException("Cannot get integer value from "
790                 + convertTypeToString(mDataType));
791     }
792
793     /**
794      * Gets the {@link #TYPE_ASCII} data.
795      *
796      * @exception IllegalArgumentException If the type is NOT
797      *                {@link #TYPE_ASCII}.
798      */
799     protected String getString() {
800         if (mDataType != TYPE_ASCII) {
801             throw new IllegalArgumentException("Cannot get ASCII value from "
802                     + convertTypeToString(mDataType));
803         }
804         return new String((byte[]) mValue, US_ASCII);
805     }
806
807     /*
808      * Get the converted ascii byte. Used by ExifOutputStream.
809      */
810     protected byte[] getStringByte() {
811         return (byte[]) mValue;
812     }
813
814     /**
815      * Gets the {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL} data.
816      *
817      * @exception IllegalArgumentException If the type is NOT
818      *                {@link #TYPE_RATIONAL} or {@link #TYPE_UNSIGNED_RATIONAL}.
819      */
820     protected Rational getRational(int index) {
821         if ((mDataType != TYPE_RATIONAL) && (mDataType != TYPE_UNSIGNED_RATIONAL)) {
822             throw new IllegalArgumentException("Cannot get RATIONAL value from "
823                     + convertTypeToString(mDataType));
824         }
825         return ((Rational[]) mValue)[index];
826     }
827
828     /**
829      * Equivalent to getBytes(buffer, 0, buffer.length).
830      */
831     protected void getBytes(byte[] buf) {
832         getBytes(buf, 0, buf.length);
833     }
834
835     /**
836      * Gets the {@link #TYPE_UNDEFINED} or {@link #TYPE_UNSIGNED_BYTE} data.
837      *
838      * @param buf the byte array in which to store the bytes read.
839      * @param offset the initial position in buffer to store the bytes.
840      * @param length the maximum number of bytes to store in buffer. If length >
841      *            component count, only the valid bytes will be stored.
842      * @exception IllegalArgumentException If the type is NOT
843      *                {@link #TYPE_UNDEFINED} or {@link #TYPE_UNSIGNED_BYTE}.
844      */
845     protected void getBytes(byte[] buf, int offset, int length) {
846         if ((mDataType != TYPE_UNDEFINED) && (mDataType != TYPE_UNSIGNED_BYTE)) {
847             throw new IllegalArgumentException("Cannot get BYTE value from "
848                     + convertTypeToString(mDataType));
849         }
850         System.arraycopy(mValue, 0, buf, offset,
851                 (length > mComponentCountActual) ? mComponentCountActual : length);
852     }
853
854     /**
855      * Gets the offset of this tag. This is only valid if this data size > 4 and
856      * contains an offset to the location of the actual value.
857      */
858     protected int getOffset() {
859         return mOffset;
860     }
861
862     /**
863      * Sets the offset of this tag.
864      */
865     protected void setOffset(int offset) {
866         mOffset = offset;
867     }
868
869     protected void setHasDefinedCount(boolean d) {
870         mHasDefinedDefaultComponentCount = d;
871     }
872
873     private boolean checkBadComponentCount(int count) {
874         if (mHasDefinedDefaultComponentCount && (mComponentCountActual != count)) {
875             return true;
876         }
877         return false;
878     }
879
880     private static String convertTypeToString(short type) {
881         switch (type) {
882             case TYPE_UNSIGNED_BYTE:
883                 return "UNSIGNED_BYTE";
884             case TYPE_ASCII:
885                 return "ASCII";
886             case TYPE_UNSIGNED_SHORT:
887                 return "UNSIGNED_SHORT";
888             case TYPE_UNSIGNED_LONG:
889                 return "UNSIGNED_LONG";
890             case TYPE_UNSIGNED_RATIONAL:
891                 return "UNSIGNED_RATIONAL";
892             case TYPE_UNDEFINED:
893                 return "UNDEFINED";
894             case TYPE_LONG:
895                 return "LONG";
896             case TYPE_RATIONAL:
897                 return "RATIONAL";
898             default:
899                 return "";
900         }
901     }
902
903     private boolean checkOverflowForUnsignedShort(int[] value) {
904         for (int v : value) {
905             if (v > UNSIGNED_SHORT_MAX || v < 0) {
906                 return true;
907             }
908         }
909         return false;
910     }
911
912     private boolean checkOverflowForUnsignedLong(long[] value) {
913         for (long v : value) {
914             if (v < 0 || v > UNSIGNED_LONG_MAX) {
915                 return true;
916             }
917         }
918         return false;
919     }
920
921     private boolean checkOverflowForUnsignedLong(int[] value) {
922         for (int v : value) {
923             if (v < 0) {
924                 return true;
925             }
926         }
927         return false;
928     }
929
930     private boolean checkOverflowForUnsignedRational(Rational[] value) {
931         for (Rational v : value) {
932             if (v.getNumerator() < 0 || v.getDenominator() < 0
933                     || v.getNumerator() > UNSIGNED_LONG_MAX
934                     || v.getDenominator() > UNSIGNED_LONG_MAX) {
935                 return true;
936             }
937         }
938         return false;
939     }
940
941     private boolean checkOverflowForRational(Rational[] value) {
942         for (Rational v : value) {
943             if (v.getNumerator() < LONG_MIN || v.getDenominator() < LONG_MIN
944                     || v.getNumerator() > LONG_MAX
945                     || v.getDenominator() > LONG_MAX) {
946                 return true;
947             }
948         }
949         return false;
950     }
951
952     @Override
953     public boolean equals(Object obj) {
954         if (obj == null) {
955             return false;
956         }
957         if (obj instanceof ExifTag) {
958             ExifTag tag = (ExifTag) obj;
959             if (tag.mTagId != this.mTagId
960                     || tag.mComponentCountActual != this.mComponentCountActual
961                     || tag.mDataType != this.mDataType) {
962                 return false;
963             }
964             if (mValue != null) {
965                 if (tag.mValue == null) {
966                     return false;
967                 } else if (mValue instanceof long[]) {
968                     if (!(tag.mValue instanceof long[])) {
969                         return false;
970                     }
971                     return Arrays.equals((long[]) mValue, (long[]) tag.mValue);
972                 } else if (mValue instanceof Rational[]) {
973                     if (!(tag.mValue instanceof Rational[])) {
974                         return false;
975                     }
976                     return Arrays.equals((Rational[]) mValue, (Rational[]) tag.mValue);
977                 } else if (mValue instanceof byte[]) {
978                     if (!(tag.mValue instanceof byte[])) {
979                         return false;
980                     }
981                     return Arrays.equals((byte[]) mValue, (byte[]) tag.mValue);
982                 } else {
983                     return mValue.equals(tag.mValue);
984                 }
985             } else {
986                 return tag.mValue == null;
987             }
988         }
989         return false;
990     }
991
992     @Override
993     public String toString() {
994         return String.format("tag id: %04X\n", mTagId) + "ifd id: " + mIfd + "\ntype: "
995                 + convertTypeToString(mDataType) + "\ncount: " + mComponentCountActual
996                 + "\noffset: " + mOffset + "\nvalue: " + forceGetValueAsString() + "\n";
997     }
998
999 }