File: ifdentry.h

package info (click to toggle)
libopenraw 0.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,964 kB
  • ctags: 1,936
  • sloc: sh: 10,199; cpp: 9,279; ansic: 1,466; makefile: 544; xml: 465; perl: 42
file content (262 lines) | stat: -rw-r--r-- 6,050 bytes parent folder | download | duplicates (4)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * libopenraw - ifdentry.h
 *
 * Copyright (C) 2006-2008 Hubert Figuiere
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */


#ifndef _OPENRAW_INTERNALS_IFDENTRY_H
#define _OPENRAW_INTERNALS_IFDENTRY_H

#include <vector>
#include <boost/shared_ptr.hpp>
#include <libopenraw/types.h>

#include "exception.h"
#include "endianutils.h"
#include "rawcontainer.h"
#include "ifd.h"

namespace OpenRaw {
	namespace Internals {

		class IFDFileContainer;

		class IFDEntry;

		/** Describe and IFDType */
		template <typename T>
		struct IFDTypeTrait
		{
			static const uint16_t type; /**< the EXIF enum for the type */
			static const size_t   size; /**< the storage size unit in IFD*/
			static T EL(const uint8_t* d);
			static T BE(const uint8_t* d);
			static T get(IFDEntry & e, uint32_t idx = 0, bool ignore_type = false)
				throw (BadTypeException, OutOfRangeException, TooBigException);
		};


		template <>
		inline uint8_t IFDTypeTrait<uint8_t>::EL(const uint8_t* b)
		{
			return *b;
		}

		template <>
		inline uint8_t IFDTypeTrait<uint8_t>::BE(const uint8_t* b)
		{
			return *b;
		}


		template <>
		inline uint16_t IFDTypeTrait<uint16_t>::EL(const uint8_t* b)
		{
			return EL16(b);
		}

		template <>
		inline uint16_t IFDTypeTrait<uint16_t>::BE(const uint8_t* b)
		{
			return BE16(b);
		}

		template <>
		inline uint32_t IFDTypeTrait<uint32_t>::EL(const uint8_t* b)
		{
			return EL32(b);
		}

		template <>
		inline uint32_t IFDTypeTrait<uint32_t>::BE(const uint8_t* b)
		{
			return BE32(b);
		}

		template <>
		inline std::string IFDTypeTrait<std::string>::EL(const uint8_t* b)
		{
			return std::string((const char*)b);
		}

		template <>
		inline std::string IFDTypeTrait<std::string>::BE(const uint8_t* b)
		{
			return std::string((const char*)b);
		}
		
#if defined(__APPLE_CC__)
// Apple broken C++ needs this
		template <>
		inline unsigned long IFDTypeTrait<unsigned long>::EL(const uint8_t* b)
		{
			return EL32(b);
		}

		template <>
		inline unsigned long IFDTypeTrait<unsigned long>::BE(const uint8_t* b)
		{
			return BE32(b);
		}
#endif

		class IFDEntry
		{
		public:
			/** Ref (ie shared pointer) */
			typedef boost::shared_ptr<IFDEntry> Ref;

			IFDEntry(uint16_t _id, int16_t _type, int32_t _count, 
					 uint32_t _data,
					 IFDFileContainer &_container);
			virtual ~IFDEntry();

			int16_t type() const
				{
					return m_type;
				}
			
			/** the count of items in the entry */
			uint16_t count() const
				{
					return m_count;
				}

			/** the offset of the data. It can just be the value
			 * if the entry is self contained.
			 */
			off_t offset()
				{
					if (endian() == RawContainer::ENDIAN_LITTLE) {
						return IFDTypeTrait<uint32_t>::EL((uint8_t*)&m_data);
					}
					return IFDTypeTrait<uint32_t>::BE((uint8_t*)&m_data);
				}

			RawContainer::EndianType endian() const;

		public:
			/** load the data for the entry 
			 * if all the data fits in m_data, it is a noop
			 * @param unit_size the size of 1 unit of data
			 * @return true if success.
			 */
			bool loadData(size_t unit_size);


			/** get the array values of type T
			 * @param T the type of the value needed
			 * @param array the storage
			 * @throw whatever is thrown
			 */
			template <typename T>
			void getArray(std::vector<T> & array)
				{
					try {
						array.reserve(m_count);
						for (uint32_t i = 0; i < m_count; i++) {
							array.push_back(IFDTypeTrait<T>::get(*this, i));
						}
					}
					catch(std::exception & e)
					{
						throw e;
					}
				}


		private:
			uint16_t m_id;
			uint16_t m_type;
			uint32_t m_count;
			uint32_t m_data; /**< raw data without endian conversion */
			bool m_loaded;
			uint8_t *m_dataptr;
			IFDFileContainer & m_container;
			template <typename T> friend struct IFDTypeTrait;

			/** private copy constructor to make sure it is not called */
			IFDEntry(const IFDEntry& f);
			/** private = operator to make sure it is never called */
			IFDEntry & operator=(const IFDEntry&);

		};



		/** get the value of type T
		 * @param T the type of the value needed
		 * @param idx the index, by default 0
		 * @param ignore_type if true, don't check type. *DANGEROUS* Default is false.
		 * @return the value
		 * @throw BadTypeException in case of wrong typing.
		 * @throw OutOfRangeException in case of subscript out of range
		 */
		template <typename T> 
		T IFDTypeTrait<T>::get(IFDEntry & e, uint32_t idx, bool ignore_type)
			throw (BadTypeException, OutOfRangeException, TooBigException)
		{
			/* format undefined means that we don't check the type */
			if(!ignore_type && (e.m_type != IFD::EXIF_FORMAT_UNDEFINED)) {
				if (e.m_type != IFDTypeTrait<T>::type) {
					throw BadTypeException();
				}
			}
			if (idx + 1 > e.m_count) {
				throw OutOfRangeException();
			}
			if (!e.m_loaded) {
				e.m_loaded = e.loadData(IFDTypeTrait<T>::size);
				if (!e.m_loaded) {
					throw TooBigException();
				}
			}
			uint8_t *data;
			if (e.m_dataptr == NULL) {
				data = (uint8_t*)&e.m_data;
			}
			else {
				data = e.m_dataptr;
			}
			data += (IFDTypeTrait<T>::size * idx);
			T val;
			if (e.endian() == RawContainer::ENDIAN_LITTLE) {
				val = IFDTypeTrait<T>::EL(data);
			}
			else {
				val = IFDTypeTrait<T>::BE(data);
			}
			return val;
		}

	}
}


/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0))
  indent-tabs-mode:nil
  fill-column:80
  End:
*/
#endif