ImfDeepCompositing.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "ImfDeepCompositing.h"
  34. #include "ImfNamespace.h"
  35. #include <algorithm>
  36. #include <vector>
  37. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  38. using std::sort;
  39. using std::vector;
  40. DeepCompositing::DeepCompositing()
  41. {
  42. }
  43. DeepCompositing::~DeepCompositing()
  44. {
  45. }
  46. void
  47. DeepCompositing::composite_pixel (float outputs[],
  48. const float* inputs[],
  49. const char*channel_names[],
  50. int num_channels,
  51. int num_samples,
  52. int sources)
  53. {
  54. for(int i=0;i<num_channels;i++) outputs[i]=0.0;
  55. // no samples? do nothing
  56. if(num_samples==0)
  57. {
  58. return;
  59. }
  60. vector<int> sort_order;
  61. if(sources>1)
  62. {
  63. sort_order.resize(num_samples);
  64. for(int i=0;i<num_samples;i++) sort_order[i]=i;
  65. sort(&sort_order[0],inputs,channel_names,num_channels,num_samples,sources);
  66. }
  67. for(int i=0;i<num_samples;i++)
  68. {
  69. int s=(sources>1) ? sort_order[i] : i;
  70. float alpha=outputs[2];
  71. if(alpha>=1.0) return;
  72. for(int c=0;c<num_channels;c++)
  73. {
  74. outputs[c]+=(1.0-alpha)*inputs[c][s];
  75. }
  76. }
  77. }
  78. struct sort_helper
  79. {
  80. const float ** inputs;
  81. bool operator() (int a,int b)
  82. {
  83. if(inputs[0][a] < inputs[0][b]) return true;
  84. if(inputs[0][a] > inputs[0][b]) return false;
  85. if(inputs[1][a] < inputs[1][b]) return true;
  86. if(inputs[1][a] > inputs[1][b]) return false;
  87. return a<b;
  88. }
  89. sort_helper(const float ** i) : inputs(i) {}
  90. };
  91. void
  92. DeepCompositing::sort(int order[], const float* inputs[], const char* channel_names[], int num_channels, int num_samples, int sources)
  93. {
  94. std::sort(order+0,order+num_samples,sort_helper(inputs));
  95. }
  96. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT