• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/processing/TIFF.h

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     // Currently only handles first IFD (roughly, first page) of a TIFF file
00016     class TIFFFile {
00017     public:
00018 
00019         // A class representing a TIFF directory entry
00020         // Only reads in its data when asked for, which may require file IO
00021         class IfdEntry;
00022         // An object representing a TIFF directory, with accessors for
00023         // interpreting/constructing them
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         // Add a new top-level IFD to the file, and return a pointer to it
00036         Ifd *addIfd();
00037         // Remove all Ifds from the file
00038         void eraseIfds();
00039 
00040         // Access the whole set of Ifds in this file
00041         const std::vector<Ifd *> &ifds() const;
00042         // Get a pointer to a specific Ifd
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         // Read an array of bytes from the file.
00062         bool readByteArray(uint32_t offset, uint32_t count, uint8_t *data);
00063 
00064         // Read an array of shorts (probably image data) from the file into dest
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         // Construct an IfdEntry from a raw TIFF IFD structure
00083         // Used when loading a TIFF file
00084         IfdEntry(const TiffIfdEntry &entry, TIFFFile *parent);
00085         // Construct an IfdEntry from a tag/value pair
00086         // Used when creating/modifying a TIFF file
00087         IfdEntry(uint16_t tag, const TagValue &val, TIFFFile *parent);
00088         // Construct an IfdEntry with just a tag
00089         // Used for searching for a matching IfdEntry in an Ifd
00090         IfdEntry(uint16_t tag, TIFFFile *parent=NULL);
00091 
00092         // Check for entry validity. May not be valid due to
00093         // type mismatches
00094         bool valid() const;
00095 
00096         // Retrieve the TIFF tag number of this entry
00097         uint16_t tag() const;
00098         // Retrieve the name of this entry
00099         const char *name() const;
00100         // Retrieve the current value of this entry
00101         // May cause file IO if loading a TIFF file and this
00102         // entry has not been read before
00103         const TagValue& value() const;
00104 
00105         // Change the value of this entry
00106         bool setValue(const TagValue &);
00107         // Writes excess data to file, and updates local offset pointer
00108         bool writeDataBlock(FILE *fw);
00109         // Writes entry to file. Assumes writeDataBlock has already been done to update entry offset field.
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; // Tracks caching state
00126 
00127         mutable TagValue val; // Cached value
00128     };
00129 
00130     class TIFFFile::Ifd {
00131     public:
00132         Ifd(TIFFFile *parent);
00133         ~Ifd();
00134 
00135         // Return a pointer to the raw IFD entry structure matching tag in the given IFD, if one
00136         // exists, NULL otherwise
00137         const IfdEntry *find(uint16_t tag) const;
00138         IfdEntry *find(uint16_t tag);
00139 
00140         // Adds an entry created from a raw TIFF entry structure
00141         // Used in reading a TIFF.
00142         // Does not replace an existing entry if there is one already
00143         // with the same tag.
00144         bool add(const TiffIfdEntry &);
00145         // Adds an entry with a given tag and value.
00146         // In case of type mismatch, returns false.
00147         // Replaces the existing tag if there is one.
00148         // Used when writing a TIFF.
00149         bool add(uint16_t tag, const TagValue &val);
00150         // Adds an entry with a given tag name and value. If name
00151         // is unknown, returns false.  Used when writing a TIFF.
00152         // Replaces an existing tag if there is one.
00153         bool add(const std::string &tagName, const TagValue &val);
00154 
00155         // Constructs a new subIfd for this Ifd, and returns a pointer to it
00156         Ifd* addSubIfd();
00157         // Erase all subIfds
00158         void eraseSubIfds();
00159 
00160         // Get a reference to a vector of subIfds
00161         const std::vector<Ifd *> &subIfds();
00162         // Get a pointer to a specific subIfd
00163         Ifd* subIfds(int index);
00164 
00165         // Constructs the image referred to by this Ifd. Will require IO to the file if the image
00166         // hasn't been read already.
00167         Image getImage();
00168         // Sets the image to be saved in this Ifd.
00169         bool setImage(Image newImg);
00170 
00171         // Write all entries, subIFds, and image data to file
00172         // Retuns success/failure, and the starting location of the Ifd in
00173         // the file in offset.
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         // Subfunction to write image data out, and to update the IFD
00190         // entry offsets for it
00191         bool writeImage(FILE *fw);
00192     };
00193 
00194 }
00195 
00196 #endif

Generated on Thu Jul 15 2010 17:51:28 for FCam by  doxygen 1.7.1