本系列文章由 @YhL_Leo 出品,转载请注明出处。
文章链接: http://blog.csdn.net/YhL_Leo/article/details/49848391
1 头文件
libtiff定义一系列C
语言类型的数据结构,调用时包含的头文件为:
#include "tiffio.h"
2 文件读写
/* read from an existing TIFF image */
void main()
{
TIFF* tif = TIFFOpen("foo.tif", "r");
... do stuff ...
TIFFClose(tif); // or TIFFFlush(tif);
}
/* create or overwrite a TIFF image */
void main()
{
TIFF* tif = TIFFOpen("foo.tif", "w");
... do stuff ...
TIFFClose(tif); // or TIFFFlush(tif);
}
不同于stdio library
对TIFF文件的操作可以同时支持读和写,libtiff
对于TIFF
文件的操作模式是不可变更的,也就是说对一个指定的TIFF
文件,一次只能支持对文件的读或写中的一种操作。
3 多目录文件读写
TIFF
格式支持将多个图像文件存储为一个文件的功能,每个图片都有一个对应的数据结构称为一个目录,其中包括全部的信息格式和图像数据内容。图像之间可以是相关的也可以使不相关的。
#include "tiffio.h"
int main(int argc, char* argv[])
{
TIFF* tif = TIFFOpen(argv[1], "r");
if (tif)
{
int dircount = 0;
do {
dircount++;
} while (TIFFReadDirectory(tif));
printf("%d directories in %s\n", dircount, argv[1]);
TIFFClose(tif);
}
return 0;
}
// write: TIFFWriteDirectory()
4 标签读取与设置
图像相关的信息例如宽、高、通道数、定向信息、颜色信息等。libtiff
中提供了获取和设置标签值的函数:TIFFGetField
和TIFFSetField
:
/* read the tags */
uint32 width, height;
uint16 ncn;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width); // image width in pixels
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); // image height in pixels
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &ncn); // samples per pixel -> channels
cout << width << " " << height << " " << ncn << endl;
/* set the tags */
TIFFSetField( imageWrite, TIFFTAG_IMAGEWIDTH, width );
TIFFSetField( imageWr