diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2017-08-21 20:46:45 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2017-08-21 20:46:45 +0000 |
commit | e8c1ce27a45914ee719935dd24bb140942c86edc (patch) | |
tree | af7c16b1d23044e2591576020f37d3ce0dada2a0 | |
parent | 845b57f8b878c8ca0864be00c612a438f194a513 (diff) | |
download | netpbm-mirror-e8c1ce27a45914ee719935dd24bb140942c86edc.tar.gz netpbm-mirror-e8c1ce27a45914ee719935dd24bb140942c86edc.tar.xz netpbm-mirror-e8c1ce27a45914ee719935dd24bb140942c86edc.zip |
Use pm_rlenc_compressbyte instead of native code for packbits compression, to fix incorrect compression
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3050 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r-- | converter/other/pnmtopalm/pnmtopalm.c | 114 | ||||
-rw-r--r-- | doc/HISTORY | 4 |
2 files changed, 30 insertions, 88 deletions
diff --git a/converter/other/pnmtopalm/pnmtopalm.c b/converter/other/pnmtopalm/pnmtopalm.c index 50a05968..f15533cf 100644 --- a/converter/other/pnmtopalm/pnmtopalm.c +++ b/converter/other/pnmtopalm/pnmtopalm.c @@ -13,7 +13,13 @@ * * See LICENSE file for licensing information. * - * + * References for the Palm Bitmap format: + * + * https://web.archive.org/web/20030621112139/http://www.palmos.com:80/dev/support/docs/ + * https://web.archive.org/web/20030413080018/http://www.palmos.com:80/dev/support/docs/palmos/ReferenceTOC.html + * + * http://www.trantor.de/kawt/doc/palmimages.html + * (above retrieved August 2017) */ #include <string.h> @@ -26,6 +32,7 @@ #include "palm.h" #include "shhopt.h" #include "mallocvar.h" +#include "runlength.h" enum compressionType {COMP_NONE, COMP_SCANLINE, COMP_RLE, COMP_PACKBITS}; @@ -836,79 +843,6 @@ rleCompressAndBufferRow(const unsigned char * const rowdata, -/* FIXME Incorrect for images with pixelSize == 16 */ -static void -computeNextPackbitsRun(const unsigned char * const rowdata, - unsigned int const rowbytes, - unsigned int const startPos, - unsigned int * const nextPosP, - unsigned char * const output, - int * const countP) { - - unsigned int pos; - int count; - - pos = startPos; - count = 0; - - if (rowdata[pos] == rowdata[pos + 1]) { - ++pos; - --count; - while ((count > -127) && (pos < (rowbytes - 1)) && - (rowdata[pos] == rowdata[pos + 1])) { - ++pos; - --count; - } - ++pos; /* push pos past end of this run */ - } else { - output[count] = rowdata[pos]; - ++pos; - while ((count < 127) && (pos < (rowbytes - 1)) && - (rowdata[pos] != rowdata[pos + 1])) { - ++count; - output[count] = rowdata[pos]; - ++pos; - } - /* trailing literal */ - if ((count < 127) && (pos == (rowbytes - 1)) && - (rowdata[pos - 1] != rowdata[pos])) { - ++count; - output[count] = rowdata[pos]; - ++pos; - } - } - *nextPosP = pos; - *countP = count; -} - - - -static void -addPackbitsRunToBuffer(const unsigned char * const rowdata, - unsigned int const rowbytes, - unsigned int const pos, - unsigned char * const output, - int const count, - struct seqBuffer * const rasterBufferP) { - - addByteToBuffer(rasterBufferP, (unsigned char)(signed char)count); - if (count < 0) { - addByteToBuffer(rasterBufferP, rowdata[pos - 1]); - } else { - unsigned int j; - for (j = 0; j <= count; j++) - addByteToBuffer(rasterBufferP, output[j]); - } - - if (pos == (rowbytes - 1) && (rowdata[pos - 1] != rowdata[pos])) { - /* orphaned byte, treat as literal */ - addByteToBuffer(rasterBufferP, 0); - addByteToBuffer(rasterBufferP, rowdata[pos]); - } -} - - - static void packbitsCompressAndBufferRow(const unsigned char * const rowdata, unsigned int const rowbytes, @@ -918,21 +852,18 @@ packbitsCompressAndBufferRow(const unsigned char * const rowdata, add the packbits-compressed representation of it to the buffer with handle 'rasterBufferP'. -----------------------------------------------------------------------------*/ - unsigned int position; - /* byte position within the row */ - - position = 0; /* Start at beginning of row */ + unsigned char * compressedData; + size_t compressedDataCt; + unsigned int byteCt; - while (position < rowbytes - 1) { - unsigned char output[128]; - int count; + pm_rlenc_allocoutbuf(&compressedData, rowbytes, PM_RLE_PACKBITS); + pm_rlenc_compressbyte(rowdata, compressedData, PM_RLE_PACKBITS, + rowbytes, &compressedDataCt); - computeNextPackbitsRun(rowdata, rowbytes, position, - &position, output, &count); + for (byteCt = 0; byteCt < compressedDataCt; ++byteCt) + addByteToBuffer(rasterBufferP, compressedData[byteCt]); - addPackbitsRunToBuffer(rowdata, rowbytes, position, output, count, - rasterBufferP); - } + free(compressedData); } @@ -1029,7 +960,13 @@ bufferRaster(xel ** const xels, createBuffer(rasterBufferPP); MALLOCARRAY_NOFAIL(rowdata, rowbytes); - MALLOCARRAY_NOFAIL(lastrow, rowbytes); + if (compression == COMP_SCANLINE) + MALLOCARRAY_NOFAIL(lastrow, rowbytes); + else + lastrow = NULL; + + /* clear pad bytes to suppress valgrind error */ + rowdata[rowbytes - 1] = rowdata[rowbytes - 2] = 0x00; /* And write out the data. */ for (row = 0; row < rows; ++row) { @@ -1041,8 +978,9 @@ bufferRaster(xel ** const xels, if (compression == COMP_SCANLINE) memcpy(lastrow, rowdata, rowbytes); } - free(lastrow); free(rowdata); + if (compression == COMP_SCANLINE) + free(lastrow); } diff --git a/doc/HISTORY b/doc/HISTORY index 3a1a0c25..d9541c6a 100644 --- a/doc/HISTORY +++ b/doc/HISTORY @@ -17,6 +17,10 @@ not yet BJH Release 10.80.00 contrast images (i.e. little difference between foreground and background). + pnmtopalm: fix incorrect output with certain input files and + -packbits_compression. Always broken. -packbits_compression + was new in Netpbm 10.27 (March 2005). + libnetpbm: font facilities: fix invalid memory reference with certain font files. |