/src/core/FileFormatSpi1D.cpp

http://github.com/imageworks/OpenColorIO · C++ · 261 lines · 165 code · 38 blank · 58 comment · 37 complexity · 8359afc16fe38344aa5761cb92e7f066 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 "FileTransform.h"
  29. #include "Lut1DOp.h"
  30. #include "pystring/pystring.h"
  31. #include <cstdio>
  32. #include <sstream>
  33. /*
  34. Version 1
  35. From -7.5 3.7555555555555555
  36. Components 1
  37. Length 4096
  38. {
  39. 0.031525943963232252
  40. 0.045645604561056156
  41. ...
  42. }
  43. */
  44. OCIO_NAMESPACE_ENTER
  45. {
  46. ////////////////////////////////////////////////////////////////
  47. namespace
  48. {
  49. class LocalCachedFile : public CachedFile
  50. {
  51. public:
  52. LocalCachedFile()
  53. {
  54. lut = Lut1D::Create();
  55. };
  56. ~LocalCachedFile() {};
  57. Lut1DRcPtr 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 = "spi1d";
  77. info.extension = "spi1d";
  78. info.capabilities = FORMAT_CAPABILITY_READ;
  79. formatInfoVec.push_back(info);
  80. }
  81. // Try and load the format
  82. // Raise an exception if it can't be loaded.
  83. CachedFileRcPtr LocalFileFormat::Read(std::istream & istream) const
  84. {
  85. Lut1DRcPtr lut1d = Lut1D::Create();
  86. // Parse Header Info
  87. int lut_size = -1;
  88. float from_min = 0.0;
  89. float from_max = 1.0;
  90. int version = -1;
  91. int components = -1;
  92. const int MAX_LINE_SIZE = 4096;
  93. char lineBuffer[MAX_LINE_SIZE];
  94. // PARSE HEADER INFO
  95. {
  96. std::string headerLine("");
  97. do
  98. {
  99. istream.getline(lineBuffer, MAX_LINE_SIZE);
  100. headerLine = std::string(lineBuffer);
  101. if(pystring::startswith(headerLine, "Version"))
  102. {
  103. if(sscanf(lineBuffer, "Version %d", &version)!=1)
  104. throw Exception("Invalid 'Version' Tag");
  105. }
  106. else if(pystring::startswith(headerLine, "From"))
  107. {
  108. if(sscanf(lineBuffer, "From %f %f", &from_min, &from_max)!=2)
  109. throw Exception("Invalid 'From' Tag");
  110. }
  111. else if(pystring::startswith(headerLine, "Components"))
  112. {
  113. if(sscanf(lineBuffer, "Components %d", &components)!=1)
  114. throw Exception("Invalid 'Components' Tag");
  115. }
  116. else if(pystring::startswith(headerLine, "Length"))
  117. {
  118. if(sscanf(lineBuffer, "Length %d", &lut_size)!=1)
  119. throw Exception("Invalid 'Length' Tag");
  120. }
  121. }
  122. while (istream.good() && !pystring::startswith(headerLine,"{"));
  123. }
  124. if(version == -1)
  125. throw Exception("Could not find 'Version' Tag");
  126. if(version != 1)
  127. throw Exception("Only format version 1 supported.");
  128. if (lut_size == -1)
  129. throw Exception("Could not find 'Length' Tag");
  130. if (components == -1)
  131. throw Exception("Could not find 'Components' Tag");
  132. if (components<0 || components>3)
  133. throw Exception("Components must be [1,2,3]");
  134. for(int i=0; i<3; ++i)
  135. {
  136. lut1d->from_min[i] = from_min;
  137. lut1d->from_max[i] = from_max;
  138. lut1d->luts[i].clear();
  139. lut1d->luts[i].reserve(lut_size);
  140. }
  141. {
  142. istream.getline(lineBuffer, MAX_LINE_SIZE);
  143. int lineCount=0;
  144. float values[4];
  145. while (istream.good())
  146. {
  147. // If 1 component is specificed, use x1 x1 x1 defaultA
  148. if(components==1 && sscanf(lineBuffer,"%f",&values[0])==1)
  149. {
  150. lut1d->luts[0].push_back(values[0]);
  151. lut1d->luts[1].push_back(values[0]);
  152. lut1d->luts[2].push_back(values[0]);
  153. ++lineCount;
  154. }
  155. // If 2 components are specificed, use x1 x2 0.0
  156. else if(components==2 && sscanf(lineBuffer,"%f %f",&values[0],&values[1])==2)
  157. {
  158. lut1d->luts[0].push_back(values[0]);
  159. lut1d->luts[1].push_back(values[1]);
  160. lut1d->luts[2].push_back(0.0);
  161. ++lineCount;
  162. }
  163. // If 3 component is specificed, use x1 x2 x3 defaultA
  164. else if(components==3 && sscanf(lineBuffer,"%f %f %f",&values[0],&values[1],&values[2])==3)
  165. {
  166. lut1d->luts[0].push_back(values[0]);
  167. lut1d->luts[1].push_back(values[1]);
  168. lut1d->luts[2].push_back(values[2]);
  169. ++lineCount;
  170. }
  171. if(lineCount == lut_size) break;
  172. istream.getline(lineBuffer, MAX_LINE_SIZE);
  173. }
  174. if(lineCount!=lut_size)
  175. throw Exception("Not enough entries found.");
  176. }
  177. // 1e-5 rel error is a good threshold when float numbers near 0
  178. // are written out with 6 decimal places of precision. This is
  179. // a bit aggressive, I.e., changes in the 6th decimal place will
  180. // be considered roundoff error, but changes in the 5th decimal
  181. // will be considered lut 'intent'.
  182. // 1.0
  183. // 1.000005 equal to 1.0
  184. // 1.000007 equal to 1.0
  185. // 1.000010 not equal
  186. // 0.0
  187. // 0.000001 not equal
  188. lut1d->maxerror = 1e-5f;
  189. lut1d->errortype = ERROR_RELATIVE;
  190. LocalCachedFileRcPtr cachedFile = LocalCachedFileRcPtr(new LocalCachedFile());
  191. cachedFile->lut = lut1d;
  192. return cachedFile;
  193. }
  194. void LocalFileFormat::BuildFileOps(OpRcPtrVec & ops,
  195. const Config& /*config*/,
  196. const ConstContextRcPtr & /*context*/,
  197. CachedFileRcPtr untypedCachedFile,
  198. const FileTransform& fileTransform,
  199. TransformDirection dir) const
  200. {
  201. LocalCachedFileRcPtr cachedFile = DynamicPtrCast<LocalCachedFile>(untypedCachedFile);
  202. if(!cachedFile) // This should never happen.
  203. {
  204. std::ostringstream os;
  205. os << "Cannot build Spi1D Op. Invalid cache type.";
  206. throw Exception(os.str().c_str());
  207. }
  208. TransformDirection newDir = CombineTransformDirections(dir,
  209. fileTransform.getDirection());
  210. CreateLut1DOp(ops,
  211. cachedFile->lut,
  212. fileTransform.getInterpolation(),
  213. newDir);
  214. }
  215. }
  216. FileFormat * CreateFileFormatSpi1D()
  217. {
  218. return new LocalFileFormat();
  219. }
  220. }
  221. OCIO_NAMESPACE_EXIT