PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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