Page 1 of 1

Page offsets are lost when saving TIFF

Posted: 2013-07-24T02:48:02-07:00
by argv[0]
When saving a TIFF both horizontal and vertical page offsets are lost when any of them (horizontal or vertical) <= 0:

Code: Select all

$ sf=tif; sz=16x16; for x in -16 +0 +16; do for y in -16 +0 +16; do convert -size $sz -page $sz$x$y -depth 8 -units PixelsPerInch -density 300 xc:gray50 $sz$x$y.$sf; identify -format '%f  (%m)    %g\n' $sz$x$y.$sf; done; done
16x16-16-16.tif (TIFF)  16x16+0+0
16x16-16+0.tif  (TIFF)  16x16+0+0
16x16-16+16.tif (TIFF)  16x16+0+0
16x16+0-16.tif  (TIFF)  16x16+0+0
16x16+0+0.tif   (TIFF)  16x16+0+0
16x16+0+16.tif  (TIFF)  16x16+0+0
16x16+16-16.tif (TIFF)  16x16+0+0
16x16+16+0.tif  (TIFF)  16x16+0+0
16x16+16+16.tif (TIFF)  16x16+16+16
The same saving PNG works properly:

Code: Select all

$ sf=png; sz=16x16; for x in -16 +0 +16; do for y in -16 +0 +16; do convert -size $sz -page $sz$x$y -depth 8 -units PixelsPerInch -density 300 xc:gray50 $sz$x$y.$sf; identify -format '%f  (%m)    %g\n' $sz$x$y.$sf; done; done
16x16-16-16.png (PNG)   16x16-16-16
16x16-16+0.png  (PNG)   16x16-16+0
16x16-16+16.png (PNG)   16x16-16+16
16x16+0-16.png  (PNG)   16x16+0-16
16x16+0+0.png   (PNG)   16x16+0+0
16x16+0+16.png  (PNG)   16x16+0+16
16x16+16-16.png (PNG)   16x16+16-16
16x16+16+0.png  (PNG)   16x16+16+0
16x16+16+16.png (PNG)   16x16+16+16
Re-paging.
Using TIFF:

Code: Select all

$ sf=tif; sz=16x16; for x in -16 +0 +16; do for y in -16 +0 +16; do convert -size $sz -page $sz+1+1 -depth 8 -units PixelsPerInch -density 300 xc:gray50 $sz$x$y.$sf; identify -format '%f  (%m)    %g\n' $sz$x$y.$sf; convert $sz$x$y.$sf -repage $sz$x$y $sz$x$y.$sf; identify -format '%f        (%m)    %g\n' $sz$x$y.$sf; done; done
16x16-16-16.tif (TIFF)  16x16+1+1
16x16-16-16.tif (TIFF)  16x16+0+0
16x16-16+0.tif  (TIFF)  16x16+1+1
16x16-16+0.tif  (TIFF)  16x16+0+0
16x16-16+16.tif (TIFF)  16x16+1+1
16x16-16+16.tif (TIFF)  16x16+0+0
16x16+0-16.tif  (TIFF)  16x16+1+1
16x16+0-16.tif  (TIFF)  16x16+0+0
16x16+0+0.tif   (TIFF)  16x16+1+1
16x16+0+0.tif   (TIFF)  16x16+0+0
16x16+0+16.tif  (TIFF)  16x16+1+1
16x16+0+16.tif  (TIFF)  16x16+0+0
16x16+16-16.tif (TIFF)  16x16+1+1
16x16+16-16.tif (TIFF)  16x16+0+0
16x16+16+0.tif  (TIFF)  16x16+1+1
16x16+16+0.tif  (TIFF)  16x16+0+0
16x16+16+16.tif (TIFF)  16x16+1+1
16x16+16+16.tif (TIFF)  16x16+16+16
Using PNG:

Code: Select all

