1
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
3
* This library is open source and may be redistributed and/or modified under
4
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5
* (at your option) any later version. The full license is in LICENSE file
6
* included with this distribution, and on the openscenegraph.org website.
8
* This library is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* OpenSceneGraph Public License for more details.
13
// Written by Wang Rui, (C) 2010
16
#include <osgDB/Registry>
17
#include <osgDB/Registry>
18
#include <osgDB/ObjectWrapper>
21
using namespace osgDB;
23
// Example compressor copying data to/from stream directly
24
class NullCompressor : public BaseCompressor
29
virtual bool compress( std::ostream& fout, const std::string& src )
31
int size = src.size();
32
fout.write( (char*)&size, INT_SIZE );
33
fout.write( src.c_str(), src.size() );
37
virtual bool decompress( std::istream& fin, std::string& target )
39
int size = 0; fin.read( (char*)&size, INT_SIZE );
42
target.resize( size );
43
fin.read( (char*)target.c_str(), size );
49
REGISTER_COMPRESSOR( "null", NullCompressor )
58
class ZLibCompressor : public BaseCompressor
63
virtual bool compress( std::ostream& fout, const std::string& src )
65
int ret, flush = Z_FINISH;
68
unsigned char out[CHUNK];
71
int stategy = Z_DEFAULT_STRATEGY;
73
/* allocate deflate state */
77
ret = deflateInit2( &strm, level, Z_DEFLATED,
78
15+16, // +16 to use gzip encoding
81
if ( ret != Z_OK ) return false;
83
strm.avail_in = src.size();
84
strm.next_in = (Bytef*)( &(*src.begin()) );
86
/* run deflate() on input until output buffer not full, finish
87
compression if all of source has been read in */
90
strm.avail_out = CHUNK;
92
ret = deflate(&strm, flush); /* no bad return value */
94
if ( ret == Z_STREAM_ERROR )
96
OSG_NOTICE << "Z_STREAM_ERROR" << std::endl;
100
have = CHUNK - strm.avail_out;
101
if ( have>0 ) fout.write( (const char*)out, have );
105
(void)deflateEnd( &strm );
108
} while ( strm.avail_out==0 );
110
/* clean up and return */
111
(void)deflateEnd( &strm );
115
virtual bool decompress( std::istream& fin, std::string& target )
120
unsigned char in[CHUNK];
121
unsigned char out[CHUNK];
123
/* allocate inflate state */
124
strm.zalloc = Z_NULL;
126
strm.opaque = Z_NULL;
128
strm.next_in = Z_NULL;
129
ret = inflateInit2( &strm,
130
15 + 32 ); // autodected zlib or gzip header
134
OSG_INFO << "failed to init" << std::endl;
138
/* decompress until deflate stream ends or end of file */
141
fin.read( (char *)in, CHUNK );
142
strm.avail_in = fin.gcount();
143
if (strm.avail_in==0 ) break;
145
/* run inflate() on input until output buffer not full */
149
strm.avail_out = CHUNK;
151
ret = inflate( &strm, Z_NO_FLUSH );
158
(void)inflateEnd( &strm );
161
have = CHUNK - strm.avail_out;
162
target.append( (char*)out, have );
163
} while ( strm.avail_out==0 );
165
/* done when inflate() says it's done */
166
} while ( ret!=Z_STREAM_END );
168
/* clean up and return */
169
(void)inflateEnd( &strm );
170
return ret==Z_STREAM_END ? true : false;
174
REGISTER_COMPRESSOR( "zlib", ZLibCompressor )