libtiff库使用
此文章为了记录我在使用libtiff库中的一些问题而写,将不断补充。
libtiff库是读取和写入tiff文件最主要的一个开源库,但文档写的实在不敢恭维。相对资料也是异常稀少。
libtiff库的安装
libtiff库的最新的最新版本可以从http://www.libtiff.org/下载,即可以编译源码也可以采用预先编译好的二进制文件。
本人推荐使用预编译版本,自己编译容易缺少依赖库,同时也可能出现一些莫名其妙的问题。
tiff文件的读写函数:
//获取strip大小
tsize_t TIFFStripSize(TIFF *tif);
//读取strip数据,buf缓冲区可由TIFFStripSize计算,size取-1代表读取整个strip
tsize_t TIFFReadEncodedStrip(TIFF *tif, tstrip_t strip, tdata_t buf, tsize_t size);
将多色tiff文件分割
uint32 imageWidth, imageLength, TileWidth, TileLength, imageRowsPerStrip ;
uint16 imageCompression, imageSamplePerPixel ;
uint16 imagePlanarConfig, imagePhotoMetric, ResolutUnit, Orientation ;
uint16 bps ;
float X_Resolut, Y_Resolut ; TIFF *tif_r, *tif_w ;
unsigned char *buf;
tstrip_t strip ; tif_r = TIFFOpen("image_4plane.tif", "r");
if (!tif_r)
{
error_handler("Open Tiff File Error!");
return -;
}
/* 讀取 TIFF 標籤 */
TIFFGetField(tif_r, TIFFTAG_IMAGEWIDTH, &imageWidth);
TIFFGetField(tif_r, TIFFTAG_IMAGELENGTH, &imageLength); TIFFGetField(tif_r, TIFFTAG_BITSPERSAMPLE, &bps);
TIFFGetField(tif_r, TIFFTAG_COMPRESSION, &imageCompression);
TIFFGetField(tif_r, TIFFTAG_PHOTOMETRIC, &imagePhotoMetric); TIFFGetField(tif_r, TIFFTAG_SAMPLESPERPIXEL, &imageSamplePerPixel);
TIFFGetField(tif_r, TIFFTAG_ROWSPERSTRIP, &imageRowsPerStrip);
if (imageRowsPerStrip != )
{
error_handler("Rows Each Strip Is Not 1!");
return -;
} TIFFGetField(tif_r, TIFFTAG_XRESOLUTION, &X_Resolut);
TIFFGetField(tif_r, TIFFTAG_YRESOLUTION, &Y_Resolut);
TIFFGetField(tif_r, TIFFTAG_RESOLUTIONUNIT, &ResolutUnit); TIFFGetField(tif_r, TIFFTAG_PLANARCONFIG, &imagePlanarConfig);
TIFFGetField(tif_r, TIFFTAG_ORIENTATION, &Orientation); int stripsize = TIFFStripSize(tif_r);
buf = (unsigned char *) malloc(stripsize) ;
if (!buf)
{
error_handler("Allocate Buffer Failed!");
}
Mat ht_img(Size(imageWidth, imageLength),CV_8UC1,Scalar::all());
const int color[] = {,,,,,,};
unsigned char * pRow; for (strip = ; strip < TIFFNumberOfStrips(tif_r); strip++)
{
TIFFReadEncodedStrip(tif_r, strip, buf, (tsize_t) -);
pRow = ht_img.ptr(strip);
for (int i_pixel = ; i_pixel < imageWidth; i_pixel++)
{
pRow[i_pixel] = buf[i_pixel*imageSamplePerPixel + color[]];
}
}
imwrite("strip_out.tiff", ht_img); free(buf);
TIFFClose(tif_r);
printf("Done!\n");
参考文献:
1. libtiff库的使用
http://darkranger.no-ip.org/archives/v5/document/develop/libtiff_tutorial.htm
2. 关于如何判断一个tiff文件是tile或者是strip的说明
http://www.asmail.be/msg0054551721.html
libtiff库使用的更多相关文章
- C++学习笔记23:库
		静态库(Archives) 后缀一般为"*.a" 使用两个目标文件创建单一静态库的编译与链接命令:ar cr libtest.a test1.o test2.o 链接器搜索静态库 ... 
