PageRenderTime 64ms CodeModel.GetById 18ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 1ms

/src/core/FileFormatSpi3D.cpp

http://github.com/imageworks/OpenColorIO
C++ | 209 lines | 126 code | 37 blank | 46 comment | 9 complexity | 96ac93a46481d13a381b865bcde23bc1 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
 31#include "FileTransform.h"
 32#include "Lut3DOp.h"
 33#include "pystring/pystring.h"
 34
 35#include <cstdio>
 36#include <sstream>
 37
 38/*
 39SPILUT 1.0
 403 3
 4132 32 32
 420 0 0 0.0132509 0.0158522 0.0156622
 430 0 1 0.0136178 0.018843 0.033921
 440 0 2 0.0136487 0.0240918 0.0563014
 450 0 3 0.015706 0.0303061 0.0774135
 46
 47... entries can be in any order
 48... after the expected number of entries is found, file can contain anything
 49*/
 50
 51
 52OCIO_NAMESPACE_ENTER
 53{
 54    ////////////////////////////////////////////////////////////////
 55    
 56    namespace
 57    {
 58        class LocalCachedFile : public CachedFile
 59        {
 60        public:
 61            LocalCachedFile()
 62            {
 63                lut = Lut3D::Create();
 64            };
 65            ~LocalCachedFile() {};
 66            
 67            Lut3DRcPtr lut;
 68        };
 69        
 70        typedef OCIO_SHARED_PTR<LocalCachedFile> LocalCachedFileRcPtr;
 71        
 72        
 73        
 74        class LocalFileFormat : public FileFormat
 75        {
 76        public:
 77            
 78            ~LocalFileFormat() {};
 79            
 80            virtual void GetFormatInfo(FormatInfoVec & formatInfoVec) const;
 81            
 82            virtual CachedFileRcPtr Read(std::istream & istream) const;
 83            
 84            virtual void BuildFileOps(OpRcPtrVec & ops,
 85                         const Config& config,
 86                         const ConstContextRcPtr & context,
 87                         CachedFileRcPtr untypedCachedFile,
 88                         const FileTransform& fileTransform,
 89                         TransformDirection dir) const;
 90        };
 91        
 92        
 93        void LocalFileFormat::GetFormatInfo(FormatInfoVec & formatInfoVec) const
 94        {
 95            FormatInfo info;
 96            info.name = "spi3d";
 97            info.extension = "spi3d";
 98            info.capabilities = FORMAT_CAPABILITY_READ;
 99            formatInfoVec.push_back(info);
100        }
101        
102        CachedFileRcPtr
103        LocalFileFormat::Read(std::istream & istream) const
104        {
105            const int MAX_LINE_SIZE = 4096;
106            char lineBuffer[MAX_LINE_SIZE];
107
108            Lut3DRcPtr lut3d = Lut3D::Create();
109
110            // Read header information
111            istream.getline(lineBuffer, MAX_LINE_SIZE);
112            if(!pystring::startswith(pystring::lower(lineBuffer), "spilut"))
113            {
114                std::ostringstream os;
115                os << "Lut does not appear to be valid spilut format. ";
116                os << "Expected 'SPILUT'.  Found, '" << lineBuffer << "'.";
117                throw Exception(os.str().c_str());
118            }
119
120            // TODO: Assert 2nd line is 3 3
121            istream.getline(lineBuffer, MAX_LINE_SIZE);
122
123            // Get LUT Size
124            // TODO: Error handling
125            int rSize, gSize, bSize;
126            istream.getline(lineBuffer, MAX_LINE_SIZE);
127            sscanf(lineBuffer, "%d %d %d", &rSize, &gSize, &bSize);
128
129            lut3d->size[0] = rSize;
130            lut3d->size[1] = gSize;
131            lut3d->size[2] = bSize;
132            lut3d->lut.resize(rSize * gSize * bSize * 3);
133
134            // Parse table
135            int index = 0;
136            int rIndex, gIndex, bIndex;
137            float redValue, greenValue, blueValue;
138
139            int entriesRemaining = rSize * gSize * bSize;
140
141            while (istream.good() && entriesRemaining > 0)
142            {
143                istream.getline(lineBuffer, MAX_LINE_SIZE);
144
145                if (sscanf(lineBuffer, "%d %d %d %f %f %f",
146                    &rIndex, &gIndex, &bIndex,
147                    &redValue, &greenValue, &blueValue) == 6)
148                {
149                    index = GetLut3DIndex_B(rIndex, gIndex, bIndex,
150                                            rSize, gSize, bSize);
151                    if(index < 0 || index >= (int) lut3d->lut.size())
152                    {
153                        std::ostringstream os;
154                        os << "Cannot load .spi3d lut, data is invalid. ";
155                        os << "A lut entry is specified (";
156                        os << rIndex << " " << gIndex << " " << bIndex;
157                        os << " that falls outside of the cube.";
158                        throw Exception(os.str().c_str());
159                    }
160
161                    lut3d->lut[index+0] = redValue;
162                    lut3d->lut[index+1] = greenValue;
163                    lut3d->lut[index+2] = blueValue;
164
165                    entriesRemaining--;
166                }
167            }
168
169            // Have we fully populated the table?
170            if (entriesRemaining>0) 
171                throw Exception("Not enough entries found.");
172
173            LocalCachedFileRcPtr cachedFile = LocalCachedFileRcPtr(new LocalCachedFile());
174            cachedFile->lut = lut3d;
175            return cachedFile;
176        }
177
178        void LocalFileFormat::BuildFileOps(OpRcPtrVec & ops,
179                                  const Config& /*config*/,
180                                  const ConstContextRcPtr & /*context*/,
181                                  CachedFileRcPtr untypedCachedFile,
182                                  const FileTransform& fileTransform,
183                                  TransformDirection dir) const
184        {
185            LocalCachedFileRcPtr cachedFile = DynamicPtrCast<LocalCachedFile>(untypedCachedFile);
186
187            if(!cachedFile) // This should never happen.
188            {
189                std::ostringstream os;
190                os << "Cannot build Spi3D Op. Invalid cache type.";
191                throw Exception(os.str().c_str());
192            }
193
194            TransformDirection newDir = CombineTransformDirections(dir,
195                fileTransform.getDirection());
196
197            CreateLut3DOp(ops,
198                          cachedFile->lut,
199                          fileTransform.getInterpolation(),
200                          newDir);
201        }
202    }
203    
204    FileFormat * CreateFileFormatSpi3D()
205    {
206        return new LocalFileFormat();
207    }
208}
209OCIO_NAMESPACE_EXIT