PageRenderTime 79ms CodeModel.GetById 27ms RepoModel.GetById 10ms app.codeStats 0ms

/OpenEXR/IlmImf/ImfCompositeDeepScanLine.cpp

https://github.com/meshula/openexr
C++ | 590 lines | 388 code | 122 blank | 80 comment | 49 complexity | 4f6b311a988774b39d0130a93fc9f4c0 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause
  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, std::vector< unsigned int > & counts, vector< std::vector< float* > > & pointers, const Header& header, int start, int end)
  146. {
  147. int width=_dataWindow.size().x+1;
  148. size_t pixelcount = width * (end-start+1);
  149. pointers.resize(_channels.size());
  150. counts.resize(pixelcount);
  151. buf.insertSampleCountSlice(Slice(UINT,(char *) (&counts[0]-_dataWindow.min.x-start*width),
  152. sizeof(unsigned int),
  153. sizeof(unsigned int)*width
  154. )
  155. );
  156. pointers[0].resize(pixelcount);
  157. buf.insert("Z",DeepSlice(FLOAT,(char *)(&pointers[0][0]-_dataWindow.min.x-start*width),
  158. sizeof(float *),
  159. sizeof(float *)*width,
  160. sizeof(float)
  161. )
  162. )
  163. ;
  164. if(_zback)
  165. {
  166. pointers[1].resize(pixelcount);
  167. buf.insert("ZBack",DeepSlice(FLOAT,(char *)(&pointers[1][0]-_dataWindow.min.x-start*width),
  168. sizeof(float *),
  169. sizeof(float *)*width,
  170. sizeof(float)
  171. )
  172. );
  173. }
  174. pointers[2].resize(pixelcount);
  175. buf.insert("A",DeepSlice(FLOAT,(char *)(&pointers[2][0]-_dataWindow.min.x-start*width),
  176. sizeof(float *),
  177. sizeof(float *)*width,
  178. sizeof(float)
  179. )
  180. );
  181. size_t i =0;
  182. for(FrameBuffer::ConstIterator qt = _outputFrameBuffer.begin();qt !=_outputFrameBuffer.end();qt++)
  183. {
  184. int channel_in_source = _bufferMap[i];
  185. if(channel_in_source>2)
  186. {
  187. // not dealt with yet (0,1,2 previously inserted)
  188. pointers[channel_in_source].resize(pixelcount);
  189. buf.insert(qt.name(),
  190. DeepSlice(FLOAT,(char *)(&pointers[channel_in_source][0]-_dataWindow.min.x-start*width),
  191. sizeof(float *),
  192. sizeof(float *)*width,
  193. sizeof(float)
  194. )
  195. );
  196. }
  197. i++;
  198. }
  199. }
  200. void
  201. CompositeDeepScanLine::setCompositing(DeepCompositing* c)
  202. {
  203. _Data->_comp=c;
  204. }
  205. const IMATH_NAMESPACE::Box2i& CompositeDeepScanLine::dataWindow() const
  206. {
  207. return _Data->_dataWindow;
  208. }
  209. void
  210. CompositeDeepScanLine::setFrameBuffer(const FrameBuffer& fr)
  211. {
  212. //
  213. // count channels; build map between channels in frame buffer
  214. // and channels in internal buffers
  215. //
  216. _Data->_channels.resize(3);
  217. _Data->_channels[0]="Z";
  218. _Data->_channels[1]=_Data->_zback ? "ZBack" : "Z";
  219. _Data->_channels[2]="A";
  220. _Data->_bufferMap.resize(0);
  221. for(FrameBuffer::ConstIterator q=fr.begin();q!=fr.end();q++)
  222. {
  223. string name(q.name());
  224. if(name=="ZBack")
  225. {
  226. _Data->_bufferMap.push_back(1);
  227. }else if(name=="Z")
  228. {
  229. _Data->_bufferMap.push_back(0);
  230. }else if(name=="A")
  231. {
  232. _Data->_bufferMap.push_back(2);
  233. }else{
  234. _Data->_bufferMap.push_back(_Data->_channels.size());
  235. _Data->_channels.push_back(name);
  236. }
  237. }
  238. _Data->_outputFrameBuffer=fr;
  239. }
  240. namespace
  241. {
  242. class LineCompositeTask : public Task
  243. {
  244. public:
  245. LineCompositeTask ( TaskGroup* group ,
  246. CompositeDeepScanLine::Data * data,
  247. int y,
  248. int start,
  249. vector<const char*>* names,
  250. vector<vector< vector<float *> > >* pointers,
  251. vector<unsigned int>* total_sizes,
  252. vector<unsigned int>* num_sources
  253. ) : Task(group) ,
  254. _Data(data),
  255. _y(y),
  256. _start(start),
  257. _names(names),
  258. _pointers(pointers),
  259. _total_sizes(total_sizes),
  260. _num_sources(num_sources)
  261. {}
  262. virtual ~LineCompositeTask () {}
  263. virtual void execute ();
  264. CompositeDeepScanLine::Data* _Data;
  265. int _y;
  266. int _start;
  267. vector<const char *>* _names;
  268. vector<vector< vector<float *> > >* _pointers;
  269. vector<unsigned int>* _total_sizes;
  270. vector<unsigned int>* _num_sources;
  271. };
  272. void
  273. composite_line(int y,
  274. int start,
  275. CompositeDeepScanLine::Data * _Data,
  276. vector<const char *> & names,
  277. const vector<vector< vector<float *> > > & pointers,
  278. const vector<unsigned int> & total_sizes,
  279. const vector<unsigned int> & num_sources
  280. )
  281. {
  282. vector<float> output_pixel(names.size()); //the pixel we'll output to
  283. vector<const float *> inputs(names.size());
  284. DeepCompositing d; // fallback compositing engine
  285. DeepCompositing * comp= _Data->_comp ? _Data->_comp : &d;
  286. int pixel = (y-start)*(_Data->_dataWindow.max.x+1-_Data->_dataWindow.min.x);
  287. for(int x=_Data->_dataWindow.min.x;x<=_Data->_dataWindow.max.x;x++)
  288. {
  289. // set inputs[] to point to the first sample of the first part of each channel
  290. // if there's a zback, set all channel independently...
  291. if(_Data->_zback)
  292. {
  293. for(size_t channel=0;channel<names.size();channel++)
  294. {
  295. inputs[channel]=pointers[0][channel][pixel];
  296. }
  297. }else{
  298. // otherwise, set 0 and 1 to point to Z
  299. inputs[0]=pointers[0][0][pixel];
  300. inputs[1]=pointers[0][0][pixel];
  301. for(size_t channel=2;channel<names.size();channel++)
  302. {
  303. inputs[channel]=pointers[0][channel][pixel];
  304. }
  305. }
  306. comp->composite_pixel(&output_pixel[0],
  307. &inputs[0],
  308. &names[0],
  309. names.size(),
  310. total_sizes[pixel],
  311. num_sources[pixel]
  312. );
  313. size_t channel_number=0;
  314. //
  315. // write out composited value into internal frame buffer
  316. //
  317. for(FrameBuffer::Iterator it = _Data->_outputFrameBuffer.begin();it !=_Data->_outputFrameBuffer.end();it++)
  318. {
  319. float value = output_pixel[ _Data->_bufferMap[channel_number] ]; // value to write
  320. // cast to half float if necessary
  321. if(it.slice().type==FLOAT)
  322. {
  323. * (float *)(it.slice().base + y*it.slice().yStride + x*it.slice().xStride) = value;
  324. }
  325. else if(it.slice().type==HALF)
  326. {
  327. * (half *)(it.slice().base + y*it.slice().yStride + x*it.slice().xStride) = half(value);
  328. }
  329. channel_number++;
  330. }
  331. pixel++;
  332. }// next pixel on row
  333. }
  334. void LineCompositeTask::execute()
  335. {
  336. composite_line(_y,_start,_Data,*_names,*_pointers,*_total_sizes,*_num_sources);
  337. }
  338. }
  339. void
  340. CompositeDeepScanLine::readPixels(int start, int end)
  341. {
  342. size_t parts = _Data->_file.size() + _Data->_part.size(); // total of files+parts
  343. vector<DeepFrameBuffer> framebuffers(parts);
  344. vector< vector<unsigned int> > counts(parts);
  345. //
  346. // for each part, a pointer to an array of channels
  347. //
  348. vector<vector< vector<float *> > > pointers(parts);
  349. vector<const Header *> headers(parts);
  350. {
  351. size_t i;
  352. for(i=0;i<_Data->_file.size();i++)
  353. {
  354. headers[i] = &_Data->_file[i]->header();
  355. }
  356. for(size_t j=0;j<_Data->_part.size();j++)
  357. {
  358. headers[i+j] = &_Data->_part[j]->header();
  359. }
  360. }
  361. for(size_t i=0;i<parts;i++)
  362. {
  363. _Data->handleDeepFrameBuffer(framebuffers[i],counts[i],pointers[i],*headers[i],start,end);
  364. }
  365. //
  366. // set frame buffers and read scanlines from all parts
  367. // TODO what happens if SCANLINE not in data window?
  368. //
  369. {
  370. size_t i=0;
  371. for(i=0;i<_Data->_file.size();i++)
  372. {
  373. _Data->_file[i]->setFrameBuffer(framebuffers[i]);
  374. _Data->_file[i]->readPixelSampleCounts(start,end);
  375. }
  376. for(size_t j=0;j<_Data->_part.size();j++)
  377. {
  378. _Data->_part[j]->setFrameBuffer(framebuffers[i+j]);
  379. _Data->_part[j]->readPixelSampleCounts(start,end);
  380. }
  381. }
  382. //
  383. // total width
  384. //
  385. size_t total_width = _Data->_dataWindow.size().x+1;
  386. size_t total_pixels = total_width*(end-start+1);
  387. vector<unsigned int> total_sizes(total_pixels);
  388. vector<unsigned int> num_sources(total_pixels); //number of parts with non-zero sample count
  389. size_t overall_sample_count=0; // sum of all samples in all images between start and end
  390. //
  391. // accumulate pixel counts
  392. //
  393. for(size_t ptr=0;ptr<total_pixels;ptr++)
  394. {
  395. total_sizes[ptr]=0;
  396. num_sources[ptr]=0;
  397. for(size_t j=0;j<parts;j++)
  398. {
  399. total_sizes[ptr]+=counts[j][ptr];
  400. if(counts[j][ptr]>0) num_sources[ptr]++;
  401. }
  402. overall_sample_count+=total_sizes[ptr];
  403. }
  404. //
  405. // allocate arrays for pixel data
  406. // samples array accessed as in pixels[channel][sample]
  407. //
  408. vector<vector<float> > samples( _Data->_channels.size() );
  409. for(size_t channel=0;channel<_Data->_channels.size();channel++)
  410. {
  411. if( channel!=1 || _Data->_zback)
  412. {
  413. samples[channel].resize(overall_sample_count);
  414. }
  415. }
  416. for(size_t channel=0;channel<samples.size();channel++)
  417. {
  418. if( channel!=1 || _Data->_zback)
  419. {
  420. samples[channel].resize(overall_sample_count);
  421. //
  422. // allocate pointers for channel data
  423. //
  424. size_t offset=0;
  425. for(size_t pixel=0;pixel<total_pixels;pixel++)
  426. {
  427. for(size_t part=0 ; part<parts && offset<overall_sample_count ; part++ )
  428. {
  429. pointers[part][channel][pixel]=&samples[channel][offset];
  430. offset+=counts[part][pixel];
  431. }
  432. }
  433. }
  434. }
  435. //
  436. // read data
  437. //
  438. for(size_t i=0;i<_Data->_file.size();i++)
  439. {
  440. _Data->_file[i]->readPixels(start,end);
  441. }
  442. for(size_t j=0;j<_Data->_part.size();j++)
  443. {
  444. _Data->_part[j]->readPixels(start,end);
  445. }
  446. //
  447. // composite pixels and write back to framebuffer
  448. //
  449. // turn vector of strings into array of char *
  450. // and make sure 'ZBack' channel is correct
  451. vector<const char *> names(_Data->_channels.size());
  452. for(size_t i=0;i<names.size();i++)
  453. {
  454. names[i]=_Data->_channels[i].c_str();
  455. }
  456. if(!_Data->_zback) names[1]=names[0]; // no zback channel, so make it point to z
  457. TaskGroup g;
  458. for(int y=start;y<=end;y++)
  459. {
  460. ThreadPool::addGlobalTask(new LineCompositeTask(&g,_Data,y,start,&names,&pointers,&total_sizes,&num_sources));
  461. }//next row
  462. }
  463. const FrameBuffer&
  464. CompositeDeepScanLine::frameBuffer() const
  465. {
  466. return _Data->_outputFrameBuffer;
  467. }
  468. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT