/src/FreeImage/Source/OpenEXR/IlmImf/ImfIO.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 252 lines · 69 code · 68 blank · 115 comment · 2 complexity · 9142f230a09013dc15b2c4e4c7fa0c62 MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMF_IO_H
  35. #define INCLUDED_IMF_IO_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // Low-level file input and output for OpenEXR.
  39. //
  40. //-----------------------------------------------------------------------------
  41. #include <ImfInt64.h>
  42. #include <string>
  43. namespace Imf {
  44. //-----------------------------------------------------------
  45. // class IStream -- an abstract base class for input streams.
  46. //-----------------------------------------------------------
  47. class IStream
  48. {
  49. public:
  50. //-----------
  51. // Destructor
  52. //-----------
  53. virtual ~IStream ();
  54. //-------------------------------------------------
  55. // Does this input stream support memory-mapped IO?
  56. //
  57. // Memory-mapped streams can avoid an extra copy;
  58. // memory-mapped read operations return a pointer
  59. // to an internal buffer instead of copying data
  60. // into a buffer supplied by the caller.
  61. //-------------------------------------------------
  62. virtual bool isMemoryMapped () const;
  63. //------------------------------------------------------
  64. // Read from the stream:
  65. //
  66. // read(c,n) reads n bytes from the stream, and stores
  67. // them in array c. If the stream contains less than n
  68. // bytes, or if an I/O error occurs, read(c,n) throws
  69. // an exception. If read(c,n) reads the last byte from
  70. // the file it returns false, otherwise it returns true.
  71. //------------------------------------------------------
  72. virtual bool read (char c[/*n*/], int n) = 0;
  73. //---------------------------------------------------
  74. // Read from a memory-mapped stream:
  75. //
  76. // readMemoryMapped(n) reads n bytes from the stream
  77. // and returns a pointer to the first byte. The
  78. // returned pointer remains valid until the stream
  79. // is closed. If there are less than n byte left to
  80. // read in the stream or if the stream is not memory-
  81. // mapped, readMemoryMapped(n) throws an exception.
  82. //---------------------------------------------------
  83. virtual char * readMemoryMapped (int n);
  84. //--------------------------------------------------------
  85. // Get the current reading position, in bytes from the
  86. // beginning of the file. If the next call to read() will
  87. // read the first byte in the file, tellg() returns 0.
  88. //--------------------------------------------------------
  89. virtual Int64 tellg () = 0;
  90. //-------------------------------------------
  91. // Set the current reading position.
  92. // After calling seekg(i), tellg() returns i.
  93. //-------------------------------------------
  94. virtual void seekg (Int64 pos) = 0;
  95. //------------------------------------------------------
  96. // Clear error conditions after an operation has failed.
  97. //------------------------------------------------------
  98. virtual void clear ();
  99. //------------------------------------------------------
  100. // Get the name of the file associated with this stream.
  101. //------------------------------------------------------
  102. const char * fileName () const;
  103. protected:
  104. IStream (const char fileName[]);
  105. private:
  106. IStream (const IStream &); // not implemented
  107. IStream & operator = (const IStream &); // not implemented
  108. std::string _fileName;
  109. };
  110. //-----------------------------------------------------------
  111. // class OStream -- an abstract base class for output streams
  112. //-----------------------------------------------------------
  113. class OStream
  114. {
  115. public:
  116. //-----------
  117. // Destructor
  118. //-----------
  119. virtual ~OStream ();
  120. //----------------------------------------------------------
  121. // Write to the stream:
  122. //
  123. // write(c,n) takes n bytes from array c, and stores them
  124. // in the stream. If an I/O error occurs, write(c,n) throws
  125. // an exception.
  126. //----------------------------------------------------------
  127. virtual void write (const char c[/*n*/], int n) = 0;
  128. //---------------------------------------------------------
  129. // Get the current writing position, in bytes from the
  130. // beginning of the file. If the next call to write() will
  131. // start writing at the beginning of the file, tellp()
  132. // returns 0.
  133. //---------------------------------------------------------
  134. virtual Int64 tellp () = 0;
  135. //-------------------------------------------
  136. // Set the current writing position.
  137. // After calling seekp(i), tellp() returns i.
  138. //-------------------------------------------
  139. virtual void seekp (Int64 pos) = 0;
  140. //------------------------------------------------------
  141. // Get the name of the file associated with this stream.
  142. //------------------------------------------------------
  143. const char * fileName () const;
  144. protected:
  145. OStream (const char fileName[]);
  146. private:
  147. OStream (const OStream &); // not implemented
  148. OStream & operator = (const OStream &); // not implemented
  149. std::string _fileName;
  150. };
  151. //-----------------------
  152. // Helper classes for Xdr
  153. //-----------------------
  154. struct StreamIO
  155. {
  156. static void
  157. writeChars (OStream &os, const char c[/*n*/], int n)
  158. {
  159. os.write (c, n);
  160. }
  161. static bool
  162. readChars (IStream &is, char c[/*n*/], int n)
  163. {
  164. return is.read (c, n);
  165. }
  166. };
  167. struct CharPtrIO
  168. {
  169. static void
  170. writeChars (char *&op, const char c[/*n*/], int n)
  171. {
  172. while (n--)
  173. *op++ = *c++;
  174. }
  175. static bool
  176. readChars (const char *&ip, char c[/*n*/], int n)
  177. {
  178. while (n--)
  179. *c++ = *ip++;
  180. return true;
  181. }
  182. };
  183. } // namespace Imf
  184. #endif