Detailed description of TIFF Image File Format

Source: Internet
Author: User

This text is transferred from:

Http://blog.csdn.net/dcraw/archive/2011/05/24/6443537.aspx

 

1. What is Tiff?
Tiff is the abbreviation of Tagged Image File Format. In the current standard, only tiff exists, and other terms are no longer used. As a markup language, the biggest difference between TIFF and other file formats is that in addition to image data, it can also record a lot of other information about the image. It records image data in a more flexible way. Theoretically, any other image format can be used by TIFF and embedded into tiff. For example, JPEG, lossless JPEG, MPEG-4, and original uncompressed data of any data width can be easily embedded into tiff. Due to its scalability, Tiff has been widely used in digital influence, remote sensing, medicine, and other fields. The suffix of The TIFF file is. tif or. Tiff.

2. TIFF file structure
The three keywords in the TIFF file are: Image File Header (IFH), image file directory (IFD), and directory item directory entry (de ). Each image starts with an 8-byte IFH, which points to the first IFD. IFD contains various information about the image and a pointer to the actual image data.

IFH composition:
Byte 0-1: byte sequence flag. The value is II or mm. II indicates that the smallest byte is in front, also known as Little-Endian. MM indicates that the size of the byte goes first and becomes big-Endian.
Byte 2-3: indicates the position of TIFF, generally 42
Byte 4-7: the offset of the first IFD. It can be anywhere, but must be at the boundary of a word, that is, it must be an integer multiple of 2.

IFD structure (0 indicates the starting position of the IFD ):
Byte 0-1: indicates the number of de contained in the IFD. Assume that the number is N.
Byte 2-(N * 12 + 1): n de
Byte (N * 12 + 2)-(N * 12 + 5): offset of the next IFD. If not, set it to 0.

Structure of de:
Byte 0-1: the unique identifier of this tag
Byte 2-3: data type.
Byte 4-7: quantity. The type and quantity can be used to determine the number of bytes required to store the tag.
Byte 8-11: if the number of bytes occupied is less than 4, the data is stored directly here. If there are more than four, the pointer to the actual data is stored here.