- 在Window下编译LibGeotiff(含Libtiff)
		核心提示:1.GeoTiff简介 GeoTiff是包含地理信息的一种Tiff格式的文件. 1.GeoTiff简介 GeoTiff是包含地理信息的一种Tiff格式的文件.Libgeotiff就是一个操作 ... 
- QT库在Windows上的编译
		1.从http://www.qtcentre.org/下载QT源代码,注意是源代码.我下载到的是QT4.5.1版,文件名为qt-win-opensource-src-4.5.1.zip: 2.解压,注 ... 
- VC++下编译Libgeotiff(含Libtiff)
		转自原文Win10+VC++下编译Libgeotiff(含Libtiff)详细图文教程 GeoTiff是包含地理信息的一种Tiff格式的文件.Libgeotiff就是一个操作GeoTiff文件的库.同 ... 
- libTIFF 图像读取与保存
		本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/YhL_Leo/article/details/49848391 1 头文件 libtif ... 
- 利用开源程序(ImageMagick+tesseract-ocr)实现图像验证码识别
		--------------------------------------------------低调的分割线-------------------------------------------- ... 
- 安装Ubuntu双系统系列——64位Ubuntu安装H3C的INode客户端
		学校使用的是Inode客户端认证上网的.如果是使用Ubuntu 32位版本,可以完美地安装并能够连接到网站.但是如果安装的是Ubuntu desktop 12.10 amd64版本,则发现之前的&qu ... 
- Qt configure 参数不完全说明
		只需要加个 -fast参数就ok了.其他参数视自己情况而定,比如你不需要qt3支持可以添加-no-qt3support,或者不需要webkit插件 -no-webkit配置参数选项: 前面是*号的表示 ... 
- CCImage
		#ifndef __CC_IMAGE_H__ #define __CC_IMAGE_H__ //派生于CCObject #include "cocoa/CCObject.h" // ... 
随机推荐
- LeetCode OJ 86. Partition List
			Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ... 
- [科普]DNS相关的攻击介绍
			一 什么是DNS DNS 是域名系统 (Domain Name System) 的缩写,是因特网的一项核心服务,它作为可以将域名和IP地址相互映射的一个分布式数据库,能够使人更方便的访问互联网,而不 ... 
- http get with body data
			http://stackoverflow.com/questions/978061/http-get-with-request-body Yes, you can send a request bod ... 
- ios 贝塞尔画图
			CGContextRef context = UIGraphicsGetCurrentContext(); //写文字 CGContextSetRGBFillColor(context, 1, 0, ... 
- 扩展方法where方法查询不到数据,不会抛异常,也不是返回的null
			如题,“扩展方法where方法查询不到数据,不会抛异常,也不是返回的null”,示例代码如下: Product类: public class Product { private string name ... 
- http & json
			TCP(传输层协议) (1) 面向连接 (2) 可靠的 (3) 基于字节流的 连接建立阶段: 客户端 ------->SYN 服务端(服务器被动打开) 客 ... 
- “strcmp()” Anyone?
			“strcmp()” Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two ... 
- NumPy基础:数组和矢量计算
			今天被老板fire了,还是继续抄书吧,安抚我受伤的小心脏.知识还是得慢慢积累,一步一个脚印,这样或许才是最快的捷径. ------2015-2-16-------------------------- ... 
- jquery的校验规则的方法
			//validate 选项*********************************************************** $("form").validat ... 
- JS取非行间样式
			<title>无标题文档</title> <style> #div1{width:200px;height:200px; background:red;} < ... 
