PageRenderTime 49ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/core/FileFormatSpi3D.cpp

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