编程语言
首页 > 编程语言> > C++处理tiff图片--libtiff库(附读写图片代码)

C++处理tiff图片--libtiff库(附读写图片代码)

作者:互联网

环境

VS2017
libtiff 4.0.9

配置

官网下载合适的版本后
打开控制台 切换当前目录到tiff-x.x.x,找到vs2017的vcvars64.bat脚本
控制台中执行 vcvars64.bat
然后 执行 nmake /f makefile.vc
本人操作如下

d:\tiff-4.0.9> D:\VS2017\VC\Auxiliary\Build\vcvars64.bat
d:\tiff-4.0.9> nmake /f makefile.vc

编译好的tifflib.dll tifflib.lib 以及头文件都在libtiff 目录中
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
链接器-输入加入:libtiff.lib

读写图片代码

各函数参数和用法强烈建议google!!
常见函数文档

#include <iostream>
#include <vector>
#include "libtiff/tiff.h"
#include "libtiff/tiffio.h"
using namespace std;
void open_Fun2(const char* file, uint16 **buffer, int *size)
{
	TIFF *tif = TIFFOpen(file, "r");      //使用TIFFOpen函数以只读形式打开图像。
	if (tif == nullptr)
	{
		cout << "读入图像路径错误,请重新确认";
		return;
	}

	int width, height;

	//-------------获取单帧图像的长高
	TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
	TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
	//TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &channel);

	//------------ 获取图片帧数
	int nTotalFrame = TIFFNumberOfDirectories(tif);
	printf("width: %d\n", width);
	printf("Slice: %d\n", nTotalFrame);
	printf("height: %d\n", height);

	//---------------获取每一帧的像素点数目
	//int stripSize = TIFFStripSize(tif);
	int stripSize = width * height;
	printf("stripSize: %d\n", stripSize);

	*buffer = new uint16[nTotalFrame*stripSize];
	int N_size = 0;
	for (int s = 0; s < nTotalFrame; s++)
	{
		for (int row = 0; row < height; row++)
		{
			TIFFReadScanline(tif, (&(*buffer)[N_size] + row * int(width)), row);      //---按行读取
		}
		N_size += width * height;
		TIFFReadDirectory(tif);

	}


	TIFFClose(tif);


	size[0] = width;
	size[1] = height;
	size[2] = nTotalFrame;
}
void saveTiff(const char *path, uint16 *buffer, int *size)
{
	int width = size[0];
	int height = size[1];
	int slice = size[2];

	TIFF* out = TIFFOpen(path, "a");
	if (out)
	{
		int N_size = 0;
		size_t nCur = 0;
		//UChar den = (sizeof(T) == 1) ? 1 : 4;
		do {
			TIFFSetField(out, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
			TIFFSetField(out, TIFFTAG_PAGENUMBER, slice);
			TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32)width);
			TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32)height);
			//TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, 2);
			/*TIFFSetField(out, TIFFTAG_YRESOLUTION, 196.0f);
			TIFFSetField(out, TIFFTAG_XRESOLUTION, 204.0f);*/
			TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
			// 
			TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 16);    //根据图像位深填不同的值
			TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
			TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
			TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
			TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
			TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, height);


			for (int m = 0; m < height; m++)
			{
				TIFFWriteScanline(out, &buffer[N_size + width * m], m, 0);
			}
			//TIFFWriteEncodedStrip(out, 0, &buffer[N_size], width * height);      //另一种写入方法

			++nCur;
			N_size = N_size + width * height;
		} while (TIFFWriteDirectory(out) && nCur < slice);
		TIFFClose(out);

		cout << "save over";
	}
}
int main() {
	//const char* fileName = "C:\\Users\\lxy\\Desktop\\model-and-data\\test\\720-noprepro-02_neutrophil_0.25power_zoom4.0_30Hz_lowSNR.tif";
	const char* fileName2 = "C:\\Users\\lxy\\Desktop\\model-and-data\\test\\tiffwrite_try.tif";
	const int Nsample = 512 * 512 * 100;
	uint16 *Three_tiff_buffer=new uint16 [Nsample];
	for (int i = 0; i < Nsample; i++)
	{
		Three_tiff_buffer[i] = rand() % 65535;
	}
	int size_tiff[3] = { 512,512,100 };
	saveTiff(fileName2, Three_tiff_buffer, size_tiff);
	/*open_Fun2(fileName, &Three_tiff_buffer, size_tiff);*/

	return 0;

}

环境配置参考链接:https://blog.csdn.net/nwpulei/article/details/7473669
参考代码链接:https://blog.csdn.net/u013921430/article/details/79758906
都是写的非常认真的参考资料

标签:TIFFTAG,tiff,libtiff,图片,int,TIFFSetField,out,size
来源: https://blog.csdn.net/weixin_44886683/article/details/119188230