how to control TIFF image header's 1st IFD offset value while generating TIFF?

1.5k views Asked by At

I would like to know is there a way to control the 4 byte data in TIFF image header which stores the 1st IFD offset value.

I have a situation, when I scan three different image and store directly into file system, it comes with the value [4949 002A 00000080] (hex)

But, using libTiff .net api, when I combine all three images and create one single TIFF, the last four bytes in the header are coming with random value, which is 1st IFD offset.

I have a requirement, where 1st IFD offset should be 00000080. I am using C# with libTiff .net api to generate TIFF.

Please find the snippet of Code: Below code creates a Tiff file using the image captured:

using (FileStream file = new FileStream("C:\\imgs\\front.tif", FileMode.Create, System.IO.FileAccess.Write))
{
    streamBitmapFront.WriteTo(file);
}
using (FileStream file = new FileStream("C:\\imgs\\gray.tif", FileMode.Create, System.IO.FileAccess.Write))
{
    streamBitmapFront2.WriteTo(file);
}

using (FileStream file = new FileStream("C:\\imgs\\rear.tif", FileMode.Create, System.IO.FileAccess.Write))
{
    streamBitmapRear.WriteTo(file);
}

front.tif, gray.tif and rear.tif have the correct header data. [4949 002A 00000080] (hex)

Below code uses libTiff.net api to bind all there images and create single tiff image.

using (Tiff output = Tiff.Open( path + uiString + ".IMG", "w"))
{
    using (Tiff header = Tiff.Open("C:\\imgs\\gray.tif", "r"))
    {
        WritePage(output, header, 1, 3, uiString);
    }
    using (Tiff header = Tiff.Open("C:\\imgs\\front.tif", "r"))
    {
        WritePage(output, header, 2, 3, uiString);
    }
    using (Tiff header = Tiff.Open("C:\\imgs\\rear.tif", "r"))
    {
        WritePage(output, header, 3, 3, uiString);
    }
}


public static void WritePage(Tiff output, Tiff input, int pageNumber, int numberOfPages, String name)
{
    int height = input.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
    int width = input.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();
    int samplesPerPixel = 1;
    int bitsPerSample = input.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt();
    int scanlineSize = input.ScanlineSize();

    byte[][] buffer = new byte[height][];
    for (int i = 0; i < height; ++i)
    {
        buffer[i] = new byte[scanlineSize];
        input.ReadScanline(buffer[i], i);
    }

    output.SetField(TiffTag.IMAGEWIDTH, width / samplesPerPixel);
    output.SetField(TiffTag.IMAGELENGTH, height);
    output.SetField(TiffTag.SAMPLESPERPIXEL, samplesPerPixel);
    output.SetField(TiffTag.ORIENTATION, BitMiracle.LibTiff.Classic.Orientation.TOPLEFT);
    output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
    output.SetField(TiffTag.SUBFILETYPE, 0);
    output.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISWHITE);
    output.SetField(TiffTag.ROWSPERSTRIP, height);

    if (pageNumber == 1)
    {
        output.SetField(TiffTag.STRIPOFFSETS, input.GetField(TiffTag.STRIPOFFSETS)[0].ToIntArray()[0]);
        //output.SetField(TiffTag.COMPRESSION, Compression.JPEG); 
        output.SetField(TiffTag.BITSPERSAMPLE, 8);
        output.SetField(TiffTag.XRESOLUTION, 120.0);
        output.SetField(TiffTag.YRESOLUTION, 120.0);
        output.SetField(TiffTag.DOCUMENTNAME, name + "G.JPEG");

    }
    else
    {
        if ( pageNumber == 2 )
            output.SetField(TiffTag.DOCUMENTNAME, name + "F.TIFF");
        if  (pageNumber == 3 )
            output.SetField(TiffTag.DOCUMENTNAME, name + "R.TIFF");

        output.SetField(TiffTag.BITSPERSAMPLE, 1);
        output.SetField(TiffTag.COMPRESSION, Compression.CCITT_T6);
        output.SetField(TiffTag.XRESOLUTION, 200.0);
        output.SetField(TiffTag.YRESOLUTION, 200.0);
    }


    output.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.INCH);
    output.SetField(TiffTag.SUBFILETYPE, FileType.PAGE);
    output.SetField(TiffTag.PAGENUMBER, pageNumber, numberOfPages);

    for (int i = 0; i < height; ++i)
        output.WriteScanline(buffer[i], i);

    output.WriteDirectory();
}

Once TIFF image with three pages created the header value is inconsistent. [4949 002A 0003B210] (hex)

[Update 1]

Stackoverflow answer described about calling output.CheckpointDirectory(); before writing any data into the file. I tried that method also and found, now the last four bytes of the header is changed and it is always same unlike, earlier it was always random. Now I am getting 00000010

Thanks

0

There are 0 answers