diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2015-07-03 16:54:51 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2015-07-03 16:54:51 +0000 |
commit | 1b17cc595dbefdf5f5111db8534ec3bce500412e (patch) | |
tree | 29d914152182ac6149cd55a5042d8cf65ca1b59e /converter/pbm | |
parent | a5dc3942f3204d46702832ee2c21c2bf4b99012b (diff) | |
download | netpbm-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/pbm')
-rw-r--r-- | converter/pbm/pbmtomacp.c | 84 |
1 files changed, 15 insertions, 69 deletions
diff --git a/converter/pbm/pbmtomacp.c b/converter/pbm/pbmtomacp.c index 7ddb1ef5..df5cbb0c 100644 --- a/converter/pbm/pbmtomacp.c +++ b/converter/pbm/pbmtomacp.c @@ -41,6 +41,7 @@ #include "pbm.h" #include "shhopt.h" #include "mallocvar.h" +#include "runlength.h" #include "macp.h" #define MIN3(a,b,c) (MIN((MIN((a),(b))),(c))) @@ -282,60 +283,6 @@ writeMacpRowPacked(const bit * const packedBits, static void -packit(const bit * const sourceBits, - unsigned int const imageColCharCt, - unsigned char * const packedBits, - unsigned int * const packedImageLengthP ) { -/*-------------------------------------------------------------------------- - Compress according to packbits algorithm, a byte-level run-length encoding - scheme. - - Each row is encoded separately. - - The following code does not produce optimum output when there are 2-byte - long sequences between longer ones: the 2-byte run in between does not get - packed, using up 3 bytes where 2 would do. -----------------------------------------------------------------------------*/ - int charcount, packcount; - enum {EQUAL, UNEQUAL} status; - bit * count; - - for (packcount = 0, charcount = 0, status = EQUAL; - charcount < imageColCharCt; - ) { - bit const save = sourceBits[charcount++]; - - int newcount; - - newcount = 1; - while (charcount < imageColCharCt && sourceBits[charcount] == save) { - ++charcount; - ++newcount; - } - if (newcount > 2) { - count = (unsigned char *) &packedBits[packcount++]; - *count = 257 - newcount; - packedBits[packcount++] = save; - status = EQUAL; - } else { - if (status == EQUAL) { - count = (unsigned char *) &packedBits[packcount++]; - *count = newcount - 1; - } else - *count += newcount; - - for( ; newcount > 0; --newcount) { - packedBits[packcount++] = save; - } - status = UNEQUAL; - } - } - *packedImageLengthP = packcount; -} - - - -static void writeMacpRow(bit * const imageBits, unsigned int const leftMarginCharCt, unsigned int const imageColCharCt, @@ -352,31 +299,30 @@ writeMacpRow(bit * const imageBits, else { unsigned int const rightMarginCharCt = MACP_COLCHARS - leftMarginCharCt - imageColCharCt; - unsigned char * const packedBits = malloc(MACP_COLCHARS+1); - - unsigned int packedImageLength; + unsigned char packedBits[MACP_COLCHARS+1]; + size_t packedImageLength; - if (packedBits == NULL) - pm_error("Failed to get memory for a %u-column row buffer", - MACP_COLCHARS); + if (pm_rlenc_maxbytes(MACP_COLCHARS, PM_RLE_PACKBITS) + > MACP_COLCHARS + 1) + pm_error("INTERNAL ERROR: RLE buffer too small"); - packit(imageBits, imageColCharCt, packedBits, &packedImageLength); + pm_rlenc_compressbyte(imageBits, packedBits, PM_RLE_PACKBITS, + imageColCharCt, &packedImageLength); - /* Check if we are we better off with compression. If not, send row - unpacked. See note at top of file. - */ - if (packedImageLength + (leftMarginCharCt > 0 ? 1 : 0) * 2 + (rightMarginCharCt > 0 ? 1 : 0) * 2 - < MACP_COLCHARS) + < MACP_COLCHARS) { + /* It's smaller compressed, so do that */ writeMacpRowPacked(packedBits, leftMarginCharCt, packedImageLength, rightMarginCharCt, ofP); - else /* Extremely rare */ + } else { /* Extremely rare */ + /* It's larger compressed, so do it uncompressed. See note + at top of file. + */ writeMacpRowUnpacked(imageBits, leftMarginCharCt, imageColCharCt, ofP); - - free(packedBits); + } } } |