a TIF image introduction
TIFF is one of the most complex bitmap file formats. TIFF is a markup-based file format, which is widely used in the storage and conversion of images that require higher image quality. Because of its flexible and inclusive structure, it has become a standard in image file format, and most image systems support this format.
TIFF is a flexible, adaptable file format that can handle multiple images and data in a single file by including "tags" in the header. Labels can indicate the basic geometric dimensions of an image, such as the size of an image, or define how image data is arranged and whether various image compression options are used. For example, TIFF can contain images of JPEG and stroke-length coded compression. A TIFF file can also contain a cropped region that is based on a vector (cut or form the contour of the body image). The ability to store images in a lossless format makes TIFF files an effective way to archive images. Unlike JPEG, TIFF files can be edited and then stored without compression loss. Some of the other TIFF file options include multiple or multiple pages.
Although today it is a widely accepted standard format, its scalability brings a lot of compatibility issues when TIFF first appears. Programmers are free to define new tags and options, but not all of them can support all of these created tags. As a result, it integrates a minimal feature for "This" tiff, even in today's large number of TIFF files and the code that reads them is based on a simple 32-bit uncompressed image.
TIFF has an option to use LZW compression, which is a lossless technique for reducing file size, but this technology is covered by several patents within different jurisdictions. By the year 2005, all but one of these patents had expired, including Unisys's well-known and controversial patents. Another notable patent is IBM's patent, which expires on August 11, 2006, and IBM has no intention of strengthening it (who has shown no interest to date in enforcing it).
Each TIFF file starts with two bytes indicating the byte order. "II" indicates that a small byte precedes, and "MM" indicates a large byte in the first byte order. The following two bytes represent the number 42. The number 42 is chosen for its profound philosophical significance. The 42 Read method depends on the byte order represented by the first two bytes. The entire file is read according to the byte order indicated.
BYTE order can create compatibility issues between Apple Macintosh and Microsoft Windows programs, which typically use different byte sequences for TIFF files. Some programs provide options for saving to Mac or Windows byte order so that files can be used on a cross platform. two TIF image reading 1.TIF Storage Structure
The TIFF file has the . tif name extension. Its data format is a 3-level architecture, from highest to lowest: file headers, one or more directories and data that contain tag pointers called IfD. 1. File header (IFH)
The first data structure in each TIFF file, called the image file header or IFH, is the highest level of the image file architecture. This structure is unique in a TIFF file and has a fixed location. It is at the beginning of the file and contains the necessary information to properly interpret the other parts of the TIFF file.
The IFH data structure contains 3 members totaling 8 bytes, the byte order member may be "MM" (0X4D4D) or "II" (0x4949), 0X4D4D indicates that the TIFF graph is a Motorola integer format 0x4949 indicates that the graph is an Intel integer format The version member always contains the decimal 0x2a, which is used to further verify that the file is in TIF format, and that the number 42 is not the version of the TIF software as most people think it is, in fact, the number 42 will probably never change. The third member is the offset from the beginning of the file that is the IFD (the second data structure next). 2. Image file directory (IFD)
An IFD is the 2nd data structure in a TIFF file, a table called tag (tag) that distinguishes one or more variable-length blocks of data, and contains all information about the image. IFD provides a series of pointers (indexes) that tell us where the various data fields begin in the file and give the data type and length of each field. This method allows data fields to be positioned anywhere in the file and can be arbitrary in length, so the file format is flexible.
IFD is the most important data structure in a TIF chart, it contains the most important information in a TIF file, a TIF diagram may have multiple IfD, which indicates that there are multiple images in the file, each IFD identifies the basic properties of 1 images. The IFD structure contains three categories of members, and directory Entry count indicates how many directory portals there are in the structure, followed by the n linear array of de sequences (which is why the TIF format file is an extensible tag). Even the user can add custom tag attributes), each de identifies one of the attributes of the image, and finally an offset that identifies the location of the next file directory relative to the beginning of the file, and of course, if the TIF file contains only one image, then there is only one IfD, which is obviously equal to 0;
A total of 12 bytes, see Figure Ii. Simply put, a de is a property of an image. For example, the size of the image, resolution, whether compression, the number of rows of pixels, a pixel by several representations (1-bit representative of Black-and-white, 8-bit 256-color, etc.) and so on. Where: The tag member is the number of the attribute, in the image file directory, it is in ascending order. We can read these numbers and then go to the official White Paper in TIF format to find the meaning. Attributes are represented by data, and type is the kind that represents the data, and TIF officially specifies 5 types of data. Type=1 is the byte type (8-bit unmarked integer), the type=2 is the ASCII type (7-bit ASCII plus 1-bit binary 0), the type=3 is the short type (16-bit unsigned integer), the type=4 is a long (32-bit unmarked integer), type= 5 is the rational type (2 long, the first is a molecule, and the second is the denominator). The length member is the number of data rather than the size of the data type. The 4th member, Valueoffset, is important, which is the offset of the variable value represented by the tag's attribute relative to the beginning of the file. If the value of the variable occupies less than 4 bytes, the value is stored in the Valueoffset, and there is no need to point another place.
3. Image Data
Depending on the address that the IfD points to. Store related image information
Here is the code that runs under 6.0
#ifndef tifreader_h #define Tifreader_h #include <stdio.h> #include <string.h> #ifndef null #define NULL 0 #endif #ifndef true #define TRUE 1 #define FALSE 0 #endif typedef struct {unsigned short byte_order;//uns igned Short version;//Verify that the file is a TIF file unsigned int offsettofirstfid;//relative to the offset at the beginning of the file//unsigned short wdecount;//How many directory entries}
IFH; typedef struct {Unsigned short tag;//attribute number unsigned short type;//data type unsigned long length;//number of data unsigned long Valu
Eoffset;//tag the identity attribute represents the variable value relative to the offset at the beginning of the file}de;
typedef struct {int width;
int height;
}size; typedef struct {int *data;}
DATA;
typedef struct {DE *pde;
int wdecount;
}pde;
BOOL Readtif (char* path,ifh &ifh,pde &de,size &size,data) {&data char unsigned;
int *dat;
Unsigned short wdecount;//How many directory entrances//ZeroMemory (&IFH, sizeof (IFH));
ZeroMemory (&de, sizeof (DE));
FILE *FP;
Fp=fopen (Path, "RB"); if (fp = = NULL) {cout<< "Open file Error" << Endl;
return false;
} if (sizeof (IFH)!= fread (&IFH, 1,sizeof (IFH), FP)) {cout<< "read TIF file header failed";
return FALSE; } if (0x2a!= ifh.
Version) {cout<< "The file is not a TIF format, read the file failed";
return FALSE; } if (0x4949!= ifh.
Byte_order) {cout<< "This TIF file is not a IBMPC byte order, read file failed";
return FALSE; } fseek (FP,IFH.
Offsettofirstfid,seek_set)//Position the file pointer to the number of directory entries in the IFD//Read file (2!= fread (&wdecount,1,sizeof (unsigned short), FP)
{cout<< "Cannot get the number of directory entries for TIF files";
return FALSE;
cout<< "This TIF file has" <<wDECount<< "directory entry <<endl;"
Create de array, receive information, there are wdecount elements in the array de.pde = new De[wdecount];
de* ptemp = De.pde;
De.wdecount = Wdecount;
memset (de.pde, 0, sizeof (DE) *wdecount);
if (sizeof (DE) *wdecount!= fread (de.pde,1, sizeof (DE) *wdecount,fp)) {cout<< "read image file directory failed";
delete []de.pde;
return false;
//The size of the image and the capacity of the image data are saved to the member variable int m_size_x;
int m_size_y;
int m_size;
int i; for (i=0; i<wdeCount;
i++) {ptemp =de.pde + i;
The variable (256 = = Ptemp->tag)//tag The directory entry for 256 identifies the image width {m_size_x = ptemp->valueoffset;
} if (257 = = Ptemp->tag)//Image Height {m_size_y = ptemp->valueoffset;
} if (273 = = Ptemp->tag)//The computed image data occupies the byte number {//m_dwbmsize = ptemp->valueoffset-sizeof (IFH);
or multiply the tag=256 valueoffset by tag=257 valueoffset m_size = m_size_x * M_SIZE_Y;
}//Fill all pixel data, invert image data from the last line start reading int j = 0;
int i = 0;
data = (unsigned char*) malloc (m_size*sizeof (BYTE));
DAT = (int*) malloc (m_size*sizeof (int));
For (i=m_size_y-1 i>=0; i--) {fseek (fp,sizeof (IFH) + i*m_size_x, seek_set);
Fread ((byte*) (data + 1) + j*m_size_x,sizeof (BYTE), M_SIZE_X,FP);
j + +;
} cout<< "width:" <<m_size_x<<endl;
cout<< "Height:" <<m_size_y<<endl;
unsigned char* p;
p = data;
int *ptr;
ptr = dat;
for (i=0;i<m_size;i++,p++,ptr++) {*ptr = (int) (*P); Int H= *ptr;
cout<
This code is read for specific TIF, do not know why in the save, can not be shown under Windows Normal, look forward to the warrior to solve the.