about summary refs log tree commit diff
path: root/lib/libpbm2.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2008-07-06 17:07:36 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2008-07-06 17:07:36 +0000
commit773463d6db696dc2910c83b7fa311bd91ca1d69c (patch)
tree4be0f5f94b5cd76a5dd1e2e39c7a284e4e5f6f11 /lib/libpbm2.c
parente96eb23dd2b9056014120b132801a35c8856dec6 (diff)
downloadnetpbm-mirror-773463d6db696dc2910c83b7fa311bd91ca1d69c.tar.gz
netpbm-mirror-773463d6db696dc2910c83b7fa311bd91ca1d69c.tar.xz
netpbm-mirror-773463d6db696dc2910c83b7fa311bd91ca1d69c.zip
Add fast PBM path for Pnmpad
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@661 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/libpbm2.c')
-rw-r--r--lib/libpbm2.c70
1 files changed, 63 insertions, 7 deletions
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;