about summary refs log tree commit diff
path: root/lib/libpbm1.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2019-06-28 23:07:55 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2019-06-28 23:07:55 +0000
commit11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32 (patch)
tree7c40f096dd973943ef563ec87b2a68d8205db4fb /lib/libpbm1.c
parent89c6ec14eb7514630aea5abc4b90b51d1473d33a (diff)
downloadnetpbm-mirror-11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32.tar.gz
netpbm-mirror-11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32.tar.xz
netpbm-mirror-11fd0bc3fdbe7b5eb9266a728a81d0bcac91fe32.zip
Promote Stable to Super_stable
git-svn-id: http://svn.code.sf.net/p/netpbm/code/super_stable@3640 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/libpbm1.c')
-rw-r--r--lib/libpbm1.c79
1 files changed, 44 insertions, 35 deletions
diff --git a/lib/libpbm1.c b/lib/libpbm1.c
index fc20071c..49ab7fdf 100644
--- a/lib/libpbm1.c
+++ b/lib/libpbm1.c
@@ -18,9 +18,10 @@
 
 #include <stdio.h>
 
-#include "pm_c_util.h"
-#include "mallocvar.h"
-#include "shhopt.h"
+#include "netpbm/pm_c_util.h"
+#include "netpbm/mallocvar.h"
+#include "netpbm/shhopt.h"
+
 #include "pbm.h"
 
 
@@ -57,27 +58,28 @@ pbm_nextimage(FILE *file, int * const eofP) {
 
 
 void
-pbm_check(FILE * file, const enum pm_check_type check_type, 
-          const int format, const int cols, const int rows,
-          enum pm_check_code * const retval_p) {
+pbm_check(FILE *               const fileP,
+          enum pm_check_type   const checkType, 
+          int                  const format,
+          int                  const cols,
+          int                  const rows,
+          enum pm_check_code * const retvalP) {
 
     if (rows < 0)
         pm_error("Invalid number of rows passed to pbm_check(): %d", rows);
     if (cols < 0)
         pm_error("Invalid number of columns passed to pbm_check(): %d", cols);
     
-    if (check_type != PM_CHECK_BASIC) {
-        if (retval_p) *retval_p = PM_CHECK_UNKNOWN_TYPE;
+    if (checkType != PM_CHECK_BASIC) {
+        if (retvalP)
+            *retvalP = PM_CHECK_UNKNOWN_TYPE;
     } else if (format != RPBM_FORMAT) {
-        if (retval_p) *retval_p = PM_CHECK_UNCHECKABLE;
+        if (retvalP)
+            *retvalP = PM_CHECK_UNCHECKABLE;
     } else {        
-        pm_filepos const bytes_per_row = (cols+7)/8;
-        pm_filepos const need_raster_size = rows * bytes_per_row;
-#ifdef LARGEFILEDEBUG
-        pm_message("pm_filepos passed to pm_check() is %u bytes",
-                   sizeof(pm_filepos));
-#endif
-        pm_check(file, check_type, need_raster_size, retval_p);
+        pm_filepos const bytesPerRow    = (cols+7)/8;
+        pm_filepos const needRasterSize = rows * bytesPerRow;
+        pm_check(fileP, checkType, needRasterSize, retvalP);
     }
 }
 
@@ -113,23 +115,36 @@ static unsigned int const p[256] = {
 
 static int
 bitpop(const unsigned char * const packedRow,
-       unsigned int          const cols) {
+       unsigned int          const cols,
+       unsigned int          const offset) {
 /*----------------------------------------------------------------------------
-  Return the number of 1 bits in 'packedRow'.
+  Return the number of 1 bits in 'packedRow', ignoring 0 to 7 bits
+  at the row start (= on the left edge), indicated by offset.
 -----------------------------------------------------------------------------*/
-    unsigned int const colByteCnt  = pbm_packed_bytes(cols);
-    unsigned int const fullByteCnt = cols/8;
+    unsigned int const fullLength = cols + offset;
 
-    unsigned int i;
     unsigned int sum;
 
-    sum = 0;  /* initial value */
+    if (fullLength <= 8) {
+        /* All bits are in a single byte */
+        sum = bitpop8((packedRow[0] << offset ) & (0xff << (8 - cols)));
+    } else {
+        unsigned int const colByteCnt  = pbm_packed_bytes(fullLength);
+        unsigned int const fullByteCnt = fullLength/8;
+
+        unsigned int i;
+
+        /* First byte, whether it is full or not */
+        sum = bitpop8(packedRow[0] << offset );
 
-    for (i = 0; i < fullByteCnt; ++i)
-        sum += bitpop8(packedRow[i]);
+        /* Second byte to last full byte */
+        for (i = 1; i < fullByteCnt; ++i)
+            sum += bitpop8(packedRow[i]);
 
-    if (colByteCnt > fullByteCnt)
-        sum += bitpop8(packedRow[i] >> (8-cols%8));
+        /* Partial byte at the right end */
+        if (colByteCnt > fullByteCnt)
+            sum += bitpop8(packedRow[i] >> (8 - fullLength%8));
+    }
 
     return sum;
 }
@@ -151,19 +166,13 @@ pbm_backgroundbitrow(unsigned const char * const packedBits,
 
     unsigned int retval;
 
-    unsigned int firstbit, lastbit;
-    unsigned int totalBitpop, headBitpop;
-
-    firstbit = (row[0] >> (7-rs)) & 0x01;
-    lastbit  = (row[last] >> (7- (cols+rs-1)%8)) & 0x01;
+    bool const firstbit = (row[0] >> (7-rs)) & 0x01;
+    bool const lastbit  = (row[last] >> (7- (cols+rs-1)%8)) & 0x01;
 
     if (firstbit == lastbit)
         retval = firstbit;
     else {
-        totalBitpop = bitpop(row, cols + rs);
-        headBitpop  = (rs == 0) ? 0 : bitpop(row, rs);
-
-        if (totalBitpop - headBitpop >= cols/2)
+        if (bitpop(row, cols, rs) >= cols/2)
             retval = PBM_BLACK;
         else
             retval = PBM_WHITE;