It can be represented by the following diagram (from the http://www.cppblog.com/windcsn/archive/2009/03/12/1158.html)

 

In iis6.0, 12 data types are defined:

1 = byte 8-bit unsigned integer.
2 = ASCII 8-bit byte that contains a 7-bit ASCII code; the last byte
Must be NUL (Binary zero ).
3 = short 16-bit (2-byte) unsigned integer.
4 = long 32-bit (4-byte) unsigned integer.
5 = rational two longs: The first represents the numerator
6 = sbyte an 8-Bit Signed (twos-complement) integer.
7 = undefined an 8-bit byte that may contain anything, depending on
The definition of the field.
8 = sshort a 16-bit (2-byte) signed (twos-complement) integer.
9 = slong a 32-bit (4-byte) signed (twos-complement) integer.
10 = srational two slong's: the first represents the numerator of
Fraction, the second the denominator.
11 = float single precision (4-byte) IEEE format.
12 = double precision (8-byte) IEEE format.

-A tiff file may contain multiple ifds. Each IFD is a subfile. The baseline decoder only needs to solve the image data corresponding to the first IFD. Extended tiff images often contain multiple ifds, each of which contains different information.

 

3. Content that may be involved in the future

(1) important basic tags

(2) Important extended tags

(3) important private tags

(4) tiff/EP and DNG

(5) Tiff-related software

(6) Limitations of TIFF and Future Development

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/dcraw/archive/2011/05/24/6443537.aspx

 

1 Relationship Between tiff, Tiff, EP, and DNG

 

The full name of TIFF/EP is "tag image file format/electronic photography ". It is an ISO standard named "Electronic still-picture imaging-removable memory-Part 2: tiff/EP image data format", with the standard number ISO 12234-2

DNG (Digital Negative) is an open raw image file format developed by Adobe. The tags used in are basically defined in TIFF or tiff/EP. In DNG sepcification, only the data organization mode and color space conversion are defined or recommended.

In my personal understanding, the relationship between the three should be as follows:

(1) TIFF and DNG are both specification, and the file formats with the suffix. tif/. Tiff and. DNG are defined respectively.

(2) At the same time, Tiff specification also defines a baseline and some extended tags. Tiff/EP defines and standardizes the tags used in electronic images.

(3) DNG is compatible with Tiff and Tiff/EP and contains EXIF and XMP information. DNG is actually an expanded Tiff. You can directly preview the image by changing the DNG extension to Tif.

(4) Although all Copyrights belong to Adobe, they can be used for free.

Before the emergence of DNG, various digital camera manufacturers had their own formats, such as Canon (CR2/CrW), Nikon (NEF), Olympus (LF), and pentex (PST. The reason why there are so many formats is that there is no uniform raw format before, but more importantly, each Vendor wants to use this only public data format to protect their private information. Adobe released DNG to unify raw world. However, unfortunately, only a few digital manufacturers have responded, such as Sony and Hasselblad. The current results are only in the raw format. However, DNG is still successful. Many small manufacturers are willing to use DNG as their own file formats because of their mature DNG coding/decoding and conversion companies. As time went on, large digital manufacturers were forced to support DNG as consumers wanted. Finally, DNG unified the digital raw format is still the trend of the times.

 

 

 

2 tiff Application

(1) tiff is flexible and can record various types of images with different precision. It is an important basic image format. This province is a very important application

(2) As mentioned above, DNG is also a very important application.

(3) Medicine

(4) Geographic Remote Sensing

(5) Printing Industry

3. Limitations of TIFF and Future Development

The biggest limitation of TIFF is to use 4 bytes to represent the offset, so that the maximum file size is 4 GB. When the tiff standard was specified 20 years ago, 4G may be enough. However, this is indeed a bottleneck restricting the back-propagation of tiff. At present, bigtiff has proposed to use 8 bytes to represent the offset. The data size should be large enough. It may become the baseline of the new tiff in the near future.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/dcraw/archive/2011/05/24/6443679.aspx

 

 

Basic tiff tags

The code is basically taken from dcraw (www.cybercom.net /~ Dcoffin/dcraw/) was modified and sorted out.

// First define some necessary global variables and some useful functions, mainly to read functions of various data types

Short order;

File * IFP = fopen ("filename. Tiff", "rb ");

Ushort class sget2 (uchar * s)
{
If (Order = 0x4949)/* "II" means little-Endian */
Return s [0] | s [1] <8;
Else/* "mm" means big-Endian */
Return s [0] <8 | s [1];
}

Ushort class get2 ()
{
Uchar STR [2] = {0xff, 0xff };
Fread (STR, 1, 2, IFP );
Return sget2 (STR );
}

Unsigned class sget4 (uchar * s)
{
If (Order = 0x4949)
Return s [0] | s [1] <8 | s [2] <16 | s [3] <24;
Else
Return s [0] <24 | s [1] <16 | s [2] <8 | s [3];
}
# Define sget4 (s) sget4 (uchar *) S)

Unsigned class get4 ()
{
Uchar STR [4] = {0xff, 0xff, 0xff, 0xff };
Fread (STR, 1, 4, IFP );
Return sget4 (STR );
}

Unsigned class getint (INT type)
{
Return type = 3? Get2 (): get4 ();
}

Float class int_to_float (int I)
{
Union {int I; float F;} U;
U. I = I;
Return U. F;
}

Double class getreal (INT type)
{
Union {char C [8]; double D;} U;
Int I, Rev;

Switch (type ){
Case 3: Return (unsigned short) get2 ();
Case 4: Return (unsigned INT) get4 ();
Case 5: U. D = (unsigned INT) get4 ();
Return U. D/(unsigned INT) get4 ();
Case 8: Return (signed short) get2 ();
Case 9: Return (signed INT) get4 ();
Case 10: U. D = (signed INT) get4 ();
Return U. D/(signed INT) get4 ();
Case 11: Return int_to_float (get4 ());
Case 12:
REV = 7 * (Order = 0x4949) = (ntohs (0x1234) = 0x1234 ));
For (I = 0; I <8; I ++)
U. C [I ^ rev] = fgetc (IFP );
Return U. D;
Default: Return fgetc (IFP );
}
}

// First parse IFH to get the offset of order and the first IFD

// Then decode IFD

// Finally, the image data

// Use the same processing method for multiple ifds

Int class parse_tiff ()
{
Int doff;

Fseek (IFP, 0, seek_set );
Order = get2 (); // "II" or "mm"
Get2 (); // The value should be 42, skip
Doff = get4 (); // offset of the first IFD
Fseek (IFP, doff, seek_set );
Parse_tiff_ifd ();

// Obtain or decompress the corresponding image data based on the value obtained from the tag
}

Int class parse_tiff_ifd ()
{
Entries = get2 ();
If (Entries & gt; 512) return 1;
While (entries --){

// Read the values of tag, type, and count first.

// Determine whether the stored value or location is determined based on the type and count,

}

// Get the offset of the next IFD

}


This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/dcraw/archive/2011/05/26/6446444.aspx

 

As we all know, JPEG is a lossy compression image format based on DCT transformation. Since the establishment of the ITU t.81 standard in 1992, JPEG has achieved great success. JPEG has become the most popular image format. Any image-related tool system must support JPEG decoding or even encoding. Although it has one or more disadvantages, it has been the most popular image format until now due to its high compression rate, simplicity, flexibility, and wide application. Although it has many advantages, it does not shake the top node of JPEG.

Although JPEG is widely used, it is only limited in daily life. In some cases, lossless compression is required, and JPEG is powerless, such as medical images and raw image storage in digital photography. Fortunately, this situation is taken into account when the JPEG standard is specified, and the lossless compression format is reserved. Later ITU t.87 improved lossless compression encoding, which is known as JPEG-LS or lossless JPEG.

JPEG-LS uses predictive entropy encoding. Is a line scan encoding method. In this respect, it has nothing in common with JPEG. However, the mark (Marker) It uses is defined in JPEG, so it cannot be separated from JPEG. To understand t87, you must understand t81.

1. Prediction Model

C B

A x

X is the pixel to be encoded. A, B, and C are the three neighboring values.

Select Value
Predicted value
 
0 no prediction
1
2 B
3 C
4 A + B-C
5 A + (B-c)/2
6 B + (a-c)/2
7 (A + B)/2

During the prediction process, all nonexistent values are initialized to 0.

2 coding process of JPEG-LS

First, calculate the travel score and the number of bits required to store the value based on the prediction method.

Then, find the Huffman code word for this bit number based on the Huffman code table and output the difference value of the corresponding number of digits.

3 JPEG-LS code stream

The following code streams are captured from an image of a JPEG-LS, which can be easily understood by each bit in comparison with ITU t81

FF D8 % SOI (start of image)

FF C3 % sof3

00 0b % length = 11, minus the current two bytes, followed by 9 bytes

10% bits

0a 92% Height = 2706

38 60% width = 14432

01 00 11 00

FF c4 % DHT

00 24% Length

00 00 02 01 04 02 01 01 01 01 01 00 00 00 00 00 06 07 05 04 08 09 0a 03 0b 01 02 0C 00 0d 0e 0f 10

FF da % SOS

00 08% length = 8

01 00 00 06 00 00

% Actual image data

Ff 00 87 BC 7f 6f 03 F8 7E 7f DC F3 Fe 1A A7 F7 4A 7f 86 3f FD B7 5f E2 20 7f 72 17 F8 69 5f dB EB Fe 1f 47 F7 2B BF 86 d1 FD D3 2f E1 5E 7f 70 8f F8 93 ff 00 DC 1B Fe 1D F7 F7 59 ff 00 84 A1 fd cb 4f E1 7A 7f 70 FB F8 82 5f dc cc Fe 18 57 f7 25 BF 86 87 FD D0 4f E1 BD ff 00 6e ff 00 F8 86 bf dc 55 Fe 1D 97 F7 15 BF 86 B5 FD C4 2f E1 A1 7f 6C 53 F8 7A 9f D2 4B Fe 61 1f F4 37 BF 9C 25 FD 0a 6f 87 ef Fb ac ff 00 C4 A3 Fe dB EF F1 29 3f af F5 FC 5f 6f B0 D3 F9 D8 7d AC 7d 00 3f 47 07 42 77 F4 0d be 3B ff 00 EE D8 ff 00 16 Cf FB B7 7f C3 02 Fe E3 77 F0 E7 3f B9 C1 FC 2f df ee 33 7f 0e 7b FB 9C 9f C3 16 Fe E1 B7 F1 04 BF B9 85 FC 36 CF EE 3E 7f 0d D3 FB A0 BF C2 82 Fe ......

FF D9 % EOI

4. Details

4.1 how do I select a prediction model?

At first, I was confused about this problem for a long time. If the global image uses the same model, are these options directly different? Later, when I saw the raw image encoding process of the Bayer pattern, I finally understood the differences and usage of these prediction models.

The Bayer pattern image is as follows:

Bgbgbgbg

Grgrgrgr

Bgbgbgbg

Grgrgrgr

Bgbgbgbg

Grgrgrgr

Bgbgbgbg

Grgrgrgr

Only predictions in the same channel are meaningful, but if the original arrangement is used for direct encoding, it will find that either method is not suitable. When processing raw images, we usually use the following method to combine two rows into one row:

Bgbgbgbggrgrgrgrgr

Bgbgbgbggrgrgrgrgr

Bgbgbgbggrgrgrgrgr

Bgbgbgbggrgrgrgrgr

In this way, prediction models 2, 4, and 6 can be used. Generally, 6 is used in DNG. If it is a common image, since each pixel is the same channel, the prediction method is similar. From this perspective, we feel that JPEG-LS is designed to store raw images.

4.2 How to Deal with FF in the data stream?

Because 'ff' is a reserved code word of marker. In order to facilitate the detection of marker, in JPEG and JPEG-LS, x'ff 'are made special processing. In JPEG, if FF occurs in encoding and it is not a marker, insert an X '00' after it '. In case of ff00 during decoding, the 00 is directly lost.

The ITU t87 claims that the JPEG-LS is treated differently from JPEG. Because the marker of the JPEG-LS is> = ff80, that is to say, the highest bit of the marker that follows FF must be 1. If there is X 'ff 'during data stream compilation, insert only one bit 0 to distinguish whether x 'ff' is a data stream or a marker. If it is 0, this bit will be discarded.

However, lossless JPEG in DNG still adopts the JPEG method, which can be verified in the code of dcraw and dng sdk, which is strange.


This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/dcraw/archive/2011/05/31/6455972.aspx

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.