Path: blob/master/3rdparty/openexr/IlmImf/ImfHeader.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IMF_HEADER_H37#define INCLUDED_IMF_HEADER_H3839//-----------------------------------------------------------------------------40//41// class Header42//43//-----------------------------------------------------------------------------4445#include <ImfLineOrder.h>46#include <ImfCompression.h>47#include <ImfName.h>48#include <ImfTileDescription.h>49#include <ImfInt64.h>50#include "ImathVec.h"51#include "ImathBox.h"52#include "IexBaseExc.h"53#include <map>54#include <iosfwd>55#include <string>5657namespace Imf {585960class Attribute;61class ChannelList;62class IStream;63class OStream;64class PreviewImage;656667class Header68{69public:7071//----------------------------------------------------------------72// Default constructor -- the display window and the data window73// are both set to Box2i (V2i (0, 0), V2i (width-1, height-1).74//----------------------------------------------------------------7576Header (int width = 64,77int height = 64,78float pixelAspectRatio = 1,79const Imath::V2f &screenWindowCenter = Imath::V2f (0, 0),80float screenWindowWidth = 1,81LineOrder lineOrder = INCREASING_Y,82Compression = ZIP_COMPRESSION);838485//--------------------------------------------------------------------86// Constructor -- the data window is specified explicitly; the display87// window is set to Box2i (V2i (0, 0), V2i (width-1, height-1).88//--------------------------------------------------------------------8990Header (int width,91int height,92const Imath::Box2i &dataWindow,93float pixelAspectRatio = 1,94const Imath::V2f &screenWindowCenter = Imath::V2f (0, 0),95float screenWindowWidth = 1,96LineOrder lineOrder = INCREASING_Y,97Compression = ZIP_COMPRESSION);9899100//----------------------------------------------------------101// Constructor -- the display window and the data window are102// both specified explicitly.103//----------------------------------------------------------104105Header (const Imath::Box2i &displayWindow,106const Imath::Box2i &dataWindow,107float pixelAspectRatio = 1,108const Imath::V2f &screenWindowCenter = Imath::V2f (0, 0),109float screenWindowWidth = 1,110LineOrder lineOrder = INCREASING_Y,111Compression = ZIP_COMPRESSION);112113114//-----------------115// Copy constructor116//-----------------117118Header (const Header &other);119120121//-----------122// Destructor123//-----------124125~Header ();126127128//-----------129// Assignment130//-----------131132Header & operator = (const Header &other);133134135//---------------------------------------------------------------136// Add an attribute:137//138// insert(n,attr) If no attribute with name n exists, a new139// attribute with name n, and the same type as140// attr, is added, and the value of attr is141// copied into the new attribute.142//143// If an attribute with name n exists, and its144// type is the same as attr, the value of attr145// is copied into this attribute.146//147// If an attribute with name n exists, and its148// type is different from attr, an Iex::TypeExc149// is thrown.150//151//---------------------------------------------------------------152153void insert (const char name[],154const Attribute &attribute);155156void insert (const std::string &name,157const Attribute &attribute);158159//------------------------------------------------------------------160// Access to existing attributes:161//162// [n] Returns a reference to the attribute163// with name n. If no attribute with164// name n exists, an Iex::ArgExc is thrown.165//166// typedAttribute<T>(n) Returns a reference to the attribute167// with name n and type T. If no attribute168// with name n exists, an Iex::ArgExc is169// thrown. If an attribute with name n170// exists, but its type is not T, an171// Iex::TypeExc is thrown.172//173// findTypedAttribute<T>(n) Returns a pointer to the attribute with174// name n and type T, or 0 if no attribute175// with name n and type T exists.176//177//------------------------------------------------------------------178179Attribute & operator [] (const char name[]);180const Attribute & operator [] (const char name[]) const;181182Attribute & operator [] (const std::string &name);183const Attribute & operator [] (const std::string &name) const;184185template <class T> T& typedAttribute (const char name[]);186template <class T> const T& typedAttribute (const char name[]) const;187188template <class T> T& typedAttribute (const std::string &name);189template <class T> const T& typedAttribute (const std::string &name) const;190191template <class T> T* findTypedAttribute (const char name[]);192template <class T> const T* findTypedAttribute (const char name[]) const;193194template <class T> T* findTypedAttribute (const std::string &name);195template <class T> const T* findTypedAttribute (const std::string &name)196const;197198//---------------------------------------------199// Iterator-style access to existing attributes200//---------------------------------------------201202typedef std::map <Name, Attribute *> AttributeMap;203204class Iterator;205class ConstIterator;206207Iterator begin ();208ConstIterator begin () const;209210Iterator end ();211ConstIterator end () const;212213Iterator find (const char name[]);214ConstIterator find (const char name[]) const;215216Iterator find (const std::string &name);217ConstIterator find (const std::string &name) const;218219220//--------------------------------221// Access to predefined attributes222//--------------------------------223224Imath::Box2i & displayWindow ();225const Imath::Box2i & displayWindow () const;226227Imath::Box2i & dataWindow ();228const Imath::Box2i & dataWindow () const;229230float & pixelAspectRatio ();231const float & pixelAspectRatio () const;232233Imath::V2f & screenWindowCenter ();234const Imath::V2f & screenWindowCenter () const;235236float & screenWindowWidth ();237const float & screenWindowWidth () const;238239ChannelList & channels ();240const ChannelList & channels () const;241242LineOrder & lineOrder ();243const LineOrder & lineOrder () const;244245Compression & compression ();246const Compression & compression () const;247248249//----------------------------------------------------------------------250// Tile Description:251//252// The tile description is a TileDescriptionAttribute whose name253// is "tiles". The "tiles" attribute must be present in any tiled254// image file. When present, it describes various properties of the255// tiles that make up the file.256//257// Convenience functions:258//259// setTileDescription(td)260// calls insert ("tiles", TileDescriptionAttribute (td))261//262// tileDescription()263// returns typedAttribute<TileDescriptionAttribute>("tiles").value()264//265// hasTileDescription()266// return findTypedAttribute<TileDescriptionAttribute>("tiles") != 0267//268//----------------------------------------------------------------------269270void setTileDescription (const TileDescription & td);271272TileDescription & tileDescription ();273const TileDescription & tileDescription () const;274275bool hasTileDescription() const;276277278//----------------------------------------------------------------------279// Preview image:280//281// The preview image is a PreviewImageAttribute whose name is "preview".282// This attribute is special -- while an image file is being written,283// the pixels of the preview image can be changed repeatedly by calling284// OutputFile::updatePreviewImage().285//286// Convenience functions:287//288// setPreviewImage(p)289// calls insert ("preview", PreviewImageAttribute (p))290//291// previewImage()292// returns typedAttribute<PreviewImageAttribute>("preview").value()293//294// hasPreviewImage()295// return findTypedAttribute<PreviewImageAttribute>("preview") != 0296//297//----------------------------------------------------------------------298299void setPreviewImage (const PreviewImage &p);300301PreviewImage & previewImage ();302const PreviewImage & previewImage () const;303304bool hasPreviewImage () const;305306307//-------------------------------------------------------------308// Sanity check -- examines the header, and throws an exception309// if it finds something wrong (empty display window, negative310// pixel aspect ratio, unknown compression sceme etc.)311//312// set isTiled to true if you are checking a tiled/multi-res313// header314//-------------------------------------------------------------315316void sanityCheck (bool isTiled = false) const;317318319//----------------------------------------------------------------320// Maximum image size and maximim tile size:321//322// sanityCheck() will throw an exception if the width or height of323// the data window exceeds the maximum image width or height, or324// if the size of a tile exceeds the maximum tile width or height.325//326// At program startup the maximum image and tile width and height327// are set to zero, meaning that width and height are unlimited.328//329// Limiting image and tile width and height limits how much memory330// will be allocated when a file is opened. This can help protect331// applications from running out of memory while trying to read332// a damaged image file.333//----------------------------------------------------------------334335static void setMaxImageSize (int maxWidth, int maxHeight);336static void setMaxTileSize (int maxWidth, int maxHeight);337338339//------------------------------------------------------------------340// Input and output:341//342// If the header contains a preview image attribute, then writeTo()343// returns the position of that attribute in the output stream; this344// information is used by OutputFile::updatePreviewImage().345// If the header contains no preview image attribute, then writeTo()346// returns 0.347//------------------------------------------------------------------348349350Int64 writeTo (OStream &os,351bool isTiled = false) const;352353void readFrom (IStream &is, int &version);354355private:356357AttributeMap _map;358};359360361//----------362// Iterators363//----------364365class Header::Iterator366{367public:368369Iterator ();370Iterator (const Header::AttributeMap::iterator &i);371372Iterator & operator ++ ();373Iterator operator ++ (int);374375const char * name () const;376Attribute & attribute () const;377378private:379380friend class Header::ConstIterator;381382Header::AttributeMap::iterator _i;383};384385386class Header::ConstIterator387{388public:389390ConstIterator ();391ConstIterator (const Header::AttributeMap::const_iterator &i);392ConstIterator (const Header::Iterator &other);393394ConstIterator & operator ++ ();395ConstIterator operator ++ (int);396397const char * name () const;398const Attribute & attribute () const;399400private:401402friend bool operator == (const ConstIterator &, const ConstIterator &);403friend bool operator != (const ConstIterator &, const ConstIterator &);404405Header::AttributeMap::const_iterator _i;406};407408409//------------------------------------------------------------------------410// Library initialization:411//412// In a multithreaded program, staticInitialize() must be called once413// during startup, before the program accesses any other functions or414// classes in the IlmImf library. Calling staticInitialize() in this415// way avoids races during initialization of the library's global416// variables.417//418// Single-threaded programs are not required to call staticInitialize();419// initialization of the library's global variables happens automatically.420//421//------------------------------------------------------------------------422423void staticInitialize ();424425426//-----------------427// Inline Functions428//-----------------429430431inline432Header::Iterator::Iterator (): _i()433{434// empty435}436437438inline439Header::Iterator::Iterator (const Header::AttributeMap::iterator &i): _i (i)440{441// empty442}443444445inline Header::Iterator &446Header::Iterator::operator ++ ()447{448++_i;449return *this;450}451452453inline Header::Iterator454Header::Iterator::operator ++ (int)455{456Iterator tmp = *this;457++_i;458return tmp;459}460461462inline const char *463Header::Iterator::name () const464{465return *_i->first;466}467468469inline Attribute &470Header::Iterator::attribute () const471{472return *_i->second;473}474475476inline477Header::ConstIterator::ConstIterator (): _i()478{479// empty480}481482inline483Header::ConstIterator::ConstIterator484(const Header::AttributeMap::const_iterator &i): _i (i)485{486// empty487}488489490inline491Header::ConstIterator::ConstIterator (const Header::Iterator &other):492_i (other._i)493{494// empty495}496497inline Header::ConstIterator &498Header::ConstIterator::operator ++ ()499{500++_i;501return *this;502}503504505inline Header::ConstIterator506Header::ConstIterator::operator ++ (int)507{508ConstIterator tmp = *this;509++_i;510return tmp;511}512513514inline const char *515Header::ConstIterator::name () const516{517return *_i->first;518}519520521inline const Attribute &522Header::ConstIterator::attribute () const523{524return *_i->second;525}526527528inline bool529operator == (const Header::ConstIterator &x, const Header::ConstIterator &y)530{531return x._i == y._i;532}533534535inline bool536operator != (const Header::ConstIterator &x, const Header::ConstIterator &y)537{538return !(x == y);539}540541542//---------------------543// Template definitions544//---------------------545546template <class T>547T &548Header::typedAttribute (const char name[])549{550Attribute *attr = &(*this)[name];551T *tattr = dynamic_cast <T*> (attr);552553if (tattr == 0)554throw Iex::TypeExc ("Unexpected attribute type.");555556return *tattr;557}558559560template <class T>561const T &562Header::typedAttribute (const char name[]) const563{564const Attribute *attr = &(*this)[name];565const T *tattr = dynamic_cast <const T*> (attr);566567if (tattr == 0)568throw Iex::TypeExc ("Unexpected attribute type.");569570return *tattr;571}572573574template <class T>575T &576Header::typedAttribute (const std::string &name)577{578return typedAttribute<T> (name.c_str());579}580581582template <class T>583const T &584Header::typedAttribute (const std::string &name) const585{586return typedAttribute<T> (name.c_str());587}588589590template <class T>591T *592Header::findTypedAttribute (const char name[])593{594AttributeMap::iterator i = _map.find (name);595return (i == _map.end())? 0: dynamic_cast <T*> (i->second);596}597598599template <class T>600const T *601Header::findTypedAttribute (const char name[]) const602{603AttributeMap::const_iterator i = _map.find (name);604return (i == _map.end())? 0: dynamic_cast <const T*> (i->second);605}606607608template <class T>609T *610Header::findTypedAttribute (const std::string &name)611{612return findTypedAttribute<T> (name.c_str());613}614615616template <class T>617const T *618Header::findTypedAttribute (const std::string &name) const619{620return findTypedAttribute<T> (name.c_str());621}622623624} // namespace Imf625626#endif627628629