Multipage Tiff Extraction Functions Incorrectly

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
locriani
Posts: 13
Joined: 2016-08-20T00:01:48-07:00
Authentication code: 1151

Multipage Tiff Extraction Functions Incorrectly

Post by locriani »

Hello!

With either the latest ImageMagick 6.9.5 or ImageMagick 7.0.2, tiff multipage extraction does not function correctly:

Code: Select all

vagrant@vagrant-ubuntu-trusty-64:~$ convert --version
Version: ImageMagick 7.0.2-9 Q16 x86_64 2016-08-26 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP 
Delegates (built-in): bzlib djvu fontconfig freetype gvc jbig jng jpeg lcms lqr lzma openexr png tiff wmf x xml zlib

Code: Select all

vagrant@vagrant-ubuntu-trusty-64:~$ identify 5c7cf6f8-7d4d-47a8-b2f4-fff17911a224.tif 
5c7cf6f8-7d4d-47a8-b2f4-fff17911a224.tif[0] TIFF 2025x650 2025x650+0+0 8-bit CIELab 218KB 0.010u 0:00.000
5c7cf6f8-7d4d-47a8-b2f4-fff17911a224.tif[1] TIFF 2025x650 2025x650+0+0 8-bit CIELab 0.110u 0:00.120
vagrant@vagrant-ubuntu-trusty-64:~$ convert 5c7cf6f8-7d4d-47a8-b2f4-fff17911a224.tif[0] out.tiff
vagrant@vagrant-ubuntu-trusty-64:~$ identify out.tiff 
out.tiff[0] TIFF 2025x650 2025x650+0+0 8-bit CIELab 378KB 0.010u 0:00.009
out.tiff[1] TIFF 2025x650 2025x650+0+0 8-bit CIELab 0.110u 0:00.120
vagrant@vagrant-ubuntu-trusty-64:~$ convert 5c7cf6f8-7d4d-47a8-b2f4-fff17911a224.tif[1] out_1.tiff
vagrant@vagrant-ubuntu-trusty-64:~$ identify out_1.tiff 
out_1.tiff TIFF 2025x650 2025x650+0+0 8-bit CIELab 5.332MB 0.010u 0:00.000

Code: Select all

vagrant@vagrant-ubuntu-trusty-64:~$ convert 5c7cf6f8-7d4d-47a8-b2f4-fff17911a224.tif -scene 0 a%d.tif
vagrant@vagrant-ubuntu-trusty-64:~$ identify a0.tif 
a0.tif[0] TIFF 2025x650 2025x650+0+0 8-bit CIELab 378KB 0.010u 0:00.010
a0.tif[1] TIFF 2025x650 2025x650+0+0 8-bit CIELab 0.110u 0:00.119
vagrant@vagrant-ubuntu-trusty-64:~$ identify a1.tif 
a1.tif[1] TIFF 2025x650 2025x650+0+0 8-bit CIELab 285KB 0.000u 0:00.000
The expected behavior would be for index 0 to extract the first page of the tiff out. Instead, it copies the image. The expected behavior for index 1 .. whatever is for it to extract that specific page. That does work properly.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Multipage Tiff Extraction Functions Incorrectly

Post by dlemstra »

Can you share your file on something like dropbox?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multipage Tiff Extraction Functions Incorrectly

Post by snibgo »

What do you think doesn't work correctly?
locriani wrote:convert 5c7cf6f8-7d4d-47a8-b2f4-fff17911a224.tif -scene 0 a%d.tif
When the input tiff contains two images, this should create one output tiff file that contains those two images. The output file will be named "a0.tif".
snibgo's IM pages: im.snibgo.com
locriani
Posts: 13
Joined: 2016-08-20T00:01:48-07:00
Authentication code: 1151

Re: Multipage Tiff Extraction Functions Incorrectly

Post by locriani »

locriani
Posts: 13
Joined: 2016-08-20T00:01:48-07:00
Authentication code: 1151

Re: Multipage Tiff Extraction Functions Incorrectly

Post by locriani »

snibgo wrote:What do you think doesn't work correctly?
locriani wrote:convert 5c7cf6f8-7d4d-47a8-b2f4-fff17911a224.tif -scene 0 a%d.tif
When the input tiff contains two images, this should create one output tiff file that contains those two images. The output file will be named "a0.tif".
It is impossible to extract both images from the tiff into separate images. You can extract [0, 1] into one image, and [1] into another image, but not [0].
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multipage Tiff Extraction Functions Incorrectly

Post by snibgo »

locriani wrote:It is impossible to extract both images from the tiff into separate images. You can extract [0, 1] into one image, and [1] into another image, but not [0].
Have you tried using "[0]"?

Code: Select all

convert 5c7cf6f8-7d4d-47a8-b2f4-fff17911a224.tif[0] -scene 0 a%d.tif
("-scene 0" is irrelevant, as that is the default.)
snibgo's IM pages: im.snibgo.com
locriani
Posts: 13
Joined: 2016-08-20T00:01:48-07:00
Authentication code: 1151

Re: Multipage Tiff Extraction Functions Incorrectly

Post by locriani »

snibgo wrote:
locriani wrote:It is impossible to extract both images from the tiff into separate images. You can extract [0, 1] into one image, and [1] into another image, but not [0].
Have you tried using "[0]"?

Code: Select all

convert 5c7cf6f8-7d4d-47a8-b2f4-fff17911a224.tif[0] -scene 0 a%d.tif
("-scene 0" is irrelevant, as that is the default.)
Yes, look above. Second code block in the original post.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multipage Tiff Extraction Functions Incorrectly

Post by snibgo »

Ah, yes, I didn't see that. Can you make a simple reproducible example, eg:

Code: Select all

convert rose: wizard: a.tiff
convert a.tiff[0] out.tiff
If that works fine, then can you post the problem tiff.
snibgo's IM pages: im.snibgo.com
locriani
Posts: 13
Joined: 2016-08-20T00:01:48-07:00
Authentication code: 1151

Re: Multipage Tiff Extraction Functions Incorrectly

Post by locriani »

Here's an image with the issue:
https://assets.weaveup.com/uploads/uplo ... 11a224.tif

It appears to affect all the images we have had uploaded (generally created in Photoshop or NedGraphics), but not the example generated by imagemagick you just posted.
locriani
Posts: 13
Joined: 2016-08-20T00:01:48-07:00
Authentication code: 1151

Re: Multipage Tiff Extraction Functions Incorrectly

Post by locriani »

It appears the difference is that the images I have with the issue have the following subfile tag:

Code: Select all

  Subfile Type: (0 = 0x0)
While the generated image above that works has the following subfile tag:

Code: Select all

  Subfile Type: multi-page document (2 = 0x2)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multipage Tiff Extraction Functions Incorrectly

Post by fmw42 »

Your image from the link has only one layer and the channels are LAB as reported by my Photoshop CS.
locriani
Posts: 13
Joined: 2016-08-20T00:01:48-07:00
Authentication code: 1151

Re: Multipage Tiff Extraction Functions Incorrectly

Post by locriani »

The subfile is there, I assure you.
As per the TIFF spec:
There may be more than one IFD in a TIFF file. Each IFD defines a subfile. One potential use of subfiles is to describe related images, such as the pages of a facsimile transmission. A Baseline TIFF reader is not required to read any IFDs beyond the first one.
Photoshop is not required to read any additional subfiles. However, the first subfile is the canonical image in this case. ImageMagick is incapable of extracting the canonical image from this TIFF representation.

It's another TIFF format extension that implements paging support, as in PDFs. ImageMagick handles that case correctly, but not the subfile case correctly.
locriani
Posts: 13
Joined: 2016-08-20T00:01:48-07:00
Authentication code: 1151

Re: Multipage Tiff Extraction Functions Incorrectly

Post by locriani »

Here's another example image:

https://assets.weaveup.com/print_raster ... 2325b7.tif

Code: Select all

locriani@Schreckhorn:~ $ identify c6f95170d8a5aebb1ee7e32bf52325b7.tif 
c6f95170d8a5aebb1ee7e32bf52325b7.tif[0] TIFF 1013x325 1013x325+0+0 8-bit CIELab 232KB 0.000u 0:00.000
c6f95170d8a5aebb1ee7e32bf52325b7.tif[1] TIFF 2025x650 2025x650+0+0 8-bit CIELab 0.070u 0:00.069

locriani@Schreckhorn:~ $ convert c6f95170d8a5aebb1ee7e32bf52325b7.tif[0] out_0.tiff
locriani@Schreckhorn:~ $ identify out_0.tiff
out_0.tiff[0] TIFF 1013x325 1013x325+0+0 8-bit CIELab 232KB 0.000u 0:00.000
out_0.tiff[1] TIFF 2025x650 2025x650+0+0 8-bit CIELab 0.070u 0:00.070

locriani@Schreckhorn:~ $ convert c6f95170d8a5aebb1ee7e32bf52325b7.tif[1] out_1.tiff

locriani@Schreckhorn:~ $ identify out_1.tiff
out_1.tiff TIFF 2025x650 2025x650+0+0 8-bit CIELab 5.332MB 0.000u 0:00.009
As you can see, the 2 images in this file are different sizes (and have different colors). It is possible to extract image[1], but not the image[0]. Instead, you extract image[0, 1] (basically copying the image), which is not useful.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Multipage Tiff Extraction Functions Incorrectly

Post by dlemstra »

You have found a feature that was added last year. The first image has a resource block that contains layer information. Extracting one of the layers is possible as you have seen. But you cannot 'only' get the first image because the second image (layer) is part of the first image. You can disable this feature with the following define:

Code: Select all

D:\>identify -define tiff:ignore-layers=true c6f95170d8a5aebb1ee7e32bf52325b7.tif
c6f95170d8a5aebb1ee7e32bf52325b7.tif TIFF 1013x325 1013x325+0+0 8-bit CIELab 232KB 0.000u 0:00.000
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
locriani
Posts: 13
Joined: 2016-08-20T00:01:48-07:00
Authentication code: 1151

Re: Multipage Tiff Extraction Functions Incorrectly

Post by locriani »

For anyone else who encounters this issue, you can use libtiff directly to isolate the first extent:

Code: Select all

tiffcp image.tiff,0 out.tiff
Post Reply