 -  err = gFalse;
 -  if (codingLine[a0] >= columns) {
 +  if (outputBits == 0) {
- 
++
 +    // if at eof just return EOF
 +    if (eof) {
 +      return EOF;
 +    }
 +
 +    err = gFalse;
-+
+ 
      // 2-D encoding
      if (nextLine2D) {
 -      // state:

       }
-    } while (bits > 0 && codingLine[a0] < columns);
+    } while (bits);
   }
-  buf = black ? (ret ^ 0xff) : ret;
+  if (black) {
+    buf ^= 0xff;
+  }
   return buf;
 }
 

-	++a0;
-	if (codingLine[a0] >= columns) {
-	  break;
-	}
-	code2 = 0;
-	do {
-	  code2 += code3 = getBlackCode();
-	} while (code3 >= 64);
-	codingLine[a0+1] = codingLine[a0] + code2;
-	++a0;
-	if (codingLine[a0] >= columns) {
-	  break;
+	if (blackPixels) {
+	  do {
+	    code1 += code3 = getBlackCode();
