From 853d1761ee6b8796373a977603b31707b9c6459d Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 16 May 2015 01:53:28 +0000 Subject: Add and use pbm_cleanrowend_packed git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2488 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- converter/other/bmptopnm.c | 11 ++--------- converter/other/pnmtops.c | 8 ++------ converter/pbm/brushtopbm.c | 6 +----- converter/pbm/pbmtomgr.c | 6 +----- converter/pbm/pbmtoxbm.c | 9 +-------- converter/pbm/xbmtopbm.c | 8 ++------ converter/ppm/ppmtobmp.c | 8 ++------ editor/pamenlarge.c | 7 ++----- editor/pbmclean.c | 12 ++++-------- editor/pbmpscale.c | 11 ++--------- editor/pnminvert.c | 7 ++----- generator/pbmmake.c | 21 +++++++++------------ lib/libpbm2.c | 22 +++++++++++++++++++++- lib/pbm.h | 4 ++++ 14 files changed, 55 insertions(+), 85 deletions(-) diff --git a/converter/other/bmptopnm.c b/converter/other/bmptopnm.c index 84d39989..a069092f 100644 --- a/converter/other/bmptopnm.c +++ b/converter/other/bmptopnm.c @@ -1442,8 +1442,6 @@ writeRasterPbm(unsigned char ** const bmpRaster, We destroy *bmpRaster as a side effect. -----------------------------------------------------------------------------*/ - unsigned int const charBits = (sizeof(unsigned char) * 8); - /* Number of bits in a character */ unsigned int const colChars = pbm_packed_bytes(cols); int row; @@ -1463,13 +1461,8 @@ writeRasterPbm(unsigned char ** const bmpRaster, for (i = 0; i < colChars; ++i) bitrow[i] = ~bitrow[i]; /* flip all pixels */ } - - if (cols % 8 > 0) { - /* adjust final partial byte */ - bitrow[colChars-1] >>= charBits - cols % charBits; - bitrow[colChars-1] <<= charBits - cols % charBits; - } - + + pbm_cleanrowend_packed(bitrow, cols); pbm_writepbmrow_packed(stdout, bitrow, cols, FALSE); } } diff --git a/converter/other/pnmtops.c b/converter/other/pnmtops.c index cf6b2873..6cd6be95 100644 --- a/converter/other/pnmtops.c +++ b/converter/other/pnmtops.c @@ -1853,18 +1853,14 @@ convertRowPbm(struct pam * const pamP, ----------------------------------------------------------------------*/ unsigned int colChar; unsigned int const colChars = pbm_packed_bytes(pamP->width); - unsigned int const padRight = (8 - pamP->width %8) %8; pbm_readpbmrow_packed(pamP->file, bitrow, pamP->width, pamP->format); for (colChar = 0; colChar < colChars; ++colChar) bitrow[colChar] = ~ bitrow[colChar]; - if (padRight > 0) { - bitrow[colChars-1] >>= padRight; /* Zero clear padding beyond */ - bitrow[colChars-1] <<= padRight; /* right edge */ - } - + /* Zero clear padding beyond right edge */ + pbm_cleanrowend_packed(bitrow, pamP->width); writeFile(bitrow, colChars, "PBM reader", fP); } diff --git a/converter/pbm/brushtopbm.c b/converter/pbm/brushtopbm.c index c50fe8a1..ebd817be 100644 --- a/converter/pbm/brushtopbm.c +++ b/converter/pbm/brushtopbm.c @@ -93,11 +93,7 @@ main(int argc, const char ** argv) { bitrow[i] = ~bitrow[i]; /* Clean off remainder of fractional last character */ - if (cols % 8 > 0) { - unsigned int const colChars = pbm_packed_bytes(cols); - bitrow[colChars-1] >>= 8 - cols % 8; - bitrow[colChars-1] <<= 8 - cols % 8; - } + pbm_cleanrowend_packed(bitrow, cols); pbm_writepbmrow_packed(stdout, bitrow, cols, 0); } diff --git a/converter/pbm/pbmtomgr.c b/converter/pbm/pbmtomgr.c index d12e6635..e8e30148 100644 --- a/converter/pbm/pbmtomgr.c +++ b/converter/pbm/pbmtomgr.c @@ -89,11 +89,7 @@ main(int argc, size_t bytesWritten; pbm_readpbmrow_packed(ifP, bitrow, cols, format); - - if (padright > 0) { - bitrow[bytesPerRow-1] >>= padright; - bitrow[bytesPerRow-1] <<= padright; - } + pbm_cleanrowend_packed(bitrow, cols); bytesWritten = fwrite(bitrow, 1, bytesPerRow, stdout); if (bytesWritten != bytesPerRow ) diff --git a/converter/pbm/pbmtoxbm.c b/converter/pbm/pbmtoxbm.c index 2b59a17c..2d71fd7b 100644 --- a/converter/pbm/pbmtoxbm.c +++ b/converter/pbm/pbmtoxbm.c @@ -342,17 +342,10 @@ convertRaster(FILE * const ifP, bitrow[bitrowBytes-1] = 0; for (row = 0; row < rows; ++row) { - int const bitrowInBytes = pbm_packed_bytes(cols); - int const padrightIn = bitrowInBytes * 8 - cols; - unsigned int i; pbm_readpbmrow_packed(ifP, bitrow, cols, format); - - if (padrightIn > 0) { - bitrow[bitrowInBytes - 1] >>= padrightIn; - bitrow[bitrowInBytes - 1] <<= padrightIn; - } + pbm_cleanrowend_packed(bitrow, cols); for (i = 0; i < bitrowBytes; ++i) putitem(bitrow[i]); diff --git a/converter/pbm/xbmtopbm.c b/converter/pbm/xbmtopbm.c index 0cbebc5e..bbf4e395 100644 --- a/converter/pbm/xbmtopbm.c +++ b/converter/pbm/xbmtopbm.c @@ -362,12 +362,8 @@ main(int argc, for (i = 0; i < bytesPerRow; ++i) bitrow[i] = bitreverse[*p++]; - - if (cols % 8 > 0) { - bitrow[bytesPerRow-1] >>= 8 - cols % 8; - bitrow[bytesPerRow-1] <<= 8 - cols % 8; - } - + + pbm_cleanrowend_packed(bitrow, cols); pbm_writepbmrow_packed(stdout, bitrow, cols, 0); } diff --git a/converter/ppm/ppmtobmp.c b/converter/ppm/ppmtobmp.c index 24b1b3e5..78cce396 100644 --- a/converter/ppm/ppmtobmp.c +++ b/converter/ppm/ppmtobmp.c @@ -821,7 +821,6 @@ doPbm(FILE * const ifP, 32 bit borders and that in BMP the bottom row comes first in order. */ - int const CHARBITS = (sizeof(unsigned char)*8); int const colChars = pbm_packed_bytes(cols); int const adjustedCols = (cols+31) /32 * 32; int const packedBytes = adjustedCols /8; @@ -853,11 +852,8 @@ doPbm(FILE * const ifP, some BMP viewers may get confused with that. */ - if (cols % 8 >0) { - /* adjust final partial byte */ - thisRow[colChars-1] >>= CHARBITS - cols % CHARBITS; - thisRow[colChars-1] <<= CHARBITS - cols % CHARBITS; - } + /* Clean off remainder of fractional last character */ + pbm_cleanrowend_packed(thisRow, cols); } bmpEncodePbm(ofP, class, cols, rows, bitrow); diff --git a/editor/pamenlarge.c b/editor/pamenlarge.c index dd61f2cf..187bfb6e 100644 --- a/editor/pamenlarge.c +++ b/editor/pamenlarge.c @@ -312,11 +312,8 @@ enlargePbm(struct pam * const inpamP, pbm_readpbmrow_packed(inpamP->file, inrow, inpamP->width, inpamP->format); - if (outcols % 8 > 0) { - /* clean final partial byte */ - inrow[inColChars-1] >>= 8 - inpamP->width % 8; - inrow[inColChars-1] <<= 8 - inpamP->width % 8; - } + if (outcols % 8 > 0) /* clean final partial byte */ + pbm_cleanrowend_packed(inrow, inpamP->width); enlargePbmRowHorizontally(inpamP, inrow, inColChars, outColChars, scaleFactor, outrow); diff --git a/editor/pbmclean.c b/editor/pbmclean.c index 6d813090..46e7dee6 100644 --- a/editor/pbmclean.c +++ b/editor/pbmclean.c @@ -361,10 +361,8 @@ setupInputBuffers(FILE * const ifP, pbm_readpbmrow_packed(ifP, nextRow, cols, format); - if (cols % 8 > 0){ - nextRow[pbm_packed_bytes(cols) -1 ] >>= (8 - cols % 8); - nextRow[pbm_packed_bytes(cols) -1 ] <<= (8 - cols % 8); - } + pbm_cleanrowend_packed(nextRow, cols); + *bufferP = buffer; *edgeRowP = edgeRow; *thisRowP = &edgeRow[1]; @@ -418,10 +416,8 @@ cleanSimple(FILE * const ifP, handling of the initial edgerow. */ pbm_readpbmrow_packed(ifP, nextRow, cols, format); - if (cols % 8 > 0){ - nextRow[pbm_packed_bytes(cols) -1 ] >>= (8 - cols % 8); - nextRow[pbm_packed_bytes(cols) -1 ] <<= (8 - cols % 8); - } + pbm_cleanrowend_packed(nextRow, cols); + } else /* Bottom of image. */ nextRow = &edgeRow[1]; diff --git a/editor/pbmpscale.c b/editor/pbmpscale.c index 15302c56..9ab89350 100644 --- a/editor/pbmpscale.c +++ b/editor/pbmpscale.c @@ -412,11 +412,7 @@ main(int argc, const char ** argv) { /* Read the top line into nextrow and clean the right end. */ pbm_readpbmrow_packed(ifP, nextrow, cols, format); - - if (cols % 8 > 0) { - nextrow[pbm_packed_bytes(cols) - 1] >>= (8 - cols % 8); - nextrow[pbm_packed_bytes(cols) - 1] <<= (8 - cols % 8); - } + pbm_cleanrowend_packed(nextrow, cols); outrow = pbm_allocrow_packed(outcols); for (i = 0; i < pbm_packed_bytes(outcols); ++i) @@ -440,10 +436,7 @@ main(int argc, const char ** argv) { This provision is for proper handling of the initial edgerow. */ pbm_readpbmrow_packed(ifP, nextrow, cols, format); - if (cols % 8 > 0) { - nextrow[pbm_packed_bytes(cols) - 1] >>= (8 - cols % 8); - nextrow[pbm_packed_bytes(cols) - 1] <<= (8 - cols % 8); - } + pbm_cleanrowend_packed(nextrow, cols); } else /* Bottom of image. */ nextrow = edgerow; diff --git a/editor/pnminvert.c b/editor/pnminvert.c index ceb1ef81..4bee8837 100644 --- a/editor/pnminvert.c +++ b/editor/pnminvert.c @@ -50,11 +50,8 @@ invertPbm(FILE * const ifP, for (colChar = 0; colChar < colChars; ++colChar) bitrow[colChar] = ~ bitrow[colChar]; - /* Clean off remainder of fractional last character */ - if (cols % CHARBITS > 0) { - bitrow[colChars-1] >>= CHARBITS - cols % CHARBITS; - bitrow[colChars-1] <<= CHARBITS - cols % CHARBITS; - } + /* Clean off remainder of fractional last character and write */ + pbm_cleanrowend_packed(bitrow, cols); pbm_writepbmrow_packed(ofP, bitrow, cols, 0); } pbm_freerow_packed(bitrow); diff --git a/generator/pbmmake.c b/generator/pbmmake.c index fb47bd10..600440f0 100644 --- a/generator/pbmmake.c +++ b/generator/pbmmake.c @@ -101,13 +101,11 @@ writeGrayRaster(unsigned int const cols, bitrow1[i] = (PBM_WHITE*0x55) | (PBM_BLACK*0xaa); /* 0xaa = 10101010 ; 0x55 = 01010101 */ } - if (cols % 8 > 0) { - bitrow0[lastCol] >>= 8 - cols % 8; - bitrow0[lastCol] <<= 8 - cols % 8; - bitrow1[lastCol] >>= 8 - cols % 8; - bitrow1[lastCol] <<= 8 - cols % 8; - } - if (rows > 1) { + + pbm_cleanrowend_packed(bitrow0, cols); + pbm_cleanrowend_packed(bitrow1, cols); + + if (rows > 1) { unsigned int row; for (row = 1; row < rows; row += 2) { pbm_writepbmrow_packed(ofP, bitrow0, cols, 0); @@ -139,11 +137,10 @@ writeSingleColorRaster(unsigned int const cols, for (i = 0; i <= lastCol; ++i) bitrow0[i] = color*0xff; - if (cols % 8 > 0) { - bitrow0[lastCol] >>= 8 - cols % 8; - bitrow0[lastCol] <<= 8 - cols % 8; - /* row end trimming, really not necessary with white */ - } + if (color != 0) + pbm_cleanrowend_packed(bitrow0, cols); + /* row end trimming, not necessary with white */ + { unsigned int row; for (row = 0; row < rows; ++row) diff --git a/lib/libpbm2.c b/lib/libpbm2.c index 0d824877..e12b1e33 100644 --- a/lib/libpbm2.c +++ b/lib/libpbm2.c @@ -10,6 +10,7 @@ ** implied warranty. */ +#include #include #include "pbm.h" @@ -257,7 +258,26 @@ pbm_readpbmrow_bitoffset(FILE * const ifP, window[last] = leftBits | rightBits; } -} +} + + + +void +pbm_cleanrowend_packed(unsigned char * const packedBits, + unsigned int const cols) { +/*---------------------------------------------------------------------------- + Set fractional "don't care" bits at end of row to zero. +----------------------------------------------------------------------------*/ + unsigned int const last = pbm_packed_bytes(cols) - 1; + unsigned int const bitsPerChar = 8; + + assert(cols > 0); + + if (cols % bitsPerChar > 0) { + packedBits[last] >>= bitsPerChar - cols % bitsPerChar; + packedBits[last] <<= bitsPerChar - cols % bitsPerChar; + } +} diff --git a/lib/pbm.h b/lib/pbm.h index 520e4331..a29adb48 100644 --- a/lib/pbm.h +++ b/lib/pbm.h @@ -87,6 +87,10 @@ pbm_readpbmrow_bitoffset(FILE * const fileP, int const format, unsigned int const offset); +void +pbm_cleanrowend_packed(unsigned char * const packedBits, + unsigned int const cols); + void pbm_writepbminit(FILE * const fileP, int const cols, -- cgit 1.4.1