The pixels of each image are cut into strips or tiles, which can sit anywhere in the file. Each strip or tile is compressed on its own, so it can be read without the others. This page explains how the pieces are found and laid out, then how each compression scheme in the TIFF 6.0 specification and its technical notes works. The fields named here are listed on Tags and their codes on Tag values.
Strips
A striped image is cut into bands of whole rows. RowsPerStrip gives the rows in each band; the last band holds what is left and is not padded.
- StripsPerImage = floor((ImageLength + RowsPerStrip − 1) ÷ RowsPerStrip). With ImageLength 24 and RowsPerStrip 10 there are three strips of 10, 10 and 4 rows.
- StripOffsets and StripByteCounts hold one value per strip: where it starts in the file and how many bytes it takes after compression.
- Strips may be stored in any order and anywhere, before or after the directory. Never assume strip N + 1 follows strip N. An offset of 0 is not allowed to mean anything special.
- The specification recommends about 8 KB per strip, and no more than 64 KB for old MS-DOS and Windows readers.
Tiles
A tiled image uses TileWidth, TileLength, TileOffsets and TileByteCounts in place of the strip fields; a file must not have both.
- TilesAcross = (ImageWidth + TileWidth − 1) ÷ TileWidth, TilesDown = (ImageLength + TileLength − 1) ÷ TileLength, TilesPerImage = TilesAcross × TilesDown, in integer arithmetic.
- TileWidth and TileLength must be multiples of 16. Tiles need not be square, and about 4 to 32 KB each before compression is recommended.
- Every tile has the full tile size. Tiles on the right and bottom edges are padded past the image; readers show only ImageWidth × ImageLength pixels, and the padding value does not matter.
- Offsets run left to right, then top to bottom. Each row of a tile counts as a separate scan line when compressing.
Planes and sample order
With PlanarConfiguration 1 (chunky) the components of each pixel are stored together in PhotometricInterpretation’s order: R G B R G B… With 2 (planar) each component has its own strips or tiles. The offsets then form SamplesPerPixel groups: every strip or tile of the first component, then every one of the second, and so on. Planar files need not be supported by baseline readers.
Extra samples, such as alpha, are always the last components of a pixel, and their size comes from BitsPerSample like any other. With associated alpha the colour components are stored premultiplied: a pixel (r, g, b) with opacity A, all scaled to 0 to 1, is stored as (A·r, A·g, A·b, A).
Uncompressed samples (Compression 1)
- Samples are packed as tightly as possible, with no unused bits except at the end of a row.
- If every sample is 16 bits the data is an array of SHORTs; if every sample is 32 bits, an array of LONGs; otherwise an array of bytes. SHORTs and LONGs use the file’s byte order.
- Each row starts on a byte boundary (a SHORT or LONG boundary in those two cases) and is padded to one.
- With FillOrder 1, the default, the first pixel of a row is in the most significant bit of its byte. A 1-bit row of width W takes (W + 7) ÷ 8 bytes.
- Every other compression scheme, once decoded, produces data laid out exactly like this.
YCbCr data units
A chunky YCbCr image is a sequence of data units. Each unit is the luma samples of a block of H × V pixels, row by row, then one Cb and one Cr sample, where H and V are the YCbCrSubSampling factors. With H = 4 and V = 2 the order is:
Y00 Y01 Y02 Y03 Y10 Y11 Y12 Y13 Cb00 Cr00 Y04 Y05 …
A planar YCbCr image stores the full-size Y plane and the two smaller chroma planes separately. RGB is recovered with the YCbCrCoefficients: R = Cr × (2 − 2 × LumaRed) + Y, B = Cb × (2 − 2 × LumaBlue) + Y, G = (Y − LumaBlue × B − LumaRed × R) ÷ LumaGreen, after ReferenceBlackWhite has turned codes into full-range values: value = (code − black) × range ÷ (white − black), where range is 2BPS − 1 for Y and RGB and 127 for Cb and Cr.
CIE L*a*b* samples
With PhotometricInterpretation 8 each component is 8 bits: L* unsigned, 0 to 255 standing for 0 to 100, and a* and b* signed, −127 to 127. One component means L* only. The reference white is the perfect reflecting diffuser.
PackBits (Compression 32773)
Each row is packed separately. A decoder reads a control byte n as a signed value:
| n | Action |
|---|---|
| 0 to 127 | Copy the next n + 1 bytes as they are. |
| −127 to −1 | Repeat the next byte −n + 1 times. |
| −128 | Do nothing. |
The decoded data is then read as uncompressed data. Runs longer than 128 bytes are split, and the worst case adds one byte per 128.
| Packed bytes | Unpacked |
|---|---|
FE AA | AA AA AA (−2: three copies) |
02 80 00 2A | 80 00 2A (three literals) |
FD AA | AA AA AA AA (−3: four copies) |
03 80 00 2A 22 | 80 00 2A 22 (four literals) |
F7 AA | ten AA bytes (−9) |
Modified Huffman (Compression 2)
CCITT Group 3 one-dimensional coding, adapted for files. Each row is a series of alternating white and black runs, always starting with white: a row that starts black begins with a white run of 0. Each run is zero or more make-up codes followed by exactly one terminating code.
- Runs of 0 to 63 use one terminating code.
- Runs of 64 to 2623 use the largest make-up code not longer than the run, then the terminating code for the rest.
- Runs of 2624 or more use make-up codes of 2560 until less than 2560 is left, then continue as above.
- Each row starts on a new byte. There are no EOL codes, no fill bits and no RTC. The runs of a row must add up to ImageWidth, or the data is broken.
- White runs are white when PhotometricInterpretation is 0, the normal value; with 1 the image is shown reversed.
Terminating codes
| Run | White code | Black code |
|---|---|---|
| 0 | 00110101 | 0000110111 |
| 1 | 000111 | 010 |
| 2 | 0111 | 11 |
| 3 | 1000 | 10 |
| 4 | 1011 | 011 |
| 5 | 1100 | 0011 |
| 6 | 1110 | 0010 |
| 7 | 1111 | 00011 |
| 8 | 10011 | 000101 |
| 9 | 10100 | 000100 |
| 10 | 00111 | 0000100 |
| 11 | 01000 | 0000101 |
| 12 | 001000 | 0000111 |
| 13 | 000011 | 00000100 |
| 14 | 110100 | 00000111 |
| 15 | 110101 | 000011000 |
| 16 | 101010 | 0000010111 |
| 17 | 101011 | 0000011000 |
| 18 | 0100111 | 0000001000 |
| 19 | 0001100 | 00001100111 |
| 20 | 0001000 | 00001101000 |
| 21 | 0010111 | 00001101100 |
| 22 | 0000011 | 00000110111 |
| 23 | 0000100 | 00000101000 |
| 24 | 0101000 | 00000010111 |
| 25 | 0101011 | 00000011000 |
| 26 | 0010011 | 000011001010 |
| 27 | 0100100 | 000011001011 |
| 28 | 0011000 | 000011001100 |
| 29 | 00000010 | 000011001101 |
| 30 | 00000011 | 000001101000 |
| 31 | 00011010 | 000001101001 |
| 32 | 00011011 | 000001101010 |
| 33 | 00010010 | 000001101011 |
| 34 | 00010011 | 000011010010 |
| 35 | 00010100 | 000011010011 |
| 36 | 00010101 | 000011010100 |
| 37 | 00010110 | 000011010101 |
| 38 | 00010111 | 000011010110 |
| 39 | 00101000 | 000011010111 |
| 40 | 00101001 | 000001101100 |
| 41 | 00101010 | 000001101101 |
| 42 | 00101011 | 000011011010 |
| 43 | 00101100 | 000011011011 |
| 44 | 00101101 | 000001010100 |
| 45 | 00000100 | 000001010101 |
| 46 | 00000101 | 000001010110 |
| 47 | 00001010 | 000001010111 |
| 48 | 00001011 | 000001100100 |
| 49 | 01010010 | 000001100101 |
| 50 | 01010011 | 000001010010 |
| 51 | 01010100 | 000001010011 |
| 52 | 01010101 | 000000100100 |
| 53 | 00100100 | 000000110111 |
| 54 | 00100101 | 000000111000 |
| 55 | 01011000 | 000000100111 |
| 56 | 01011001 | 000000101000 |
| 57 | 01011010 | 000001011000 |
| 58 | 01011011 | 000001011001 |
| 59 | 01001010 | 000000101011 |
| 60 | 01001011 | 000000101100 |
| 61 | 00110010 | 000001011010 |
| 62 | 00110011 | 000001100110 |
| 63 | 00110100 | 000001100111 |
Make-up codes
| Run | White code | Black code |
|---|---|---|
| 64 | 11011 | 0000001111 |
| 128 | 10010 | 000011001000 |
| 192 | 010111 | 000011001001 |
| 256 | 0110111 | 000001011011 |
| 320 | 00110110 | 000000110011 |
| 384 | 00110111 | 000000110100 |
| 448 | 01100100 | 000000110101 |
| 512 | 01100101 | 0000001101100 |
| 576 | 01101000 | 0000001101101 |
| 640 | 01100111 | 0000001001010 |
| 704 | 011001100 | 0000001001011 |
| 768 | 011001101 | 0000001001100 |
| 832 | 011010010 | 0000001001101 |
| 896 | 011010011 | 0000001110010 |
| 960 | 011010100 | 0000001110011 |
| 1024 | 011010101 | 0000001110100 |
| 1088 | 011010110 | 0000001110101 |
| 1152 | 011010111 | 0000001110110 |
| 1216 | 011011000 | 0000001110111 |
| 1280 | 011011001 | 0000001010010 |
| 1344 | 011011010 | 0000001010011 |
| 1408 | 011011011 | 0000001010100 |
| 1472 | 010011000 | 0000001010101 |
| 1536 | 010011001 | 0000001011010 |
| 1600 | 010011010 | 0000001011011 |
| 1664 | 011000 | 0000001100100 |
| 1728 | 010011011 | 0000001100101 |
Make-up codes shared by white and black
| Run | Code |
|---|---|
| 1792 | 00000001000 |
| 1856 | 00000001100 |
| 1920 | 00000001101 |
| 1984 | 000000010010 |
| 2048 | 000000010011 |
| 2112 | 000000010100 |
| 2176 | 000000010101 |
| 2240 | 000000010110 |
| 2304 | 000000010111 |
| 2368 | 000000011100 |
| 2432 | 000000011101 |
| 2496 | 000000011110 |
| 2560 | 000000011111 |
The end-of-line code, used by Compression 3 but not by 2, is
000000000001. (The specification prints the black EOL with eleven zeros
and no 1; the code is the same for both colours.)
T4 and T6 coding (Compression 3 and 4)
These store CCITT Recommendations T.4 and T.6 coding. Each strip or tile is coded as a separate image, starting on a fresh byte, so it can be decoded alone; segments of a multiple of 4 rows suit the coding best. FillOrder should always be written, and 1 should be used for interchange.
- T4, one-dimensional (T4Options bit 0 clear): the runs above, with
each row ended by EOL. With bit 2 set, zero fill bits before each EOL make it end
on a byte boundary, so it appears as
xxxx0000 00000001. - T4, two-dimensional (bit 0 set): rows coded relative to the row above, with a one-dimensional row every K rows. Each strip must start with a one-dimensional row, so RowsPerStrip should be a multiple of K.
- T6: every row is coded two-dimensionally; the first is coded
against an imaginary all-white row. Rows follow each other with no EOL or fill, and
the last is followed by the 24-bit EOFB
000000000001000000000001, then 0 bits to the next byte. Readers stop at EOFB or after the expected number of rows; rows missing before EOFB are white.
Two-dimensional rows are made of these mode codes, from ITU-T T.4 section 4.2. b1 is the first changing element on the row above and a1 the next one on the current row.
| Mode | Code | Meaning |
|---|---|---|
| Pass | 0001 | Skip past b2 without a change. |
| Horizontal | 001 | Two runs follow, coded with the one-dimensional codes. |
| Vertical V(0) | 1 | a1 is directly under b1. |
| Vertical VR(1) | 011 | a1 is 1 to the right of b1. |
| Vertical VR(2) | 000011 | 2 to the right. |
| Vertical VR(3) | 0000011 | 3 to the right. |
| Vertical VL(1) | 010 | 1 to the left. |
| Vertical VL(2) | 000010 | 2 to the left. |
| Vertical VL(3) | 0000010 | 3 to the left. |
| Extension | 0000001xxx | Escape; 0000001111 enters uncompressed mode. |
Uncompressed mode starts with 0000001111. The pixels
follow as bits, 0 white and 1 black, with a 1 inserted after every five consecutive
0s. It ends with 0000001t, where t is the colour of the next run. The
specification calls this mode poorly defined and discourages it.
LZW (Compression 5)
Each strip is coded separately, with a string table that starts afresh.
- The characters are bytes of the uncompressed data, whatever BitsPerSample is.
- Codes 0 to 255 are the single bytes. 256 is Clear and 257 is EndOfInformation; the first new string gets code 258. The table holds at most 4096 entries.
- Codes are 9 bits wide at first. A decoder switches to 10 bits as soon as entry 510 is added, to 11 bits after 1022 and to 12 bits after 2046. (An encoder switches one entry later, after 511, 1023 and 2047, because it is one code ahead.)
- The encoder writes Clear as soon as it uses entry 4094, then starts again with 9-bit codes.
- Each strip starts with Clear and ends with EndOfInformation, and starts on a byte boundary. Codes are packed most significant bit first (as if FillOrder were 1) and written as bytes, so the stream is the same in II and MM files.
The decoder: on Clear, reset the table and output the next code’s string. Otherwise, if the code is in the table, output its string and add the previous string plus the first byte of this one; if it is not yet in the table, the new string is the previous string plus its own first byte; output and add that.
The specification’s example codes the bytes 07 07 07 08 08 07 07 06 06
as the 9-bit codes 256, 7, 258, 8, 8, 258, 6, 6, 257 (Clear, 7, “7 7”, 8, 8, “7 7”,
6, 6, EndOfInformation), adding table entries 258 “7 7”, 259 “7 7 8”, 260 “8 8”, 261
“8 7”, 262 “7 7 6” and 263 “6 6”. Packed most significant bit first, the 81 bits
make the bytes 80 01 E0 40 80 44 08 0C 06 80 80.
Predictor
Predictor 2, horizontal differencing, is applied before compression (in TIFF 6.0, LZW). Each sample except the first of a row is replaced by its difference from the previous sample of the same component, so in chunky data the step is SamplesPerPixel samples. Arithmetic wraps at the sample size, so nothing is lost. To decode, add each sample to the decoded one before it, left to right. Samples smaller than a byte are expanded to bytes for the subtraction, and 16-bit samples are handled as 16-bit values; BitsPerSample must be the same for every component.
Predictor 3, for floating-point samples, comes from Adobe Photoshop TIFF Technical Note 3, which this site has not read. As LibTIFF implements it, the bytes of each row’s values are regrouped so that all the most significant bytes come first, then all the next bytes, and so on, and the resulting byte row is differenced with a step of SamplesPerPixel bytes.
Deflate (Compression 8 and 32946)
Each strip or tile is a zlib stream (RFC 1950) wrapping Deflate data (RFC 1951). Predictor 2 or 3 is often used with it.
JPEG (Compression 7, and the old Compression 6)
TIFF Technical Note 2 replaced the TIFF 6.0 design. With Compression 7:
- Each strip or tile is a complete JPEG stream with a single frame: SOI, then optional DQT, DHT, DAC, DRI, APPn and COM, SOFn, one SOS per scan, then EOI. DNL is not allowed, and only sequential processes may be used.
- JPEGTables (347), if present, is an abbreviated stream from SOI to EOI holding tables that every segment may use. A segment may add its own tables but may not redefine one from JPEGTables. Without it, each segment must define every table it uses.
- The SOFn size is that of the strip or tile: ImageWidth by RowsPerStrip (fewer for the last strip), or TileWidth by TileLength. RowsPerStrip, or the tile size, must be a multiple of 8 times the largest sampling factor.
- The SOFn precision must match BitsPerSample. All segments use the same process.
- PhotometricInterpretation describes the colour space actually stored: YCbCr data is subsampled 2 × 2 unless YCbCrSubSampling says otherwise, and ReferenceBlackWhite must be present. Palette and mask images may not use JPEG.
- With PlanarConfiguration 2 each segment holds one component.
The old design, Compression 6, stores bare entropy-coded data in each strip or tile and puts the tables at offsets given by JPEGQTables, JPEGDCTables and JPEGACTables, the process in JPEGProc and the restart interval in JPEGRestartInterval; strip and tile offsets point at the start of a restart interval. Technical Note 2 strongly discourages writing it, and files using it vary between writers.