Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split multipage Tiff using LibTiff.Net

Tags:

c#

libtiff.net

In the LibTiff.Net documentation I've found that it is possible to get a specific page of the Tiff document.

But, if it is possible to split multipage Tiff (using LibTiff.Net) without knowing how many pages are there? How?

Using this example it returns only the first page.

Btw, the main problem is that Windows XP can't handle different tiff images, so I want to split it into jpeg ones.

like image 847
1gn1ter Avatar asked Nov 01 '12 13:11

1gn1ter


People also ask

How do I split a multi page TIFF file?

❓ How can I split TIFF document? First, you need to add a file for split: drag & drop your TIFF file or click inside the white area for choose a file. Then click the 'Split' button. When split TIFF document is completed, you can download your result files.

What is TIFF splitter?

Tiff Splitter is a tool for splitting TIFF files into several pages. The program supports all the documents in TIFF format and compressed TIFF files. It offers two ways to name the files and allows to delete, add or insert TIFF file page in batch mode.

Can TIFF store multiple images?

TIFF files can store an unlimited number of separate images. Each image has an IFD, or Image File Directory, that records where that image's data and metadata is stored in the file. The interesting thing is that the image data and the IFDs can be stored anywhere in the file, in any order.


1 Answers

//open tif file
var tif = Tiff.Open(@"file", "r");

//get number of pages
var num = tif.NumberOfDirectories();

for (short i = 0; i < num; i++)
{
    //set current page
    tif.SetDirectory(i); 

    Bitmap bmp = GetBitmapFormTiff(tif);
    bmp.Save(string.Format(@"newfile{0}.bmp", i));
}

The code of GetBitmapFormTiff is from example:

private static Bitmap GetBitmapFormTiff(Tiff tif)
        {
            FieldValue[] value = tif.GetField(TiffTag.IMAGEWIDTH);
            int width = value[0].ToInt();

            value = tif.GetField(TiffTag.IMAGELENGTH);
            int height = value[0].ToInt();

            //Read the image into the memory buffer
            var raster = new int[height * width];
            if (!tif.ReadRGBAImage(width, height, raster))
            {
                return null;
            }

            var bmp = new Bitmap(width, height, PixelFormat.Format32bppRgb);

            var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

            BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
            var bits = new byte[bmpdata.Stride * bmpdata.Height];

            for (int y = 0; y < bmp.Height; y++)
            {
                int rasterOffset = y * bmp.Width;
                int bitsOffset = (bmp.Height - y - 1) * bmpdata.Stride;

                for (int x = 0; x < bmp.Width; x++)
                {
                    int rgba = raster[rasterOffset++];
                    bits[bitsOffset++] = (byte)((rgba >> 16) & 0xff);
                    bits[bitsOffset++] = (byte)((rgba >> 8) & 0xff);
                    bits[bitsOffset++] = (byte)(rgba & 0xff);
                    bits[bitsOffset++] = (byte)((rgba >> 24) & 0xff);
                }
            }

            System.Runtime.InteropServices.Marshal.Copy(bits, 0, bmpdata.Scan0, bits.Length);
            bmp.UnlockBits(bmpdata);

            return bmp;
        }
like image 108
1gn1ter Avatar answered Sep 29 '22 00:09

1gn1ter