[sword-svn] r3117 - in trunk: include src/modules/common

chrislit at crosswire.org chrislit at crosswire.org
Thu Mar 13 00:42:32 MST 2014


Author: chrislit
Date: 2014-03-13 00:42:32 -0700 (Thu, 13 Mar 2014)
New Revision: 3117

Modified:
   trunk/include/swcomprs.h
   trunk/include/xzcomprs.h
   trunk/src/modules/common/bz2comprs.cpp
   trunk/src/modules/common/swcomprs.cpp
   trunk/src/modules/common/xzcomprs.cpp
   trunk/src/modules/common/zipcomprs.cpp
Log:
added compression level throughout compressor classes


Modified: trunk/include/swcomprs.h
===================================================================
--- trunk/include/swcomprs.h	2014-03-13 04:40:53 UTC (rev 3116)
+++ trunk/include/swcomprs.h	2014-03-13 07:42:32 UTC (rev 3117)
@@ -35,15 +35,18 @@
 protected:
   char *buf, *zbuf, direct;	// 0 - encode; 1 - decode
   unsigned long zlen, zpos, pos, slen;
+  int level;
 public:
-    SWCompress ();
-    virtual ~ SWCompress ();
+  SWCompress ();
+  virtual ~ SWCompress ();
   virtual char *Buf (const char *buf = 0, unsigned long *len = 0);
   virtual char *zBuf (unsigned long *len, char *buf = 0);
   virtual unsigned long GetChars (char *buf, unsigned long len);	// override for other than buffer compression
   virtual unsigned long SendChars (char *buf, unsigned long len);	// override for other than buffer compression
   virtual void Encode (void);	// override to provide compression algorythm
   virtual void Decode (void);	// override to provide compression algorythm
+  virtual void SetLevel(int l) {level = l;};
+  virtual int GetLevel() {return level;};
 };
 
 SWORD_NAMESPACE_END

Modified: trunk/include/xzcomprs.h
===================================================================
--- trunk/include/xzcomprs.h	2014-03-13 04:40:53 UTC (rev 3116)
+++ trunk/include/xzcomprs.h	2014-03-13 07:42:32 UTC (rev 3117)
@@ -41,6 +41,7 @@
 
 	virtual void Encode(void);
 	virtual void Decode(void);
+	virtual void SetLevel(int l);
 private:
 	uint64_t memlimit; // memory usage limit during decompression
 };

Modified: trunk/src/modules/common/bz2comprs.cpp
===================================================================
--- trunk/src/modules/common/bz2comprs.cpp	2014-03-13 04:40:53 UTC (rev 3116)
+++ trunk/src/modules/common/bz2comprs.cpp	2014-03-13 07:42:32 UTC (rev 3117)
@@ -36,6 +36,7 @@
  */
 
 Bzip2Compress::Bzip2Compress() : SWCompress() {
+	level = 9;
 }
 
 
