Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TIFF LZW style - When to change the code length #36

Open
lpatiny opened this issue Oct 30, 2021 · 1 comment
Open

TIFF LZW style - When to change the code length #36

lpatiny opened this issue Oct 30, 2021 · 1 comment

Comments

@lpatiny
Copy link
Member

lpatiny commented Oct 30, 2021

Looking at the spec: https://www.fileformat.info/format/tiff/corion-lzw.htm

The function GetNextCode() retrieves the next code from the LZW-
coded data. It must keep track of bit boundaries. It knows that
the first code that it gets will be a 9-bit code. We add a table
entry each time we get a code, so GetNextCode() must switch over
to 10-bit codes as soon as string #511 is stored into the table.
We need to change the code length as soon at #511 is stored.

However in the code the change is done at 510:

tiff/src/lzw.ts

Lines 94 to 96 in 73ca971

if (this.tableLength + 1 === 2 ** this.currentBitLength) {
this.currentBitLength++;
}

I don't know what is the correct version but the confusion could be due to TIFF version:

https://stackoverflow.com/questions/26366659/whats-special-about-tiff-5-0-style-lzw-compression

  • LZW codes are written to the stream in reversed bit order.
  • "New-style" increases the code size one symbol earlier than "old-style" (so-called "Early Change").

The current implementation in 'debug-lzw' branch seems however correct based on the lzw images we have and the comparison with convert from imagemagick.

199aa6d

@targos
Copy link
Member

targos commented Oct 31, 2021

However in the code the change is done at 510:

I think it's just because #511 is if you count from one, and we count from zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants