How do I Convert a TIFF into Tiled TIFF with LibTiff.Net?

64 Views Asked by At

I am trying to convert Tiff files to PDF. I have been able to convert them from Tiff to PDF with the sample code for Tiff2PDF here. However, some of the Tiff files contain very large images (50000+ x 22000+) and so even though they are able to be embedded into PDF files as images, PDF viewers like pdf.js cannot read them due to memory allocation issues. According to this comment, the memory max allocation is 2**31 - 1 and by that math, the images I am converting far exceed that limit.

I believe I can make a Tiff file tiled to possibly get over this issue. What does that mean and how do I do that?

I think it means breaking up the Tiff file into chunks without messing up the pages but I am not sure how to do that in LibTiff.Net. An image instead of being one image per page, it still be one image per page but it will be broken up into "tiles" or "strips".

I tried implementing the code with code from the "ConvertToSingleStrip.cs" sample project, but keep ending up with a single strip.

The part I tried to change was in copyStrips:

private static void copyStrips(Tiff input, Tiff output)
        {
            bool encoded = false;
            FieldValue[] compressionTagValue = input.GetField(TiffTag.COMPRESSION);
            if (compressionTagValue != null)
                encoded = compressionTagValue[0].ToInt() != (int)Compression.NONE;

            int numberOfStrips = input.NumberOfStrips();

            int offset = 0;
            byte[] stripsData = new byte[numberOfStrips * input.StripSize() / 2];
            byte[] stripsData2 = new byte[numberOfStrips * input.StripSize() / 2];
            for (int i = 0; i < numberOfStrips; ++i)
            {
                int bytesRead = readStrip(input, i, stripsData, offset, encoded);
                offset += bytesRead;
            }

            writeStrip(output, stripsData, offset, encoded);
            for (int i = 0; i < numberOfStrips; ++i)
            {
                int bytesRead = readStrip(input, i, stripsData2, input.StripSize() / 2, encoded);
                offset += bytesRead;
            }

            writeStrip(output, stripsData2, input.StripSize() / 2, encoded);
        }
0

There are 0 best solutions below