• 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 
00016     class TiffFile;
00017     class TiffIfd;
00018     class TiffIfdEntry;
00019 
00020     // A class representing a TIFF directory entry
00021     // Only reads in its data when asked for, which may require file IO
00022     class TiffIfdEntry {
00023     public:
00024         // Construct an IfdEntry from a raw TIFF IFD structure
00025         // Used when loading a TIFF file
00026         TiffIfdEntry(const RawTiffIfdEntry &entry, TiffFile *parent);
00027         // Construct an IfdEntry from a tag/value pair
00028         // Used when creating/modifying a TIFF file
00029         TiffIfdEntry(uint16_t tag, const TagValue &val, TiffFile *parent);
00030         // Construct an IfdEntry with just a tag
00031         // Used for searching for a matching IfdEntry in an Ifd
00032         TiffIfdEntry(uint16_t tag, TiffFile *parent=NULL);
00033 
00034         // Check for entry validity. May not be valid due to
00035         // type mismatches
00036         bool valid() const;
00037 
00038         // Retrieve the TIFF tag number of this entry
00039         uint16_t tag() const;
00040         // Retrieve the name of this entry
00041         const char *name() const;
00042         // Retrieve the current value of this entry
00043         // May cause file IO if loading a TIFF file and this
00044         // entry has not been read before
00045         const TagValue& value() const;
00046 
00047         // Change the value of this entry
00048         bool setValue(const TagValue &);
00049         // Writes excess data to file, and updates local offset pointer
00050         bool writeDataBlock(FILE *fw);
00051         // Writes entry to file. Assumes writeDataBlock has already been done to update entry offset field.
00052         bool write(FILE *fw);
00053 
00054         bool operator<(const TiffIfdEntry &other) const;
00055     private:
00056         RawTiffIfdEntry entry;
00057         const TiffEntryInfo *info;
00058         TiffFile *parent;
00059 
00060         TagValue parse() const;
00061 
00062         mutable enum {
00063             INVALID,
00064             UNREAD,
00065             READ,
00066             WRITTEN
00067         } state; // Tracks caching state
00068 
00069         mutable TagValue val; // Cached value
00070     };
00071 
00072     // An object representing a TIFF directory, with accessors for
00073     // interpreting/constructing them
00074     class TiffIfd {
00075     public:
00076         TiffIfd(TiffFile *parent);
00077         ~TiffIfd();
00078 
00079         // Return a pointer to the raw IFD entry structure matching tag in the given IFD, if one
00080         // exists, NULL otherwise
00081         const TiffIfdEntry *find(uint16_t tag) const;
00082         TiffIfdEntry *find(uint16_t tag);
00083 
00084         // Adds an entry created from a raw TIFF entry structure
00085         // Used in reading a TIFF.
00086         // Does not replace an existing entry if there is one already
00087         // with the same tag.
00088         bool add(const RawTiffIfdEntry &);
00089         // Adds an entry with a given tag and value.
00090         // In case of type mismatch, returns false.
00091         // Replaces the existing tag if there is one.
00092         // Used when writing a TIFF.
00093         bool add(uint16_t tag, const TagValue &val);
00094         // Adds an entry with a given tag name and value. If name
00095         // is unknown, returns false.  Used when writing a TIFF.
00096         // Replaces an existing tag if there is one.
00097         bool add(const std::string &tagName, const TagValue &val);
00098 
00099         // Constructs a new subIfd for this Ifd, and returns a pointer to it
00100         TiffIfd* addSubIfd();
00101         // Erase all subIfds
00102         void eraseSubIfds();
00103 
00104         // Adds an Exif IFD to the Ifd, or returns pointer to existing one
00105         TiffIfd* addExifIfd();
00106         // Removes existing Exif IFD
00107         void eraseExifIfd();
00108 
00109         // Get a reference to a vector of subIfds
00110         const std::vector<TiffIfd *> &subIfds();
00111         // Get a pointer to a specific subIfd
00112         TiffIfd* subIfds(int index) const;
00113 
00114         // Constructs the image referred to by this Ifd. Will require IO to the file if the image
00115         // hasn't been read already. Optionally, use memory mapped IO to manage the image memory.
00116     // This is only allowable for images that have been stored contiguously in the source file.
00117         Image getImage(bool memMap = true);
00118         // Sets the image to be saved in this Ifd.
00119         bool setImage(Image newImg);
00120 
00121         // Write all entries, subIFds, and image data to file
00122         // Retuns success/failure, and the starting location of the Ifd in
00123         // the file in offset.
00124         bool write(FILE *fw, uint32_t prevIfdOffset, uint32_t *offset);
00125     private:
00126         TiffFile * const parent;
00127 
00128         std::vector<TiffIfd *> _subIfds;
00129         TiffIfd *exifIfd;
00130 
00131         typedef std::map<int, TiffIfdEntry> entryMap;
00132         entryMap entries;
00133 
00134         enum {
00135             UNREAD,
00136             NONE,
00137             CACHED
00138         } imgState;
00139         Image imgCache;
00140 
00141         // Subfunction to write image data out, and to update the IFD
00142         // entry offsets for it
00143         bool writeImage(FILE *fw);
00144     };
00145 
00146     // High-level interface to reading and writing TIFF
00147     // files. Implemented functionality limited to those needed for
00148     // DNG file access (uncompressed striped data, only a few color
00149     // spaces)
00150     class TiffFile {
00151     public:
00152 
00153         TiffFile(const std::string &file);
00154         TiffFile();
00155         ~TiffFile();
00156 
00157         bool readFrom(const std::string &file);
00158         bool writeTo(const std::string &file);
00159 
00160         bool valid;
00161         const std::string &filename() const;
00162 
00163         // Add a new top-level IFD to the file, and return a pointer to it
00164         TiffIfd *addIfd();
00165         // Remove all Ifds from the file
00166         void eraseIfds();
00167 
00168         // Access the whole set of Ifds in this file
00169         const std::vector<TiffIfd *> &ifds() const;
00170         // Get a pointer to a specific Ifd
00171         TiffIfd* ifds(int index);
00172 
00173         Event lastEvent;
00174     private:
00175         FILE *fp;
00176         std::string _filename;
00177 
00178         bool littleEndian;
00179         uint32_t offsetToIfd0;
00180 
00181         std::vector<TiffIfd *> _ifds;
00182 
00183         uint16_t convShort(void const *src);
00184         uint32_t convLong(void const *src);
00185         float convFloat(void const *src);
00186         double convDouble(void const *src);
00187         TiffRational convRational(void const *src);
00188 
00189         // Read an array of bytes from the file.
00190         bool readByteArray(uint32_t offset, uint32_t count, uint8_t *data);
00191 
00192         // Read an array of shorts (probably image data) from the file into dest
00193         bool readShortArray(uint32_t offset, uint32_t count, uint16_t *dest);
00194 
00195         bool readHeader();
00196         bool readIfd(uint32_t offsetToIFD, TiffIfd *ifd, uint32_t *offsetToNextIFD=NULL);
00197         bool readSubIfds(TiffIfd *ifd);
00198 
00199         void setError(std::string module, std::string description) {
00200             lastEvent.creator = NULL;
00201             lastEvent.type = Event::Error;
00202             lastEvent.data = Event::FileLoadError;
00203             lastEvent.time = Time::now();
00204             lastEvent.description = "TiffFile::"+module+":"+filename()+": "+description;
00205         }
00206 
00207         friend class TiffIfd;
00208         friend class TiffIfdEntry;
00209     };
00210 
00211 
00212 }
00213 
00214 #endif

Generated on Mon Aug 16 2010 14:25:45 for FCam by  doxygen 1.7.1