环境
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");
if (tif == nullptr)
{
cout << "读入图像路径错误,请重新确认";
return;
}
int width, height;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
int nTotalFrame = TIFFNumberOfDirectories(tif);
printf("width: %d\n", width);
printf("Slice: %d\n", nTotalFrame);
printf("height: %d\n", height);
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;
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_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);
}
++nCur;
N_size = N_size + width * height;
} while (TIFFWriteDirectory(out) && nCur < slice);
TIFFClose(out);
cout << "save over";
}
}
int main() {
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);
return 0;
}
环境配置参考链接:https://blog.csdn.net/nwpulei/article/details/7473669 参考代码链接:https://blog.csdn.net/u013921430/article/details/79758906 都是写的非常认真的参考资料
|