Page 1 of 1

Get an individual pagesize from a multipage tiff

Posted: Wed Oct 28, 2015 7:54 pm
by philip.y
Hi

Is it possible to obtain the page size (in bytes) of individual pages in a tiff file?

I'm wanting to determine the collective size of all colour pages within a tiff, ignoring non-colour so was looking to do something like:

Code: Select all

Using tif As New Vintasoft.Imaging.Codecs.Tiff.TiffFile(sourceFile)
                For Each page In tif.Pages
                    If (page.BitsPerPixel) >= 24 Then
                        lColourSize = lColourSize + page.Length
                    End If
                Next
            End Using
"page.length" always returns zero. Is there a way to do this without copying the page out?

Cheers

Phil

Re: Get an individual pagesize from a multipage tiff

Posted: Sat Oct 31, 2015 5:20 pm
by Alex
Hi Phil,

Length, in bytes, of TIFF image data can be calcuated from TIFF image metadata.

Here is an example that shows how to get length, in bytes, of TIFF image data:

Code: Select all

using System;
using Vintasoft.Imaging.Codecs.ImageFiles.Tiff;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(string.Format("'strips.tif', page 0, image data length in bytes: {0}", GetTiffImageDataLengthInBytes("strips.tif", 0)));
            Console.WriteLine(string.Format("'tiles.tif', page 0, image data length in bytes: {0}", GetTiffImageDataLengthInBytes("tiles.tif", 0)));

            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }

        /// <summary>
        /// Gets the length, in bytes, of TIFF image data.
        /// </summary>
        /// <param name="filename">Name of TIFF file.</param>
        /// <param name="pageIndex">Zero-based index of page in TIFF file.</param>
        /// <returns></returns>
        static uint GetTiffImageDataLengthInBytes(string filename, int pageIndex)
        {
            // length, in bytes, of image data
            uint imageDataLengthInBytes = 0; 
            
            // open TIFF file
            using (TiffFile tiffFile = new TiffFile(filename))
            {
                // get TIFF tag collection of specified TIFF page
                TiffTagCollection tags = tiffFile.Pages[pageIndex].IFD.Tags;

                // get StripByteCounts tag
                TiffTag stripByteCountsTag = tags.Find(TiffTagId.StripByteCounts);
                // if tag is found image data are stored in strips
                if (stripByteCountsTag != null)
                {
                    object stripByteCountsTagData = stripByteCountsTag.Data;
                    uint[] stripByteCountTagAsUIntArray = stripByteCountsTagData as uint[];
                    if (stripByteCountTagAsUIntArray != null)
                    {
                        for (int i = 0; i < stripByteCountTagAsUIntArray.Length; i++)
                            imageDataLengthInBytes = stripByteCountTagAsUIntArray[i];
                    }
                }

                // get TileByteCounts tag
                TiffTag tileByteCountsTag = tags.Find(TiffTagId.TileByteCounts);
                // if tag is found image data are stored in tiles
                if (tileByteCountsTag != null)
                {
                    object tileByteCountsTagData = tileByteCountsTag.Data;
                    uint[] tileByteCountsTagAsUIntArray = tileByteCountsTagData as uint[];
                    if (tileByteCountsTagAsUIntArray != null)
                    {
                        for (int i = 0; i < tileByteCountsTagAsUIntArray.Length; i++)
                            imageDataLengthInBytes = tileByteCountsTagAsUIntArray[i];
                    }
                }
            }

            return imageDataLengthInBytes;
        }
    }
}
Best regards, Alexander

Re: Get an individual pagesize from a multipage tiff

Posted: Mon Nov 02, 2015 2:48 pm
by philip.y
That's exactly what I needed!

Thanks (as always) for your reply.