简体   繁体   English

使用libtiff.net编写大量TIFF页的有效方法

[英]Efficient way to write very large number of TIFF pages using libtiff.net

I'm using the following code to write a sequence of 16-bit grayscale images (empty array for the purpose of this question) to a multi-page TIFF : 我正在使用以下代码将16位灰度图像序列(出于此问题的目的是空数组)写入多页TIFF:

int numberOfPages = 1000;
int width = 256;
int height = 256;
string fileName = "test.tif";

ushort[] image = new ushort[width * height];
byte[] buffer = new byte[width * height * sizeof(ushort)];

Stopwatch stopWatch = new Stopwatch();

using (Tiff output = Tiff.Open(fileName, "w"))
{
    if (output == null)
    {
        return;
    }
    stopWatch.Start();
    for (int i = 0; i < numberOfPages; i++)
    {
        Buffer.BlockCopy(image, 0, buffer, 0, buffer.Length);

        output.SetField(TiffTag.IMAGEWIDTH, width);
        output.SetField(TiffTag.IMAGELENGTH, height);
        output.SetField(TiffTag.SAMPLESPERPIXEL, 1);
        output.SetField(TiffTag.BITSPERSAMPLE, 16);
        output.SetField(TiffTag.ORIENTATION, Orientation.TOPLEFT);
        output.SetField(TiffTag.XRESOLUTION, 96);
        output.SetField(TiffTag.YRESOLUTION, 96);
        output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
        output.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK);
        output.SetField(TiffTag.COMPRESSION, Compression.NONE);
        output.SetField(TiffTag.FILLORDER, FillOrder.MSB2LSB);
        output.SetField(TiffTag.SUBFILETYPE, FileType.PAGE);
        output.SetField(TiffTag.PAGENUMBER, i + 1, numberOfPages);

        output.WriteEncodedStrip(0, buffer, buffer.Length);

        output.WriteDirectory();
    }
    stopWatch.Stop();
}

Debug.WriteLine(stopWatch.ElapsedMilliseconds);

It works fine up to a few hundred pages, but it seems the execution time does not scale linearly with increasing number of pages. 它可以正常工作多达几百页,但是执行时间似乎不会随着页数的增加而线性增长。 For example : 例如 :

1000 pages --- 3130 ms 1000页-3130毫秒

2000 pages --- 11778 ms 2000页-11778毫秒

3000 pages --- 25830 ms 3000页-25830毫秒

I also tried using append mode inside the loop but got similar results. 我也尝试在循环内使用添加模式,但得到了相似的结果。

Am I doing this wrong or should I expect this kind of overhead? 我做错了还是应该预料到这种开销?

I profiled your code in Visual Studio (Analyze -> Performance Profiler) using CPU Usage tool and here are my findings: 我使用CPU使用率工具在Visual Studio(分析->性能分析器)中分析了您的代码,这是我的发现:

For 5000 pages about 91% of the time is spent to write TIFF directories. 对于5000页,大约需要91%的时间用于编写TIFF目录。 Not the data but the structure which describes the directory. 不是数据,而是描述目录的结构。 This looked suspicious so I looked what does WriteDirectory do that long. 这看起来很可疑,所以我看了WriteDirectory做了那么长时间。

The WriteDirectory tries to link previous and the newly created directories. WriteDirectory尝试链接以前的目录和新创建的目录。 To do this, it searches for a previous directory always starting from the first directory. 为此,它将始终从第一个目录开始搜索上一个目录。 The more directories are there in the TIFF, the longer it takes to add each new one. TIFF中的目录越多,添加每个新目录所花费的时间就越长。

There is no way to change this behaviour without changing code of the library, I am afraid. 恐怕没有更改库代码就无法更改此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM