Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
zstream.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_ZSTREAM_H_)
21 #define __MITSUBA_CORE_ZSTREAM_H_
22 
23 #include <mitsuba/mitsuba.h>
24 #include <zlib.h>
25 
26 /// Buffer size used to communicate with zlib. The larger, the better.
27 #define ZSTREAM_BUFSIZE 32768
28 
30 
31 /**
32  * \brief Transparent compression/decompression stream based on \c zlib.
33  *
34  * This class transparently decompresses and compresses reads and writes
35  * to a nested stream, respectively.
36  *
37  * \ingroup libcore
38  */
39 class MTS_EXPORT_CORE ZStream : public Stream {
40 public:
41  // =============================================================
42  //! @{ \name Constructors
43  // =============================================================
44  enum EStreamType {
45  /// A raw deflate stream
47  /// A gzip-compatible stream
48  EGZipStream
49  };
50 
51  /// Create a new compression stream
52  ZStream(Stream *childStream, EStreamType streamType = EDeflateStream,
53  int level = Z_DEFAULT_COMPRESSION);
54 
55  //! @}
56  // =============================================================
57 
58  // =============================================================
59  //! @{ \name Compression stream-specific features
60  // =============================================================
61 
62  /// Return the child stream of this compression stream
63  inline const Stream *getChildStream() const { return m_childStream.get(); }
64 
65  /// Return the child stream of this compression stream
66  inline Stream *getChildStream() { return m_childStream; }
67 
68  //! @}
69  // =============================================================
70 
71  // =============================================================
72  //! @{ \name Implementation of the Stream interface
73  // =============================================================
74 
75  void read(void *ptr, size_t size);
76  void write(const void *ptr, size_t size);
77  void seek(size_t pos);
78  size_t getPos() const;
79  size_t getSize() const;
80  void truncate(size_t size);
81  void flush();
82  bool canWrite() const;
83  bool canRead() const;
84 
85  //! @}
86  // =============================================================
87 
88  /// Return a string representation
89  std::string toString() const;
90 
92 protected:
93  // \brief Virtual destructor
94  virtual ~ZStream();
95 private:
96  ref<Stream> m_childStream;
97  z_stream m_deflateStream, m_inflateStream;
98  uint8_t m_deflateBuffer[ZSTREAM_BUFSIZE];
99  uint8_t m_inflateBuffer[ZSTREAM_BUFSIZE];
100  bool m_didWrite;
101 };
102 
104 
105 #endif /* __MITSUBA_CORE_ZSTREAM_H_ */
Transparent compression/decompression stream based on zlib.
Definition: zstream.h:39
virtual void flush()=0
Flush the stream&#39;s buffers.
virtual bool canRead() const =0
Can we read from the stream?
const Stream * getChildStream() const
Return the child stream of this compression stream.
Definition: zstream.h:63
#define MTS_EXPORT_CORE
Definition: getopt.h:29
EStreamType
Create a new compression stream.
Definition: zstream.h:44
virtual bool canWrite() const =0
Can we write to the stream?
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
virtual void read(void *ptr, size_t size)=0
Read a specified amount of data from the stream.
A raw deflate stream.
Definition: zstream.h:46
Abstract seekable stream class.
Definition: stream.h:58
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Reference counting helper.
Definition: ref.h:40
#define ZSTREAM_BUFSIZE
Buffer size used to communicate with zlib. The larger, the better.
Definition: zstream.h:27
virtual void write(const void *ptr, size_t size)=0
Write a specified amount of data into the stream.
virtual size_t getPos() const =0
Get the current position inside the stream.
#define MTS_NAMESPACE_END
Definition: platform.h:138
virtual std::string toString() const
Return a string representation.
virtual void seek(size_t pos)=0
Seek to a position inside the stream.
Stream * getChildStream()
Return the child stream of this compression stream.
Definition: zstream.h:66
virtual size_t getSize() const =0
Return the size of the stream.
virtual void truncate(size_t size)=0
Truncate the stream to a given size.