Implemented support for changing zlib compression levels from the
Tight encoder.


git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@334 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/rdr/ZlibOutStream.cxx b/rdr/ZlibOutStream.cxx
index 181e356..df1dbee 100644
--- a/rdr/ZlibOutStream.cxx
+++ b/rdr/ZlibOutStream.cxx
@@ -25,7 +25,8 @@
 enum { DEFAULT_BUF_SIZE = 16384 };
 
 ZlibOutStream::ZlibOutStream(OutStream* os, int bufSize_, int compressLevel)
-  : underlying(os), bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
+  : underlying(os), compressionLevel(compressLevel), newLevel(compressLevel),
+    bufSize(bufSize_ ? bufSize_ : DEFAULT_BUF_SIZE), offset(0)
 {
   zs = new z_stream;
   zs->zalloc    = Z_NULL;
@@ -55,6 +56,14 @@
   underlying = os;
 }
 
+void ZlibOutStream::setCompressionLevel(int level)
+{
+  if (level < -1 || level > 9)
+    level = -1;                 // Z_DEFAULT_COMPRESSION
+
+  newLevel = level;
+}
+
 int ZlibOutStream::length()
 {
   return offset + ptr - start;
@@ -76,6 +85,7 @@
 
 //        fprintf(stderr,"zos flush: calling deflate, avail_in %d, avail_out %d\n",
 //                zs->avail_in,zs->avail_out);
+      checkCompressionLevel();
       int rc = deflate(zs, Z_SYNC_FLUSH);
       if (rc != Z_OK) throw Exception("ZlibOutStream: deflate failed");
 
@@ -109,6 +119,7 @@
 //        fprintf(stderr,"zos overrun: calling deflate, avail_in %d, avail_out %d\n",
 //                zs->avail_in,zs->avail_out);
 
+      checkCompressionLevel();
       int rc = deflate(zs, 0);
       if (rc != Z_OK) throw Exception("ZlibOutStream: deflate failed");
 
@@ -138,3 +149,13 @@
 
   return nItems;
 }
+
+void ZlibOutStream::checkCompressionLevel()
+{
+  if (newLevel != compressionLevel) {
+    if (deflateParams (zs, newLevel, Z_DEFAULT_STRATEGY) != Z_OK) {
+      throw Exception("ZlibOutStream: deflateParams failed");
+    }
+    compressionLevel = newLevel;
+  }
+}