private int ReadEOL(bool isFirstEOL) {
if (fillBits == 0) {
int next12Bits = NextNBits(12);
if (isFirstEOL && next12Bits == 0) {
// Might have the case of EOL padding being used even
// though it was not flagged in the T4Options field.
// This was observed to be the case in TIFFs produced
// by a well known vendor who shall remain nameless.
if (NextNBits(4) == 1) {
// EOL must be padded: reset the fillBits flag.
fillBits = 1;
return 1;
}
}
if (next12Bits != 1) {
throw new Exception("Scanline must begin with EOL code word.");
}
} else if (fillBits == 1) {
// First EOL code word xxxx 0000 0000 0001 will occur
// As many fill bits will be present as required to make
// the EOL code of 12 bits end on a byte boundary.
int bitsLeft = 8 - bitPointer;
if (NextNBits(bitsLeft) != 0) {
throw new Exception("All fill bits preceding EOL code must be 0.");
}
// If the number of bitsLeft is less than 8, then to have a 12
// bit EOL sequence, two more bytes are certainly going to be
// required. The first of them has to be all zeros, so ensure
// that.
if (bitsLeft < 4) {
if (NextNBits(8) != 0) {
throw new Exception("All fill bits preceding EOL code must be 0.");
}
}
// There might be a random number of fill bytes with 0s, so
// loop till the EOL of 0000 0001 is found, as long as all
// the bytes preceding it are 0's.
int n;
while ((n = NextNBits(8)) != 1) {
// If not all zeros
if (n != 0) {
throw new Exception("All fill bits preceding EOL code must be 0.");
}
}
}
// If one dimensional encoding mode, then always return 1
if (oneD == 0) {
return 1;
} else {
// Otherwise for 2D encoding mode,
// The next one bit signifies 1D/2D encoding of next line.
return NextLesserThan8Bits(1);
}
}