ImfHeader.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. //-----------------------------------------------------------------------------
  35. //
  36. // class Header
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include <ImfHeader.h>
  40. #include <ImfStdIO.h>
  41. #include <ImfVersion.h>
  42. #include <ImfCompressor.h>
  43. #include <ImfMisc.h>
  44. #include <ImfBoxAttribute.h>
  45. #include <ImfChannelListAttribute.h>
  46. #include <ImfChromaticitiesAttribute.h>
  47. #include <ImfCompressionAttribute.h>
  48. #include <ImfDeepImageStateAttribute.h>
  49. #include <ImfDoubleAttribute.h>
  50. #include <ImfDwaCompressor.h>
  51. #include <ImfEnvmapAttribute.h>
  52. #include <ImfFloatAttribute.h>
  53. #include <ImfFloatVectorAttribute.h>
  54. #include <ImfIntAttribute.h>
  55. #include <ImfKeyCodeAttribute.h>
  56. #include <ImfLineOrderAttribute.h>
  57. #include <ImfMatrixAttribute.h>
  58. #include <ImfOpaqueAttribute.h>
  59. #include <ImfPreviewImageAttribute.h>
  60. #include <ImfRationalAttribute.h>
  61. #include <ImfStringAttribute.h>
  62. #include <ImfStringVectorAttribute.h>
  63. #include <ImfTileDescriptionAttribute.h>
  64. #include <ImfTimeCodeAttribute.h>
  65. #include <ImfVecAttribute.h>
  66. #include <ImfPartType.h>
  67. #include "IlmThreadMutex.h"
  68. #include "Iex.h"
  69. #include <sstream>
  70. #include <stdlib.h>
  71. #include <time.h>
  72. #include "ImfNamespace.h"
  73. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  74. using namespace std;
  75. using IMATH_NAMESPACE::Box2i;
  76. using IMATH_NAMESPACE::V2i;
  77. using IMATH_NAMESPACE::V2f;
  78. using ILMTHREAD_NAMESPACE::Mutex;
  79. using ILMTHREAD_NAMESPACE::Lock;
  80. namespace {
  81. int maxImageWidth = 0;
  82. int maxImageHeight = 0;
  83. int maxTileWidth = 0;
  84. int maxTileHeight = 0;
  85. void
  86. initialize (Header &header,
  87. const Box2i &displayWindow,
  88. const Box2i &dataWindow,
  89. float pixelAspectRatio,
  90. const V2f &screenWindowCenter,
  91. float screenWindowWidth,
  92. LineOrder lineOrder,
  93. Compression compression)
  94. {
  95. header.insert ("displayWindow", Box2iAttribute (displayWindow));
  96. header.insert ("dataWindow", Box2iAttribute (dataWindow));
  97. header.insert ("pixelAspectRatio", FloatAttribute (pixelAspectRatio));
  98. header.insert ("screenWindowCenter", V2fAttribute (screenWindowCenter));
  99. header.insert ("screenWindowWidth", FloatAttribute (screenWindowWidth));
  100. header.insert ("lineOrder", LineOrderAttribute (lineOrder));
  101. header.insert ("compression", CompressionAttribute (compression));
  102. header.insert ("channels", ChannelListAttribute ());
  103. }
  104. template <size_t N>
  105. void checkIsNullTerminated (const char (&str)[N], const char *what)
  106. {
  107. for (size_t i = 0; i < N; ++i) {
  108. if (str[i] == '\0')
  109. return;
  110. }
  111. std::stringstream s;
  112. s << "Invalid " << what << ": it is more than " << (N - 1)
  113. << " characters long.";
  114. throw IEX_NAMESPACE::InputExc(s);
  115. }
  116. } // namespace
  117. Header::Header (int width,
  118. int height,
  119. float pixelAspectRatio,
  120. const V2f &screenWindowCenter,
  121. float screenWindowWidth,
  122. LineOrder lineOrder,
  123. Compression compression)
  124. :
  125. _map()
  126. {
  127. staticInitialize();
  128. Box2i displayWindow (V2i (0, 0), V2i (width - 1, height - 1));
  129. initialize (*this,
  130. displayWindow,
  131. displayWindow,
  132. pixelAspectRatio,
  133. screenWindowCenter,
  134. screenWindowWidth,
  135. lineOrder,
  136. compression);
  137. }
  138. Header::Header (int width,
  139. int height,
  140. const Box2i &dataWindow,
  141. float pixelAspectRatio,
  142. const V2f &screenWindowCenter,
  143. float screenWindowWidth,
  144. LineOrder lineOrder,
  145. Compression compression)
  146. :
  147. _map()
  148. {
  149. staticInitialize();
  150. Box2i displayWindow (V2i (0, 0), V2i (width - 1, height - 1));
  151. initialize (*this,
  152. displayWindow,
  153. dataWindow,
  154. pixelAspectRatio,
  155. screenWindowCenter,
  156. screenWindowWidth,
  157. lineOrder,
  158. compression);
  159. }
  160. Header::Header (const Box2i &displayWindow,
  161. const Box2i &dataWindow,
  162. float pixelAspectRatio,
  163. const V2f &screenWindowCenter,
  164. float screenWindowWidth,
  165. LineOrder lineOrder,
  166. Compression compression)
  167. :
  168. _map()
  169. {
  170. staticInitialize();
  171. initialize (*this,
  172. displayWindow,
  173. dataWindow,
  174. pixelAspectRatio,
  175. screenWindowCenter,
  176. screenWindowWidth,
  177. lineOrder,
  178. compression);
  179. }
  180. Header::Header (const Header &other): _map()
  181. {
  182. for (AttributeMap::const_iterator i = other._map.begin();
  183. i != other._map.end();
  184. ++i)
  185. {
  186. insert (*i->first, *i->second);
  187. }
  188. }
  189. Header::~Header ()
  190. {
  191. for (AttributeMap::iterator i = _map.begin();
  192. i != _map.end();
  193. ++i)
  194. {
  195. delete i->second;
  196. }
  197. }
  198. Header &
  199. Header::operator = (const Header &other)
  200. {
  201. if (this != &other)
  202. {
  203. for (AttributeMap::iterator i = _map.begin();
  204. i != _map.end();
  205. ++i)
  206. {
  207. delete i->second;
  208. }
  209. _map.erase (_map.begin(), _map.end());
  210. for (AttributeMap::const_iterator i = other._map.begin();
  211. i != other._map.end();
  212. ++i)
  213. {
  214. insert (*i->first, *i->second);
  215. }
  216. }
  217. return *this;
  218. }
  219. void
  220. Header::erase (const char name[])
  221. {
  222. if (name[0] == 0)
  223. THROW (IEX_NAMESPACE::ArgExc, "Image attribute name cannot be an empty string.");
  224. AttributeMap::iterator i = _map.find (name);
  225. if (i != _map.end())
  226. _map.erase (i);
  227. }
  228. void
  229. Header::erase (const string &name)
  230. {
  231. erase (name.c_str());
  232. }
  233. void
  234. Header::insert (const char name[], const Attribute &attribute)
  235. {
  236. if (name[0] == 0)
  237. THROW (IEX_NAMESPACE::ArgExc, "Image attribute name cannot be an empty string.");
  238. AttributeMap::iterator i = _map.find (name);
  239. if (i == _map.end())
  240. {
  241. Attribute *tmp = attribute.copy();
  242. try
  243. {
  244. _map[name] = tmp;
  245. }
  246. catch (...)
  247. {
  248. delete tmp;
  249. throw;
  250. }
  251. }
  252. else
  253. {
  254. if (strcmp (i->second->typeName(), attribute.typeName()))
  255. THROW (IEX_NAMESPACE::TypeExc, "Cannot assign a value of "
  256. "type \"" << attribute.typeName() << "\" "
  257. "to image attribute \"" << name << "\" of "
  258. "type \"" << i->second->typeName() << "\".");
  259. Attribute *tmp = attribute.copy();
  260. delete i->second;
  261. i->second = tmp;
  262. }
  263. }
  264. void
  265. Header::insert (const string &name, const Attribute &attribute)
  266. {
  267. insert (name.c_str(), attribute);
  268. }
  269. Attribute &
  270. Header::operator [] (const char name[])
  271. {
  272. AttributeMap::iterator i = _map.find (name);
  273. if (i == _map.end())
  274. THROW (IEX_NAMESPACE::ArgExc, "Cannot find image attribute \"" << name << "\".");
  275. return *i->second;
  276. }
  277. const Attribute &
  278. Header::operator [] (const char name[]) const
  279. {
  280. AttributeMap::const_iterator i = _map.find (name);
  281. if (i == _map.end())
  282. THROW (IEX_NAMESPACE::ArgExc, "Cannot find image attribute \"" << name << "\".");
  283. return *i->second;
  284. }
  285. Attribute &
  286. Header::operator [] (const string &name)
  287. {
  288. return this->operator[] (name.c_str());
  289. }
  290. const Attribute &
  291. Header::operator [] (const string &name) const
  292. {
  293. return this->operator[] (name.c_str());
  294. }
  295. Header::Iterator
  296. Header::begin ()
  297. {
  298. return _map.begin();
  299. }
  300. Header::ConstIterator
  301. Header::begin () const
  302. {
  303. return _map.begin();
  304. }
  305. Header::Iterator
  306. Header::end ()
  307. {
  308. return _map.end();
  309. }
  310. Header::ConstIterator
  311. Header::end () const
  312. {
  313. return _map.end();
  314. }
  315. Header::Iterator
  316. Header::find (const char name[])
  317. {
  318. return _map.find (name);
  319. }
  320. Header::ConstIterator
  321. Header::find (const char name[]) const
  322. {
  323. return _map.find (name);
  324. }
  325. Header::Iterator
  326. Header::find (const string &name)
  327. {
  328. return find (name.c_str());
  329. }
  330. Header::ConstIterator
  331. Header::find (const string &name) const
  332. {
  333. return find (name.c_str());
  334. }
  335. IMATH_NAMESPACE::Box2i &
  336. Header::displayWindow ()
  337. {
  338. return static_cast <Box2iAttribute &>
  339. ((*this)["displayWindow"]).value();
  340. }
  341. const IMATH_NAMESPACE::Box2i &
  342. Header::displayWindow () const
  343. {
  344. return static_cast <const Box2iAttribute &>
  345. ((*this)["displayWindow"]).value();
  346. }
  347. IMATH_NAMESPACE::Box2i &
  348. Header::dataWindow ()
  349. {
  350. return static_cast <Box2iAttribute &>
  351. ((*this)["dataWindow"]).value();
  352. }
  353. const IMATH_NAMESPACE::Box2i &
  354. Header::dataWindow () const
  355. {
  356. return static_cast <const Box2iAttribute &>
  357. ((*this)["dataWindow"]).value();
  358. }
  359. float &
  360. Header::pixelAspectRatio ()
  361. {
  362. return static_cast <FloatAttribute &>
  363. ((*this)["pixelAspectRatio"]).value();
  364. }
  365. const float &
  366. Header::pixelAspectRatio () const
  367. {
  368. return static_cast <const FloatAttribute &>
  369. ((*this)["pixelAspectRatio"]).value();
  370. }
  371. IMATH_NAMESPACE::V2f &
  372. Header::screenWindowCenter ()
  373. {
  374. return static_cast <V2fAttribute &>
  375. ((*this)["screenWindowCenter"]).value();
  376. }
  377. const IMATH_NAMESPACE::V2f &
  378. Header::screenWindowCenter () const
  379. {
  380. return static_cast <const V2fAttribute &>
  381. ((*this)["screenWindowCenter"]).value();
  382. }
  383. float &
  384. Header::screenWindowWidth ()
  385. {
  386. return static_cast <FloatAttribute &>
  387. ((*this)["screenWindowWidth"]).value();
  388. }
  389. const float &
  390. Header::screenWindowWidth () const
  391. {
  392. return static_cast <const FloatAttribute &>
  393. ((*this)["screenWindowWidth"]).value();
  394. }
  395. ChannelList &
  396. Header::channels ()
  397. {
  398. return static_cast <ChannelListAttribute &>
  399. ((*this)["channels"]).value();
  400. }
  401. const ChannelList &
  402. Header::channels () const
  403. {
  404. return static_cast <const ChannelListAttribute &>
  405. ((*this)["channels"]).value();
  406. }
  407. LineOrder &
  408. Header::lineOrder ()
  409. {
  410. return static_cast <LineOrderAttribute &>
  411. ((*this)["lineOrder"]).value();
  412. }
  413. const LineOrder &
  414. Header::lineOrder () const
  415. {
  416. return static_cast <const LineOrderAttribute &>
  417. ((*this)["lineOrder"]).value();
  418. }
  419. Compression &
  420. Header::compression ()
  421. {
  422. return static_cast <CompressionAttribute &>
  423. ((*this)["compression"]).value();
  424. }
  425. const Compression &
  426. Header::compression () const
  427. {
  428. return static_cast <const CompressionAttribute &>
  429. ((*this)["compression"]).value();
  430. }
  431. void
  432. Header::setName(const string& name)
  433. {
  434. insert ("name", StringAttribute (name));
  435. }
  436. bool
  437. Header::hasName() const
  438. {
  439. return findTypedAttribute <StringAttribute> ("name") != 0;
  440. }
  441. string &
  442. Header::name()
  443. {
  444. return typedAttribute <StringAttribute> ("name").value();
  445. }
  446. const string &
  447. Header::name() const
  448. {
  449. return typedAttribute <StringAttribute> ("name").value();
  450. }
  451. void
  452. Header::setType(const string& type)
  453. {
  454. if (isSupportedType(type) == false)
  455. {
  456. throw IEX_NAMESPACE::ArgExc (type + "is not a supported image type." +
  457. "The following are supported: " +
  458. SCANLINEIMAGE + ", " +
  459. TILEDIMAGE + ", " +
  460. DEEPSCANLINE + " or " +
  461. DEEPTILE + ".");
  462. }
  463. insert ("type", StringAttribute (type));
  464. // (TODO) Should we do it here?
  465. if (isDeepData(type) && hasVersion() == false)
  466. {
  467. setVersion(1);
  468. }
  469. }
  470. bool
  471. Header::hasType() const
  472. {
  473. return findTypedAttribute <StringAttribute> ("type") != 0;
  474. }
  475. string &
  476. Header::type()
  477. {
  478. return typedAttribute <StringAttribute> ("type").value();
  479. }
  480. const string &
  481. Header::type() const
  482. {
  483. return typedAttribute <StringAttribute> ("type").value();
  484. }
  485. void
  486. Header::setView(const string& view)
  487. {
  488. insert ("view", StringAttribute (view));
  489. }
  490. bool
  491. Header::hasView() const
  492. {
  493. return findTypedAttribute <StringAttribute> ("view") != 0;
  494. }
  495. string &
  496. Header::view()
  497. {
  498. return typedAttribute <StringAttribute> ("view").value();
  499. }
  500. const string &
  501. Header::view() const
  502. {
  503. return typedAttribute <StringAttribute> ("view").value();
  504. }
  505. void
  506. Header::setVersion(const int version)
  507. {
  508. if (version != 1)
  509. {
  510. throw IEX_NAMESPACE::ArgExc ("We can only process version 1");
  511. }
  512. insert ("version", IntAttribute (version));
  513. }
  514. bool
  515. Header::hasVersion() const
  516. {
  517. return findTypedAttribute <IntAttribute> ("version") != 0;
  518. }
  519. int &
  520. Header::version()
  521. {
  522. return typedAttribute <IntAttribute> ("version").value();
  523. }
  524. const int &
  525. Header::version() const
  526. {
  527. return typedAttribute <IntAttribute> ("version").value();
  528. }
  529. void
  530. Header::setChunkCount(int chunks)
  531. {
  532. insert("chunkCount",IntAttribute(chunks));
  533. }
  534. bool
  535. Header::hasChunkCount() const
  536. {
  537. return findTypedAttribute<IntAttribute>("chunkCount") != 0;
  538. }
  539. int&
  540. Header::chunkCount()
  541. {
  542. return typedAttribute <IntAttribute> ("chunkCount").value();
  543. }
  544. const int&
  545. Header::chunkCount() const
  546. {
  547. return typedAttribute <IntAttribute> ("chunkCount").value();
  548. }
  549. void
  550. Header::setTileDescription(const TileDescription& td)
  551. {
  552. insert ("tiles", TileDescriptionAttribute (td));
  553. }
  554. bool
  555. Header::hasTileDescription() const
  556. {
  557. return findTypedAttribute <TileDescriptionAttribute> ("tiles") != 0;
  558. }
  559. TileDescription &
  560. Header::tileDescription ()
  561. {
  562. return typedAttribute <TileDescriptionAttribute> ("tiles").value();
  563. }
  564. const TileDescription &
  565. Header::tileDescription () const
  566. {
  567. return typedAttribute <TileDescriptionAttribute> ("tiles").value();
  568. }
  569. void
  570. Header::setPreviewImage (const PreviewImage &pi)
  571. {
  572. insert ("preview", PreviewImageAttribute (pi));
  573. }
  574. PreviewImage &
  575. Header::previewImage ()
  576. {
  577. return typedAttribute <PreviewImageAttribute> ("preview").value();
  578. }
  579. const PreviewImage &
  580. Header::previewImage () const
  581. {
  582. return typedAttribute <PreviewImageAttribute> ("preview").value();
  583. }
  584. bool
  585. Header::hasPreviewImage () const
  586. {
  587. return findTypedAttribute <PreviewImageAttribute> ("preview") != 0;
  588. }
  589. void
  590. Header::sanityCheck (bool isTiled, bool isMultipartFile) const
  591. {
  592. //
  593. // The display window and the data window must each
  594. // contain at least one pixel. In addition, the
  595. // coordinates of the window corners must be small
  596. // enough to keep expressions like max-min+1 or
  597. // max+min from overflowing.
  598. //
  599. const Box2i &displayWindow = this->displayWindow();
  600. if (displayWindow.min.x > displayWindow.max.x ||
  601. displayWindow.min.y > displayWindow.max.y ||
  602. displayWindow.min.x <= -(INT_MAX / 2) ||
  603. displayWindow.min.y <= -(INT_MAX / 2) ||
  604. displayWindow.max.x >= (INT_MAX / 2) ||
  605. displayWindow.max.y >= (INT_MAX / 2))
  606. {
  607. throw IEX_NAMESPACE::ArgExc ("Invalid display window in image header.");
  608. }
  609. const Box2i &dataWindow = this->dataWindow();
  610. if (dataWindow.min.x > dataWindow.max.x ||
  611. dataWindow.min.y > dataWindow.max.y ||
  612. dataWindow.min.x <= -(INT_MAX / 2) ||
  613. dataWindow.min.y <= -(INT_MAX / 2) ||
  614. dataWindow.max.x >= (INT_MAX / 2) ||
  615. dataWindow.max.y >= (INT_MAX / 2))
  616. {
  617. throw IEX_NAMESPACE::ArgExc ("Invalid data window in image header.");
  618. }
  619. if (maxImageWidth > 0 &&
  620. maxImageWidth < (dataWindow.max.x - dataWindow.min.x + 1))
  621. {
  622. THROW (IEX_NAMESPACE::ArgExc, "The width of the data window exceeds the "
  623. "maximum width of " << maxImageWidth << "pixels.");
  624. }
  625. if (maxImageHeight > 0 &&
  626. maxImageHeight < dataWindow.max.y - dataWindow.min.y + 1)
  627. {
  628. THROW (IEX_NAMESPACE::ArgExc, "The width of the data window exceeds the "
  629. "maximum width of " << maxImageHeight << "pixels.");
  630. }
  631. // chunk table must be smaller than the maximum image area
  632. // (only reachable for unknown types or damaged files: will have thrown earlier
  633. // for regular image types)
  634. if( maxImageHeight>0 && maxImageWidth>0 &&
  635. hasChunkCount() && chunkCount()>Int64(maxImageWidth)*Int64(maxImageHeight))
  636. {
  637. THROW (IEX_NAMESPACE::ArgExc, "chunkCount exceeds maximum area of "
  638. << Int64(maxImageWidth)*Int64(maxImageHeight) << " pixels." );
  639. }
  640. //
  641. // The pixel aspect ratio must be greater than 0.
  642. // In applications, numbers like the the display or
  643. // data window dimensions are likely to be multiplied
  644. // or divided by the pixel aspect ratio; to avoid
  645. // arithmetic exceptions, we limit the pixel aspect
  646. // ratio to a range that is smaller than theoretically
  647. // possible (real aspect ratios are likely to be close
  648. // to 1.0 anyway).
  649. //
  650. float pixelAspectRatio = this->pixelAspectRatio();
  651. const float MIN_PIXEL_ASPECT_RATIO = 1e-6f;
  652. const float MAX_PIXEL_ASPECT_RATIO = 1e+6f;
  653. if (pixelAspectRatio < MIN_PIXEL_ASPECT_RATIO ||
  654. pixelAspectRatio > MAX_PIXEL_ASPECT_RATIO)
  655. {
  656. throw IEX_NAMESPACE::ArgExc ("Invalid pixel aspect ratio in image header.");
  657. }
  658. //
  659. // The screen window width must not be less than 0.
  660. // The size of the screen window can vary over a wide
  661. // range (fish-eye lens to astronomical telescope),
  662. // so we can't limit the screen window width to a
  663. // small range.
  664. //
  665. float screenWindowWidth = this->screenWindowWidth();
  666. if (screenWindowWidth < 0)
  667. throw IEX_NAMESPACE::ArgExc ("Invalid screen window width in image header.");
  668. //
  669. // If the file has multiple parts, verify that each header has attribute
  670. // name and type.
  671. // (TODO) We may want to check more stuff here.
  672. //
  673. if (isMultipartFile)
  674. {
  675. if (!hasName())
  676. {
  677. throw IEX_NAMESPACE::ArgExc ("Headers in a multipart file should"
  678. " have name attribute.");
  679. }
  680. if (!hasType())
  681. {
  682. throw IEX_NAMESPACE::ArgExc ("Headers in a multipart file should"
  683. " have type attribute.");
  684. }
  685. }
  686. const std::string & part_type=hasType() ? type() : "";
  687. if(part_type!="" && !isSupportedType(part_type))
  688. {
  689. //
  690. // skip remaining sanity checks with unsupported types - they may not hold
  691. //
  692. return;
  693. }
  694. //
  695. // If the file is tiled, verify that the tile description has reasonable
  696. // values and check to see if the lineOrder is one of the predefined 3.
  697. // If the file is not tiled, then the lineOrder can only be INCREASING_Y
  698. // or DECREASING_Y.
  699. //
  700. LineOrder lineOrder = this->lineOrder();
  701. if (isTiled)
  702. {
  703. if (!hasTileDescription())
  704. {
  705. throw IEX_NAMESPACE::ArgExc ("Tiled image has no tile "
  706. "description attribute.");
  707. }
  708. const TileDescription &tileDesc = tileDescription();
  709. if (tileDesc.xSize <= 0 || tileDesc.ySize <= 0)
  710. throw IEX_NAMESPACE::ArgExc ("Invalid tile size in image header.");
  711. if (maxTileWidth > 0 &&
  712. maxTileWidth < int(tileDesc.xSize))
  713. {
  714. THROW (IEX_NAMESPACE::ArgExc, "The width of the tiles exceeds the maximum "
  715. "width of " << maxTileWidth << "pixels.");
  716. }
  717. if (maxTileHeight > 0 &&
  718. maxTileHeight < int(tileDesc.ySize))
  719. {
  720. THROW (IEX_NAMESPACE::ArgExc, "The width of the tiles exceeds the maximum "
  721. "width of " << maxTileHeight << "pixels.");
  722. }
  723. if (tileDesc.mode != ONE_LEVEL &&
  724. tileDesc.mode != MIPMAP_LEVELS &&
  725. tileDesc.mode != RIPMAP_LEVELS)
  726. throw IEX_NAMESPACE::ArgExc ("Invalid level mode in image header.");
  727. if (tileDesc.roundingMode != ROUND_UP &&
  728. tileDesc.roundingMode != ROUND_DOWN)
  729. throw IEX_NAMESPACE::ArgExc ("Invalid level rounding mode in image header.");
  730. if (lineOrder != INCREASING_Y &&
  731. lineOrder != DECREASING_Y &&
  732. lineOrder != RANDOM_Y)
  733. throw IEX_NAMESPACE::ArgExc ("Invalid line order in image header.");
  734. }
  735. else
  736. {
  737. if (lineOrder != INCREASING_Y &&
  738. lineOrder != DECREASING_Y)
  739. throw IEX_NAMESPACE::ArgExc ("Invalid line order in image header.");
  740. }
  741. //
  742. // The compression method must be one of the predefined values.
  743. //
  744. if (!isValidCompression (this->compression()))
  745. throw IEX_NAMESPACE::ArgExc ("Unknown compression type in image header.");
  746. if(isDeepData(part_type))
  747. {
  748. if (!isValidDeepCompression (this->compression()))
  749. throw IEX_NAMESPACE::ArgExc ("Compression type in header not valid for deep data");
  750. }
  751. //
  752. // Check the channel list:
  753. //
  754. // If the file is tiled then for each channel, the type must be one of the
  755. // predefined values, and the x and y sampling must both be 1.
  756. //
  757. // If the file is not tiled then for each channel, the type must be one
  758. // of the predefined values, the x and y coordinates of the data window's
  759. // upper left corner must be divisible by the x and y subsampling factors,
  760. // and the width and height of the data window must be divisible by the
  761. // x and y subsampling factors.
  762. //
  763. const ChannelList &channels = this->channels();
  764. if (isTiled)
  765. {
  766. for (ChannelList::ConstIterator i = channels.begin();
  767. i != channels.end();
  768. ++i)
  769. {
  770. if (i.channel().type != OPENEXR_IMF_INTERNAL_NAMESPACE::UINT &&
  771. i.channel().type != OPENEXR_IMF_INTERNAL_NAMESPACE::HALF &&
  772. i.channel().type != OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT)
  773. {
  774. THROW (IEX_NAMESPACE::ArgExc, "Pixel type of \"" << i.name() << "\" "
  775. "image channel is invalid.");
  776. }
  777. if (i.channel().xSampling != 1)
  778. {
  779. THROW (IEX_NAMESPACE::ArgExc, "The x subsampling factor for the "
  780. "\"" << i.name() << "\" channel "
  781. "is not 1.");
  782. }
  783. if (i.channel().ySampling != 1)
  784. {
  785. THROW (IEX_NAMESPACE::ArgExc, "The y subsampling factor for the "
  786. "\"" << i.name() << "\" channel "
  787. "is not 1.");
  788. }
  789. }
  790. }
  791. else
  792. {
  793. for (ChannelList::ConstIterator i = channels.begin();
  794. i != channels.end();
  795. ++i)
  796. {
  797. if (i.channel().type != OPENEXR_IMF_INTERNAL_NAMESPACE::UINT &&
  798. i.channel().type != OPENEXR_IMF_INTERNAL_NAMESPACE::HALF &&
  799. i.channel().type != OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT)
  800. {
  801. THROW (IEX_NAMESPACE::ArgExc, "Pixel type of \"" << i.name() << "\" "
  802. "image channel is invalid.");
  803. }
  804. if (i.channel().xSampling < 1)
  805. {
  806. THROW (IEX_NAMESPACE::ArgExc, "The x subsampling factor for the "
  807. "\"" << i.name() << "\" channel "
  808. "is invalid.");
  809. }
  810. if (i.channel().ySampling < 1)
  811. {
  812. THROW (IEX_NAMESPACE::ArgExc, "The y subsampling factor for the "
  813. "\"" << i.name() << "\" channel "
  814. "is invalid.");
  815. }
  816. if (dataWindow.min.x % i.channel().xSampling)
  817. {
  818. THROW (IEX_NAMESPACE::ArgExc, "The minimum x coordinate of the "
  819. "image's data window is not a multiple "
  820. "of the x subsampling factor of "
  821. "the \"" << i.name() << "\" channel.");
  822. }
  823. if (dataWindow.min.y % i.channel().ySampling)
  824. {
  825. THROW (IEX_NAMESPACE::ArgExc, "The minimum y coordinate of the "
  826. "image's data window is not a multiple "
  827. "of the y subsampling factor of "
  828. "the \"" << i.name() << "\" channel.");
  829. }
  830. if ((dataWindow.max.x - dataWindow.min.x + 1) %
  831. i.channel().xSampling)
  832. {
  833. THROW (IEX_NAMESPACE::ArgExc, "Number of pixels per row in the "
  834. "image's data window is not a multiple "
  835. "of the x subsampling factor of "
  836. "the \"" << i.name() << "\" channel.");
  837. }
  838. if ((dataWindow.max.y - dataWindow.min.y + 1) %
  839. i.channel().ySampling)
  840. {
  841. THROW (IEX_NAMESPACE::ArgExc, "Number of pixels per column in the "
  842. "image's data window is not a multiple "
  843. "of the y subsampling factor of "
  844. "the \"" << i.name() << "\" channel.");
  845. }
  846. }
  847. }
  848. }
  849. void
  850. Header::setMaxImageSize (int maxWidth, int maxHeight)
  851. {
  852. maxImageWidth = maxWidth;
  853. maxImageHeight = maxHeight;
  854. }
  855. void
  856. Header::setMaxTileSize (int maxWidth, int maxHeight)
  857. {
  858. maxTileWidth = maxWidth;
  859. maxTileHeight = maxHeight;
  860. }
  861. bool
  862. Header::readsNothing()
  863. {
  864. return _readsNothing;
  865. }
  866. Int64
  867. Header::writeTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os, bool isTiled) const
  868. {
  869. //
  870. // Write a "magic number" to identify the file as an image file.
  871. // Write the current file format version number.
  872. //
  873. int version = EXR_VERSION;
  874. //
  875. // Write all attributes. If we have a preview image attribute,
  876. // keep track of its position in the file.
  877. //
  878. Int64 previewPosition = 0;
  879. const Attribute *preview =
  880. findTypedAttribute <PreviewImageAttribute> ("preview");
  881. for (ConstIterator i = begin(); i != end(); ++i)
  882. {
  883. //
  884. // Write the attribute's name and type.
  885. //
  886. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, i.name());
  887. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, i.attribute().typeName());
  888. //
  889. // Write the size of the attribute value,
  890. // and the value itself.
  891. //
  892. StdOSStream oss;
  893. i.attribute().writeValueTo (oss, version);
  894. std::string s = oss.str();
  895. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, (int) s.length());
  896. if (&i.attribute() == preview)
  897. previewPosition = os.tellp();
  898. os.write (s.data(), int(s.length()));
  899. }
  900. //
  901. // Write zero-length attribute name to mark the end of the header.
  902. //
  903. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::write <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (os, "");
  904. return previewPosition;
  905. }
  906. void
  907. Header::readFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, int &version)
  908. {
  909. //
  910. // Read all attributes.
  911. //
  912. int attrCount = 0;
  913. while (true)
  914. {
  915. //
  916. // Read the name of the attribute.
  917. // A zero-length attribute name indicates the end of the header.
  918. //
  919. char name[Name::SIZE];
  920. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, Name::MAX_LENGTH, name);
  921. if (name[0] == 0)
  922. {
  923. if (attrCount == 0) _readsNothing = true;
  924. else _readsNothing = false;
  925. break;
  926. }
  927. attrCount++;
  928. checkIsNullTerminated (name, "attribute name");
  929. //
  930. // Read the attribute type and the size of the attribute value.
  931. //
  932. char typeName[Name::SIZE];
  933. int size;
  934. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, Name::MAX_LENGTH, typeName);
  935. checkIsNullTerminated (typeName, "attribute type name");
  936. OPENEXR_IMF_INTERNAL_NAMESPACE::Xdr::read <OPENEXR_IMF_INTERNAL_NAMESPACE::StreamIO> (is, size);
  937. AttributeMap::iterator i = _map.find (name);
  938. if (i != _map.end())
  939. {
  940. //
  941. // The attribute already exists (for example,
  942. // because it is a predefined attribute).
  943. // Read the attribute's new value from the file.
  944. //
  945. if (strncmp (i->second->typeName(), typeName, sizeof (typeName)))
  946. THROW (IEX_NAMESPACE::InputExc, "Unexpected type for image attribute "
  947. "\"" << name << "\".");
  948. i->second->readValueFrom (is, size, version);
  949. }
  950. else
  951. {
  952. //
  953. // The new attribute does not exist yet.
  954. // If the attribute type is of a known type,
  955. // read the attribute value. If the attribute
  956. // is of an unknown type, read its value and
  957. // store it as an OpaqueAttribute.
  958. //
  959. Attribute *attr;
  960. if (Attribute::knownType (typeName))
  961. attr = Attribute::newAttribute (typeName);
  962. else
  963. attr = new OpaqueAttribute (typeName);
  964. try
  965. {
  966. attr->readValueFrom (is, size, version);
  967. _map[name] = attr;
  968. }
  969. catch (...)
  970. {
  971. delete attr;
  972. throw;
  973. }
  974. }
  975. }
  976. }
  977. void
  978. staticInitialize ()
  979. {
  980. static Mutex criticalSection;
  981. Lock lock (criticalSection);
  982. static bool initialized = false;
  983. if (!initialized)
  984. {
  985. //
  986. // One-time initialization -- register
  987. // some predefined attribute types.
  988. //
  989. Box2fAttribute::registerAttributeType();
  990. Box2iAttribute::registerAttributeType();
  991. ChannelListAttribute::registerAttributeType();
  992. CompressionAttribute::registerAttributeType();
  993. ChromaticitiesAttribute::registerAttributeType();
  994. DeepImageStateAttribute::registerAttributeType();
  995. DoubleAttribute::registerAttributeType();
  996. EnvmapAttribute::registerAttributeType();
  997. FloatAttribute::registerAttributeType();
  998. FloatVectorAttribute::registerAttributeType();
  999. IntAttribute::registerAttributeType();
  1000. KeyCodeAttribute::registerAttributeType();
  1001. LineOrderAttribute::registerAttributeType();
  1002. M33dAttribute::registerAttributeType();
  1003. M33fAttribute::registerAttributeType();
  1004. M44dAttribute::registerAttributeType();
  1005. M44fAttribute::registerAttributeType();
  1006. PreviewImageAttribute::registerAttributeType();
  1007. RationalAttribute::registerAttributeType();
  1008. StringAttribute::registerAttributeType();
  1009. StringVectorAttribute::registerAttributeType();
  1010. TileDescriptionAttribute::registerAttributeType();
  1011. TimeCodeAttribute::registerAttributeType();
  1012. V2dAttribute::registerAttributeType();
  1013. V2fAttribute::registerAttributeType();
  1014. V2iAttribute::registerAttributeType();
  1015. V3dAttribute::registerAttributeType();
  1016. V3fAttribute::registerAttributeType();
  1017. V3iAttribute::registerAttributeType();
  1018. DwaCompressor::initializeFuncs();
  1019. initialized = true;
  1020. }
  1021. }
  1022. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT