PageRenderTime 72ms CodeModel.GetById 19ms app.highlight 47ms RepoModel.GetById 1ms app.codeStats 0ms

/src/core/ColorSpace.cpp

http://github.com/imageworks/OpenColorIO
C++ | 270 lines | 195 code | 47 blank | 28 comment | 17 complexity | 53c85540876cccbd40f6f0029a3435d3 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 <cstring>
 30#include <sstream>
 31#include <vector>
 32
 33#include <OpenColorIO/OpenColorIO.h>
 34
 35OCIO_NAMESPACE_ENTER
 36{
 37    ColorSpaceRcPtr ColorSpace::Create()
 38    {
 39        return ColorSpaceRcPtr(new ColorSpace(), &deleter);
 40    }
 41    
 42    void ColorSpace::deleter(ColorSpace* c)
 43    {
 44        delete c;
 45    }
 46    
 47    
 48    class ColorSpace::Impl
 49    {
 50    public:
 51        std::string name_;
 52        std::string family_;
 53        std::string equalityGroup_;
 54        std::string description_;
 55        
 56        BitDepth bitDepth_;
 57        bool isData_;
 58        
 59        Allocation allocation_;
 60        std::vector<float> allocationVars_;
 61        
 62        TransformRcPtr toRefTransform_;
 63        TransformRcPtr fromRefTransform_;
 64        
 65        bool toRefSpecified_;
 66        bool fromRefSpecified_;
 67        
 68        Impl() :
 69            bitDepth_(BIT_DEPTH_UNKNOWN),
 70            isData_(false),
 71            allocation_(ALLOCATION_UNIFORM),
 72            toRefSpecified_(false),
 73            fromRefSpecified_(false)
 74        { }
 75        
 76        ~Impl()
 77        { }
 78        
 79        Impl& operator= (const Impl & rhs)
 80        {
 81            name_ = rhs.name_;
 82            family_ = rhs.family_;
 83            equalityGroup_ = rhs.equalityGroup_;
 84            description_ = rhs.description_;
 85            bitDepth_ = rhs.bitDepth_;
 86            isData_ = rhs.isData_;
 87            allocation_ = rhs.allocation_;
 88            allocationVars_ = rhs.allocationVars_;
 89            
 90            toRefTransform_ = rhs.toRefTransform_;
 91            if(toRefTransform_) toRefTransform_ = toRefTransform_->createEditableCopy();
 92            
 93            fromRefTransform_ = rhs.fromRefTransform_;
 94            if(fromRefTransform_) fromRefTransform_ = fromRefTransform_->createEditableCopy();
 95            
 96            toRefSpecified_ = rhs.toRefSpecified_;
 97            fromRefSpecified_ = rhs.fromRefSpecified_;
 98            return *this;
 99        }
100    };
101    
102    
103    ///////////////////////////////////////////////////////////////////////////
104    
105    
106    
107    ColorSpace::ColorSpace()
108    : m_impl(new ColorSpace::Impl)
109    {
110    }
111    
112    ColorSpace::~ColorSpace()
113    {
114        delete m_impl;
115        m_impl = NULL;
116    }
117    
118    ColorSpaceRcPtr ColorSpace::createEditableCopy() const
119    {
120        ColorSpaceRcPtr cs = ColorSpace::Create();
121        *cs->m_impl = *m_impl;
122        return cs;
123    }
124    
125    const char * ColorSpace::getName() const
126    {
127        return getImpl()->name_.c_str();
128    }
129    
130    void ColorSpace::setName(const char * name)
131    {
132        getImpl()->name_ = name;
133    }
134    const char * ColorSpace::getFamily() const
135    {
136        return getImpl()->family_.c_str();
137    }
138    
139    void ColorSpace::setFamily(const char * family)
140    {
141        getImpl()->family_ = family;
142    }
143    
144    const char * ColorSpace::getEqualityGroup() const
145    {
146        return getImpl()->equalityGroup_.c_str();
147    }
148    
149    void ColorSpace::setEqualityGroup(const char * equalityGroup)
150    {
151        getImpl()->equalityGroup_ = equalityGroup;
152    }
153    
154    const char * ColorSpace::getDescription() const
155    {
156        return getImpl()->description_.c_str();
157    }
158    
159    void ColorSpace::setDescription(const char * description)
160    {
161        getImpl()->description_ = description;
162    }
163    
164    BitDepth ColorSpace::getBitDepth() const
165    {
166        return getImpl()->bitDepth_;
167    }
168    
169    void ColorSpace::setBitDepth(BitDepth bitDepth)
170    {
171        getImpl()->bitDepth_ = bitDepth;
172    }
173    
174    bool ColorSpace::isData() const
175    {
176        return getImpl()->isData_;
177    }
178    
179    void ColorSpace::setIsData(bool val)
180    {
181        getImpl()->isData_ = val;
182    }
183    
184    Allocation ColorSpace::getAllocation() const
185    {
186        return getImpl()->allocation_;
187    }
188    
189    void ColorSpace::setAllocation(Allocation allocation)
190    {
191        getImpl()->allocation_ = allocation;
192    }
193    
194    int ColorSpace::getAllocationNumVars() const
195    {
196        return static_cast<int>(getImpl()->allocationVars_.size());
197    }
198    
199    void ColorSpace::getAllocationVars(float * vars) const
200    {
201        if(!getImpl()->allocationVars_.empty())
202        {
203            memcpy(vars,
204                &getImpl()->allocationVars_[0],
205                getImpl()->allocationVars_.size()*sizeof(float));
206        }
207    }
208    
209    void ColorSpace::setAllocationVars(int numvars, const float * vars)
210    {
211        getImpl()->allocationVars_.resize(numvars);
212        
213        if(!getImpl()->allocationVars_.empty())
214        {
215            memcpy(&getImpl()->allocationVars_[0],
216                vars,
217                numvars*sizeof(float));
218        }
219    }
220    
221    ConstTransformRcPtr ColorSpace::getTransform(ColorSpaceDirection dir) const
222    {
223        if(dir == COLORSPACE_DIR_TO_REFERENCE)
224            return getImpl()->toRefTransform_;
225        else if(dir == COLORSPACE_DIR_FROM_REFERENCE)
226            return getImpl()->fromRefTransform_;
227        
228        throw Exception("Unspecified ColorSpaceDirection");
229    }
230    
231    void ColorSpace::setTransform(const ConstTransformRcPtr & transform,
232                                  ColorSpaceDirection dir)
233    {
234        TransformRcPtr transformCopy;
235        if(transform) transformCopy = transform->createEditableCopy();
236        
237        if(dir == COLORSPACE_DIR_TO_REFERENCE)
238            getImpl()->toRefTransform_ = transformCopy;
239        else if(dir == COLORSPACE_DIR_FROM_REFERENCE)
240            getImpl()->fromRefTransform_ = transformCopy;
241        else
242            throw Exception("Unspecified ColorSpaceDirection");
243    }
244    
245    std::ostream& operator<< (std::ostream& os, const ColorSpace& cs)
246    {
247        os << "<ColorSpace ";
248        os << "name=" << cs.getName() << ", ";
249        os << "family=" << cs.getFamily() << ", ";
250        os << "equalityGroup=" << cs.getEqualityGroup() << ", ";
251        os << "bitDepth=" << BitDepthToString(cs.getBitDepth()) << ", ";
252        os << "isData=" << BoolToString(cs.isData()) << ", ";
253        os << "allocation=" << AllocationToString(cs.getAllocation()) << ", ";
254        os << ">\n";
255        
256        if(cs.getTransform(COLORSPACE_DIR_TO_REFERENCE))
257        {
258            os << "\t" << cs.getName() << " --> Reference\n";
259            os << cs.getTransform(COLORSPACE_DIR_TO_REFERENCE);
260        }
261        
262        if(cs.getTransform(COLORSPACE_DIR_FROM_REFERENCE))
263        {
264            os << "\tReference --> " << cs.getName() << "\n";
265            os << cs.getTransform(COLORSPACE_DIR_FROM_REFERENCE);
266        }
267        return os;
268    }
269}
270OCIO_NAMESPACE_EXIT