Page 1 of 1

TIF: improper header

Posted: 2015-11-23T07:13:34-07:00
by Bonestorm
Hi, when trying to convert a multi image TIFF to a different format, I get an error message:

Code: Select all

convert: improper image header 'test.tif' @ error/tiff.c/ReadTIFFImage/1218
The TIFF file works fine in other programms. I gets created by a C programm like this:

Code: Select all

TIFF* tif;
 tif = TIFFOpen(filename, "w");
 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, columns);
 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, rows);
 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
 TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);

 for (i = 0; i < rows; i++)
     TIFFWriteScanline(tif, &data[i * columns], i, 0);

 TIFFWriteDirectory(tif);
 TIFFClose(tif);
What might be the issue here? Thanks!

Re: TIF: improper header

Posted: 2015-11-23T07:22:45-07:00
by dlemstra
Can you add a link for the TIFF file to your post? And what is your version of ImageMagick and your operating system.

Re: TIF: improper header

Posted: 2015-11-23T07:36:48-07:00
by Bonestorm
You may download the .TIFF file from https://onedrive.live.com/redir?resid=E ... hoto%2ctif

I'm using ImageMagick 6.9.2-6 on a Linux Mint 17.1 Cinnamon 64-bit machine.

Code: Select all

convert -list format
tells me

Code: Select all

TIFF* rw+ (LIBTIFF, Version 4.0.3)
TIFF64* rw-  (LIBTIFF, Version 4.0.3)
PS: the TIFF wont display correctly on the linked page - using FIJI, it's displayed properly.

Re: TIF: improper header

Posted: 2015-12-15T07:01:04-07:00
by ahmad.eldefrawy
I've managed to workaround this issue by editing the file /coders/Tiff.c under ImageMagick directory by replacing the following checks with their version of ImageMagick 6.5.

From like this:

Code: Select all

  if ((TIFFGetField(tiff,TIFFTAG_IMAGEWIDTH,&width) != 1) ||
        (TIFFGetField(tiff,TIFFTAG_IMAGELENGTH,&height) != 1) ||
        (TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag) != 1) ||
        (TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian) != 1) ||
        (TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace) != 1) ||
        (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,&samples_per_pixel) != 1) ||
        (TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample) != 1) ||
        (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format) != 1) ||
        (TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value) != 1) ||
        (TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value) != 1) ||
        (TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric) != 1))
      {
        TIFFClose(tiff);
        ThrowReaderException(CorruptImageError,"ImproperImageHeader");
      }      
To like this:

Code: Select all

    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
    (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);

Re: TIF: improper header

Posted: 2015-12-15T08:12:09-07:00
by dlemstra
You really don't want to make this change. Those checks are there to make sure that all the required fields have a value. Not checking the result will give unexpected results.

The file from the OP does not contain a TIFFTAG_PHOTOMETRIC which is required for us to read the file.

Re: TIF: improper header

Posted: 2015-12-16T04:46:17-07:00
by ahmad.eldefrawy
Thank you dlemstra,

I can see your point but i just can't find any other way around it. I used to convert these images on 6.5 but my workflow stopped after upgrading to 6.9.
Is there a way to add the "TIFFTAG_PHOTOMETRIC" field to my images to fix them? My TIFF images are currently missing few fields according to the checks found in the code!

Thanks in advance.