$ sf=png; sz=16x16; for x in -16 +0 +16; do for y in -16 +0 +16; do convert -size $sz -page $sz+1+1 -depth 8 -units PixelsPerInch -density 300 xc:gray50 $sz$x$y.$sf; identify -format '%f  (%m)    %g\n' $sz$x$y.$sf; convert $sz$x$y.$sf -repage $sz$x$y $sz$x$y.$sf; identify -format '%f        (%m)    %g\n' $sz$x$y.$sf; done; done
16x16-16-16.png (PNG)   16x16+1+1
16x16-16-16.png (PNG)   16x16-16-16
16x16-16+0.png  (PNG)   16x16+1+1
16x16-16+0.png  (PNG)   16x16-16+0
16x16-16+16.png (PNG)   16x16+1+1
16x16-16+16.png (PNG)   16x16-16+16
16x16+0-16.png  (PNG)   16x16+1+1
16x16+0-16.png  (PNG)   16x16+0-16
16x16+0+0.png   (PNG)   16x16+1+1
16x16+0+0.png   (PNG)   16x16+0+0
16x16+0+16.png  (PNG)   16x16+1+1
16x16+0+16.png  (PNG)   16x16+0+16
16x16+16-16.png (PNG)   16x16+1+1
16x16+16-16.png (PNG)   16x16+16-16
16x16+16+0.png  (PNG)   16x16+1+1
16x16+16+0.png  (PNG)   16x16+16+0
16x16+16+16.png (PNG)   16x16+1+1
16x16+16+16.png (PNG)   16x16+16+16
Software versions:

Code: Select all

$ uname -srm
FreeBSD 8.4-STABLE amd64
$ gcc -v
Using built-in specs.
Target: amd64-undermydesk-freebsd
Configured with: FreeBSD/amd64 system compiler
Thread model: posix
gcc version 4.2.1 20070831 patched [FreeBSD]
$ identify -version
Version: ImageMagick 6.8.6-6 2013-07-23 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2013 ImageMagick Studio LLC
Features: DPC HDRI OpenMP
Delegates: bzlib fftw fpx fontconfig freetype jbig jng jp2 jpeg lcms lqr openexr png ps rsvg tiff wmf x xml zlib

$ pkg_info -E -x ^tiff -x ^png
png-1.5.16
tiff-4.0.3

Re: Page offsets are lost when saving TIFF

Posted: 2013-07-24T06:04:50-07:00
by argv[0]
This looks like a bug in the "coders/tiff.c".
Quick and dirty patch is:

Code: Select all

--- coders/tiff.c.orig  2013-06-30 17:44:02.000000000 +0000
+++ coders/tiff.c       2013-07-24 12:43:54.333966829 +0000
@@ -3176,7 +3176,7 @@
         (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
         (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
         (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
-        if ((image->page.x > 0) && (image->page.y > 0))
+        if (((image->page.x > 0) && (image->page.y >= 0)) || ((image->page.y > 0) && (image->page.x >= 0)))
           {
             /*
               Set image position.
But, just IMHO, silently drop the negative page offsets - isn't Right Thing.

Re: Page offsets are lost when saving TIFF

Posted: 2013-07-25T04:37:08-07:00
by argv[0]
More complex patch (to 6.8.6-7):

Code: Select all

--- coders/tiff.c.orig	2013-07-24 17:25:21.000000000 +0000
+++ coders/tiff.c	2013-07-25 10:58:06.812011876 +0000
@@ -3178,18 +3178,24 @@
         (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
         (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
         (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
-        if (((image->page.x > 0) && (image->page.y >= 0)) ||
-            ((image->page.y > 0) && (image->page.x >= 0)))
+        if ((image->page.x < 0) || (image->page.y < 0))
+          ThrowWriterException(CoderError,
+            "TIFF: negative image positions unsupported");
+        if ((image->page.x > 0) && (image->x_resolution > 0.0))
           {
             /*
-              Set image position.
+              Set horizontal image position.
             */
-            if (image->x_resolution != 0.0)
-              (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
-                image->x_resolution);
-            if (image->y_resolution != 0.0)
-              (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
-                image->y_resolution);
+            (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
+              image->x_resolution);
+          }
+        if ((image->page.y > 0) && (image->y_resolution > 0.0))
+          {
+            /*
+              Set vertical image position.
+            */
+            (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
+              image->y_resolution);
           }
       }
     if (image->chromaticity.white_point.x != 0.0)

Re: Page offsets are lost when saving TIFF

Posted: 2013-07-25T05:01:08-07:00
by magick
Thanks for the patch, look for it in the subversion branches by sometime tomorrow.