@@ -82,7 +83,7 @@
 	if (len)
 	{
 		//printf("Doing compress\n");
-		if (BZ2_bzBuffToBuffCompress(zbuf, (unsigned int*)&zlen, buf, len, 9, 0, 0) != BZ_OK)
+		if (BZ2_bzBuffToBuffCompress(zbuf, (unsigned int*)&zlen, buf, len, level, 0, 0) != BZ_OK)
 		{
 			printf("ERROR in compression\n");
 		}

Modified: trunk/src/modules/common/swcomprs.cpp
===================================================================
--- trunk/src/modules/common/swcomprs.cpp	2014-03-13 04:40:53 UTC (rev 3116)
+++ trunk/src/modules/common/swcomprs.cpp	2014-03-13 07:42:32 UTC (rev 3117)
@@ -35,6 +35,7 @@
 SWCompress::SWCompress()
 {
 	buf = zbuf = 0;
+	level = 6;
 	Init();
 }
 

Modified: trunk/src/modules/common/xzcomprs.cpp
===================================================================
--- trunk/src/modules/common/xzcomprs.cpp	2014-03-13 04:40:53 UTC (rev 3116)
+++ trunk/src/modules/common/xzcomprs.cpp	2014-03-13 07:42:32 UTC (rev 3117)
@@ -29,8 +29,6 @@
 #define LZMA_API_STATIC
 #include <lzma.h>
 
-#define XZ_PRESET 3 | LZMA_PRESET_EXTREME
-
 SWORD_NAMESPACE_START
 
 /******************************************************************************
@@ -39,8 +37,10 @@
  */
 
 XzCompress::XzCompress() : SWCompress() {
+	level = 3;
+	
 	// start with the estimated memory usage for our preset
-	memlimit = lzma_easy_decoder_memusage(XZ_PRESET);
+	memlimit = lzma_easy_decoder_memusage(level | LZMA_PRESET_EXTREME);
 	
 	// and round up to a power of 2--
 	// bit twiddle hack to determine next greatest power of 2 from:
@@ -102,7 +102,7 @@
 	if (len)
 	{
 		//printf("Doing compress\n");
-		switch (lzma_easy_buffer_encode(XZ_PRESET, LZMA_CHECK_CRC64, NULL, (const uint8_t*)buf, (size_t)len, (uint8_t*)zbuf, &zpos, (size_t)zlen)) {
+		switch (lzma_easy_buffer_encode(level | LZMA_PRESET_EXTREME, LZMA_CHECK_CRC64, NULL, (const uint8_t*)buf, (size_t)len, (uint8_t*)zbuf, &zpos, (size_t)zlen)) {
 		        case LZMA_OK: SendChars(zbuf, zpos);  break;
 			case LZMA_BUF_ERROR: fprintf(stderr, "ERROR: not enough room in the out buffer during compression.\n"); break;
 			case LZMA_UNSUPPORTED_CHECK: fprintf(stderr, "ERROR: unsupported_check error encountered during decompression.\n"); break;
@@ -180,4 +180,34 @@
 	free (zbuf);
 }
 
+
+/******************************************************************************
+ * XzCompress::SetLevel - This function sets the compression level of the
+ *			compressor.
+ */
+
+void XzCompress::SetLevel(int l) {
+	level = l;
+
+	// having changed the compression level, we need to adjust our memlimit accordingly,
+	// as in the constructor:
+
+	// start with the estimated memory usage for our preset
+	memlimit = lzma_easy_decoder_memusage(level | LZMA_PRESET_EXTREME);
+	
+	// and round up to a power of 2--
+	// bit twiddle hack to determine next greatest power of 2 from:
+	// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
+	memlimit--;
+	memlimit |= memlimit >> 1;
+	memlimit |= memlimit >> 2;
+	memlimit |= memlimit >> 4;
+	memlimit |= memlimit >> 8;
+	memlimit |= memlimit >> 16;
+	memlimit++;
+
+	// double that for safety's sake
+	memlimit <<= 1;
+};
+
 SWORD_NAMESPACE_END

Modified: trunk/src/modules/common/zipcomprs.cpp
===================================================================
--- trunk/src/modules/common/zipcomprs.cpp	2014-03-13 04:40:53 UTC (rev 3116)
+++ trunk/src/modules/common/zipcomprs.cpp	2014-03-13 07:42:32 UTC (rev 3117)
@@ -38,6 +38,7 @@
 ZipCompress::ZipCompress() : SWCompress()
 {
 //	fprintf(stderr, "init compress\n");
+	level = Z_DEFAULT_COMPRESSION;
 }
 
 
@@ -62,18 +63,20 @@
 void ZipCompress::Encode(void)
 {
 /*
-ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
-                                 const Bytef *source, uLong sourceLen));
-     Compresses the source buffer into the destination buffer.  sourceLen is
-   the byte length of the source buffer. Upon entry, destLen is the total
-   size of the destination buffer, which must be at least 0.1% larger than
-   sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
+ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
+                                  const Bytef *source, uLong sourceLen,
+                                  int level));
+
+     Compresses the source buffer into the destination buffer.  The level
+   parameter has the same meaning as in deflateInit.  sourceLen is the byte
+   length of the source buffer.  Upon entry, destLen is the total size of the
+   destination buffer, which must be at least the value returned by
+   compressBound(sourceLen).  Upon exit, destLen is the actual size of the
    compressed buffer.
-     This function can be used to compress a whole file at once if the
-   input file is mmap'ed.
-     compress returns Z_OK if success, Z_MEM_ERROR if there was not
-   enough memory, Z_BUF_ERROR if there was not enough room in the output
-   buffer.
+
+     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
+   Z_STREAM_ERROR if the level parameter is invalid.
 */
 	direct = 0;	// set direction needed by parent [Get|Send]Chars()
 
@@ -98,7 +101,7 @@
 	if (len)
 	{
 		//printf("Doing compress\n");
-		if (compress((Bytef*)zbuf, &zlen, (const Bytef*)buf, len) != Z_OK)
+		if (compress2((Bytef*)zbuf, &zlen, (const Bytef*)buf, len, level) != Z_OK)
 		{
 			printf("ERROR in compression\n");
 		}




More information about the sword-cvs mailing list