diff options
Diffstat (limited to 'converter/ppm')
-rw-r--r-- | converter/ppm/ppmtoilbm.c | 72 | ||||
-rw-r--r-- | converter/ppm/ppmtopjxl.c | 53 |
2 files changed, 28 insertions, 97 deletions
diff --git a/converter/ppm/ppmtoilbm.c b/converter/ppm/ppmtoilbm.c index 9f1aca46..595aa3f4 100644 --- a/converter/ppm/ppmtoilbm.c +++ b/converter/ppm/ppmtoilbm.c @@ -38,8 +38,11 @@ ** - added IFF text chunks ** ** Feb 2010: afu -** Added dimension check to prevent short int from overflowing. -** +** - added dimension check to prevent short int from overflowing. +** +** June 2015: afu +** - moved byterun1 (or Packbits) compression to lib/util/runlenth.c +** - fixed bug with HAM -nocompress ** ** TODO: ** - multipalette capability (PCHG chunk) for std and HAM @@ -64,6 +67,7 @@ ** implied warranty. */ +#include <assert.h> #include <string.h> #include "pm_c_util.h" @@ -71,6 +75,7 @@ #include "ppm.h" #include "ppmfloyd.h" #include "pbm.h" +#include "runlength.h" #include "ilbm.h" #include "lum.h" @@ -418,46 +423,6 @@ writeBmhd(int const cols, /************ compression ************/ - - -static int -runbyte1(int const size) { - -/* runbyte1 algorithm by Robert A. Knop (rknop@mop.caltech.edu) */ - - int in,out,count,hold; - unsigned char *inbuf = coded_rowbuf; - unsigned char *outbuf = compr_rowbuf; - - - in=out=0; - while( in<size ) { - if( (in<size-1) && (inbuf[in]==inbuf[in+1]) ) { - /*Begin replicate run*/ - for( count=0, hold=in; - in < size && inbuf[in] == inbuf[hold] && count < 128; - in++, count++) - ; - outbuf[out++]=(unsigned char)(char)(-count+1); - outbuf[out++]=inbuf[hold]; - } - else { /*Do a literal run*/ - hold=out; out++; count=0; - while( ((in>=size-2)&&(in<size)) || - ((in<size-2) && ((inbuf[in]!=inbuf[in+1]) - ||(inbuf[in]!=inbuf[in+2]))) ) { - outbuf[out++]=inbuf[in++]; - if( ++count>=128 ) - break; - } - outbuf[hold]=count-1; - } - } - return(out); -} - - - static void storeBodyrow(unsigned char * const row, int const len) { @@ -480,22 +445,26 @@ storeBodyrow(unsigned char * const row, -static int -compressRow(int const bytes) { +static unsigned int +compressRow(unsigned int const bytes) { - int newbytes; + size_t compressedByteCt; - switch( compmethod ) { + switch (compmethod) { case cmpByteRun1: - newbytes = runbyte1(bytes); + pm_rlenc_compressbyte( + coded_rowbuf, compr_rowbuf, PM_RLE_PACKBITS, bytes, + &compressedByteCt); break; default: pm_error("compressRow(): unknown compression method %d", compmethod); } - storeBodyrow(compr_rowbuf, newbytes); + storeBodyrow(compr_rowbuf, compressedByteCt); + + assert((unsigned)compressedByteCt == compressedByteCt); - return newbytes; + return (unsigned)compressedByteCt; } @@ -2020,7 +1989,8 @@ main(int argc, char ** argv) { if( ++argn >= argc ) pm_error("-camg requires a value"); value = strtol(argv[argn], &tail, 16); - /* TODO: should do some error checking here */ + if(argv[argn] == tail) + pm_error("-camg requires a value"); viewportmodes |= value; gen_camg = 1; } @@ -2329,7 +2299,7 @@ main(int argc, char ** argv) { for (i = 0; i < RowBytes(cols); ++i) coded_rowbuf[i] = 0; if (DO_COMPRESS) - MALLOCARRAY_NOFAIL(compr_rowbuf, WORSTCOMPR(RowBytes(cols))); + pm_rlenc_allocoutbuf(&compr_rowbuf, RowBytes(cols), PM_RLE_PACKBITS); } switch (mode) { 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; |