krita

kis_exif_value.h

00001 /*
00002  * This file is part of Krita
00003  *
00004  *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 #ifndef KIS_EXIF_VALUE_H
00021 #define KIS_EXIF_VALUE_H
00022 
00023 #include <qdom.h> 
00024 
00025 #include <qcstring.h>
00026 #include <qstring.h>
00027 
00028 typedef QMemArray<Q_UINT8> UByteArray;
00029 
00030 struct KisExifRational {
00031     Q_UINT32 numerator;
00032     Q_UINT32 denominator;
00033 };
00034 
00035 struct KisExifSRational {
00036     Q_INT32 numerator;
00037     Q_INT32 denominator;
00038 };
00039 
00040 class ExifValue {
00041     typedef union {
00042         Q_UINT8 m_byte;
00043         Q_UINT16 m_short;
00044         Q_UINT32 m_long;
00045         KisExifRational m_rational;
00046         Q_INT8 m_sbyte;
00047         Q_INT16 m_sshort;
00048         Q_INT32 m_slong;
00049         KisExifSRational m_srational;
00050         float m_float;
00051         double m_double;
00052     } ExifNumber;
00053     public:
00054         enum ExifType {
00055             EXIF_TYPE_BYTE       =  1,
00056             EXIF_TYPE_ASCII      =  2,
00057             EXIF_TYPE_SHORT      =  3,
00058             EXIF_TYPE_LONG       =  4,
00059             EXIF_TYPE_RATIONAL   =  5,
00060             EXIF_TYPE_SBYTE      =  6,
00061             EXIF_TYPE_UNDEFINED  =  7,
00062             EXIF_TYPE_SSHORT     =  8,
00063             EXIF_TYPE_SLONG      =  9,
00064             EXIF_TYPE_SRATIONAL  = 10,
00065             EXIF_TYPE_FLOAT      = 11,
00066             EXIF_TYPE_DOUBLE     = 12,
00067             EXIF_TYPE_UNKNOW     = 13
00068         };
00069         enum ByteOrder {
00070             BYTE_ORDER_MOTOROLA,
00071             BYTE_ORDER_INTEL
00072         };
00073         ExifValue() : m_ifd(-1), m_type(EXIF_TYPE_UNKNOW), m_components(0), m_value(0) { }
00074         ExifValue(ExifType type, unsigned char *data, unsigned int size, int ifd, uint components, ExifValue::ByteOrder order);
00075         
00076         virtual bool load(const QDomElement& elmt);
00077         virtual QDomElement save(QDomDocument& doc);
00078 
00082         inline ExifType type() { return m_type; }
00083         inline const UByteArray asUndefined() {
00084             if(m_type == EXIF_TYPE_UNDEFINED)
00085                 return *(UByteArray*) m_value;
00086             return UByteArray();
00087         }
00088         inline void setAsUndefined(const unsigned char *data, unsigned int size)
00089         {
00090             if(m_type == EXIF_TYPE_UNDEFINED)
00091             {
00092                 ((UByteArray*)m_value)->duplicate(data, size);
00093                 m_components = size;
00094             }
00095         }
00096         inline const QString asAscii() {
00097             if(m_type == EXIF_TYPE_ASCII)
00098                 return QString(*(QString*) m_value);
00099             return QString();
00100         }
00101         inline void setAsAscii(char* data)
00102         {
00103             if(m_type == EXIF_TYPE_ASCII)
00104             {
00105                 QString str = QString((char*) data);
00106                 *(QString*)m_value = str;
00107                 m_components = str.length();
00108             }
00109         }
00110         inline void setAsAscii(QString str)
00111         {
00112             *(QString*)m_value = str;
00113             m_components = str.length();
00114         }
00115         void convertToData(unsigned char ** data, unsigned int* size, ExifValue::ByteOrder order);
00119         inline int ifd() { return m_ifd; }
00123         inline uint components() { return m_components; }
00124         
00128         QString toString();
00129         
00130         inline Q_UINT8 asByte(uint i)
00131         {
00132             if(m_type == EXIF_TYPE_BYTE)
00133                 return asExifNumber(i).m_byte;
00134             return 0;
00135         }
00136         inline void setValue(uint i, Q_UINT8 v)
00137         {
00138             ((ExifNumber*)m_value)[i].m_byte = v;
00139         }
00140         inline Q_UINT8 asShort(uint i)
00141         {
00142             if(m_type == EXIF_TYPE_SHORT)
00143                 return asExifNumber(i).m_short;
00144             return 0;
00145         }
00146         inline void setValue(uint i, Q_UINT16 v)
00147         {
00148             ((ExifNumber*)m_value)[i].m_short = v;
00149         }
00150         inline Q_UINT8 asLong(uint i)
00151         {
00152             if(m_type == EXIF_TYPE_LONG)
00153                 return asExifNumber(i).m_long;
00154             return 0;
00155         }
00156         inline void setValue(uint i, Q_UINT32 v)
00157         {
00158             ((ExifNumber*)m_value)[i].m_long = v;
00159         }
00160         inline KisExifRational asRational(uint i)
00161         {
00162             if(m_type == EXIF_TYPE_RATIONAL)
00163                 return asExifNumber(i).m_rational;
00164             return KisExifRational();
00165         }
00166         inline void setValue(uint i, Q_UINT32 n, Q_UINT32 d)
00167         {
00168             ((ExifNumber*)m_value)[i].m_rational.numerator = n;
00169             ((ExifNumber*)m_value)[i].m_rational.denominator = d;
00170         }
00171         inline void setValue(uint i, KisExifRational r)
00172         {
00173             ((ExifNumber*)m_value)[i].m_rational = r;
00174         }
00175         inline Q_INT8 asSByte(uint i)
00176         {
00177             if(m_type == EXIF_TYPE_SBYTE)
00178                 return asExifNumber(i).m_sbyte;
00179             return 0;
00180         }
00181         inline void setValue(uint i, Q_INT8 v)
00182         {
00183             ((ExifNumber*)m_value)[i].m_sbyte = v;
00184         }
00185         inline Q_INT16 asSShort(uint i)
00186         {
00187             if(m_type == EXIF_TYPE_SSHORT)
00188                 return asExifNumber(i).m_sshort;
00189             return 0;
00190         }
00191         inline void setValue(uint i, Q_INT16 v)
00192         {
00193             ((ExifNumber*)m_value)[i].m_sshort = v;
00194         }
00195         inline Q_INT32 asSLong(uint i)
00196         {
00197             if(m_type == EXIF_TYPE_SLONG)
00198                 return asExifNumber(i).m_slong;
00199             return 0;
00200         }
00201         inline void setValue(uint i, Q_INT32 v)
00202         {
00203             ((ExifNumber*)m_value)[i].m_slong = v;
00204         }
00205         inline KisExifSRational asSRational(uint i)
00206         {
00207             if(m_type == EXIF_TYPE_SRATIONAL)
00208                 return asExifNumber(i).m_srational;
00209             return KisExifSRational();
00210         }
00211         inline void setValue(uint i, KisExifSRational r)
00212         {
00213             ((ExifNumber*)m_value)[i].m_srational = r;
00214         }
00215         inline void setValue(uint i, Q_INT32 n, Q_INT32 d)
00216         {
00217             ((ExifNumber*)m_value)[i].m_srational.numerator = n;
00218             ((ExifNumber*)m_value)[i].m_srational.denominator = d;
00219         }
00220         inline float asFloat(uint i)
00221         {
00222             if(m_type == EXIF_TYPE_FLOAT)
00223                 return asExifNumber(i).m_float;
00224             return 0.;
00225         }
00226         inline void setValue(uint i, float v)
00227         {
00228             ((ExifNumber*)m_value)[i].m_float = v;
00229         }
00230         inline double asDouble(uint i)
00231         {
00232             if(m_type == EXIF_TYPE_DOUBLE)
00233                 return asExifNumber(i).m_double;
00234             return 0.;
00235         }
00236         inline void setValue(uint i, double v)
00237         {
00238             ((ExifNumber*)m_value)[i].m_double = v;
00239         }
00240     private:
00244         QString toString(uint i);
00245         void setValue(const unsigned char *data, unsigned int size, ExifValue::ByteOrder order);
00249         inline const ExifNumber asExifNumber(uint index)
00250         {
00251             Q_ASSERT(index < m_components);
00252             return ((ExifNumber*)m_value)[index];
00253         }
00254         inline void setAsExifNumber(uint index, ExifNumber n)
00255         {
00256             Q_ASSERT(index < m_components);
00257             ((ExifNumber*)m_value)[index] = n;
00258         }
00262         void allocData();
00263     private:
00264         int m_ifd;
00265         ExifType m_type;
00266         uint m_components;
00267         void *m_value;
00268 };
00269 
00270 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys