ImfIO.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "ImfNamespace.h"
  43. #include "ImfExport.h"
  44. #include <string>
  45. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  46. //-----------------------------------------------------------
  47. // class IStream -- an abstract base class for input streams.
  48. //-----------------------------------------------------------
  49. class IStream
  50. {
  51. public:
  52. //-----------
  53. // Destructor
  54. //-----------
  55. IMF_EXPORT
  56. virtual ~IStream ();
  57. //-------------------------------------------------
  58. // Does this input stream support memory-mapped IO?
  59. //
  60. // Memory-mapped streams can avoid an extra copy;
  61. // memory-mapped read operations return a pointer
  62. // to an internal buffer instead of copying data
  63. // into a buffer supplied by the caller.
  64. //-------------------------------------------------
  65. IMF_EXPORT
  66. virtual bool isMemoryMapped () const;
  67. //------------------------------------------------------
  68. // Read from the stream:
  69. //
  70. // read(c,n) reads n bytes from the stream, and stores
  71. // them in array c. If the stream contains less than n
  72. // bytes, or if an I/O error occurs, read(c,n) throws
  73. // an exception. If read(c,n) reads the last byte from
  74. // the file it returns false, otherwise it returns true.
  75. //------------------------------------------------------
  76. virtual bool read (char c[/*n*/], int n) = 0;
  77. //---------------------------------------------------
  78. // Read from a memory-mapped stream:
  79. //
  80. // readMemoryMapped(n) reads n bytes from the stream
  81. // and returns a pointer to the first byte. The
  82. // returned pointer remains valid until the stream
  83. // is closed. If there are less than n byte left to
  84. // read in the stream or if the stream is not memory-
  85. // mapped, readMemoryMapped(n) throws an exception.
  86. //---------------------------------------------------
  87. IMF_EXPORT
  88. virtual char * readMemoryMapped (int n);
  89. //--------------------------------------------------------
  90. // Get the current reading position, in bytes from the
  91. // beginning of the file. If the next call to read() will
  92. // read the first byte in the file, tellg() returns 0.
  93. //--------------------------------------------------------
  94. virtual Int64 tellg () = 0;
  95. //-------------------------------------------
  96. // Set the current reading position.
  97. // After calling seekg(i), tellg() returns i.
  98. //-------------------------------------------
  99. virtual void seekg (Int64 pos) = 0;
  100. //------------------------------------------------------
  101. // Clear error conditions after an operation has failed.
  102. //------------------------------------------------------
  103. IMF_EXPORT
  104. virtual void clear ();
  105. //------------------------------------------------------
  106. // Get the name of the file associated with this stream.
  107. //------------------------------------------------------
  108. IMF_EXPORT
  109. const char * fileName () const;
  110. protected:
  111. IMF_EXPORT
  112. IStream (const char fileName[]);
  113. private:
  114. IStream (const IStream &); // not implemented
  115. IStream & operator = (const IStream &); // not implemented
  116. std::string _fileName;
  117. };
  118. //-----------------------------------------------------------
  119. // class OStream -- an abstract base class for output streams
  120. //-----------------------------------------------------------
  121. class OStream
  122. {
  123. public:
  124. //-----------
  125. // Destructor
  126. //-----------
  127. IMF_EXPORT
  128. virtual ~OStream ();
  129. //----------------------------------------------------------
  130. // Write to the stream:
  131. //
  132. // write(c,n) takes n bytes from array c, and stores them
  133. // in the stream. If an I/O error occurs, write(c,n) throws
  134. // an exception.
  135. //----------------------------------------------------------
  136. virtual void write (const char c[/*n*/], int n) = 0;
  137. //---------------------------------------------------------
  138. // Get the current writing position, in bytes from the
  139. // beginning of the file. If the next call to write() will
  140. // start writing at the beginning of the file, tellp()
  141. // returns 0.
  142. //---------------------------------------------------------
  143. virtual Int64 tellp () = 0;
  144. //-------------------------------------------
  145. // Set the current writing position.
  146. // After calling seekp(i), tellp() returns i.
  147. //-------------------------------------------
  148. virtual void seekp (Int64 pos) = 0;
  149. //------------------------------------------------------
  150. // Get the name of the file associated with this stream.
  151. //------------------------------------------------------
  152. IMF_EXPORT
  153. const char * fileName () const;
  154. protected:
  155. IMF_EXPORT
  156. OStream (const char fileName[]);
  157. private:
  158. OStream (const OStream &); // not implemented
  159. OStream & operator = (const OStream &); // not implemented
  160. std::string _fileName;
  161. };
  162. //-----------------------
  163. // Helper classes for Xdr
  164. //-----------------------
  165. struct StreamIO
  166. {
  167. static void
  168. writeChars (OStream &os, const char c[/*n*/], int n)
  169. {
  170. os.write (c, n);
  171. }
  172. static bool
  173. readChars (IStream &is, char c[/*n*/], int n)
  174. {
  175. return is.read (c, n);
  176. }
  177. };
  178. struct CharPtrIO
  179. {
  180. static void
  181. writeChars (char *&op, const char c[/*n*/], int n)
  182. {
  183. while (n--)
  184. *op++ = *c++;
  185. }
  186. static bool
  187. readChars (const char *&ip, char c[/*n*/], int n)
  188. {
  189. while (n--)
  190. *c++ = *ip++;
  191. return true;
  192. }
  193. };
  194. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  195. #endif