ImfCompositeDeepScanLine.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2012, Weta Digital Ltd
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Weta Digital nor the names of
  17. // its contributors may be used to endorse or promote products derived
  18. // from this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. //
  32. ///////////////////////////////////////////////////////////////////////////
  33. #include "ImfCompositeDeepScanLine.h"
  34. #include "ImfDeepScanLineInputPart.h"
  35. #include "ImfDeepScanLineInputFile.h"
  36. #include "ImfChannelList.h"
  37. #include "ImfFrameBuffer.h"
  38. #include "ImfDeepFrameBuffer.h"
  39. #include "ImfDeepCompositing.h"
  40. #include "ImfPixelType.h"
  41. #include "IlmThreadPool.h"
  42. #include <Iex.h>
  43. #include <vector>
  44. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  45. using std::vector;
  46. using std::string;
  47. using IMATH_NAMESPACE::Box2i;
  48. using ILMTHREAD_NAMESPACE::Task;
  49. using ILMTHREAD_NAMESPACE::TaskGroup;
  50. using ILMTHREAD_NAMESPACE::ThreadPool;
  51. struct CompositeDeepScanLine::Data{
  52. public :
  53. vector<DeepScanLineInputFile *> _file; // array of files
  54. vector<DeepScanLineInputPart *> _part; // array of parts
  55. FrameBuffer _outputFrameBuffer; // output frame buffer provided
  56. bool _zback; // true if we are using zback (otherwise channel 1 = channel 0)
  57. vector< vector<float> > _channeldata; // pixel values, read from the input, one array per channel
  58. vector< int > _sampleCounts; // total per-pixel sample counts,
  59. Box2i _dataWindow; // data window of combined inputs
  60. DeepCompositing * _comp; // user-provided compositor
  61. vector<string> _channels; // names of channels that will be composited
  62. vector<int> _bufferMap; // entry _outputFrameBuffer[n].name() == _channels[ _bufferMap[n] ].name()
  63. void check_valid(const Header & header); // check newly added part/file is OK; on first good call, set _zback/_dataWindow
  64. //
  65. // set up the given deep frame buffer to contain the required channels
  66. // resize counts and pointers to the width of _dataWindow
  67. // zero-out all counts, since the datawindow may be smaller than/not include this part
  68. //
  69. void handleDeepFrameBuffer (DeepFrameBuffer & buf,
  70. vector<unsigned int> & counts, //per-pixel counts
  71. vector< vector<float *> > & pointers, //per-channel-per-pixel pointers to data
  72. const Header & header,
  73. int start,
  74. int end);
  75. Data();
  76. };
  77. CompositeDeepScanLine::Data::Data() : _zback(false) , _comp(NULL) {}
  78. CompositeDeepScanLine::CompositeDeepScanLine() : _Data(new Data) {}
  79. CompositeDeepScanLine::~CompositeDeepScanLine()
  80. {
  81. delete _Data;
  82. }
  83. void
  84. CompositeDeepScanLine::addSource(DeepScanLineInputPart* part)
  85. {
  86. _Data->check_valid(part->header());
  87. _Data->_part.push_back(part);
  88. }
  89. void
  90. CompositeDeepScanLine::addSource(DeepScanLineInputFile* file)
  91. {
  92. _Data->check_valid(file->header());
  93. _Data->_file.push_back(file);
  94. }
  95. int
  96. CompositeDeepScanLine::sources() const
  97. {
  98. return int(_Data->_part.size())+int(_Data->_file.size());
  99. }
  100. void
  101. CompositeDeepScanLine::Data::check_valid(const Header & header)
  102. {
  103. bool has_z=false;
  104. bool has_alpha=false;
  105. // check good channel names
  106. for( ChannelList::ConstIterator i=header.channels().begin();i!=header.channels().end();++i)
  107. {
  108. std::string n(i.name());
  109. if(n=="ZBack")
  110. {
  111. _zback=true;
  112. }
  113. else if(n=="Z")
  114. {
  115. has_z=true;
  116. }
  117. else if(n=="A")
  118. {
  119. has_alpha=true;
  120. }
  121. }
  122. if(!has_z)
  123. {
  124. throw IEX_NAMESPACE::ArgExc("Deep data provided to CompositeDeepScanLine is missing a Z channel");
  125. }
  126. if(!has_alpha)
  127. {
  128. throw IEX_NAMESPACE::ArgExc("Deep data provided to CompositeDeepScanLine is missing an alpha channel");
  129. }
  130. if(_part.size()==0 && _file.size()==0)
  131. {
  132. // first in - update and return
  133. _dataWindow = header.dataWindow();
  134. return;
  135. }
  136. const Header * const match_header = _part.size()>0 ? &_part[0]->header() : &_file[0]->header();
  137. // check the sizes match
  138. if(match_header->displayWindow() != header.displayWindow())
  139. {
  140. throw IEX_NAMESPACE::ArgExc("Deep data provided to CompositeDeepScanLine has a different displayWindow to previously provided data");
  141. }
  142. _dataWindow.extendBy(header.dataWindow());
  143. }
  144. void
  145. CompositeDeepScanLine::Data::handleDeepFrameBuffer (DeepFrameBuffer& buf,
  146. std::vector< unsigned int > & counts,
  147. vector< std::vector< float* > > & pointers,
  148. const Header& header,
  149. int start,
  150. int end)
  151. {
  152. int width=_dataWindow.size().x+1;
  153. size_t pixelcount = width * (end-start+1);
  154. pointers.resize(_channels.size());
  155. counts.resize(pixelcount);
  156. buf.insertSampleCountSlice (Slice (OPENEXR_IMF_INTERNAL_NAMESPACE::UINT,
  157. (char *) (&counts[0]-_dataWindow.min.x-start*width),
  158. sizeof(unsigned int),
  159. sizeof(unsigned int)*width));
  160. pointers[0].resize(pixelcount);
  161. buf.insert ("Z", DeepSlice (OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT,
  162. (char *)(&pointers[0][0]-_dataWindow.min.x-start*width),
  163. sizeof(float *),
  164. sizeof(float *)*width,
  165. sizeof(float) ));
  166. if(_zback)
  167. {
  168. pointers[1].resize(pixelcount);
  169. buf.insert ("ZBack", DeepSlice (OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT,
  170. (char *)(&pointers[1][0]-_dataWindow.min.x-start*width),
  171. sizeof(float *),
  172. sizeof(float *)*width,
  173. sizeof(float) ));
  174. }
  175. pointers[2].resize(pixelcount);
  176. buf.insert ("A", DeepSlice (OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT,
  177. (char *)(&pointers[2][0]-_dataWindow.min.x-start*width),
  178. sizeof(float *),
  179. sizeof(float *)*width,
  180. sizeof(float) ));
  181. size_t i =0;
  182. for(FrameBuffer::ConstIterator qt = _outputFrameBuffer.begin();
  183. qt != _outputFrameBuffer.end();
  184. qt++)
  185. {
  186. int channel_in_source = _bufferMap[i];
  187. if(channel_in_source>2)
  188. {
  189. // not dealt with yet (0,1,2 previously inserted)
  190. pointers[channel_in_source].resize(pixelcount);
  191. buf.insert (qt.name(),
  192. DeepSlice (OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT,
  193. (char *)(&pointers[channel_in_source][0]-_dataWindow.min.x-start*width),
  194. sizeof(float *),
  195. sizeof(float *)*width,
  196. sizeof(float) ));
  197. }
  198. i++;
  199. }
  200. }
  201. void
  202. CompositeDeepScanLine::setCompositing(DeepCompositing* c)
  203. {
  204. _Data->_comp=c;
  205. }
  206. const IMATH_NAMESPACE::Box2i& CompositeDeepScanLine::dataWindow() const
  207. {
  208. return _Data->_dataWindow;
  209. }
  210. void
  211. CompositeDeepScanLine::setFrameBuffer(const FrameBuffer& fr)
  212. {
  213. //
  214. // count channels; build map between channels in frame buffer
  215. // and channels in internal buffers
  216. //
  217. _Data->_channels.resize(3);
  218. _Data->_channels[0]="Z";
  219. _Data->_channels[1]=_Data->_zback ? "ZBack" : "Z";
  220. _Data->_channels[2]="A";
  221. _Data->_bufferMap.resize(0);
  222. for(FrameBuffer::ConstIterator q=fr.begin();q!=fr.end();q++)
  223. {
  224. string name(q.name());
  225. if(name=="ZBack")
  226. {
  227. _Data->_bufferMap.push_back(1);
  228. }else if(name=="Z")
  229. {
  230. _Data->_bufferMap.push_back(0);
  231. }else if(name=="A")
  232. {
  233. _Data->_bufferMap.push_back(2);
  234. }else{
  235. _Data->_bufferMap.push_back(_Data->_channels.size());
  236. _Data->_channels.push_back(name);
  237. }
  238. }
  239. _Data->_outputFrameBuffer=fr;
  240. }
  241. namespace
  242. {
  243. class LineCompositeTask : public Task
  244. {
  245. public:
  246. LineCompositeTask ( TaskGroup* group ,
  247. CompositeDeepScanLine::Data * data,
  248. int y,
  249. int start,
  250. vector<const char*>* names,
  251. vector<vector< vector<float *> > >* pointers,
  252. vector<unsigned int>* total_sizes,
  253. vector<unsigned int>* num_sources
  254. ) : Task(group) ,
  255. _Data(data),
  256. _y(y),
  257. _start(start),
  258. _names(names),
  259. _pointers(pointers),
  260. _total_sizes(total_sizes),
  261. _num_sources(num_sources)
  262. {}
  263. virtual ~LineCompositeTask () {}
  264. virtual void execute ();
  265. CompositeDeepScanLine::Data* _Data;
  266. int _y;
  267. int _start;
  268. vector<const char *>* _names;
  269. vector<vector< vector<float *> > >* _pointers;
  270. vector<unsigned int>* _total_sizes;
  271. vector<unsigned int>* _num_sources;
  272. };
  273. void
  274. composite_line(int y,
  275. int start,
  276. CompositeDeepScanLine::Data * _Data,
  277. vector<const char *> & names,
  278. const vector<vector< vector<float *> > > & pointers,
  279. const vector<unsigned int> & total_sizes,
  280. const vector<unsigned int> & num_sources
  281. )
  282. {
  283. vector<float> output_pixel(names.size()); //the pixel we'll output to
  284. vector<const float *> inputs(names.size());
  285. DeepCompositing d; // fallback compositing engine
  286. DeepCompositing * comp= _Data->_comp ? _Data->_comp : &d;
  287. int pixel = (y-start)*(_Data->_dataWindow.max.x+1-_Data->_dataWindow.min.x);
  288. for(int x=_Data->_dataWindow.min.x;x<=_Data->_dataWindow.max.x;x++)
  289. {
  290. // set inputs[] to point to the first sample of the first part of each channel
  291. // if there's a zback, set all channel independently...
  292. if(_Data->_zback)
  293. {
  294. for(size_t channel=0;channel<names.size();channel++)
  295. {
  296. inputs[channel]=pointers[0][channel][pixel];
  297. }
  298. }else{
  299. // otherwise, set 0 and 1 to point to Z
  300. inputs[0]=pointers[0][0][pixel];
  301. inputs[1]=pointers[0][0][pixel];
  302. for(size_t channel=2;channel<names.size();channel++)
  303. {
  304. inputs[channel]=pointers[0][channel][pixel];
  305. }
  306. }
  307. comp->composite_pixel(&output_pixel[0],
  308. &inputs[0],
  309. &names[0],
  310. names.size(),
  311. total_sizes[pixel],
  312. num_sources[pixel]
  313. );
  314. size_t channel_number=0;
  315. //
  316. // write out composited value into internal frame buffer
  317. //
  318. for(FrameBuffer::Iterator it = _Data->_outputFrameBuffer.begin();it !=_Data->_outputFrameBuffer.end();it++)
  319. {
  320. float value = output_pixel[ _Data->_bufferMap[channel_number] ]; // value to write
  321. // cast to half float if necessary
  322. if(it.slice().type==OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT)
  323. {
  324. * (float *)(it.slice().base + y*it.slice().yStride + x*it.slice().xStride) = value;
  325. }
  326. else if(it.slice().type==HALF)
  327. {
  328. * (half *)(it.slice().base + y*it.slice().yStride + x*it.slice().xStride) = half(value);
  329. }
  330. channel_number++;
  331. }
  332. pixel++;
  333. }// next pixel on row
  334. }
  335. void LineCompositeTask::execute()
  336. {
  337. composite_line(_y,_start,_Data,*_names,*_pointers,*_total_sizes,*_num_sources);
  338. }
  339. }
  340. void
  341. CompositeDeepScanLine::readPixels(int start, int end)
  342. {
  343. size_t parts = _Data->_file.size() + _Data->_part.size(); // total of files+parts
  344. vector<DeepFrameBuffer> framebuffers(parts);
  345. vector< vector<unsigned int> > counts(parts);
  346. //
  347. // for each part, a pointer to an array of channels
  348. //
  349. vector<vector< vector<float *> > > pointers(parts);
  350. vector<const Header *> headers(parts);
  351. {
  352. size_t i;
  353. for(i=0;i<_Data->_file.size();i++)
  354. {
  355. headers[i] = &_Data->_file[i]->header();
  356. }
  357. for(size_t j=0;j<_Data->_part.size();j++)
  358. {
  359. headers[i+j] = &_Data->_part[j]->header();
  360. }
  361. }
  362. for(size_t i=0;i<parts;i++)
  363. {
  364. _Data->handleDeepFrameBuffer(framebuffers[i],counts[i],pointers[i],*headers[i],start,end);
  365. }
  366. //
  367. // set frame buffers and read scanlines from all parts
  368. // TODO what happens if SCANLINE not in data window?
  369. //
  370. {
  371. size_t i=0;
  372. for(i=0;i<_Data->_file.size();i++)
  373. {
  374. _Data->_file[i]->setFrameBuffer(framebuffers[i]);
  375. _Data->_file[i]->readPixelSampleCounts(start,end);
  376. }
  377. for(size_t j=0;j<_Data->_part.size();j++)
  378. {
  379. _Data->_part[j]->setFrameBuffer(framebuffers[i+j]);
  380. _Data->_part[j]->readPixelSampleCounts(start,end);
  381. }
  382. }
  383. //
  384. // total width
  385. //
  386. size_t total_width = _Data->_dataWindow.size().x+1;
  387. size_t total_pixels = total_width*(end-start+1);
  388. vector<unsigned int> total_sizes(total_pixels);
  389. vector<unsigned int> num_sources(total_pixels); //number of parts with non-zero sample count
  390. size_t overall_sample_count=0; // sum of all samples in all images between start and end
  391. //
  392. // accumulate pixel counts
  393. //
  394. for(size_t ptr=0;ptr<total_pixels;ptr++)
  395. {
  396. total_sizes[ptr]=0;
  397. num_sources[ptr]=0;
  398. for(size_t j=0;j<parts;j++)
  399. {
  400. total_sizes[ptr]+=counts[j][ptr];
  401. if(counts[j][ptr]>0) num_sources[ptr]++;
  402. }
  403. overall_sample_count+=total_sizes[ptr];
  404. }
  405. //
  406. // allocate arrays for pixel data
  407. // samples array accessed as in pixels[channel][sample]
  408. //
  409. vector<vector<float> > samples( _Data->_channels.size() );
  410. for(size_t channel=0;channel<_Data->_channels.size();channel++)
  411. {
  412. if( channel!=1 || _Data->_zback)
  413. {
  414. samples[channel].resize(overall_sample_count);
  415. }
  416. }
  417. for(size_t channel=0;channel<samples.size();channel++)
  418. {
  419. if( channel!=1 || _Data->_zback)
  420. {
  421. samples[channel].resize(overall_sample_count);
  422. //
  423. // allocate pointers for channel data
  424. //
  425. size_t offset=0;
  426. for(size_t pixel=0;pixel<total_pixels;pixel++)
  427. {
  428. for(size_t part=0 ; part<parts && offset<overall_sample_count ; part++ )
  429. {
  430. pointers[part][channel][pixel]=&samples[channel][offset];
  431. offset+=counts[part][pixel];
  432. }
  433. }
  434. }
  435. }
  436. //
  437. // read data
  438. //
  439. for(size_t i=0;i<_Data->_file.size();i++)
  440. {
  441. _Data->_file[i]->readPixels(start,end);
  442. }
  443. for(size_t j=0;j<_Data->_part.size();j++)
  444. {
  445. _Data->_part[j]->readPixels(start,end);
  446. }
  447. //
  448. // composite pixels and write back to framebuffer
  449. //
  450. // turn vector of strings into array of char *
  451. // and make sure 'ZBack' channel is correct
  452. vector<const char *> names(_Data->_channels.size());
  453. for(size_t i=0;i<names.size();i++)
  454. {
  455. names[i]=_Data->_channels[i].c_str();
  456. }
  457. if(!_Data->_zback) names[1]=names[0]; // no zback channel, so make it point to z
  458. TaskGroup g;
  459. for(int y=start;y<=end;y++)
  460. {
  461. ThreadPool::addGlobalTask(new LineCompositeTask(&g,_Data,y,start,&names,&pointers,&total_sizes,&num_sources));
  462. }//next row
  463. }
  464. const FrameBuffer&
  465. CompositeDeepScanLine::frameBuffer() const
  466. {
  467. return _Data->_outputFrameBuffer;
  468. }
  469. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT