TIF: improper header

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Bonestorm
Posts: 2
Joined: 2015-11-23T07:09:59-07:00
Authentication code: 1151

TIF: improper header

Post 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!
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: TIF: improper header

Post 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.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Bonestorm
Posts: 2
Joined: 2015-11-23T07:09:59-07:00
Authentication code: 1151

Re: TIF: improper header

Post 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.
ahmad.eldefrawy
Posts: 2
Joined: 2015-12-15T06:53:59-07:00
Authentication code: 1151

Re: TIF: improper header

Post 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);
Last edited by ahmad.eldefrawy on 2015-12-24T00:55:20-07:00, edited 1 time in total.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: TIF: improper header

Post 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.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
ahmad.eldefrawy
Posts: 2
Joined: 2015-12-15T06:53:59-07:00
Authentication code: 1151

Re: TIF: improper header

Post 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.
Post Reply