0
votes

I used LibTiff.net to crop part of a tiled Tiff and export it as a tiled Tiff but encountered the problem of "can not write tiles image to a stripped image". While the "Tiff.open("out.tif","w") make a stripped image, How can I create a tiled-Tiff to fill it with input data?

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");

    }
1
I'm no c# or LibTiff.net developer, but I would assume that setting the TiffTag.ROWSPERSTRIP field, is what makes the image "stripped". What happens if you remove that line? - Harald K
I removed this line before asking the question and nothing happened. Thanks for your hint. - Nasser Tahani
I would like to ask why you multiply tileLength and tileWidth with 5 in the for loops? (is the tile 5 times larger than it is indicated?) - Erik Thysell
@ErikThysell I wanted to crop a part of the main image so I used for instance 5 tiles along the width and 5 tiles along the height of tiff file. - Nasser Tahani

1 Answers

0
votes

To create a tiled TIFF please don't use

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

And you absolutely must set TiffTag.TILEWIDTH and TiffTag.TILELENGTH fields before using WriteTile method.