PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/core/FileFormatSpiMtx.cpp

http://github.com/imageworks/OpenColorIO
C++ | 195 lines | 128 code | 35 blank | 32 comment | 5 complexity | d150b3b350c15c77554d3493c297f994 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 "MatrixOps.h"
  30. #include "ParseUtils.h"
  31. #include "pystring/pystring.h"
  32. #include <cstdio>
  33. #include <cstring>
  34. #include <sstream>
  35. OCIO_NAMESPACE_ENTER
  36. {
  37. ////////////////////////////////////////////////////////////////
  38. namespace
  39. {
  40. class LocalCachedFile : public CachedFile
  41. {
  42. public:
  43. LocalCachedFile()
  44. {
  45. memset(m44, 0, 16*sizeof(float));
  46. memset(offset4, 0, 4*sizeof(float));
  47. };
  48. ~LocalCachedFile() {};
  49. float m44[16];
  50. float offset4[4];
  51. };
  52. typedef OCIO_SHARED_PTR<LocalCachedFile> LocalCachedFileRcPtr;
  53. class LocalFileFormat : public FileFormat
  54. {
  55. public:
  56. ~LocalFileFormat() {};
  57. virtual void GetFormatInfo(FormatInfoVec & formatInfoVec) const;
  58. virtual CachedFileRcPtr Read(std::istream & istream) const;
  59. virtual void BuildFileOps(OpRcPtrVec & ops,
  60. const Config& config,
  61. const ConstContextRcPtr & context,
  62. CachedFileRcPtr untypedCachedFile,
  63. const FileTransform& fileTransform,
  64. TransformDirection dir) const;
  65. };
  66. void LocalFileFormat::GetFormatInfo(FormatInfoVec & formatInfoVec) const
  67. {
  68. FormatInfo info;
  69. info.name = "spimtx";
  70. info.extension = "spimtx";
  71. info.capabilities = FORMAT_CAPABILITY_READ;
  72. formatInfoVec.push_back(info);
  73. }
  74. CachedFileRcPtr
  75. LocalFileFormat::Read(std::istream & istream) const
  76. {
  77. // Read the entire file.
  78. std::ostringstream fileStream;
  79. {
  80. const int MAX_LINE_SIZE = 4096;
  81. char lineBuffer[MAX_LINE_SIZE];
  82. while (istream.good())
  83. {
  84. istream.getline(lineBuffer, MAX_LINE_SIZE);
  85. fileStream << std::string(lineBuffer) << " ";
  86. }
  87. }
  88. // Turn it into parts
  89. std::vector<std::string> lineParts;
  90. pystring::split(pystring::strip(fileStream.str()), lineParts);
  91. if(lineParts.size() != 12)
  92. {
  93. std::ostringstream os;
  94. os << "Error parsing .spimtx file. ";
  95. os << "File must contain 12 float entries. ";
  96. os << lineParts.size() << " found.";
  97. throw Exception(os.str().c_str());
  98. }
  99. // Turn the parts into floats
  100. std::vector<float> floatArray;
  101. if(!StringVecToFloatVec(floatArray, lineParts))
  102. {
  103. std::ostringstream os;
  104. os << "Error parsing .spimtx file. ";
  105. os << "File must contain all float entries. ";
  106. throw Exception(os.str().c_str());
  107. }
  108. // Put the bits in the right place
  109. LocalCachedFileRcPtr cachedFile = LocalCachedFileRcPtr(new LocalCachedFile());
  110. cachedFile->m44[0] = floatArray[0];
  111. cachedFile->m44[1] = floatArray[1];
  112. cachedFile->m44[2] = floatArray[2];
  113. cachedFile->m44[3] = 0.0f;
  114. cachedFile->m44[4] = floatArray[4];
  115. cachedFile->m44[5] = floatArray[5];
  116. cachedFile->m44[6] = floatArray[6];
  117. cachedFile->m44[7] = 0.0f;
  118. cachedFile->m44[8] = floatArray[8];
  119. cachedFile->m44[9] = floatArray[9];
  120. cachedFile->m44[10] = floatArray[10];
  121. cachedFile->m44[11] = 0.0f;
  122. cachedFile->m44[12] = 0.0f;
  123. cachedFile->m44[13] = 0.0f;
  124. cachedFile->m44[14] = 0.0f;
  125. cachedFile->m44[15] = 1.0f;
  126. cachedFile->offset4[0] = floatArray[3] / 65535.0f;
  127. cachedFile->offset4[1] = floatArray[7] / 65535.0f;
  128. cachedFile->offset4[2] = floatArray[11] / 65535.0f;
  129. cachedFile->offset4[3] = 0.0f;
  130. return cachedFile;
  131. }
  132. void LocalFileFormat::BuildFileOps(OpRcPtrVec & ops,
  133. const Config& /*config*/,
  134. const ConstContextRcPtr & /*context*/,
  135. CachedFileRcPtr untypedCachedFile,
  136. const FileTransform& fileTransform,
  137. TransformDirection dir) const
  138. {
  139. LocalCachedFileRcPtr cachedFile = DynamicPtrCast<LocalCachedFile>(untypedCachedFile);
  140. if(!cachedFile) // This should never happen.
  141. {
  142. std::ostringstream os;
  143. os << "Cannot build SpiMtx Ops. Invalid cache type.";
  144. throw Exception(os.str().c_str());
  145. }
  146. TransformDirection newDir = CombineTransformDirections(dir,
  147. fileTransform.getDirection());
  148. CreateMatrixOffsetOp(ops,
  149. cachedFile->m44,
  150. cachedFile->offset4,
  151. newDir);
  152. }
  153. }
  154. FileFormat * CreateFileFormatSpiMtx()
  155. {
  156. return new LocalFileFormat();
  157. }
  158. }
  159. OCIO_NAMESPACE_EXIT