chalk

kis_exif_value.h
1/*
2 * This file is part of Chalk
3 *
4 * Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#ifndef KIS_EXIF_VALUE_H
21#define KIS_EXIF_VALUE_H
22
23#include <tqdom.h>
24
25#include <tqcstring.h>
26#include <tqstring.h>
27
28typedef TQMemArray<TQ_UINT8> UByteArray;
29
30struct KisExifRational {
31 TQ_UINT32 numerator;
32 TQ_UINT32 denominator;
33};
34
35struct KisExifSRational {
36 TQ_INT32 numerator;
37 TQ_INT32 denominator;
38};
39
40class ExifValue {
41 typedef union {
42 TQ_UINT8 m_byte;
43 TQ_UINT16 m_short;
44 TQ_UINT32 m_long;
45 KisExifRational m_rational;
46 TQ_INT8 m_sbyte;
47 TQ_INT16 m_sshort;
48 TQ_INT32 m_slong;
49 KisExifSRational m_srational;
50 float m_float;
51 double m_double;
52 } ExifNumber;
53 public:
54 enum ExifType {
55 EXIF_TYPE_BYTE = 1,
56 EXIF_TYPE_ASCII = 2,
57 EXIF_TYPE_SHORT = 3,
58 EXIF_TYPE_LONG = 4,
59 EXIF_TYPE_RATIONAL = 5,
60 EXIF_TYPE_SBYTE = 6,
61 EXIF_TYPE_UNDEFINED = 7,
62 EXIF_TYPE_SSHORT = 8,
63 EXIF_TYPE_SLONG = 9,
64 EXIF_TYPE_SRATIONAL = 10,
65 EXIF_TYPE_FLOAT = 11,
66 EXIF_TYPE_DOUBLE = 12,
67 EXIF_TYPE_UNKNOW = 13
68 };
69 enum ByteOrder {
70 BYTE_ORDER_MOTOROLA,
71 BYTE_ORDER_INTEL
72 };
73 ExifValue() : m_ifd(-1), m_type(EXIF_TYPE_UNKNOW), m_components(0), m_value(0) { }
74 ExifValue(ExifType type, unsigned char *data, unsigned int size, int ifd, uint components, ExifValue::ByteOrder order);
75
76 virtual bool load(const TQDomElement& elmt);
77 virtual TQDomElement save(TQDomDocument& doc);
78
82 inline ExifType type() { return m_type; }
83 inline const UByteArray asUndefined() {
84 if(m_type == EXIF_TYPE_UNDEFINED)
85 return *(UByteArray*) m_value;
86 return UByteArray();
87 }
88 inline void setAsUndefined(const unsigned char *data, unsigned int size)
89 {
90 if(m_type == EXIF_TYPE_UNDEFINED)
91 {
92 ((UByteArray*)m_value)->duplicate(data, size);
93 m_components = size;
94 }
95 }
96 inline const TQString asAscii() {
97 if(m_type == EXIF_TYPE_ASCII)
98 return TQString(*(TQString*) m_value);
99 return TQString();
100 }
101 inline void setAsAscii(char* data)
102 {
103 if(m_type == EXIF_TYPE_ASCII)
104 {
105 TQString str = TQString((char*) data);
106 *(TQString*)m_value = str;
107 m_components = str.length();
108 }
109 }
110 inline void setAsAscii(TQString str)
111 {
112 *(TQString*)m_value = str;
113 m_components = str.length();
114 }
115 void convertToData(unsigned char ** data, unsigned int* size, ExifValue::ByteOrder order);
119 inline int ifd() { return m_ifd; }
123 inline uint components() { return m_components; }
124
128 TQString toString();
129
130 inline TQ_UINT8 asByte(uint i)
131 {
132 if(m_type == EXIF_TYPE_BYTE)
133 return asExifNumber(i).m_byte;
134 return 0;
135 }
136 inline void setValue(uint i, TQ_UINT8 v)
137 {
138 ((ExifNumber*)m_value)[i].m_byte = v;
139 }
140 inline TQ_UINT8 asShort(uint i)
141 {
142 if(m_type == EXIF_TYPE_SHORT)
143 return asExifNumber(i).m_short;
144 return 0;
145 }
146 inline void setValue(uint i, TQ_UINT16 v)
147 {
148 ((ExifNumber*)m_value)[i].m_short = v;
149 }
150 inline TQ_UINT8 asLong(uint i)
151 {
152 if(m_type == EXIF_TYPE_LONG)
153 return asExifNumber(i).m_long;
154 return 0;
155 }
156 inline void setValue(uint i, TQ_UINT32 v)
157 {
158 ((ExifNumber*)m_value)[i].m_long = v;
159 }
160 inline KisExifRational asRational(uint i)
161 {
162 if(m_type == EXIF_TYPE_RATIONAL)
163 return asExifNumber(i).m_rational;
164 return KisExifRational();
165 }
166 inline void setValue(uint i, TQ_UINT32 n, TQ_UINT32 d)
167 {
168 ((ExifNumber*)m_value)[i].m_rational.numerator = n;
169 ((ExifNumber*)m_value)[i].m_rational.denominator = d;
170 }
171 inline void setValue(uint i, KisExifRational r)
172 {
173 ((ExifNumber*)m_value)[i].m_rational = r;
174 }
175 inline TQ_INT8 asSByte(uint i)
176 {
177 if(m_type == EXIF_TYPE_SBYTE)
178 return asExifNumber(i).m_sbyte;
179 return 0;
180 }
181 inline void setValue(uint i, TQ_INT8 v)
182 {
183 ((ExifNumber*)m_value)[i].m_sbyte = v;
184 }
185 inline TQ_INT16 asSShort(uint i)
186 {
187 if(m_type == EXIF_TYPE_SSHORT)
188 return asExifNumber(i).m_sshort;
189 return 0;
190 }
191 inline void setValue(uint i, TQ_INT16 v)
192 {
193 ((ExifNumber*)m_value)[i].m_sshort = v;
194 }
195 inline TQ_INT32 asSLong(uint i)
196 {
197 if(m_type == EXIF_TYPE_SLONG)
198 return asExifNumber(i).m_slong;
199 return 0;
200 }
201 inline void setValue(uint i, TQ_INT32 v)
202 {
203 ((ExifNumber*)m_value)[i].m_slong = v;
204 }
205 inline KisExifSRational asSRational(uint i)
206 {
207 if(m_type == EXIF_TYPE_SRATIONAL)
208 return asExifNumber(i).m_srational;
209 return KisExifSRational();
210 }
211 inline void setValue(uint i, KisExifSRational r)
212 {
213 ((ExifNumber*)m_value)[i].m_srational = r;
214 }
215 inline void setValue(uint i, TQ_INT32 n, TQ_INT32 d)
216 {
217 ((ExifNumber*)m_value)[i].m_srational.numerator = n;
218 ((ExifNumber*)m_value)[i].m_srational.denominator = d;
219 }
220 inline float asFloat(uint i)
221 {
222 if(m_type == EXIF_TYPE_FLOAT)
223 return asExifNumber(i).m_float;
224 return 0.;
225 }
226 inline void setValue(uint i, float v)
227 {
228 ((ExifNumber*)m_value)[i].m_float = v;
229 }
230 inline double asDouble(uint i)
231 {
232 if(m_type == EXIF_TYPE_DOUBLE)
233 return asExifNumber(i).m_double;
234 return 0.;
235 }
236 inline void setValue(uint i, double v)
237 {
238 ((ExifNumber*)m_value)[i].m_double = v;
239 }
240 private:
244 TQString toString(uint i);
245 void setValue(const unsigned char *data, unsigned int size, ExifValue::ByteOrder order);
249 inline const ExifNumber asExifNumber(uint index)
250 {
251 Q_ASSERT(index < m_components);
252 return ((ExifNumber*)m_value)[index];
253 }
254 inline void setAsExifNumber(uint index, ExifNumber n)
255 {
256 Q_ASSERT(index < m_components);
257 ((ExifNumber*)m_value)[index] = n;
258 }
262 void allocData();
263 private:
264 int m_ifd;
265 ExifType m_type;
266 uint m_components;
267 void *m_value;
268};
269
270#endif