我是第一次接触TIFF和LibTiff.Net,我想读取平铺的TIFF文件,并将其写入其他TIFF文件。我如何才能做到这一点?此外,任何关于Tiff文件的材料都将非常有帮助。提前谢谢。
发布于 2018-01-17 18:23:46
这段代码将迭代tiff图像中的平铺,然后迭代平铺中的行。结果数据存储在data字节数组中-您可以从中创建一个bitmapsource。下面的代码可以处理16位,8位,1位灰度的tiff图像。
_tiff = Tiff.Open(filepath, "r");
var data = new byte[header.ImageWidth * header.ImageHeight];
var buffer = new byte[_tiff.TileSize()];
for (var row = 0; row < header.ImageHeight; row += header.TileHeight)
{
for (var col = 0; col < header.ImageWidth; col += header.TileWidth)
{
// Read the tile
if (_tiff.ReadTile(buffer, 0, col, row, 0, 0) < 0)
{
throw new Exception("Error reading data");
}
var index = 0;
// Iterate the rows in the tile
for (var i = 0; i < header.TileHeight && i + row < header.ImageHeight; i++)
{
var length = header.TileWidth;
// Index of the first position in the row
var position = (row + i) * data.Width + col;
// Check we are not outside the image
if (col + length > header.ImageWidth)
{
length = header.ImageWidth - col;
}
switch (header.BitsPerPixel)
{
case 1:
{
for (var p = 0; p < (length + 7) / 8; p++)
{
// Unpack the pixels
for (var b = 0; b < 8; b++)
{
data[position + p * 8 + (7 - b)] = (buffer[index / 8 + p] & (1 << b)) != 0 ? byte.MaxValue : byte.MinValue;
}
}
break;
}
case 8:
{
for (var p = 0; p < length; p++)
{
data[position + p] = buffer[index + p];
}
break;
}
case 16:
{
for (var p = 0; p < length; p++)
{
data[position + p] = buffer[index * 2 + p * 2];
}
break;
}
default:
{
throw new NotImplementedException();
}
}
index += header.TileWidth;
}
}
}您可以像这样读取tiff报头:
public class TaggedImageHeader
{
public int BitsPerPixel { get; private set; }
public int Components { get; private set; }
public string Compression { get; private set; }
public int ImageHeight { get; private set; }
public int ImageWidth { get; private set; }
public string PlanarConfig { get; private set; }
public int TileHeight { get; private set; }
public int TileWidth { get; private set; }
internal static TaggedImageHeader Read(Tiff tiff)
{
var imageWidth = tiff.GetField(TiffTag.IMAGEWIDTH);
var imageHeight = tiff.GetField(TiffTag.IMAGELENGTH);
var bitsPerPixel = tiff.GetField(TiffTag.BITSPERSAMPLE);
var components = tiff.GetField(TiffTag.SAMPLESPERPIXEL);
var tileWidth = tiff.GetField(TiffTag.TILEWIDTH);
var tileHeight = tiff.GetField(TiffTag.TILELENGTH);
var compression = tiff.GetField(TiffTag.COMPRESSION);
var planarConfig = tiff.GetField(TiffTag.PLANARCONFIG);
return new TaggedImageHeader
{
ImageWidth = imageWidth?[0].ToInt() ?? 0,
ImageHeight = imageHeight?[0].ToInt() ?? 0,
BitsPerPixel = bitsPerPixel?[0].ToInt() ?? 0,
Components = components?[0].ToInt() ?? 0,
TileWidth = tileWidth?[0].ToInt() ?? 0,
TileHeight = tileHeight?[0].ToInt() ?? 0,
Compression = compression?[0].ToString() ?? "",
PlanarConfig = planarConfig?[0].ToString() ?? ""
};
}
}如果你正在处理类似金字塔的tiff文件,请确保在读取头文件之前调用_tiff.SetFrame(frame)。
您可以通过将数据数组替换为以下代码来直接写入WriteableBitmap后台缓冲区。这将删除不必要的副本。
var data = new UnsafeBuffer((byte*)bitmap.BackBuffer, bitmap.BackBufferStride, bitmap.PixelHeight); UnsafeBuffer:
public unsafe class UnsafeBuffer : Buffer
{
private readonly byte* _data;
public UnsafeBuffer(byte* data, int x, int y) : base(x, y)
{
_data = data;
}
public override byte this[int x, int y]
{
get => this[y * Width + x];
set => this[y * Width + x] = value;
}
public override byte this[int index]
{
get => _data[index];
set => _data[index] = value;
}
}
public abstract class Buffer
{
protected Buffer(int width, int height)
{
Width = width;
Height = height;
}
public int Height { get; private set; }
public int Length => Width * Height;
public int Width { get; private set; }
public abstract byte this[int x, int y] { get; set; }
public abstract byte this[int index] { get; set; }
}一个有用的资源是其中包含样本的documentation 。我还发现这个link对我入门非常有用。
https://stackoverflow.com/questions/47349910
复制相似问题