1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
/* This file is -*-C++-*-
------------------------------------------------------------------------------
MetaCam - Extract EXIF information from digital camera files, with
support for Vendor specific blocks.
Copyright (C) 2000 Daniel Stephens (daniel@cheeseplant.org)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
------------------------------------------------------------------------------
$Id: metatiff.h,v 1.3 2002/09/02 19:20:01 daniel Exp $
*/
#ifndef METATIFF_H_INCLUDED
#define METATIFF_H_INCLUDED
#include <vector>
#include <string>
#include <iostream>
using namespace std;
/* Classes for access to meta-infomation from tiff files */
class _TIFFDataSource;
class _IFD;
class _IFDEntry;
class TIFFDataSource;
class IFD;
class IFDEntry;
typedef unsigned long tiffUNSIGNED;
typedef signed long tiffSIGNED;
const int tBYTE = 1;
const int tASCII = 2;
const int tSHORT = 3;
const int tLONG = 4;
const int tRATIONAL = 5;
const int tSBYTE = 6;
const int tUNDEFINED = 7;
const int tSSHORT = 8;
const int tSLONG = 9;
const int tSRATIONAL = 10;
const int tFLOAT = 11;
const int tDOUBLE = 12;
// Now have special types which are parsed versions of the above ones
const int tReferencedIFD = -1;
const int tDataIFD = -2;
class tiffRATIONAL
{
private:
tiffUNSIGNED num;
tiffUNSIGNED den;
public:
tiffRATIONAL() : num(0), den(0) {};
tiffRATIONAL(tiffUNSIGNED n, tiffUNSIGNED d) : num(n), den(d) {};
bool operator == (const tiffRATIONAL &r) const {
return ((num == r.num) && (den == r.den));
}
bool operator != (const tiffRATIONAL &r) const {
return ((num != r.num) || (den != r.den));
}
tiffUNSIGNED getNumerator() const { return num; }
tiffUNSIGNED getDenominator() const { return den; }
operator double() const {
double n(num);
double d(den);
return n/d;
}
tiffRATIONAL normalize() const;
};
class tiffSRATIONAL
{
private:
tiffSIGNED num;
tiffSIGNED den;
public:
tiffSRATIONAL() : num(0), den(0) {};
tiffSRATIONAL(tiffSIGNED n, tiffSIGNED d) : num(n), den(d) {};
bool operator == (const tiffSRATIONAL &r) const {
return ((num == r.num) && (den == r.den));
}
bool operator != (const tiffSRATIONAL &r) const {
return ((num != r.num) || (den != r.den));
}
tiffSIGNED getNumerator() const { return num; }
tiffSIGNED getDenominator() const { return den; }
operator double() const {
double n(num);
double d(den);
return n/d;
}
tiffSRATIONAL normalize() const;
};
inline ostream &operator << (ostream &os, const tiffRATIONAL &r)
{
os << r.getNumerator() << "/" << r.getDenominator();
return os;
}
inline ostream &operator << (ostream &os, const tiffSRATIONAL &r)
{
os << r.getNumerator() << "/" << r.getDenominator();
return os;
}
class _ReferenceCounted
{
private:
bool do_not_delete; // True if this cannot be re-created automatically
int ref_count;
public:
_ReferenceCounted(bool dnd = false) : do_not_delete(dnd), ref_count(0) {};
virtual ~_ReferenceCounted();
// Reference counters
void addRef() {
++ref_count;
}
void delRef() {
--ref_count;
if (ref_count == 0) {
if (!do_not_delete) delete this;
}
}
int getRefCount() const { return ref_count; }
};
const int taFIRST_AVAILABLE = 10000;
class _BaseAnnotation : public _ReferenceCounted
{
private:
const int anno_type;
static int next_avail_type;
public:
_BaseAnnotation(int t, bool dnd=false) : _ReferenceCounted(dnd),
anno_type(t) {};
int getType() { return anno_type; }
virtual void display(ostream &os);
static int getAnnotationType(const char *use) {
return ++next_avail_type;
}
};
// Front-end wrapper class for Annotation's
class Annotation
{
private:
_BaseAnnotation *anno;
public:
Annotation() : anno(0) {};
Annotation(_BaseAnnotation *i) : anno(i) { if (anno) anno->addRef(); }
Annotation(const Annotation &oi) : anno(oi.anno) {if (anno) anno->addRef(); }
_BaseAnnotation *getReal() const { return anno; }
~Annotation() {
if (anno) anno->delRef();
anno = 0;
}
Annotation & operator = (const Annotation &oi) {
if (oi.anno == anno) return *this;
if (oi.anno) oi.anno->addRef();
if (anno) anno->delRef();
anno = oi.anno;
return *this;
}
bool operator!() const { return anno == 0; }
operator bool() const { return anno != 0; }
bool operator==(const Annotation &i) const { return i.anno == anno; }
bool operator!=(const Annotation &i) const { return i.anno != anno; }
int getType() const { return anno ? anno->getType() : 0; }
void display(ostream &os) const {
if (anno) anno->display(os);
}
static int getAnnotationType(const char *use) {
return _BaseAnnotation::getAnnotationType(use);
}
};
inline ostream &operator << (ostream &os, const Annotation &r)
{
r.display(os);
return os;
}
/** Represents a view into a source of data from a TIFF file */
class _TIFFDataSource : public _ReferenceCounted
{
public:
_TIFFDataSource(bool dnd=false) : _ReferenceCounted(dnd) {}
virtual ~_TIFFDataSource();
virtual bool bigEndian() const = 0;
virtual void seek(unsigned long ofs) = 0;
virtual unsigned long tell() const = 0;
virtual size_t getData(unsigned char *buf, size_t bytes) = 0;
virtual unsigned char getUByte();
virtual unsigned short getUShort();
virtual unsigned long getULong();
virtual signed char getSByte();
virtual signed short getSShort();
virtual signed long getSLong();
virtual bool isGood() const = 0;
virtual IFD getIFD();
virtual IFD getIFD(unsigned long ofs);
};
// Front-end wrapper class for data sources
class TIFFDataSource
{
private:
_TIFFDataSource *src;
public:
TIFFDataSource() : src(0) {};
TIFFDataSource(_TIFFDataSource *i) : src(i) { if (src) src->addRef(); }
TIFFDataSource(const TIFFDataSource &oi) : src(oi.src) {if (src) src->addRef(); }
~TIFFDataSource() {
if (src) src->delRef();
src = 0;
}
TIFFDataSource & operator = (const TIFFDataSource &oi) {
if (oi.src == src) return *this;
if (oi.src) oi.src->addRef();
if (src) src->delRef();
src = oi.src;
return *this;
}
bool operator!() const { return src == 0; }
operator bool() const { return src != 0; }
bool operator==(const TIFFDataSource &i) const { return i.src == src; }
bool operator!=(const TIFFDataSource &i) const { return i.src != src; }
bool bigEndian() const { return src->bigEndian(); };
void seek(unsigned long ofs) const { src->seek(ofs); };
unsigned long tell() const { return src->tell(); };
size_t getData(unsigned char *buf, size_t bytes) const {
return src->getData(buf, bytes);
};
unsigned char getUByte() const { return src->getUByte(); }
unsigned short getUShort() const { return src->getUShort(); }
unsigned long getULong() const { return src->getULong(); }
signed char getSByte() const { return src->getSByte(); }
signed short getSShort() const { return src->getSShort(); }
signed long getSLong() const { return src->getSLong(); }
bool isGood() const { return src ? src->isGood() : false; }
IFD getIFD() const;
IFD getIFD(unsigned long ofs) const;
};
class _IFD : public _ReferenceCounted
{
public:
_IFD(bool dnd=false) : _ReferenceCounted(dnd) {}
virtual ~_IFD();
virtual bool isDynamic() const = 0; // Return true if this can be changed
virtual IFDEntry operator[](int n) const = 0;
virtual IFDEntry findEntry(unsigned long tag, _IFDEntry *after=0) const = 0;
virtual unsigned short entries() const = 0;
virtual IFD nextIFD() const = 0;
};
class _IFDEntry: public _ReferenceCounted
{
friend class _IFD;
private:
unsigned long tag;
unsigned short raw_type;
int actual_type;
public:
_IFDEntry(unsigned long t,
unsigned short raw,
bool dnd=false) : _ReferenceCounted(dnd),
tag(t), raw_type(raw), actual_type(raw) {}
_IFDEntry(unsigned long t,
unsigned short raw,
int act, bool dnd=false) : _ReferenceCounted(dnd),
tag(t), raw_type(raw), actual_type(act) {}
virtual ~_IFDEntry();
static int typeLength(unsigned short);
int length() const {return typeLength(raw_type) * values();}
unsigned long getTag() const {return tag;}
unsigned short getRawType() const {return raw_type;}
int getType() const {return actual_type;}
virtual unsigned long values() const = 0;
virtual void outputValue(ostream &os) const = 0;
virtual vector<tiffUNSIGNED> getUVALUE() const = 0;
virtual vector<tiffSIGNED> getSVALUE() const = 0;
virtual vector<tiffRATIONAL> getRATIONAL() const = 0;
virtual vector<tiffSRATIONAL> getSRATIONAL() const = 0;
virtual vector<string> getSTRING() const = 0;
virtual vector<unsigned char> getOPAQUE() const = 0;
virtual TIFFDataSource getSource() const = 0;
virtual unsigned long getSourceOffset() const = 0;
};
// Front-end wrapper class for IFDEntries
class IFDEntry
{
friend class IFD;
private:
_IFDEntry *entry;
public:
IFDEntry() : entry(0) {};
IFDEntry(_IFDEntry *i) : entry(i) { if (entry) entry->addRef(); }
IFDEntry(const IFDEntry &oi) : entry(oi.entry) {if (entry) entry->addRef(); }
~IFDEntry() {
if (entry) entry->delRef();
entry = 0;
}
IFDEntry & operator = (const IFDEntry &oi) {
if (oi.entry == entry) return *this;
if (oi.entry) oi.entry->addRef();
if (entry) entry->delRef();
entry = oi.entry;
return *this;
}
_IFDEntry *getRealEntry() const { return entry; }
bool operator!() const { return entry == 0; }
operator bool() const { return entry != 0; }
bool operator==(const IFDEntry &i) const { return i.entry == entry; }
bool operator!=(const IFDEntry &i) const { return i.entry != entry; }
int length() const { return entry ? entry->length() : 0; }
unsigned long getTag() const { return entry ? entry->getTag() : 0; }
unsigned short getRawType() const { return entry ? entry->getRawType() : 0 ;}
int getType() const { return entry ? entry->getType() : 0 ; }
unsigned long values() const { return entry ? entry->values() : 0;}
void outputValue(ostream &os) const { entry->outputValue(os);}
vector<tiffUNSIGNED> getUVALUE() const {
return entry->getUVALUE();
}
vector<tiffSIGNED> getSVALUE() const {
return entry->getSVALUE();
}
vector<tiffRATIONAL> getRATIONAL() const {
return entry->getRATIONAL();
}
vector<tiffSRATIONAL> getSRATIONAL() const {
return entry->getSRATIONAL();
}
vector<string> getSTRING() const {
return entry->getSTRING();
}
vector<unsigned char> getOPAQUE() const {
return entry->getOPAQUE();
}
TIFFDataSource getSource() const { return entry->getSource(); }
unsigned long getSourceOffset() const { return entry->getSourceOffset(); }
};
// Front-end wrapper class for IFD's
class IFD
{
private:
_IFD *ifd;
public:
IFD() : ifd(0) {};
IFD(_IFD *i) : ifd(i) { if (ifd) ifd->addRef(); }
IFD(const IFD &oi) : ifd(oi.ifd) {if (ifd) ifd->addRef(); }
_IFD *getRealIFD() const { return ifd; }
~IFD() {
if (ifd) ifd->delRef();
ifd = 0;
}
IFD & operator = (const IFD &oi) {
if (oi.ifd == ifd) return *this;
if (oi.ifd) oi.ifd->addRef();
if (ifd) ifd->delRef();
ifd = oi.ifd;
return *this;
}
bool operator!() const { return ifd == 0; }
operator bool() const { return ifd != 0; }
bool operator==(const IFD &i) const { return i.ifd == ifd; }
bool operator!=(const IFD &i) const { return i.ifd != ifd; }
unsigned short entries() const { return ifd ? ifd->entries() : 0 ; }
IFD nextIFD() const { return ifd ? IFD(ifd->nextIFD()) : IFD(); }
bool isDynamic() { return ifd ? ifd->isDynamic() : false; }
IFDEntry operator[](int n) const { return IFDEntry((*ifd)[n]); }
IFDEntry findEntry(unsigned long tag) const {
return IFDEntry(ifd->findEntry(tag));
}
IFDEntry findEntry(unsigned long tag, IFDEntry &after) const {
return IFDEntry(ifd->findEntry(tag, after.entry));
}
IFD getEditable();
};
#endif /* METATIFF_H_INCLUDED */
|