1

I have a trivial RGB file saved as TIFF in Photoshop, 1000 or so pixels wide. The first row consists of 3 pixels all of which are hex 4B red, B0 green, 78 blue, and the rest of the row white.

The strip is LZW-encoded and the initial bytes of the strip are:

80 12 D6 07 80 04 16 0C B4 27 A1 E0 D0 B8 64 36 ... (actually only the first 7 or so bytes are significant to my question.)

In 9-bit segments this is:

 100000000  001001011  010110000  001111000  000000000  100000101  100000110  ...
  (0x100)     (0x4B)     (0xB0)     (0x78)     (0x00)    (0x105)    (0x106)

From what I understand 256 (0x100) is a reset code, but why is the first extended code after that 261 (0x105) instead of 257? I would expect whatever dictionary entry this points to to be the 4B/B0 pair for the second pixel (which it may well be), but how would the decompression algorithm know to place 4B/B0 at 261 instead of 257? Can someone explain what I'm missing here? Might there be something elsewhere in the .tif file that would indicate this? Thanks very much. ~

regger
  • 41
  • 7
  • The LZW encoder is always generating longer strings of the last characters emitted. The 0x105 code is the next logical code which represents the string of the last 4 characters. It is how you repeat the first 4-byte pixel. A lower valued code would be a partial string (less than 4 bytes) or an incomplete pixel. – BitBank Jan 30 '15 at 18:31
  • @BitBank - thank you - makes sense now. I should have worked it through further... – regger Jan 30 '15 at 19:07

1 Answers1

1

Let's see

256 (100h) is Clear
257 (101h) is EOF

in your case, then

4Bh B0h is 258 (102h)
B0h 78h is 259 (103h)
78h 00h is 260 (104h)
00h 00h is 261 (105h)

Looks good to me. LZW can actually encode one character ahead of what's been added to the table.

  • Thanks...evidently I have to look ahead (indefinitely?) until the code in question is inserted in the dictionary. That didn't appear to be necessary when walking through the pseudo-code I came across. This'll work now. OT - trying to up vote but "need reputation level 15?" to do so? Is there a way to acknowledge this? Thanks again... – regger Jan 30 '15 at 19:11
  • I was not aware that you needed a certain rep level to up-vote, I don't remember that far back :) - no matter, you can come back some other time and do it if you like. – 500 - Internal Server Error Jan 30 '15 at 19:53
  • Maybe someone out there with no particular interest in this subject can cast a proxy vote for me then? :) – regger Jan 30 '15 at 20:24
  • 1
    @haraldK - Thanks, just did so - didn't know it was that easy - wish my real life rep was that easy to bump up :) – regger Feb 02 '15 at 15:13