| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osg/Notify> |
|---|
| 16 | #include <osgDB/Registry> |
|---|
| 17 | #include <osgDB/Registry> |
|---|
| 18 | #include <osgDB/ObjectWrapper> |
|---|
| 19 | #include <sstream> |
|---|
| 20 | |
|---|
| 21 | using namespace osgDB; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | class NullCompressor : public BaseCompressor |
|---|
| 25 | { |
|---|
| 26 | public: |
|---|
| 27 | NullCompressor() {} |
|---|
| 28 | |
|---|
| 29 | virtual bool compress( std::ostream& fout, const std::string& src ) |
|---|
| 30 | { |
|---|
| 31 | int size = src.size(); |
|---|
| 32 | fout.write( (char*)&size, INT_SIZE ); |
|---|
| 33 | fout.write( src.c_str(), src.size() ); |
|---|
| 34 | return true; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | virtual bool decompress( std::istream& fin, std::string& target ) |
|---|
| 38 | { |
|---|
| 39 | int size = 0; fin.read( (char*)&size, INT_SIZE ); |
|---|
| 40 | if ( size ) |
|---|
| 41 | { |
|---|
| 42 | target.resize( size ); |
|---|
| 43 | fin.read( (char*)target.c_str(), size ); |
|---|
| 44 | } |
|---|
| 45 | return true; |
|---|
| 46 | } |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | REGISTER_COMPRESSOR( "null", NullCompressor ) |
|---|
| 50 | |
|---|
| 51 | #ifdef USE_ZLIB |
|---|
| 52 | |
|---|
| 53 | #include <zlib.h> |
|---|
| 54 | |
|---|
| 55 | #define CHUNK 32768 |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | class ZLibCompressor : public BaseCompressor |
|---|
| 59 | { |
|---|
| 60 | public: |
|---|
| 61 | ZLibCompressor() {} |
|---|
| 62 | |
|---|
| 63 | virtual bool compress( std::ostream& fout, const std::string& src ) |
|---|
| 64 | { |
|---|
| 65 | int ret, flush = Z_FINISH; |
|---|
| 66 | unsigned have; |
|---|
| 67 | z_stream strm; |
|---|
| 68 | unsigned char out[CHUNK]; |
|---|
| 69 | |
|---|
| 70 | int level = 6; |
|---|
| 71 | int stategy = Z_DEFAULT_STRATEGY; |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | strm.zalloc = Z_NULL; |
|---|
| 75 | strm.zfree = Z_NULL; |
|---|
| 76 | strm.opaque = Z_NULL; |
|---|
| 77 | ret = deflateInit2( &strm, level, Z_DEFLATED, |
|---|
| 78 | 15+16, |
|---|
| 79 | 8, |
|---|
| 80 | stategy ); |
|---|
| 81 | if ( ret != Z_OK ) return false; |
|---|
| 82 | |
|---|
| 83 | strm.avail_in = src.size(); |
|---|
| 84 | strm.next_in = (Bytef*)( &(*src.begin()) ); |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | do |
|---|
| 89 | { |
|---|
| 90 | strm.avail_out = CHUNK; |
|---|
| 91 | strm.next_out = out; |
|---|
| 92 | ret = deflate(&strm, flush); |
|---|
| 93 | |
|---|
| 94 | if ( ret == Z_STREAM_ERROR ) |
|---|
| 95 | { |
|---|
| 96 | OSG_NOTICE << "Z_STREAM_ERROR" << std::endl; |
|---|
| 97 | return false; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | have = CHUNK - strm.avail_out; |
|---|
| 101 | if ( have>0 ) fout.write( (const char*)out, have ); |
|---|
| 102 | |
|---|
| 103 | if ( fout.fail() ) |
|---|
| 104 | { |
|---|
| 105 | (void)deflateEnd( &strm ); |
|---|
| 106 | return false; |
|---|
| 107 | } |
|---|
| 108 | } while ( strm.avail_out==0 ); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | (void)deflateEnd( &strm ); |
|---|
| 112 | return true; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | virtual bool decompress( std::istream& fin, std::string& target ) |
|---|
| 116 | { |
|---|
| 117 | int ret; |
|---|
| 118 | unsigned have; |
|---|
| 119 | z_stream strm; |
|---|
| 120 | unsigned char in[CHUNK]; |
|---|
| 121 | unsigned char out[CHUNK]; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | strm.zalloc = Z_NULL; |
|---|
| 125 | strm.zfree = Z_NULL; |
|---|
| 126 | strm.opaque = Z_NULL; |
|---|
| 127 | strm.avail_in = 0; |
|---|
| 128 | strm.next_in = Z_NULL; |
|---|
| 129 | ret = inflateInit2( &strm, |
|---|
| 130 | 15 + 32 ); |
|---|
| 131 | |
|---|
| 132 | if ( ret!=Z_OK ) |
|---|
| 133 | { |
|---|
| 134 | OSG_INFO << "failed to init" << std::endl; |
|---|
| 135 | return ret!=0; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | do |
|---|
| 140 | { |
|---|
| 141 | fin.read( (char *)in, CHUNK ); |
|---|
| 142 | strm.avail_in = fin.gcount(); |
|---|
| 143 | if (strm.avail_in==0 ) break; |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | strm.next_in = in; |
|---|
| 147 | do |
|---|
| 148 | { |
|---|
| 149 | strm.avail_out = CHUNK; |
|---|
| 150 | strm.next_out = out; |
|---|
| 151 | ret = inflate( &strm, Z_NO_FLUSH ); |
|---|
| 152 | |
|---|
| 153 | switch (ret) |
|---|
| 154 | { |
|---|
| 155 | case Z_NEED_DICT: |
|---|
| 156 | case Z_DATA_ERROR: |
|---|
| 157 | case Z_MEM_ERROR: |
|---|
| 158 | (void)inflateEnd( &strm ); |
|---|
| 159 | return false; |
|---|
| 160 | } |
|---|
| 161 | have = CHUNK - strm.avail_out; |
|---|
| 162 | target.append( (char*)out, have ); |
|---|
| 163 | } while ( strm.avail_out==0 ); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | } while ( ret!=Z_STREAM_END ); |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | (void)inflateEnd( &strm ); |
|---|
| 170 | return ret==Z_STREAM_END ? true : false; |
|---|
| 171 | } |
|---|
| 172 | }; |
|---|
| 173 | |
|---|
| 174 | REGISTER_COMPRESSOR( "zlib", ZLibCompressor ) |
|---|
| 175 | |
|---|
| 176 | #endif |
|---|