From 773463d6db696dc2910c83b7fa311bd91ca1d69c Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sun, 6 Jul 2008 17:07:36 +0000 Subject: Add fast PBM path for Pnmpad git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@661 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- lib/libpbm2.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ lib/pbm.h | 16 +++++++++++--- 2 files changed, 76 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/libpbm2.c b/lib/libpbm2.c index df1443a3..3dbd7173 100644 --- a/lib/libpbm2.c +++ b/lib/libpbm2.c @@ -157,8 +157,8 @@ pbm_readpbmrow( file, bitrow, cols, format ) void -pbm_readpbmrow_packed(FILE * const file, - unsigned char * const packed_bits, +pbm_readpbmrow_packed(FILE * const fileP, + unsigned char * const packedBits, int const cols, int const format) { @@ -169,22 +169,22 @@ pbm_readpbmrow_packed(FILE * const file, /* We first clear the return buffer, then set ones where needed */ for (byteIndex = 0; byteIndex < pbm_packed_bytes(cols); ++byteIndex) - packed_bits[byteIndex] = 0x00; + packedBits[byteIndex] = 0x00; for (col = 0; col < cols; ++col) { unsigned char mask; - mask = getbit(file) << (7 - col % 8); - packed_bits[col / 8] |= mask; + mask = getbit(fileP) << (7 - col % 8); + packedBits[col / 8] |= mask; } } break; case RPBM_FORMAT: { int bytes_read; - bytes_read = fread(packed_bits, 1, pbm_packed_bytes(cols), file); + bytes_read = fread(packedBits, 1, pbm_packed_bytes(cols), fileP); if (bytes_read < pbm_packed_bytes(cols)) { - if (feof(file)) + if (feof(fileP)) if (bytes_read == 0) pm_error("Attempt to read a raw PBM image row, but " "no more rows left in file."); @@ -203,6 +203,62 @@ pbm_readpbmrow_packed(FILE * const file, +void +pbm_readpbmrow_bitoffset(FILE * const ifP, + unsigned char * const packedBits, + int const cols, + int const format, + unsigned int const offset) { +/*---------------------------------------------------------------------------- + Read PBM packed bitrow from file 'ifP' (raster format given by + 'cols' and 'format') and shift right 'offset' bits. + + Read it into packedBits[], preserving surrounding image data. + + Logic not tested for negative offsets. +-----------------------------------------------------------------------------*/ + unsigned int const rsh = offset % 8; + unsigned int const lsh = (8 - rsh) % 8; + unsigned char * const window = &packedBits[offset/8]; + /* Area of packed row buffer into which we read the image data. + Aligned to nearest byte boundary to the left, so the first + few bits might contain original data, not output. + */ + unsigned int const last = pbm_packed_bytes(cols+rsh) - 1; + /* Position within window of rightmost byte after shift */ + + /* The original leftmost and rightmost chars. */ + unsigned char const origHead = window[0]; + unsigned char const origEnd = window[last]; + + pbm_readpbmrow_packed(ifP, window, cols, format); + + if (rsh > 0) { + /* Target slot doesn't start on byte boundary; right-shift. */ + unsigned char carryover; + unsigned int i; + + carryover = (origHead >> lsh) << lsh; + + for (i = 0; i <= last; ++i) { + unsigned char const t = window[i] << lsh; + window[i] = carryover | window[i] >> rsh; + carryover = t; + } + } + + if ((cols + rsh) % 8 > 0) { + /* Adjust rightmost char */ + unsigned int const trs = (cols + rsh) % 8; + unsigned int const tls = 8 - trs; + unsigned char const origEndShift = (origEnd << trs) >> trs; + + window[last] = (window[last] >> tls) << tls | origEndShift; + } +} + + + bit** pbm_readpbm( file, colsP, rowsP ) FILE* file; diff --git a/lib/pbm.h b/lib/pbm.h index b9ef3479..840e1491 100644 --- a/lib/pbm.h +++ b/lib/pbm.h @@ -61,9 +61,19 @@ pbm_allocrow(unsigned int const cols); bit** pbm_readpbm(FILE* file, int* colsP, int* rowsP); void pbm_readpbminit(FILE* file, int* colsP, int* rowsP, int* formatP); void pbm_readpbmrow(FILE* file, bit* bitrow, int cols, int format); -void pbm_readpbmrow_packed( - FILE* const file, unsigned char * const packed_bits, - const int cols, const int format); + +void +pbm_readpbmrow_packed(FILE * const file, + unsigned char * const packedBits, + int const cols, + int const format); + +void +pbm_readpbmrow_bitoffset(FILE * const fileP, + unsigned char * const packedBits, + int const cols, + int const format, + unsigned int const offset); void pbm_writepbminit(FILE * const fileP, -- cgit 1.4.1