diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2008-09-27 02:59:58 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2008-09-27 02:59:58 +0000 |
commit | 4b4b88014200757f6f7f8864c85138a56835c794 (patch) | |
tree | cea2a438b384f7a4fbf85aabb9fbd23093908d49 /lib/libpbm2.c | |
parent | f04d41f61e5cf83835c6bc85122e65cfe2cf9857 (diff) | |
download | netpbm-mirror-4b4b88014200757f6f7f8864c85138a56835c794.tar.gz netpbm-mirror-4b4b88014200757f6f7f8864c85138a56835c794.tar.xz netpbm-mirror-4b4b88014200757f6f7f8864c85138a56835c794.zip |
Rebase to 10.44.00
git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@738 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/libpbm2.c')
-rw-r--r-- | lib/libpbm2.c | 73 |
1 files changed, 66 insertions, 7 deletions
diff --git a/lib/libpbm2.c b/lib/libpbm2.c index df1443a3..a8e4b0f6 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,65 @@ 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 rightBits = + ((unsigned char)(origEnd << trs) >> trs); + unsigned char const leftBits = + ((unsigned char)(window[last] >> tls) << tls); + + window[last] = leftBits | rightBits; + } +} + + + bit** pbm_readpbm( file, colsP, rowsP ) FILE* file; |