/src/core/FileFormatPandora.cpp

http://github.com/imageworks/OpenColorIO · C++ · 286 lines · 214 code · 37 blank · 35 comment · 59 complexity · ce3331cd9adf6ecc205cdddb051d4eaa 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 <cstdio>
  28. #include <iostream>
  29. #include <iterator>
  30. #include <OpenColorIO/OpenColorIO.h>
  31. #include "FileTransform.h"
  32. #include "Lut1DOp.h"
  33. #include "Lut3DOp.h"
  34. #include "ParseUtils.h"
  35. #include "pystring/pystring.h"
  36. OCIO_NAMESPACE_ENTER
  37. {
  38. namespace
  39. {
  40. class LocalCachedFile : public CachedFile
  41. {
  42. public:
  43. LocalCachedFile ()
  44. {
  45. lut3D = Lut3D::Create();
  46. };
  47. ~LocalCachedFile() {};
  48. Lut3DRcPtr lut3D;
  49. };
  50. typedef OCIO_SHARED_PTR<LocalCachedFile> LocalCachedFileRcPtr;
  51. class LocalFileFormat : public FileFormat
  52. {
  53. public:
  54. ~LocalFileFormat() {};
  55. virtual void GetFormatInfo(FormatInfoVec & formatInfoVec) const;
  56. virtual CachedFileRcPtr Read(std::istream & istream) const;
  57. virtual void BuildFileOps(OpRcPtrVec & ops,
  58. const Config& config,
  59. const ConstContextRcPtr & context,
  60. CachedFileRcPtr untypedCachedFile,
  61. const FileTransform& fileTransform,
  62. TransformDirection dir) const;
  63. };
  64. void LocalFileFormat::GetFormatInfo(FormatInfoVec & formatInfoVec) const
  65. {
  66. FormatInfo info;
  67. info.name = "pandora_mga";
  68. info.extension = "mga";
  69. info.capabilities = FORMAT_CAPABILITY_READ;
  70. formatInfoVec.push_back(info);
  71. FormatInfo info2;
  72. info2.name = "pandora_m3d";
  73. info2.extension = "m3d";
  74. info2.capabilities = FORMAT_CAPABILITY_READ;
  75. formatInfoVec.push_back(info2);
  76. }
  77. CachedFileRcPtr
  78. LocalFileFormat::Read(std::istream & istream) const
  79. {
  80. // this shouldn't happen
  81. if(!istream)
  82. {
  83. throw Exception ("File stream empty when trying to read Pandora lut");
  84. }
  85. // Validate the file type
  86. std::string line;
  87. // Parse the file
  88. std::string format;
  89. int lutEdgeLen = 0;
  90. int outputBitDepthMaxValue = 0;
  91. std::vector<int> raw3d;
  92. {
  93. std::vector<std::string> parts;
  94. std::vector<int> tmpints;
  95. bool inLut3d = false;
  96. while(nextline(istream, line))
  97. {
  98. // Strip, lowercase, and split the line
  99. pystring::split(pystring::lower(pystring::strip(line)), parts);
  100. if(parts.empty()) continue;
  101. // Skip all lines starting with '#'
  102. if(pystring::startswith(parts[0],"#")) continue;
  103. if(parts[0] == "channel")
  104. {
  105. if(parts.size() != 2 || pystring::lower(parts[1]) != "3d")
  106. {
  107. throw Exception("Parse error in Pandora lut. Only 3d luts are currently supported. (channel: 3d).");
  108. }
  109. }
  110. else if(parts[0] == "in")
  111. {
  112. int inval = 0;
  113. if(parts.size() != 2 || !StringToInt( &inval, parts[1].c_str()))
  114. {
  115. throw Exception("Malformed 'in' tag in Pandora lut.");
  116. }
  117. raw3d.reserve(inval*3);
  118. lutEdgeLen = Get3DLutEdgeLenFromNumPixels(inval);
  119. }
  120. else if(parts[0] == "out")
  121. {
  122. if(parts.size() != 2 || !StringToInt( &outputBitDepthMaxValue, parts[1].c_str()))
  123. {
  124. throw Exception("Malformed 'out' tag in Pandora lut.");
  125. }
  126. }
  127. else if(parts[0] == "format")
  128. {
  129. if(parts.size() != 2 || pystring::lower(parts[1]) != "lut")
  130. {
  131. throw Exception("Parse error in Pandora lut. Only luts are currently supported. (format: lut).");
  132. }
  133. }
  134. else if(parts[0] == "values")
  135. {
  136. if(parts.size() != 4 ||
  137. pystring::lower(parts[1]) != "red" ||
  138. pystring::lower(parts[2]) != "green" ||
  139. pystring::lower(parts[3]) != "blue")
  140. {
  141. throw Exception("Parse error in Pandora lut. Only rgb luts are currently supported. (values: red green blue).");
  142. }
  143. inLut3d = true;
  144. }
  145. else if(inLut3d)
  146. {
  147. if(!StringVecToIntVec(tmpints, parts) || tmpints.size() != 4)
  148. {
  149. std::ostringstream os;
  150. os << "Parse error in Pandora lut. Expected to find 4 integers. Instead found '"
  151. << line << "'";
  152. throw Exception(os.str().c_str());
  153. }
  154. raw3d.push_back(tmpints[1]);
  155. raw3d.push_back(tmpints[2]);
  156. raw3d.push_back(tmpints[3]);
  157. }
  158. }
  159. }
  160. // Interpret the parsed data, validate lut sizes
  161. if(lutEdgeLen*lutEdgeLen*lutEdgeLen != static_cast<int>(raw3d.size()/3))
  162. {
  163. std::ostringstream os;
  164. os << "Parse error in Pandora lut. ";
  165. os << "Incorrect number of lut3d entries. ";
  166. os << "Found " << raw3d.size()/3 << ", expected " << lutEdgeLen*lutEdgeLen*lutEdgeLen << ".";
  167. throw Exception(os.str().c_str());
  168. }
  169. if(lutEdgeLen*lutEdgeLen*lutEdgeLen == 0)
  170. {
  171. std::ostringstream os;
  172. os << "Parse error in Pandora lut. ";
  173. os << "No 3D Lut entries found.";
  174. throw Exception(os.str().c_str());
  175. }
  176. if(outputBitDepthMaxValue <= 0)
  177. {
  178. std::ostringstream os;
  179. os << "Parse error in Pandora lut. ";
  180. os << "A valid 'out' tag was not found.";
  181. throw Exception(os.str().c_str());
  182. }
  183. LocalCachedFileRcPtr cachedFile = LocalCachedFileRcPtr(new LocalCachedFile());
  184. // Reformat 3D data
  185. cachedFile->lut3D->size[0] = lutEdgeLen;
  186. cachedFile->lut3D->size[1] = lutEdgeLen;
  187. cachedFile->lut3D->size[2] = lutEdgeLen;
  188. cachedFile->lut3D->lut.reserve(raw3d.size());
  189. float scale = 1.0f / ((float)outputBitDepthMaxValue - 1.0f);
  190. for(int rIndex=0; rIndex<lutEdgeLen; ++rIndex)
  191. {
  192. for(int gIndex=0; gIndex<lutEdgeLen; ++gIndex)
  193. {
  194. for(int bIndex=0; bIndex<lutEdgeLen; ++bIndex)
  195. {
  196. int i = GetLut3DIndex_B(rIndex, gIndex, bIndex,
  197. lutEdgeLen, lutEdgeLen, lutEdgeLen);
  198. cachedFile->lut3D->lut.push_back(static_cast<float>(raw3d[i+0]) * scale);
  199. cachedFile->lut3D->lut.push_back(static_cast<float>(raw3d[i+1]) * scale);
  200. cachedFile->lut3D->lut.push_back(static_cast<float>(raw3d[i+2]) * scale);
  201. }
  202. }
  203. }
  204. return cachedFile;
  205. }
  206. void
  207. LocalFileFormat::BuildFileOps(OpRcPtrVec & ops,
  208. const Config& /*config*/,
  209. const ConstContextRcPtr & /*context*/,
  210. CachedFileRcPtr untypedCachedFile,
  211. const FileTransform& fileTransform,
  212. TransformDirection dir) const
  213. {
  214. LocalCachedFileRcPtr cachedFile = DynamicPtrCast<LocalCachedFile>(untypedCachedFile);
  215. // This should never happen.
  216. if(!cachedFile)
  217. {
  218. std::ostringstream os;
  219. os << "Cannot build Truelight .cub Op. Invalid cache type.";
  220. throw Exception(os.str().c_str());
  221. }
  222. TransformDirection newDir = CombineTransformDirections(dir,
  223. fileTransform.getDirection());
  224. if(newDir == TRANSFORM_DIR_UNKNOWN)
  225. {
  226. std::ostringstream os;
  227. os << "Cannot build file format transform,";
  228. os << " unspecified transform direction.";
  229. throw Exception(os.str().c_str());
  230. }
  231. if(newDir == TRANSFORM_DIR_FORWARD)
  232. {
  233. CreateLut3DOp(ops, cachedFile->lut3D,
  234. fileTransform.getInterpolation(), newDir);
  235. }
  236. else if(newDir == TRANSFORM_DIR_INVERSE)
  237. {
  238. CreateLut3DOp(ops, cachedFile->lut3D,
  239. fileTransform.getInterpolation(), newDir);
  240. }
  241. }
  242. }
  243. FileFormat * CreateFileFormatPandora()
  244. {
  245. return new LocalFileFormat();
  246. }
  247. }
  248. OCIO_NAMESPACE_EXIT