ImfMisc.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  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. // Miscellaneous helper functions for OpenEXR image file I/O
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include <ImfMisc.h>
  40. #include <ImfHeader.h>
  41. #include <ImfAttribute.h>
  42. #include <ImfCompressor.h>
  43. #include <ImfChannelList.h>
  44. #include <ImfXdr.h>
  45. #include <ImathFun.h>
  46. #include <Iex.h>
  47. #include <ImfStdIO.h>
  48. #include <ImfConvert.h>
  49. #include <ImfPartType.h>
  50. #include <ImfTileDescription.h>
  51. #include "ImfNamespace.h"
  52. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  53. using IMATH_NAMESPACE::Box2i;
  54. using IMATH_NAMESPACE::divp;
  55. using IMATH_NAMESPACE::modp;
  56. using std::vector;
  57. int
  58. pixelTypeSize (PixelType type)
  59. {
  60. int size;
  61. switch (type)
  62. {
  63. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  64. size = Xdr::size <unsigned int> ();
  65. break;
  66. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  67. size = Xdr::size <half> ();
  68. break;
  69. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  70. size = Xdr::size <float> ();
  71. break;
  72. default:
  73. throw IEX_NAMESPACE::ArgExc ("Unknown pixel type.");
  74. }
  75. return size;
  76. }
  77. int
  78. numSamples (int s, int a, int b)
  79. {
  80. int a1 = divp (a, s);
  81. int b1 = divp (b, s);
  82. return b1 - a1 + ((a1 * s < a)? 0: 1);
  83. }
  84. size_t
  85. bytesPerLineTable (const Header &header,
  86. vector<size_t> &bytesPerLine)
  87. {
  88. const Box2i &dataWindow = header.dataWindow();
  89. const ChannelList &channels = header.channels();
  90. bytesPerLine.resize (dataWindow.max.y - dataWindow.min.y + 1);
  91. for (ChannelList::ConstIterator c = channels.begin();
  92. c != channels.end();
  93. ++c)
  94. {
  95. int nBytes = pixelTypeSize (c.channel().type) *
  96. (dataWindow.max.x - dataWindow.min.x + 1) /
  97. c.channel().xSampling;
  98. for (int y = dataWindow.min.y, i = 0; y <= dataWindow.max.y; ++y, ++i)
  99. if (modp (y, c.channel().ySampling) == 0)
  100. bytesPerLine[i] += nBytes;
  101. }
  102. size_t maxBytesPerLine = 0;
  103. for (int y = dataWindow.min.y, i = 0; y <= dataWindow.max.y; ++y, ++i)
  104. if (maxBytesPerLine < bytesPerLine[i])
  105. maxBytesPerLine = bytesPerLine[i];
  106. return maxBytesPerLine;
  107. }
  108. static int
  109. roundToNextMultiple(int n, int d)
  110. {
  111. return ((n + d - 1) / d) * d;
  112. }
  113. static int
  114. roundToPrevMultiple(int n, int d)
  115. {
  116. return (n / d) * d;
  117. }
  118. size_t
  119. bytesPerDeepLineTable (const Header &header,
  120. int minY, int maxY,
  121. const char* base,
  122. int xStride,
  123. int yStride,
  124. vector<size_t> &bytesPerLine)
  125. {
  126. const Box2i &dataWindow = header.dataWindow();
  127. const ChannelList &channels = header.channels();
  128. for (ChannelList::ConstIterator c = channels.begin();
  129. c != channels.end();
  130. ++c)
  131. {
  132. const int ySampling = abs(c.channel().ySampling);
  133. const int xSampling = abs(c.channel().xSampling);
  134. const int pixelSize = pixelTypeSize (c.channel().type);
  135. // Here we transform from the domain over all pixels into the domain
  136. // of actual samples. We want to sample points in [minY, maxY] where
  137. // (y % ySampling) == 0. However, doing this by rejecting samples
  138. // requires O(height*width) modulo computations, which were a
  139. // significant bottleneck in the previous implementation of this
  140. // function. For the low, low price of 4 divisions per channel, we
  141. // can tighten the y & x ranges to the least and greatest roots of the
  142. // sampling function and then stride by the sampling rate.
  143. const int sampleMinY = roundToNextMultiple(minY, ySampling);
  144. const int sampleMaxY = roundToPrevMultiple(maxY, ySampling);
  145. const int sampleMinX = roundToNextMultiple(dataWindow.min.x, xSampling);
  146. const int sampleMaxX = roundToPrevMultiple(dataWindow.max.x, xSampling);
  147. for (int y = sampleMinY; y <= sampleMaxY; y+=ySampling)
  148. {
  149. int nBytes = 0;
  150. for (int x = sampleMinX; x <= sampleMaxX; x += xSampling)
  151. {
  152. nBytes += pixelSize *
  153. sampleCount(base, xStride, yStride, x, y);
  154. }
  155. bytesPerLine[y - dataWindow.min.y] += nBytes;
  156. }
  157. }
  158. size_t maxBytesPerLine = 0;
  159. for (int y = minY; y <= maxY; ++y)
  160. if (maxBytesPerLine < bytesPerLine[y - dataWindow.min.y])
  161. maxBytesPerLine = bytesPerLine[y - dataWindow.min.y];
  162. return maxBytesPerLine;
  163. }
  164. size_t
  165. bytesPerDeepLineTable (const Header &header,
  166. char* base,
  167. int xStride,
  168. int yStride,
  169. vector<size_t> &bytesPerLine)
  170. {
  171. return bytesPerDeepLineTable(header,
  172. header.dataWindow().min.y,
  173. header.dataWindow().max.y,
  174. base,
  175. xStride,
  176. yStride,
  177. bytesPerLine);
  178. }
  179. void
  180. offsetInLineBufferTable (const vector<size_t> &bytesPerLine,
  181. int scanline1, int scanline2,
  182. int linesInLineBuffer,
  183. vector<size_t> &offsetInLineBuffer)
  184. {
  185. offsetInLineBuffer.resize (bytesPerLine.size());
  186. size_t offset = 0;
  187. for (int i = scanline1; i <= scanline2; ++i)
  188. {
  189. if (i % linesInLineBuffer == 0)
  190. offset = 0;
  191. offsetInLineBuffer[i] = offset;
  192. offset += bytesPerLine[i];
  193. }
  194. }
  195. void
  196. offsetInLineBufferTable (const vector<size_t> &bytesPerLine,
  197. int linesInLineBuffer,
  198. vector<size_t> &offsetInLineBuffer)
  199. {
  200. offsetInLineBufferTable (bytesPerLine,
  201. 0, bytesPerLine.size() - 1,
  202. linesInLineBuffer,
  203. offsetInLineBuffer);
  204. }
  205. int
  206. lineBufferMinY (int y, int minY, int linesInLineBuffer)
  207. {
  208. return ((y - minY) / linesInLineBuffer) * linesInLineBuffer + minY;
  209. }
  210. int
  211. lineBufferMaxY (int y, int minY, int linesInLineBuffer)
  212. {
  213. return lineBufferMinY (y, minY, linesInLineBuffer) + linesInLineBuffer - 1;
  214. }
  215. Compressor::Format
  216. defaultFormat (Compressor * compressor)
  217. {
  218. return compressor? compressor->format(): Compressor::XDR;
  219. }
  220. int
  221. numLinesInBuffer (Compressor * compressor)
  222. {
  223. return compressor? compressor->numScanLines(): 1;
  224. }
  225. void
  226. copyIntoFrameBuffer (const char *& readPtr,
  227. char * writePtr,
  228. char * endPtr,
  229. size_t xStride,
  230. bool fill,
  231. double fillValue,
  232. Compressor::Format format,
  233. PixelType typeInFrameBuffer,
  234. PixelType typeInFile)
  235. {
  236. //
  237. // Copy a horizontal row of pixels from an input
  238. // file's line or tile buffer to a frame buffer.
  239. //
  240. if (fill)
  241. {
  242. //
  243. // The file contains no data for this channel.
  244. // Store a default value in the frame buffer.
  245. //
  246. switch (typeInFrameBuffer)
  247. {
  248. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  249. {
  250. unsigned int fillVal = (unsigned int) (fillValue);
  251. while (writePtr <= endPtr)
  252. {
  253. *(unsigned int *) writePtr = fillVal;
  254. writePtr += xStride;
  255. }
  256. }
  257. break;
  258. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  259. {
  260. half fillVal = half (fillValue);
  261. while (writePtr <= endPtr)
  262. {
  263. *(half *) writePtr = fillVal;
  264. writePtr += xStride;
  265. }
  266. }
  267. break;
  268. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  269. {
  270. float fillVal = float (fillValue);
  271. while (writePtr <= endPtr)
  272. {
  273. *(float *) writePtr = fillVal;
  274. writePtr += xStride;
  275. }
  276. }
  277. break;
  278. default:
  279. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  280. }
  281. }
  282. else if (format == Compressor::XDR)
  283. {
  284. //
  285. // The the line or tile buffer is in XDR format.
  286. //
  287. // Convert the pixels from the file's machine-
  288. // independent representation, and store the
  289. // results in the frame buffer.
  290. //
  291. switch (typeInFrameBuffer)
  292. {
  293. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  294. switch (typeInFile)
  295. {
  296. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  297. while (writePtr <= endPtr)
  298. {
  299. Xdr::read <CharPtrIO> (readPtr, *(unsigned int *) writePtr);
  300. writePtr += xStride;
  301. }
  302. break;
  303. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  304. while (writePtr <= endPtr)
  305. {
  306. half h;
  307. Xdr::read <CharPtrIO> (readPtr, h);
  308. *(unsigned int *) writePtr = halfToUint (h);
  309. writePtr += xStride;
  310. }
  311. break;
  312. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  313. while (writePtr <= endPtr)
  314. {
  315. float f;
  316. Xdr::read <CharPtrIO> (readPtr, f);
  317. *(unsigned int *)writePtr = floatToUint (f);
  318. writePtr += xStride;
  319. }
  320. break;
  321. default:
  322. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  323. }
  324. break;
  325. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  326. switch (typeInFile)
  327. {
  328. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  329. while (writePtr <= endPtr)
  330. {
  331. unsigned int ui;
  332. Xdr::read <CharPtrIO> (readPtr, ui);
  333. *(half *) writePtr = uintToHalf (ui);
  334. writePtr += xStride;
  335. }
  336. break;
  337. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  338. while (writePtr <= endPtr)
  339. {
  340. Xdr::read <CharPtrIO> (readPtr, *(half *) writePtr);
  341. writePtr += xStride;
  342. }
  343. break;
  344. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  345. while (writePtr <= endPtr)
  346. {
  347. float f;
  348. Xdr::read <CharPtrIO> (readPtr, f);
  349. *(half *) writePtr = floatToHalf (f);
  350. writePtr += xStride;
  351. }
  352. break;
  353. default:
  354. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  355. }
  356. break;
  357. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  358. switch (typeInFile)
  359. {
  360. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  361. while (writePtr <= endPtr)
  362. {
  363. unsigned int ui;
  364. Xdr::read <CharPtrIO> (readPtr, ui);
  365. *(float *) writePtr = float (ui);
  366. writePtr += xStride;
  367. }
  368. break;
  369. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  370. while (writePtr <= endPtr)
  371. {
  372. half h;
  373. Xdr::read <CharPtrIO> (readPtr, h);
  374. *(float *) writePtr = float (h);
  375. writePtr += xStride;
  376. }
  377. break;
  378. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  379. while (writePtr <= endPtr)
  380. {
  381. Xdr::read <CharPtrIO> (readPtr, *(float *) writePtr);
  382. writePtr += xStride;
  383. }
  384. break;
  385. default:
  386. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  387. }
  388. break;
  389. default:
  390. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  391. }
  392. }
  393. else
  394. {
  395. //
  396. // The the line or tile buffer is in NATIVE format.
  397. // Copy the results into the frame buffer.
  398. //
  399. switch (typeInFrameBuffer)
  400. {
  401. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  402. switch (typeInFile)
  403. {
  404. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  405. while (writePtr <= endPtr)
  406. {
  407. for (size_t i = 0; i < sizeof (unsigned int); ++i)
  408. writePtr[i] = readPtr[i];
  409. readPtr += sizeof (unsigned int);
  410. writePtr += xStride;
  411. }
  412. break;
  413. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  414. while (writePtr <= endPtr)
  415. {
  416. half h = *(half *) readPtr;
  417. *(unsigned int *) writePtr = halfToUint (h);
  418. readPtr += sizeof (half);
  419. writePtr += xStride;
  420. }
  421. break;
  422. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  423. while (writePtr <= endPtr)
  424. {
  425. float f;
  426. for (size_t i = 0; i < sizeof (float); ++i)
  427. ((char *)&f)[i] = readPtr[i];
  428. *(unsigned int *)writePtr = floatToUint (f);
  429. readPtr += sizeof (float);
  430. writePtr += xStride;
  431. }
  432. break;
  433. default:
  434. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  435. }
  436. break;
  437. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  438. switch (typeInFile)
  439. {
  440. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  441. while (writePtr <= endPtr)
  442. {
  443. unsigned int ui;
  444. for (size_t i = 0; i < sizeof (unsigned int); ++i)
  445. ((char *)&ui)[i] = readPtr[i];
  446. *(half *) writePtr = uintToHalf (ui);
  447. readPtr += sizeof (unsigned int);
  448. writePtr += xStride;
  449. }
  450. break;
  451. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  452. // If we're tightly packed, just memcpy
  453. if (xStride == sizeof(half)) {
  454. int numBytes = endPtr-writePtr+sizeof(half);
  455. memcpy(writePtr, readPtr, numBytes);
  456. readPtr += numBytes;
  457. writePtr += numBytes;
  458. } else {
  459. while (writePtr <= endPtr)
  460. {
  461. *(half *) writePtr = *(half *)readPtr;
  462. readPtr += sizeof (half);
  463. writePtr += xStride;
  464. }
  465. }
  466. break;
  467. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  468. while (writePtr <= endPtr)
  469. {
  470. float f;
  471. for (size_t i = 0; i < sizeof (float); ++i)
  472. ((char *)&f)[i] = readPtr[i];
  473. *(half *) writePtr = floatToHalf (f);
  474. readPtr += sizeof (float);
  475. writePtr += xStride;
  476. }
  477. break;
  478. default:
  479. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  480. }
  481. break;
  482. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  483. switch (typeInFile)
  484. {
  485. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  486. while (writePtr <= endPtr)
  487. {
  488. unsigned int ui;
  489. for (size_t i = 0; i < sizeof (unsigned int); ++i)
  490. ((char *)&ui)[i] = readPtr[i];
  491. *(float *) writePtr = float (ui);
  492. readPtr += sizeof (unsigned int);
  493. writePtr += xStride;
  494. }
  495. break;
  496. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  497. while (writePtr <= endPtr)
  498. {
  499. half h = *(half *) readPtr;
  500. *(float *) writePtr = float (h);
  501. readPtr += sizeof (half);
  502. writePtr += xStride;
  503. }
  504. break;
  505. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  506. while (writePtr <= endPtr)
  507. {
  508. for (size_t i = 0; i < sizeof (float); ++i)
  509. writePtr[i] = readPtr[i];
  510. readPtr += sizeof (float);
  511. writePtr += xStride;
  512. }
  513. break;
  514. default:
  515. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  516. }
  517. break;
  518. default:
  519. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  520. }
  521. }
  522. }
  523. void
  524. copyIntoDeepFrameBuffer (const char *& readPtr,
  525. char * base,
  526. const char* sampleCountBase,
  527. ptrdiff_t sampleCountXStride,
  528. ptrdiff_t sampleCountYStride,
  529. int y, int minX, int maxX,
  530. int xOffsetForSampleCount,
  531. int yOffsetForSampleCount,
  532. int xOffsetForData,
  533. int yOffsetForData,
  534. ptrdiff_t sampleStride,
  535. ptrdiff_t xPointerStride,
  536. ptrdiff_t yPointerStride,
  537. bool fill,
  538. double fillValue,
  539. Compressor::Format format,
  540. PixelType typeInFrameBuffer,
  541. PixelType typeInFile)
  542. {
  543. //
  544. // Copy a horizontal row of pixels from an input
  545. // file's line or tile buffer to a frame buffer.
  546. //
  547. if (fill)
  548. {
  549. //
  550. // The file contains no data for this channel.
  551. // Store a default value in the frame buffer.
  552. //
  553. switch (typeInFrameBuffer)
  554. {
  555. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  556. {
  557. unsigned int fillVal = (unsigned int) (fillValue);
  558. for (int x = minX; x <= maxX; x++)
  559. {
  560. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  561. if(writePtr)
  562. {
  563. int count = sampleCount(sampleCountBase,
  564. sampleCountXStride,
  565. sampleCountYStride,
  566. x - xOffsetForSampleCount,
  567. y - yOffsetForSampleCount);
  568. for (int i = 0; i < count; i++)
  569. {
  570. *(unsigned int *) writePtr = fillVal;
  571. writePtr += sampleStride;
  572. }
  573. }
  574. }
  575. }
  576. break;
  577. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  578. {
  579. half fillVal = half (fillValue);
  580. for (int x = minX; x <= maxX; x++)
  581. {
  582. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  583. if(writePtr)
  584. {
  585. int count = sampleCount(sampleCountBase,
  586. sampleCountXStride,
  587. sampleCountYStride,
  588. x - xOffsetForSampleCount,
  589. y - yOffsetForSampleCount);
  590. for (int i = 0; i < count; i++)
  591. {
  592. *(half *) writePtr = fillVal;
  593. writePtr += sampleStride;
  594. }
  595. }
  596. }
  597. }
  598. break;
  599. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  600. {
  601. float fillVal = float (fillValue);
  602. for (int x = minX; x <= maxX; x++)
  603. {
  604. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  605. if(writePtr)
  606. {
  607. int count = sampleCount(sampleCountBase,
  608. sampleCountXStride,
  609. sampleCountYStride,
  610. x - xOffsetForSampleCount,
  611. y - yOffsetForSampleCount);
  612. for (int i = 0; i < count; i++)
  613. {
  614. *(float *) writePtr = fillVal;
  615. writePtr += sampleStride;
  616. }
  617. }
  618. }
  619. }
  620. break;
  621. default:
  622. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  623. }
  624. }
  625. else if (format == Compressor::XDR)
  626. {
  627. //
  628. // The the line or tile buffer is in XDR format.
  629. //
  630. // Convert the pixels from the file's machine-
  631. // independent representation, and store the
  632. // results in the frame buffer.
  633. //
  634. switch (typeInFrameBuffer)
  635. {
  636. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  637. switch (typeInFile)
  638. {
  639. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  640. for (int x = minX; x <= maxX; x++)
  641. {
  642. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  643. int count = sampleCount(sampleCountBase,
  644. sampleCountXStride,
  645. sampleCountYStride,
  646. x - xOffsetForSampleCount,
  647. y - yOffsetForSampleCount);
  648. if(writePtr)
  649. {
  650. for (int i = 0; i < count; i++)
  651. {
  652. Xdr::read <CharPtrIO> (readPtr, *(unsigned int *) writePtr);
  653. writePtr += sampleStride;
  654. }
  655. }else{
  656. Xdr::skip <CharPtrIO> (readPtr,count*Xdr::size<unsigned int>());
  657. }
  658. }
  659. break;
  660. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  661. for (int x = minX; x <= maxX; x++)
  662. {
  663. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  664. int count = sampleCount(sampleCountBase,
  665. sampleCountXStride,
  666. sampleCountYStride,
  667. x - xOffsetForSampleCount,
  668. y - yOffsetForSampleCount);
  669. if(writePtr)
  670. {
  671. for (int i = 0; i < count; i++)
  672. {
  673. half h;
  674. Xdr::read <CharPtrIO> (readPtr, h);
  675. *(unsigned int *) writePtr = halfToUint (h);
  676. writePtr += sampleStride;
  677. }
  678. }else{
  679. Xdr::skip <CharPtrIO> (readPtr,count*Xdr::size<half>());
  680. }
  681. }
  682. break;
  683. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  684. for (int x = minX; x <= maxX; x++)
  685. {
  686. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  687. int count = sampleCount(sampleCountBase,
  688. sampleCountXStride,
  689. sampleCountYStride,
  690. x - xOffsetForSampleCount,
  691. y - yOffsetForSampleCount);
  692. if(writePtr)
  693. {
  694. for (int i = 0; i < count; i++)
  695. {
  696. float f;
  697. Xdr::read <CharPtrIO> (readPtr, f);
  698. *(unsigned int *)writePtr = floatToUint (f);
  699. writePtr += sampleStride;
  700. }
  701. }else{
  702. Xdr::skip <CharPtrIO> (readPtr,count*Xdr::size<float>());
  703. }
  704. }
  705. break;
  706. default:
  707. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  708. }
  709. break;
  710. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  711. switch (typeInFile)
  712. {
  713. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  714. for (int x = minX; x <= maxX; x++)
  715. {
  716. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  717. int count = sampleCount(sampleCountBase,
  718. sampleCountXStride,
  719. sampleCountYStride,
  720. x - xOffsetForSampleCount,
  721. y - yOffsetForSampleCount);
  722. if(writePtr)
  723. {
  724. for (int i = 0; i < count; i++)
  725. {
  726. unsigned int ui;
  727. Xdr::read <CharPtrIO> (readPtr, ui);
  728. *(half *) writePtr = uintToHalf (ui);
  729. writePtr += sampleStride;
  730. }
  731. }else{
  732. Xdr::skip <CharPtrIO> (readPtr,count*Xdr::size<unsigned int>());
  733. }
  734. }
  735. break;
  736. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  737. for (int x = minX; x <= maxX; x++)
  738. {
  739. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  740. int count = sampleCount(sampleCountBase,
  741. sampleCountXStride,
  742. sampleCountYStride,
  743. x - xOffsetForSampleCount,
  744. y - yOffsetForSampleCount);
  745. if(writePtr)
  746. {
  747. for (int i = 0; i < count; i++)
  748. {
  749. Xdr::read <CharPtrIO> (readPtr, *(half *) writePtr);
  750. writePtr += sampleStride;
  751. }
  752. }else{
  753. Xdr::skip <CharPtrIO> (readPtr,count*Xdr::size<half>());
  754. }
  755. }
  756. break;
  757. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  758. for (int x = minX; x <= maxX; x++)
  759. {
  760. char* writePtr = *(char **) (base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  761. int count = sampleCount(sampleCountBase,
  762. sampleCountXStride,
  763. sampleCountYStride,
  764. x - xOffsetForSampleCount,
  765. y - yOffsetForSampleCount);
  766. if(writePtr)
  767. {
  768. for (int i = 0; i < count; i++)
  769. {
  770. float f;
  771. Xdr::read <CharPtrIO> (readPtr, f);
  772. *(half *) writePtr = floatToHalf (f);
  773. writePtr += sampleStride;
  774. }
  775. }else{
  776. Xdr::skip <CharPtrIO> (readPtr,count*Xdr::size<float>());
  777. }
  778. }
  779. break;
  780. default:
  781. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  782. }
  783. break;
  784. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  785. switch (typeInFile)
  786. {
  787. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  788. for (int x = minX; x <= maxX; x++)
  789. {
  790. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  791. int count = sampleCount(sampleCountBase,
  792. sampleCountXStride,
  793. sampleCountYStride,
  794. x - xOffsetForSampleCount,
  795. y - yOffsetForSampleCount);
  796. if(writePtr)
  797. {
  798. for (int i = 0; i < count; i++)
  799. {
  800. unsigned int ui;
  801. Xdr::read <CharPtrIO> (readPtr, ui);
  802. *(float *) writePtr = float (ui);
  803. writePtr += sampleStride;
  804. }
  805. }else{
  806. Xdr::skip <CharPtrIO> (readPtr,count*Xdr::size<unsigned int>());
  807. }
  808. }
  809. break;
  810. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  811. for (int x = minX; x <= maxX; x++)
  812. {
  813. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  814. int count = sampleCount(sampleCountBase,
  815. sampleCountXStride,
  816. sampleCountYStride,
  817. x - xOffsetForSampleCount,
  818. y - yOffsetForSampleCount);
  819. if(writePtr)
  820. {
  821. for (int i = 0; i < count; i++)
  822. {
  823. half h;
  824. Xdr::read <CharPtrIO> (readPtr, h);
  825. *(float *) writePtr = float (h);
  826. writePtr += sampleStride;
  827. }
  828. }else{
  829. Xdr::skip <CharPtrIO> (readPtr,count*Xdr::size<half>());
  830. }
  831. }
  832. break;
  833. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  834. for (int x = minX; x <= maxX; x++)
  835. {
  836. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  837. int count = sampleCount(sampleCountBase,
  838. sampleCountXStride,
  839. sampleCountYStride,
  840. x - xOffsetForSampleCount,
  841. y - yOffsetForSampleCount);
  842. if(writePtr)
  843. {
  844. for (int i = 0; i < count; i++)
  845. {
  846. Xdr::read <CharPtrIO> (readPtr, *(float *) writePtr);
  847. writePtr += sampleStride;
  848. }
  849. } else{
  850. Xdr::skip <CharPtrIO> (readPtr,count*Xdr::size<float>());
  851. }
  852. }
  853. break;
  854. default:
  855. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  856. }
  857. break;
  858. default:
  859. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  860. }
  861. }
  862. else
  863. {
  864. //
  865. // The the line or tile buffer is in NATIVE format.
  866. // Copy the results into the frame buffer.
  867. //
  868. switch (typeInFrameBuffer)
  869. {
  870. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  871. switch (typeInFile)
  872. {
  873. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  874. for (int x = minX; x <= maxX; x++)
  875. {
  876. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  877. int count = sampleCount(sampleCountBase,
  878. sampleCountXStride,
  879. sampleCountYStride,
  880. x - xOffsetForSampleCount,
  881. y - yOffsetForSampleCount);
  882. if(writePtr)
  883. {
  884. for (int i = 0; i < count; i++)
  885. {
  886. for (size_t i = 0; i < sizeof (unsigned int); ++i)
  887. writePtr[i] = readPtr[i];
  888. readPtr += sizeof (unsigned int);
  889. writePtr += sampleStride;
  890. }
  891. }else{
  892. readPtr+=sizeof(unsigned int)*count;
  893. }
  894. }
  895. break;
  896. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  897. for (int x = minX; x <= maxX; x++)
  898. {
  899. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  900. int count = sampleCount(sampleCountBase,
  901. sampleCountXStride,
  902. sampleCountYStride,
  903. x - xOffsetForSampleCount,
  904. y - yOffsetForSampleCount);
  905. if(writePtr)
  906. {
  907. for (int i = 0; i < count; i++)
  908. {
  909. half h = *(half *) readPtr;
  910. *(unsigned int *) writePtr = halfToUint (h);
  911. readPtr += sizeof (half);
  912. writePtr += sampleStride;
  913. }
  914. }else{
  915. readPtr+=sizeof(half)*count;
  916. }
  917. }
  918. break;
  919. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  920. for (int x = minX; x <= maxX; x++)
  921. {
  922. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  923. int count = sampleCount(sampleCountBase,
  924. sampleCountXStride,
  925. sampleCountYStride,
  926. x - xOffsetForSampleCount,
  927. y - yOffsetForSampleCount);
  928. if(writePtr)
  929. {
  930. for (int i = 0; i < count; i++)
  931. {
  932. float f;
  933. for (size_t i = 0; i < sizeof (float); ++i)
  934. ((char *)&f)[i] = readPtr[i];
  935. *(unsigned int *)writePtr = floatToUint (f);
  936. readPtr += sizeof (float);
  937. writePtr += sampleStride;
  938. }
  939. }else{
  940. readPtr+=sizeof(float)*count;
  941. }
  942. }
  943. break;
  944. default:
  945. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  946. }
  947. break;
  948. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  949. switch (typeInFile)
  950. {
  951. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  952. for (int x = minX; x <= maxX; x++)
  953. {
  954. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  955. int count = sampleCount(sampleCountBase,
  956. sampleCountXStride,
  957. sampleCountYStride,
  958. x - xOffsetForSampleCount,
  959. y - yOffsetForSampleCount);
  960. if(writePtr)
  961. {
  962. for (int i = 0; i < count; i++)
  963. {
  964. unsigned int ui;
  965. for (size_t i = 0; i < sizeof (unsigned int); ++i)
  966. ((char *)&ui)[i] = readPtr[i];
  967. *(half *) writePtr = uintToHalf (ui);
  968. readPtr += sizeof (unsigned int);
  969. writePtr += sampleStride;
  970. }
  971. }else{
  972. readPtr+=sizeof(unsigned int)*count;
  973. }
  974. }
  975. break;
  976. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  977. for (int x = minX; x <= maxX; x++)
  978. {
  979. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  980. int count = sampleCount(sampleCountBase,
  981. sampleCountXStride,
  982. sampleCountYStride,
  983. x - xOffsetForSampleCount,
  984. y - yOffsetForSampleCount);
  985. if(writePtr)
  986. {
  987. for (int i = 0; i < count; i++)
  988. {
  989. *(half *) writePtr = *(half *)readPtr;
  990. readPtr += sizeof (half);
  991. writePtr += sampleStride;
  992. }
  993. }else{
  994. readPtr+=sizeof(half)*count;
  995. }
  996. }
  997. break;
  998. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  999. for (int x = minX; x <= maxX; x++)
  1000. {
  1001. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  1002. int count = sampleCount(sampleCountBase,
  1003. sampleCountXStride,
  1004. sampleCountYStride,
  1005. x - xOffsetForSampleCount,
  1006. y - yOffsetForSampleCount);
  1007. if(writePtr)
  1008. {
  1009. for (int i = 0; i < count; i++)
  1010. {
  1011. float f;
  1012. for (size_t i = 0; i < sizeof (float); ++i)
  1013. ((char *)&f)[i] = readPtr[i];
  1014. *(half *) writePtr = floatToHalf (f);
  1015. readPtr += sizeof (float);
  1016. writePtr += sampleStride;
  1017. }
  1018. }else{
  1019. readPtr+=sizeof(float)*count;
  1020. }
  1021. }
  1022. break;
  1023. default:
  1024. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1025. }
  1026. break;
  1027. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  1028. switch (typeInFile)
  1029. {
  1030. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  1031. for (int x = minX; x <= maxX; x++)
  1032. {
  1033. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  1034. int count = sampleCount(sampleCountBase,
  1035. sampleCountXStride,
  1036. sampleCountYStride,
  1037. x - xOffsetForSampleCount,
  1038. y - yOffsetForSampleCount);
  1039. if(writePtr)
  1040. {
  1041. for (int i = 0; i < count; i++)
  1042. {
  1043. unsigned int ui;
  1044. for (size_t i = 0; i < sizeof (unsigned int); ++i)
  1045. ((char *)&ui)[i] = readPtr[i];
  1046. *(float *) writePtr = float (ui);
  1047. readPtr += sizeof (unsigned int);
  1048. writePtr += sampleStride;
  1049. }
  1050. }else{
  1051. readPtr+=sizeof(unsigned int)*count;
  1052. }
  1053. }
  1054. break;
  1055. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  1056. for (int x = minX; x <= maxX; x++)
  1057. {
  1058. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  1059. int count = sampleCount(sampleCountBase,
  1060. sampleCountXStride,
  1061. sampleCountYStride,
  1062. x - xOffsetForSampleCount,
  1063. y - yOffsetForSampleCount);
  1064. if(writePtr)
  1065. {
  1066. for (int i = 0; i < count; i++)
  1067. {
  1068. half h = *(half *) readPtr;
  1069. *(float *) writePtr = float (h);
  1070. readPtr += sizeof (half);
  1071. writePtr += sampleStride;
  1072. }
  1073. }else{
  1074. readPtr+=sizeof(half)*count;
  1075. }
  1076. }
  1077. break;
  1078. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  1079. for (int x = minX; x <= maxX; x++)
  1080. {
  1081. char* writePtr = *(char **)(base+(y-yOffsetForData)*yPointerStride + (x-xOffsetForData)*xPointerStride);
  1082. int count = sampleCount(sampleCountBase,
  1083. sampleCountXStride,
  1084. sampleCountYStride,
  1085. x - xOffsetForSampleCount,
  1086. y - yOffsetForSampleCount);
  1087. if(writePtr)
  1088. {
  1089. for (int i = 0; i < count; i++)
  1090. {
  1091. for (size_t i = 0; i < sizeof (float); ++i)
  1092. writePtr[i] = readPtr[i];
  1093. readPtr += sizeof (float);
  1094. writePtr += sampleStride;
  1095. }
  1096. }else{
  1097. readPtr+=sizeof(float)*count;
  1098. }
  1099. }
  1100. break;
  1101. default:
  1102. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1103. }
  1104. break;
  1105. default:
  1106. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1107. }
  1108. }
  1109. }
  1110. void
  1111. skipChannel (const char *& readPtr,
  1112. PixelType typeInFile,
  1113. size_t xSize)
  1114. {
  1115. switch (typeInFile)
  1116. {
  1117. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  1118. Xdr::skip <CharPtrIO> (readPtr, Xdr::size <unsigned int> () * xSize);
  1119. break;
  1120. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  1121. Xdr::skip <CharPtrIO> (readPtr, Xdr::size <half> () * xSize);
  1122. break;
  1123. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  1124. Xdr::skip <CharPtrIO> (readPtr, Xdr::size <float> () * xSize);
  1125. break;
  1126. default:
  1127. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1128. }
  1129. }
  1130. void
  1131. convertInPlace (char *& writePtr,
  1132. const char *& readPtr,
  1133. PixelType type,
  1134. size_t numPixels)
  1135. {
  1136. switch (type)
  1137. {
  1138. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  1139. for (size_t j = 0; j < numPixels; ++j)
  1140. {
  1141. Xdr::write <CharPtrIO> (writePtr, *(const unsigned int *) readPtr);
  1142. readPtr += sizeof(unsigned int);
  1143. }
  1144. break;
  1145. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  1146. for (size_t j = 0; j < numPixels; ++j)
  1147. {
  1148. Xdr::write <CharPtrIO> (writePtr, *(const half *) readPtr);
  1149. readPtr += sizeof(half);
  1150. }
  1151. break;
  1152. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  1153. for (size_t j = 0; j < numPixels; ++j)
  1154. {
  1155. Xdr::write <CharPtrIO> (writePtr, *(const float *) readPtr);
  1156. readPtr += sizeof(float);
  1157. }
  1158. break;
  1159. default:
  1160. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1161. }
  1162. }
  1163. void
  1164. copyFromFrameBuffer (char *& writePtr,
  1165. const char *& readPtr,
  1166. const char * endPtr,
  1167. size_t xStride,
  1168. Compressor::Format format,
  1169. PixelType type)
  1170. {
  1171. //
  1172. // Copy a horizontal row of pixels from a frame
  1173. // buffer to an output file's line or tile buffer.
  1174. //
  1175. if (format == Compressor::XDR)
  1176. {
  1177. //
  1178. // The the line or tile buffer is in XDR format.
  1179. //
  1180. switch (type)
  1181. {
  1182. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  1183. while (readPtr <= endPtr)
  1184. {
  1185. Xdr::write <CharPtrIO> (writePtr,
  1186. *(const unsigned int *) readPtr);
  1187. readPtr += xStride;
  1188. }
  1189. break;
  1190. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  1191. while (readPtr <= endPtr)
  1192. {
  1193. Xdr::write <CharPtrIO> (writePtr, *(const half *) readPtr);
  1194. readPtr += xStride;
  1195. }
  1196. break;
  1197. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  1198. while (readPtr <= endPtr)
  1199. {
  1200. Xdr::write <CharPtrIO> (writePtr, *(const float *) readPtr);
  1201. readPtr += xStride;
  1202. }
  1203. break;
  1204. default:
  1205. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1206. }
  1207. }
  1208. else
  1209. {
  1210. //
  1211. // The the line or tile buffer is in NATIVE format.
  1212. //
  1213. switch (type)
  1214. {
  1215. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  1216. while (readPtr <= endPtr)
  1217. {
  1218. for (size_t i = 0; i < sizeof (unsigned int); ++i)
  1219. *writePtr++ = readPtr[i];
  1220. readPtr += xStride;
  1221. }
  1222. break;
  1223. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  1224. while (readPtr <= endPtr)
  1225. {
  1226. *(half *) writePtr = *(const half *) readPtr;
  1227. writePtr += sizeof (half);
  1228. readPtr += xStride;
  1229. }
  1230. break;
  1231. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  1232. while (readPtr <= endPtr)
  1233. {
  1234. for (size_t i = 0; i < sizeof (float); ++i)
  1235. *writePtr++ = readPtr[i];
  1236. readPtr += xStride;
  1237. }
  1238. break;
  1239. default:
  1240. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1241. }
  1242. }
  1243. }
  1244. void
  1245. copyFromDeepFrameBuffer (char *& writePtr,
  1246. const char * base,
  1247. char* sampleCountBase,
  1248. ptrdiff_t sampleCountXStride,
  1249. ptrdiff_t sampleCountYStride,
  1250. int y, int xMin, int xMax,
  1251. int xOffsetForSampleCount,
  1252. int yOffsetForSampleCount,
  1253. int xOffsetForData,
  1254. int yOffsetForData,
  1255. ptrdiff_t sampleStride,
  1256. ptrdiff_t dataXStride,
  1257. ptrdiff_t dataYStride,
  1258. Compressor::Format format,
  1259. PixelType type)
  1260. {
  1261. //
  1262. // Copy a horizontal row of pixels from a frame
  1263. // buffer to an output file's line or tile buffer.
  1264. //
  1265. if (format == Compressor::XDR)
  1266. {
  1267. //
  1268. // The the line or tile buffer is in XDR format.
  1269. //
  1270. switch (type)
  1271. {
  1272. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  1273. for (int x = xMin; x <= xMax; x++)
  1274. {
  1275. unsigned int count =
  1276. sampleCount(sampleCountBase,
  1277. sampleCountXStride,
  1278. sampleCountYStride,
  1279. x - xOffsetForSampleCount,
  1280. y - yOffsetForSampleCount);
  1281. const char* ptr = base + (y-yOffsetForData) * dataYStride + (x-xOffsetForData) * dataXStride;
  1282. const char* readPtr = ((const char**) ptr)[0];
  1283. for (unsigned int i = 0; i < count; i++)
  1284. {
  1285. Xdr::write <CharPtrIO> (writePtr,
  1286. *(const unsigned int *) readPtr);
  1287. readPtr += sampleStride;
  1288. }
  1289. }
  1290. break;
  1291. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  1292. for (int x = xMin; x <= xMax; x++)
  1293. {
  1294. unsigned int count =
  1295. sampleCount(sampleCountBase,
  1296. sampleCountXStride,
  1297. sampleCountYStride,
  1298. x - xOffsetForSampleCount,
  1299. y - yOffsetForSampleCount);
  1300. const char* ptr = base + (y-yOffsetForData) * dataYStride + (x-xOffsetForData) * dataXStride;
  1301. const char* readPtr = ((const char**) ptr)[0];
  1302. for (unsigned int i = 0; i < count; i++)
  1303. {
  1304. Xdr::write <CharPtrIO> (writePtr, *(const half *) readPtr);
  1305. readPtr += sampleStride;
  1306. }
  1307. }
  1308. break;
  1309. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  1310. for (int x = xMin; x <= xMax; x++)
  1311. {
  1312. unsigned int count =
  1313. sampleCount(sampleCountBase,
  1314. sampleCountXStride,
  1315. sampleCountYStride,
  1316. x - xOffsetForSampleCount,
  1317. y - yOffsetForSampleCount);
  1318. const char* ptr = base + (y-yOffsetForData) * dataYStride + (x-xOffsetForData) * dataXStride;
  1319. const char* readPtr = ((const char**) ptr)[0];
  1320. for (unsigned int i = 0; i < count; i++)
  1321. {
  1322. Xdr::write <CharPtrIO> (writePtr, *(const float *) readPtr);
  1323. readPtr += sampleStride;
  1324. }
  1325. }
  1326. break;
  1327. default:
  1328. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1329. }
  1330. }
  1331. else
  1332. {
  1333. //
  1334. // The the line or tile buffer is in NATIVE format.
  1335. //
  1336. switch (type)
  1337. {
  1338. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  1339. for (int x = xMin; x <= xMax; x++)
  1340. {
  1341. unsigned int count =
  1342. sampleCount(sampleCountBase,
  1343. sampleCountXStride,
  1344. sampleCountYStride,
  1345. x - xOffsetForSampleCount,
  1346. y - yOffsetForSampleCount);
  1347. const char* ptr = base + (y-yOffsetForData) * dataYStride + (x-xOffsetForData) * dataXStride;
  1348. const char* readPtr = ((const char**) ptr)[0];
  1349. for (unsigned int i = 0; i < count; i++)
  1350. {
  1351. for (size_t j = 0; j < sizeof (unsigned int); ++j)
  1352. *writePtr++ = readPtr[j];
  1353. readPtr += sampleStride;
  1354. }
  1355. }
  1356. break;
  1357. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  1358. for (int x = xMin; x <= xMax; x++)
  1359. {
  1360. unsigned int count =
  1361. sampleCount(sampleCountBase,
  1362. sampleCountXStride,
  1363. sampleCountYStride,
  1364. x - xOffsetForSampleCount,
  1365. y - yOffsetForSampleCount);
  1366. const char* ptr = base + (y-yOffsetForData) * dataYStride + (x-xOffsetForData) * dataXStride;
  1367. const char* readPtr = ((const char**) ptr)[0];
  1368. for (unsigned int i = 0; i < count; i++)
  1369. {
  1370. *(half *) writePtr = *(const half *) readPtr;
  1371. writePtr += sizeof (half);
  1372. readPtr += sampleStride;
  1373. }
  1374. }
  1375. break;
  1376. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  1377. for (int x = xMin; x <= xMax; x++)
  1378. {
  1379. unsigned int count =
  1380. sampleCount(sampleCountBase,
  1381. sampleCountXStride,
  1382. sampleCountYStride,
  1383. x - xOffsetForSampleCount,
  1384. y - yOffsetForSampleCount);
  1385. const char* ptr = base + (y-yOffsetForData) * dataYStride + (x-xOffsetForData) * dataXStride;
  1386. const char* readPtr = ((const char**) ptr)[0];
  1387. for (unsigned int i = 0; i < count; i++)
  1388. {
  1389. for (size_t j = 0; j < sizeof (float); ++j)
  1390. *writePtr++ = readPtr[j];
  1391. readPtr += sampleStride;
  1392. }
  1393. }
  1394. break;
  1395. default:
  1396. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1397. }
  1398. }
  1399. }
  1400. void
  1401. fillChannelWithZeroes (char *& writePtr,
  1402. Compressor::Format format,
  1403. PixelType type,
  1404. size_t xSize)
  1405. {
  1406. if (format == Compressor::XDR)
  1407. {
  1408. //
  1409. // Fill with data in XDR format.
  1410. //
  1411. switch (type)
  1412. {
  1413. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  1414. for (size_t j = 0; j < xSize; ++j)
  1415. Xdr::write <CharPtrIO> (writePtr, (unsigned int) 0);
  1416. break;
  1417. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  1418. for (size_t j = 0; j < xSize; ++j)
  1419. Xdr::write <CharPtrIO> (writePtr, (half) 0);
  1420. break;
  1421. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  1422. for (size_t j = 0; j < xSize; ++j)
  1423. Xdr::write <CharPtrIO> (writePtr, (float) 0);
  1424. break;
  1425. default:
  1426. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1427. }
  1428. }
  1429. else
  1430. {
  1431. //
  1432. // Fill with data in NATIVE format.
  1433. //
  1434. switch (type)
  1435. {
  1436. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  1437. for (size_t j = 0; j < xSize; ++j)
  1438. {
  1439. static const unsigned int ui = 0;
  1440. for (size_t i = 0; i < sizeof (ui); ++i)
  1441. *writePtr++ = ((char *) &ui)[i];
  1442. }
  1443. break;
  1444. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  1445. for (size_t j = 0; j < xSize; ++j)
  1446. {
  1447. *(half *) writePtr = half (0);
  1448. writePtr += sizeof (half);
  1449. }
  1450. break;
  1451. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  1452. for (size_t j = 0; j < xSize; ++j)
  1453. {
  1454. static const float f = 0;
  1455. for (size_t i = 0; i < sizeof (f); ++i)
  1456. *writePtr++ = ((char *) &f)[i];
  1457. }
  1458. break;
  1459. default:
  1460. throw IEX_NAMESPACE::ArgExc ("Unknown pixel data type.");
  1461. }
  1462. }
  1463. }
  1464. bool
  1465. usesLongNames (const Header &header)
  1466. {
  1467. //
  1468. // If an OpenEXR file contains any attribute names, attribute type names
  1469. // or channel names longer than 31 characters, then the file cannot be
  1470. // read by older versions of the IlmImf library (up to OpenEXR 1.6.1).
  1471. // Before writing the file header, we check if the header contains
  1472. // any names longer than 31 characters; if it does, then we set the
  1473. // LONG_NAMES_FLAG in the file version number. Older versions of the
  1474. // IlmImf library will refuse to read files that have the LONG_NAMES_FLAG
  1475. // set. Without the flag, older versions of the library would mis-
  1476. // interpret the file as broken.
  1477. //
  1478. for (Header::ConstIterator i = header.begin();
  1479. i != header.end();
  1480. ++i)
  1481. {
  1482. if (strlen (i.name()) >= 32 || strlen (i.attribute().typeName()) >= 32)
  1483. return true;
  1484. }
  1485. const ChannelList &channels = header.channels();
  1486. for (ChannelList::ConstIterator i = channels.begin();
  1487. i != channels.end();
  1488. ++i)
  1489. {
  1490. if (strlen (i.name()) >= 32)
  1491. return true;
  1492. }
  1493. return false;
  1494. }
  1495. int
  1496. getScanlineChunkOffsetTableSize(const Header& header)
  1497. {
  1498. const Box2i &dataWindow = header.dataWindow();
  1499. vector<size_t> bytesPerLine;
  1500. size_t maxBytesPerLine = bytesPerLineTable (header,
  1501. bytesPerLine);
  1502. Compressor* compressor = newCompressor(header.compression(),
  1503. maxBytesPerLine,
  1504. header);
  1505. int linesInBuffer = numLinesInBuffer (compressor);
  1506. int lineOffsetSize = (dataWindow.max.y - dataWindow.min.y +
  1507. linesInBuffer) / linesInBuffer;
  1508. delete compressor;
  1509. return lineOffsetSize;
  1510. }
  1511. //
  1512. // Located in ImfTiledMisc.cpp
  1513. //
  1514. int
  1515. getTiledChunkOffsetTableSize(const Header& header);
  1516. int
  1517. getChunkOffsetTableSize(const Header& header,bool ignore_attribute)
  1518. {
  1519. if(!ignore_attribute && header.hasChunkCount())
  1520. {
  1521. return header.chunkCount();
  1522. }
  1523. if(header.hasType() && !isSupportedType(header.type()))
  1524. {
  1525. throw IEX_NAMESPACE::ArgExc ("unsupported header type to "
  1526. "get chunk offset table size");
  1527. }
  1528. if (isTiled(header.type()) == false)
  1529. return getScanlineChunkOffsetTableSize(header);
  1530. else
  1531. return getTiledChunkOffsetTableSize(header);
  1532. }
  1533. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT