about summary refs log tree commit diff
path: root/converter/ppm/ppmtopjxl.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2015-07-03 16:54:51 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2015-07-03 16:54:51 +0000
commit1b17cc595dbefdf5f5111db8534ec3bce500412e (patch)
tree29d914152182ac6149cd55a5042d8cf65ca1b59e /converter/ppm/ppmtopjxl.c
parenta5dc3942f3204d46702832ee2c21c2bf4b99012b (diff)
downloadnetpbm-mirror-1b17cc595dbefdf5f5111db8534ec3bce500412e.tar.gz
netpbm-mirror-1b17cc595dbefdf5f5111db8534ec3bce500412e.tar.xz
netpbm-mirror-1b17cc595dbefdf5f5111db8534ec3bce500412e.zip
cleanup: factor out runlength encoding
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2600 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/ppm/ppmtopjxl.c')
-rw-r--r--converter/ppm/ppmtopjxl.c53
1 files changed, 7 insertions, 46 deletions
diff --git a/converter/ppm/ppmtopjxl.c b/converter/ppm/ppmtopjxl.c
index ddf49638..90bcef0f 100644
--- a/converter/ppm/ppmtopjxl.c
+++ b/converter/ppm/ppmtopjxl.c
@@ -20,6 +20,7 @@
 #include "pm_c_util.h"
 #include "nstring.h"
 #include "ppm.h"
+#include "runlength.h"
 
 #define MAXCOLORS 1024
 
@@ -153,52 +154,12 @@ putbits(int const bArg,
             /* remove trailing zeros */
         printf("\033*b"); 
         if (num && !nopack) {            /* TIFF 4.0 packbits encoding */
-            unsigned int i;
-            int start = 0;
-            int next;
-            runcnt[start] = 0;
-            for (i = 1; i < num; ++i) {
-                if (inrow[i] == inrow[i-1]) {
-                    if (runcnt[start] <= 0 && runcnt[start] > -127)
-                        runcnt[start]--;
-                    else
-                        runcnt[start = i] = 0;
-                } else {
-                    if (runcnt[start] >= 0 && runcnt[start] < 127)
-                        runcnt[start]++;
-                    else
-                        runcnt[start = i] = 0;
-                }
-            }
-            for (i = 0, start = 0; i < num; i = next) {
-                int count = runcnt[i];
-                int from = i;
-                if (count >= 0) { /* merge two-byte runs */
-                    for (;;) {
-                        next = i+1+runcnt[i];
-                        if(next >= num || runcnt[next] < 0 ||
-                           count+runcnt[next]+1 > 127)
-                            break;
-                        count += runcnt[next]+1;
-                        i = next;
-                    }
-                }
-                next =  i + 1 + ((runcnt[i] < 0) ? -runcnt[i] : runcnt[i]);
-                if (next < num && count > 0 &&
-                    runcnt[next] < 0 && runcnt[next] > -127) {
-                    --count;
-                    --next;
-                    runcnt[next] = runcnt[next+1]-1;
-                }
-                outrow[start++] = count;
-                if (count >= 0) {
-                    while (count-- >= 0)
-                        outrow[start++] = inrow[from++];
-                } else
-                    outrow[start++] = inrow[from];
-            }
-            if (start < num) {
-                num = start;
+            size_t outSize;
+            pm_rlenc_compressbyte(
+                (unsigned char *)inrow, (unsigned char *)outrow,
+                PM_RLE_PACKBITS, num, &outSize); 
+            if (outSize < num) {
+                num = outSize;
                 if (!pack) {
                     printf("2m");
                     pack = true;