Page 1 of 3
fast code to add rounded corners
Posted: 2012-03-13T18:04:28-07:00
by NicolasRobidoux
I'm looking for the fastest code to add rounded white corners to a 400x320 image in MPC format (because I do more than one thing with it) temporarily stored in RAM (in a RAMdisk, e.g.) with the result to be saved in jpg format. .mpc does not really make things faster than .jpg with such a small image, at least if I don't make sure to put it in RAM, but the final jpg ends up being a bit smaller if I put the result of my previous resize in MPC (it's the "compress to jpg && decompress from jpg && operate && recompress to jpg" deal).
I'm using svn (bleeding edge) IM 7.0.0 compiled with Q8 even though the version running on the target machine 6.6.9-9. (If I was manic, I'd install it; I'm not that manic. Actually, I am that manic, but my spare laptop is going to the shop, CentOS 5.x does not recognize my wireless card, I don't like the vintage desktop it uses, and installs take time, which I don't have.)
First pass:
Code: Select all
convert -size 400x320 xc:white RogerRabbit.mpc \( -size 400x320 xc:black -fill white -draw "roundRectangle 0,0 399,319 8,8" \) -composite -flatten -quality 70 FramedRogerRabbit.jpg
Time: .045s
I can cache reused pieces. Tons of RAM on the target machine, and this is done to every incoming image. So, I'll eventually have to figure out how best to store what's between parentheses.
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-13T18:20:08-07:00
by NicolasRobidoux
NicolasRobidoux wrote:...I can cache reused pieces. Tons of RAM on the target machine, and this is done to every incoming image. So, I'll eventually have to figure out how best to store what's between parentheses.
Easy enough:
Code: Select all
convert -size 400x320 xc:black -fill white -draw "roundRectangle 0,0 399,319 8,8" RoundFrame400x320.mpc
convert -size 400x320 xc:white RogerRabbit.mpc RoundFrame400x320.mpc -composite -flatten -quality 70 FramedRogerRabbit.jpg
Time: .028s
Is there a better format than MPC to store the mask in? A lot of examples use PNG.
Warning: Because .mpc is not a "fixed" format, but instead a format that depends on the ImageMagick configuration, every time to reinstall/recompile/touch ImageMagick, you should regenerate the masks. Are there other situations in which one should regenerate the mask mpc's?
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-13T18:27:19-07:00
by NicolasRobidoux
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-13T18:50:28-07:00
by fmw42
mpc is not stored in ram. you want mpr. see
http://www.imagemagick.org/Usage/files/#mpr and
http://www.imagemagick.org/Usage/files/#mpc for the differences.
Also you don't have to save an image (mpc or otherwise) if you put all the commands on one command line using parenthesis processing and either clones or .mpr.
See
http://www.imagemagick.org/Usage/basics/#parenthesis
Also see
http://www.fmwconcepts.com/imagemagick/ ... und_shadow
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-13T20:06:20-07:00
by NicolasRobidoux
Fred:
Thank you (as always).
My idea was that I could get more speed by avoiding the recomputation of the mask for every input image, so I want to compute it once and use it for thousands of framed images. (Indeed, I omitted the commands that have to do with storing the mask in, e.g., a RAMdisk.)
I'll look into MBR, but if they only live within one ImageMagick run, they are not what I want.
Although what I really want is speed (given that RAM is not an issue).
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-13T20:15:28-07:00
by fmw42
There may be a problem saving mpc from session to session or at least from IM compile to a new compile. I forget the issue, but perhaps it is explained in the links I referenced. I never use mpc except for within my scripts and it then gets deleted after the script has finished. Of course Anthony and Magick are the experts on this.
I don't know if you will get better speed by saving (a non-mpc) image such as .miff or any other format and reusing it (reading from disk each time) or if you will get better speed by using one command line with clones or mpr format. I would be interested in hearing about any speed tests you perform.
Fred
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-13T23:55:26-07:00
by anthony
MPC saves to disk in a form that does not need decoding when it is 'read' back in. It is also just 'mapped' into memory, as a disk-swap file.
MPR however saves into a named in-memory cache, and is only present while the command is running. It is a bit like a using parenthesis and clone, but with a named 'register' rather than as a 'temporary stack'. You can replace the image in a specific named register, but you can not delete a specific named register.
MPR will become more useful when using a longer running background/daemon command that will be possible with IMv7 (using co-processing techniques).
http://www.imagemagick.org/Usage/bugs/I ... ipting.txt
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-14T05:01:34-07:00
by NicolasRobidoux
fmw42 wrote:...
I don't know if you will get better speed by saving (a non-mpc) image such as .miff or any other format and reusing it (reading from disk each time) or if you will get better speed by using one command line with clones or mpr format. I would be interested in hearing about any speed tests you perform.
@Fred: Have a look at the two boldface timings at the top of this thread: Saving to .mpc instead of recomputing is much faster. The time, of course, excludes the creation/storage of the .mpc file: It only makes sense to do this if it is reused.
Note that all the tests I'm doing don't involve RAM disks: the .mpc is read from my disk drive. With RAM disks, the speed gain should be even more.
My client puts this kind of stuff in RAM disks. They do their own benchmarks. So, I test stuff with a "target" set up/machine/OS in mind on a different set up/machine/OS.
I'll do some timings for other image file formats today. The mask compresses down to nothing in png, for example.
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-14T10:57:56-07:00
by fmw42
Did you try using .mpr similar to Anthony's post or my tidbits?
One thing that you can do is save to .miff. Then each time you compile IM, you can convert the .miff to .mpc and use that until the next compile.
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-14T10:59:38-07:00
by NicolasRobidoux
I actually won't benchmark saving the mask in other image formats, because I hardly imagine anything involving a global mask going faster, and given that the image is fairly small, doing local operations in the corners is unlikely to go much faster.
RE: the flip/flop way of generating the mask:
Wouldn't it go faster to first create an intermediate mask which has two rounded corners, and then flipping the result once to get the other two?
That is: one corner -> two corners -> four corners instead of adding one corner at a time, which adds one more step?
P.S. This is what Leif Åstrand does:
http://www.imagemagick.org/Usage/thumbnails/#rounded
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-14T11:01:25-07:00
by NicolasRobidoux
fmw42 wrote:
One thing that you can do is save to .miff. Then each time you compile IM, you can convert the .miff to .mpc and use that until the next compile.
Good point. On the other hand, if I'm going to convert from .miff to .mpc, I may as well generate the .mpc from scratch.
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-14T11:01:54-07:00
by fmw42
NicolasRobidoux wrote:I actually won't benchmark saving the mask in other image formats, because I hardly imagine anything involving a global mask going faster, and given that the image is fairly small, doing pixel by pixel things in the corners is unlikely to go much faster.
RE: the flip/flop way of generating the mask:
Wouldn't it go faster to first create an intermediate mask which has two rounded corners, and then flipping the result once to get the other two?
That is: one corner -> two corners -> four corners instead of adding one corner at a time, which adds one more step?
Yes, probably so.
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-14T11:24:40-07:00
by NicolasRobidoux
I'm trying to learn more IM syntax so I can try a few other things. Only operating on the corner pixels does appear to be something that could run faster (at least on large images).
Question: Isn't the +delete a waste in the following code?
Code: Select all
convert thumbnail.gif -alpha set -compose DstOut \
\( -size 20x15 xc:none -draw "polygon 0,0 0,14 19,0" \
-write mpr:triangle +delete \) \
\( mpr:triangle \) -gravity northwest -composite \
\( mpr:triangle -flip \) -gravity southwest -composite \
\( mpr:triangle -flop \) -gravity northeast -composite \
\( mpr:triangle -rotate 180 \) -gravity southeast -composite \
corner_cutoff.png
What it seems to be doing is creating a triangle corner, storing it in mpr:triangle, deleting it (P.S. actually deleting the already loaded copy) and then immediately reloading it.
Why not
Code: Select all
convert thumbnail.gif -alpha set -compose DstOut \
\( -size 20x15 xc:none -draw "polygon 0,0 0,14 19,0" \
-write mpr:triangle \) \
-gravity northwest -composite \
\( mpr:triangle -flip \) -gravity southwest -composite \
\( mpr:triangle -flop \) -gravity northeast -composite \
\( mpr:triangle -rotate 180 \) -gravity southeast -composite \
corner_cutoff.png
? It gives exactly the same result and it appears to be a smidgeon faster.
Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-14T11:32:29-07:00
by NicolasRobidoux
Clearly I can store the four corner masks in .mpc to save on recomputation.
Still struggling with adapting the flip/flop code to jpg output (my input and output images never have transparency) and rounded corners. I'm that ignorant w.r.t. complex IM commands.

Re: fast IM code to add rounded corners to image with known
Posted: 2012-03-14T11:37:52-07:00
by fmw42
Question: Isn't the +delete a waste in the following code?
It is needed. It deletes the original image (from -size 20x15 xc:none -draw "polygon 0,0 0,14 19,0") and not the mpr image. That way the original does not get accidentally used for further processing in the command line or sent to output.