00001 #ifndef FCAM_TIFF_H
00002 #define FCAM_TIFF_H
00003
00004 #include <string>
00005 #include <map>
00006 #include <vector>
00007
00008 #include <FCam/Image.h>
00009 #include <FCam/TagValue.h>
00010 #include <FCam/Event.h>
00011
00012 #include "TIFFTags.h"
00013
00014 namespace FCam {
00015
00016 class TIFFFile {
00017 public:
00018
00019
00020
00021 class IfdEntry;
00022
00023
00024 class Ifd;
00025
00026 TIFFFile(const std::string &file);
00027 TIFFFile();
00028 ~TIFFFile();
00029
00030 bool readFrom(const std::string &file);
00031 bool writeTo(const std::string &file);
00032
00033 bool valid;
00034
00035
00036 Ifd *addIfd();
00037
00038 void eraseIfds();
00039
00040
00041 const std::vector<Ifd *> &ifds() const;
00042
00043 Ifd* ifds(int index);
00044
00045 Event lastEvent;
00046 private:
00047 FILE *fp;
00048 std::string filename;
00049
00050 bool littleEndian;
00051 uint32_t offsetToIfd0;
00052
00053 std::vector<Ifd *> _ifds;
00054
00055 uint16_t convShort(void const *src);
00056 uint32_t convLong(void const *src);
00057 float convFloat(void const *src);
00058 double convDouble(void const *src);
00059 TIFFRational convRational(void const *src);
00060
00061
00062 bool readByteArray(uint32_t offset, uint32_t count, uint8_t *data);
00063
00064
00065 bool readShortArray(uint32_t offset, uint32_t count, uint16_t *dest);
00066
00067 bool readHeader();
00068 bool readIfd(uint32_t offsetToIFD, Ifd *ifd, uint32_t *offsetToNextIFD=NULL);
00069 bool readSubIfds(Ifd *ifd);
00070
00071 void setError(std::string module, std::string description) {
00072 lastEvent.creator = NULL;
00073 lastEvent.type = Event::Error;
00074 lastEvent.data = Event::FileLoadError;
00075 lastEvent.time = Time::now();
00076 lastEvent.description = "TIFFFile::"+module+":"+filename+": "+description;
00077 }
00078 };
00079
00080 class TIFFFile::IfdEntry {
00081 public:
00082
00083
00084 IfdEntry(const TiffIfdEntry &entry, TIFFFile *parent);
00085
00086
00087 IfdEntry(uint16_t tag, const TagValue &val, TIFFFile *parent);
00088
00089
00090 IfdEntry(uint16_t tag, TIFFFile *parent=NULL);
00091
00092
00093
00094 bool valid() const;
00095
00096
00097 uint16_t tag() const;
00098
00099 const char *name() const;
00100
00101
00102
00103 const TagValue& value() const;
00104
00105
00106 bool setValue(const TagValue &);
00107
00108 bool writeDataBlock(FILE *fw);
00109
00110 bool write(FILE *fw);
00111
00112 bool operator<(const IfdEntry &other) const;
00113 private:
00114 TiffIfdEntry entry;
00115 const TiffEntryInfo *info;
00116 TIFFFile *parent;
00117
00118 TagValue parse() const;
00119
00120 mutable enum {
00121 INVALID,
00122 UNREAD,
00123 READ,
00124 WRITTEN
00125 } state;
00126
00127 mutable TagValue val;
00128 };
00129
00130 class TIFFFile::Ifd {
00131 public:
00132 Ifd(TIFFFile *parent);
00133 ~Ifd();
00134
00135
00136
00137 const IfdEntry *find(uint16_t tag) const;
00138 IfdEntry *find(uint16_t tag);
00139
00140
00141
00142
00143
00144 bool add(const TiffIfdEntry &);
00145
00146
00147
00148
00149 bool add(uint16_t tag, const TagValue &val);
00150
00151
00152
00153 bool add(const std::string &tagName, const TagValue &val);
00154
00155
00156 Ifd* addSubIfd();
00157
00158 void eraseSubIfds();
00159
00160
00161 const std::vector<Ifd *> &subIfds();
00162
00163 Ifd* subIfds(int index);
00164
00165
00166
00167 Image getImage();
00168
00169 bool setImage(Image newImg);
00170
00171
00172
00173
00174 bool write(FILE *fw, uint32_t prevIfdOffset, uint32_t *offset);
00175 private:
00176 TIFFFile * const parent;
00177
00178 std::vector<Ifd *> _subIfds;
00179
00180 typedef std::map<int, IfdEntry> entryMap;
00181 entryMap entries;
00182 enum {
00183 UNREAD,
00184 NONE,
00185 CACHED
00186 } imgState;
00187 Image imgCache;
00188
00189
00190
00191 bool writeImage(FILE *fw);
00192 };
00193
00194 }
00195
00196 #endif