【发布时间】:2015-11-02 09:17:17
【问题描述】:
我实现了一个类,它读取由Microsoft.Reporting.WinForms.ReportViewer 生成的每像素 24 位 TIFF,将其转换为每像素 1 位 TIFF 并将结果存储到文件中。
这部分工作正常 - 我可以在 TIFF 查看器中打开生成的 TIFF 并查看内容。
对于压缩,我使用以下编解码器:
outImage.SetField(TiffTag.COMPRESSION, Compression.CCITT_T6);
现在我正在尝试读取相同的每像素 1 位 TIFF 并解压缩它。我写了以下方法:
public static void DecompressTiff(byte[] inputTiffBytes)
{
using (var tiffStream = new MemoryStream(inputTiffBytes))
using (var inImage = Tiff.ClientOpen("in-memory", "r", tiffStream, new TiffStream()))
{
if (inImage == null)
return null;
int totalPages = inImage.NumberOfDirectories();
for (var i = 0; i < totalPages; )
{
if (!inImage.SetDirectory((short) i))
return null;
var decompressedTiff = DecompressTiff(inImage);
...
}
private static byte[] DecompressTiff(Tiff image)
{
// Read in the possibly multiple strips
var stripSize = image.StripSize();
var stripMax = image.NumberOfStrips();
var imageOffset = 0;
int row = 0;
var bufferSize = image.NumberOfStrips() * stripSize;
var buffer = new byte[bufferSize];
int height = 0;
var result = image.GetField(TiffTag.IMAGELENGTH);
if (result != null)
height = result[0].ToInt();
int rowsperstrip = 0;
result = image.GetField(TiffTag.ROWSPERSTRIP);
if (result != null)
rowsperstrip = result[0].ToInt();
if (rowsperstrip > height && rowsperstrip != -1)
rowsperstrip = height;
for (var stripCount = 0; stripCount < stripMax; stripCount++)
{
int countToRead = (row + rowsperstrip > height) ? image.VStripSize(height - row) : stripSize;
var readBytesCount = image.ReadEncodedStrip(stripCount, buffer, imageOffset, countToRead); // Returns -1 for the last strip of the very first page
if (readBytesCount == -1)
return null;
imageOffset += readBytesCount;
row += rowsperstrip;
}
return buffer;
}
问题是当ReadEncodedStrip() 被调用为第一页的最后一条时-它返回-1,表明有错误。即使在调试了 LibTIFF.NET 解码器代码之后,我也无法弄清楚出了什么问题。这是在预期之外的地方发现了 EOL TIFF 标记的东西。
由于某种原因,LibTIFF.NET 无法读取自己生成的 TIFF,或者很可能我遗漏了一些东西。 Here 是 TIFF 的问题。
有人可以帮忙找出根本原因吗?
【问题讨论】:
标签: c# .net image image-processing tiff