使用 LibTiff.net 创建平铺 Tiff

Create a tiled Tiff using LibTiff.net

我使用LibTiff.net裁剪了一部分平铺的Tiff并将其导出为平铺的Tiff,但遇到了"can not write tiles image to a stripped image"的问题。当 "Tiff.open("out.tif","w") 制作剥离图像时,我如何创建一个平铺 Tiff 以用输入数据填充它?

using (Tiff input = Tiff.Open(@"E:\Sample_04.tif", "r"))
        {

            int width = input.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
            int height = input.GetField(TiffTag.IMAGELENGTH)[0].ToInt();

            int tileWidth = input.GetField(TiffTag.TILEWIDTH)[0].ToInt();
            int tileLentgh = input.GetField(TiffTag.TILELENGTH)[0].ToInt();

            int samplesPerPixel = input.GetField(TiffTag.SAMPLESPERPIXEL)[0].ToInt();
            int bitsPerSample = input.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt();
            int photo = input.GetField(TiffTag.PHOTOMETRIC)[0].ToInt();
            int config = input.GetField(TiffTag.PLANARCONFIG)[0].ToInt();

            int tiles = 0;
            int tileSize = input.TileSize();
            byte[][] buffer = new byte[tileSize][];


            int tileHeightCount = height / tileLentgh;
            int tileWidthCount = width / tileWidth;


            for (int y = 0; y < tileLentgh*5; y += tileLentgh)
            {
                for (int x = 0; x < tileWidth*5; x += tileWidth)
                {
                    buffer[tiles] = new byte[tileSize];
                    input.ReadTile(buffer[tiles], 0, x, y, 0, 0);
                    tiles++;
                }
            }


            // writing
            using (Tiff output = Tiff.Open("out.tif", "w"))
            {

                output.SetField(TiffTag.SAMPLESPERPIXEL, samplesPerPixel);
                output.SetField(TiffTag.IMAGEWIDTH, width );
                output.SetField(TiffTag.IMAGELENGTH, height);
                output.SetField(TiffTag.BITSPERSAMPLE, bitsPerSample);
                output.SetField(TiffTag.ROWSPERSTRIP, output.DefaultStripSize(0));
                output.SetField(TiffTag.PHOTOMETRIC, photo);
                output.SetField(TiffTag.PLANARCONFIG, config);

                int c = 0;

                for (int y = 0; y < tileLentgh*5; y += tileLentgh)
                {
                    for (int x = 0; x < tileWidth*5; x += tileWidth)
                    {

                        output.WriteTile(buffer[c], x, y, 0, 0);
                        c++;
                    }
                }
            }
        }
        System.Diagnostics.Process.Start("555.tif");

    }

要创建平铺 TIFF,请不要使用

output.SetField(TiffTag.ROWSPERSTRIP, output.DefaultStripSize(0));

并且在使用 WriteTile 方法之前,您绝对必须设置 TiffTag.TILEWIDTHTiffTag.TILELENGTH 字段。