代码基本取自dcraw(www.cybercom.net/~dcoffin/dcraw/) 做了一些修改和整理
//首先定义一些必须的全局变量和一些有用的函数,主要是读取各种数据类型的函数
short order;
FILE *ifp = 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);
}
}
//先解IFH,得到order和第一个IFD的偏移量
//然后解IFD
//最后是图像数据
//对于多个IFD,采用同样的处理方法
int CLASS parse_tiff ()
{
int doff;
fseek (ifp, 0, SEEK_SET);
order = get2();//"II"或者"MM"
get2();//这个值应该是42, 跳过之
doff = get4();//第一个IFD的偏移量
fseek (ifp, doff, SEEK_SET);
parse_tiff_ifd ();
//根据从Tag中得到的值去获取或者解压相应的图像数据
}
int CLASS parse_tiff_ifd ()
{
entries = get2();
if (entries > 512) return 1;
while (entries--) {
//先读取Tag,Type和Count的值
//根据Type和Count确定存储的是值还是位置,
}
//获取下一个IFD的偏移量
}