MagickOptimizeImageLayers and command line -layers optimize

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
timmy_tofu
Posts: 4
Joined: 2013-12-11T09:18:04-07:00
Authentication code: 6789

MagickOptimizeImageLayers and command line -layers optimize

Post by timmy_tofu »

Hello,

I'm doing some small work on a Haskell FFI to MagickWand - not my initiative, just trying to help a little.

I recently implemented a binding to MagickOptimizeImageLayers. I was expecting it to be equivalent to using `-layers Optimize` on the command line, for example using this free image as input in.gif, if I do

Code: Select all

convert in.gif -coalesce -resize 240x180 -layers Optimize out.gif
I get a nicely optimized half-sized version of the gif with a small filesize.

If I do the same using`MagickOptimizeImageLayers`, I get a much larger file.

(n.b. I use the `cubicFilter` when doing the resize in Haskell to match what convert is using on the CLI according to `-set options:filter:verbose 1`)

So, is there more going on when using `-layers Optimize`?

This suggests that `-layers Optimize` does a framewise optimization and transparency optimization.

The API docs suggest that there's also a MagickOptimizeImageTransparency as well as MagickOptimizeImageLayers - is this the missing part? My version of imagemagick is 6.7.7 which, I believe, is before MagickOptimizeImageLayers was included, though, so I don't think that's what's happening.

Any advice?
timmy_tofu
Posts: 4
Joined: 2013-12-11T09:18:04-07:00
Authentication code: 6789

Re: MagickOptimizeImageLayers and command line -layers optim

Post by timmy_tofu »

Hmm, maybe this post tells me MagickOptimizeImageLayers does not include MagickOptimizeImageTransparency. In that case, is MagickOptimizeImageLayers equivalent to `optimize-frames`? (not at my usual computer so I can't test it myself right now).

In that case, ImageMagick 6.7.7 (the current version in Debian and *ubuntu repos) is internally using MagickOptimizeImageTransparency which it doesn't expose as part of the API until the next version, is that correct?
timmy_tofu
Posts: 4
Joined: 2013-12-11T09:18:04-07:00
Authentication code: 6789

Re: MagickOptimizeImageLayers and command line -layers optim

Post by timmy_tofu »

Alright, having built IM 6.8.7-9 from source and implemented an FFI to MagickOptimizeImageTransparency(), I am now even more confused.

Taking the GIF above, coalescing it, resizing to half width and height, and then using MagickOptimizeImageLayers() and MagickOptimizeImageTransparency(), I am getting an even larger filesize than just optimizing the layers.

Running via the command line turns the original 304k GIF to 160k.

Resizing in my program and using OptimizeImageLayers (or not - the filesize is the same with or without this step, so for this particular GIF I guess there's no frame optimization possible) the resized image is 828k.

Adding MagickOptimizeImageTransparency to the end of that process brings it up to 836k, 8k bigger than doing no 'optimizations' and 4.5 times bigger than what the CLI yields!

So my question remains - what exactly is -layers Optimize doing internally that I'm not?
timmy_tofu
Posts: 4
Joined: 2013-12-11T09:18:04-07:00
Authentication code: 6789

Re: MagickOptimizeImageLayers and command line -layers optim

Post by timmy_tofu »

Interestingly, doing

Code: Select all

convert in.gif -coalesce -resize newWidthxnewHeight -layers optimize-frame -layers optimize-transparency out.gif
gives me a slightly smaller filesize (~8k) than doing just -layers optimize, so contrary to here `-layers optimize` must be doing something different than just optimize-frame and optimize-transparency, or does them in a particular way that's greater than the sum of it's parts.

RMagick seems to quantize and remap as well as optimizing layers and transparency, I guess I'll have to look into that. Interestingly, the first time I call optimize_layers on the ImageList in RMagick, it returns an optimized and much smaller sequence, but subsequent calls return the same 840k coalesced and resized sequence.
Post Reply