IM may write GIF files with an invalid image position

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.
Post Reply
Jason S
Posts: 103
Joined: 2010-12-14T19:42:12-07:00
Authentication code: 8675308

IM may write GIF files with an invalid image position

Post by Jason S »

(from this thread)

This command

Code: Select all

convert rose: -units PixelsPerInch -density 100x100 -page 70x46+100+100 test1.gif
write a GIF image with a logical screen size of 70x46, containing a single image of size 70x46, which is positioned at offset +100+100.

The GIF89a specification says "Each image must fit within the boundaries of the Logical Screen". I interpret this to mean that the GIF file is invalid, because the image does not fit within the bounds of the logical screen (they don't even overlap).

You can't blame the user, either, because the same invalid GIF file can be created by converting from a valid TIFF file:

Code: Select all

convert rose: -units PixelsPerInch -density 100x100 -page 70x46+100+100 temp.tif
convert temp.tif test2.gif
Maybe the GIF logical screen should be enlarged so that the image fits within it (to 170x146 in this case).
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: IM may write GIF files with an invalid image position

Post by glennrp »

Enlarging the logical image would likely lead to unexpected results for other people.
The most spec-compliant thing to do seems to me to be to just write an
empty 70x46 image (i.e., only write whatever overlaps the page, in this
case nothing, over the background). That might not be what the user wanted
in this particular case, but they made an invalid request.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: IM may write GIF files with an invalid image position

Post by anthony »

Actually the most 'spec' thing to do is the same as what IM does for handling failed crop images. Create a one pixel transparent "miss" image, but that is not always a desirable thing to do either!

See IM example, Animation Modifications, Reposition Animation Frames
http://www.imagemagick.org/Usage/anim_m ... e_relative
This has an example that actually generates a incorrect image of this type!

NOTE this is more of something that highlights image handling bugs in programs. Firefox handles it fine, IE does not. Really, any applications should be checking for 'problems' like this any way! IM does, and you can force this with -repage 0x0 (which is illogical!)
Page, Repage, and the Virtual Canvas
http://www.imagemagick.org/Usage/basics/#page

Images with negative or large offsets are actually very useful with layering image. Typically the layered image does not need or desire any sort of virtual canvas size, as the processing is not finalised. The image is still in an intermediate state.

Negative or large position offsets are common in image composition, though they typically use gravity effected geometry, rather than 'layer offsets', even in the simplest of cases..
See second example of 'over' compose method....
http://www.imagemagick.org/Usage/compose/#over

Distorted images are very often 'illegal' in its positioning. It is just a useful thing to do.
For example... Arc Center Point Placement
http://www.imagemagick.org/Usage/distorts/#arc_center

A better example of this is the older examples from: Affine Matrix Processing, Positions of Results
http://www.imagemagick.org/Usage/affine/#affine_pos
IN this I use some code to generate a special 'viewport' image so as to demonstrate the actual image positions.

Shadow image generation can also generate negative offset images...
http://www.imagemagick.org/Usage/blur/#shadow
Without it, shadow can not be positioned correctly relative to the input image!

The -layers merge operator was specifically designed to deal with images with negative positions. Especially those generated by distort and shadows, but also for 'layer arrangements'
See Programmed Positioning of Layered Images
http://www.imagemagick.org/Usage/layers/#layer_prog

In summary... many Layering image technqiues will generate images that have bounds that fall outside a 'virtual canvas' This is especially the case with negative offsets, as a virtual canvas is only defined as being from the origin, to the positive size.


GIF is probably the worst in it handling of layer offsets. the image file format does not allow for negative offsets at all.. the value is unsigned, so negative offsets get clipped to zero!

PNG, and MIFF images can handle with negative or large offsets fine. In fact default PNG does not even have a 'canvas' part to the image file format, that was added by IM when it saves images. Though I would not like to display those images in a web browser until the image is made a little more 'presentable'.

In most cases is the canvas is not defined (or set to 0x0) IM will try to define a 'logical' canvas that best fits the image and its offset, though that is not possible with negative offsets, so it has a minimum canvas size of the image size.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: IM may write GIF files with an invalid image position

Post by glennrp »

After further thought I agree that the logical screen should be enlarged. In fact the GIF encoder already has the logic to find (and use) the bounding box of all images when writing multiple images, so it just needs to do that with writing a single image as well. I've updated the GIF encoder accordingly (SVN revision 5832, IM-6.7.3-4).
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: IM may write GIF files with an invalid image position

Post by anthony »

The function to do that is already provided -layers TrimBounds But that will also shrink GIF images that don't make full use of the provided area.

I would prefer to not do that. Its bad enough that you can't use negative offsets.

It is always been my oppion not to try and save users from themselves.
Because doing that would stop users from doing other things.

However having said that, I will not stop this change.

WARNING: if you change this in IMv6 you currently should apply that change to IMv7 too, which is currently in alpha development.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply