/src/core/ScanlineHelper.cpp

http://github.com/imageworks/OpenColorIO · C++ · 123 lines · 73 code · 15 blank · 35 comment · 4 complexity · 32decc9f7589d0d556108e581e9d3b02 MD5 · raw file

  1. /*
  2. Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3. All Rights Reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Sony Pictures Imageworks nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <OpenColorIO/OpenColorIO.h>
  28. #include "ScanlineHelper.h"
  29. #include <cassert>
  30. #include <cstdlib>
  31. #include <sstream>
  32. OCIO_NAMESPACE_ENTER
  33. {
  34. namespace
  35. {
  36. const int PIXELS_PER_LINE = 4096;
  37. }
  38. ////////////////////////////////////////////////////////////////////////////
  39. ScanlineHelper::ScanlineHelper(ImageDesc& img):
  40. m_buffer(0),
  41. m_imagePixelIndex(0),
  42. m_numPixelsCopied(0),
  43. m_yIndex(0),
  44. m_inPlaceMode(false)
  45. {
  46. m_img.init(img);
  47. if(m_img.isPackedRGBA())
  48. {
  49. m_inPlaceMode = true;
  50. }
  51. else
  52. {
  53. // TODO: Re-use memory from thread-safe memory pool, rather
  54. // than doing a new allocation each time.
  55. m_buffer = (float*)malloc(sizeof(float)*PIXELS_PER_LINE*4);
  56. }
  57. }
  58. ScanlineHelper::~ScanlineHelper()
  59. {
  60. free(m_buffer);
  61. }
  62. // Copy from the src image to our scanline, in our preferred
  63. // pixel layout.
  64. void ScanlineHelper::prepRGBAScanline(float** buffer, long* numPixels)
  65. {
  66. if(m_inPlaceMode)
  67. {
  68. // TODO: what if scanline is too short, or too long?
  69. if(m_yIndex >= m_img.height)
  70. {
  71. *numPixels = 0;
  72. return;
  73. }
  74. char* rowPtr = reinterpret_cast<char*>(m_img.rData);
  75. rowPtr += m_img.yStrideBytes*m_yIndex;
  76. *buffer = reinterpret_cast<float*>(rowPtr);
  77. *numPixels = m_img.width;
  78. }
  79. else
  80. {
  81. PackRGBAFromImageDesc(m_img, m_buffer,
  82. &m_numPixelsCopied,
  83. PIXELS_PER_LINE,
  84. m_imagePixelIndex);
  85. *buffer = m_buffer;
  86. *numPixels = m_numPixelsCopied;
  87. }
  88. }
  89. // Write back the result of our work, from the scanline to our
  90. // destination image.
  91. void ScanlineHelper::finishRGBAScanline()
  92. {
  93. if(m_inPlaceMode)
  94. {
  95. m_yIndex += 1;
  96. }
  97. else
  98. {
  99. UnpackRGBAToImageDesc(m_img,
  100. m_buffer,
  101. m_numPixelsCopied,
  102. m_imagePixelIndex);
  103. m_imagePixelIndex += m_numPixelsCopied;
  104. }
  105. }
  106. }
  107. OCIO_NAMESPACE_EXIT