1 /*
2  * Copyright 2014 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 #ifndef IMG_UTILS_TIFF_ENTRY
18 #define IMG_UTILS_TIFF_ENTRY
19 
20 #include <img_utils/TiffWritable.h>
21 #include <img_utils/TiffHelpers.h>
22 #include <img_utils/EndianUtils.h>
23 
24 #include <cutils/compiler.h>
25 #include <utils/String8.h>
26 #include <utils/Errors.h>
27 #include <stdint.h>
28 
29 namespace android {
30 namespace img_utils {
31 
32 #define COMPARE_DEF(op) \
33 inline bool operator op (const TiffEntry& entry) const;
34 
35 /**
36  * This class holds a single TIFF IFD entry.
37  *
38  * Subclasses are expected to support assignment and copying operations.
39  */
40 class ANDROID_API TiffEntry : public TiffWritable {
41     public:
42         virtual ~TiffEntry();
43 
44         /**
45          * Write the 12-byte IFD entry to the output. The given offset will be
46          * set as the tag value if the size of the tag value exceeds the max
47          * size for the TIFF Value field (4 bytes), and should be word aligned.
48          *
49          * Returns OK on success, or a negative error code on failure.
50          */
51         virtual status_t writeTagInfo(uint32_t offset, /*out*/EndianOutput* out) const = 0;
52 
53         /**
54          * Get the count set for this entry. This corresponds to the TIFF Count
55          * field.
56          */
57         virtual uint32_t getCount() const = 0;
58 
59         /**
60          * Get the tag id set for this entry. This corresponds to the TIFF Tag
61          * field.
62          */
63         virtual uint16_t getTag() const = 0;
64 
65         /**
66          * Get the type set for this entry.  This corresponds to the TIFF Type
67          * field.
68          */
69         virtual TagType getType() const = 0;
70 
71         /**
72          * Get the defined endianness for this entry.  If this is defined,
73          * the tag value will be written with the given byte order.
74          */
75         virtual Endianness getEndianness() const = 0;
76 
77         /**
78          * Get the value for this entry.  This corresponds to the TIFF Value
79          * field.
80          *
81          * Returns NULL if the value is NULL, or if the type used does not
82          * match the type of this tag.
83          */
84         template<typename T>
85         const T* getData() const;
86 
87         virtual String8 toString() const;
88 
89         /**
90          * Force the type used here to be a valid TIFF type.
91          *
92          * Returns NULL if the given value is NULL, or if the type given does
93          * not match the type of the value given.
94          */
95         template<typename T>
96         static const T* forceValidType(TagType type, const T* value);
97 
98         virtual const void* getDataHelper() const = 0;
99 
100         COMPARE_DEF(>)
101         COMPARE_DEF(<)
102 
103     protected:
104         enum {
105             MAX_PRINT_STRING_LENGTH = 256
106         };
107 };
108 
109 #define COMPARE(op) \
110 bool TiffEntry::operator op (const TiffEntry& entry) const { \
111     return getComparableValue() op entry.getComparableValue(); \
112 }
113 
114 COMPARE(>)
115 COMPARE(<)
116 
117 
118 template<typename T>
getData()119 const T* TiffEntry::getData() const {
120     const T* value = reinterpret_cast<const T*>(getDataHelper());
121     return forceValidType<T>(getType(), value);
122 }
123 
124 #undef COMPARE
125 #undef COMPARE_DEF
126 
127 } /*namespace img_utils*/
128 } /*namespace android*/
129 
130 #endif /*IMG_UTILS_TIFF_ENTRY*/
131