fast code to add rounded corners

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

fast code to add rounded corners

Post 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.
Last edited by NicolasRobidoux on 2012-03-16T14:40:59-07:00, edited 6 times in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: fast IM code to add rounded corners to image with known

Post 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?
Last edited by NicolasRobidoux on 2012-03-13T18:35:07-07:00, edited 4 times in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: fast IM code to add rounded corners to image with known

Post by NicolasRobidoux »

User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: fast IM code to add rounded corners to image with known

Post 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
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: fast IM code to add rounded corners to image with known

Post 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).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: fast IM code to add rounded corners to image with known

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: fast IM code to add rounded corners to image with known

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: fast IM code to add rounded corners to image with known

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: fast IM code to add rounded corners to image with known

Post 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.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: fast IM code to add rounded corners to image with known

Post 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
Last edited by NicolasRobidoux on 2012-03-15T05:19:47-07:00, edited 4 times in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: fast IM code to add rounded corners to image with known

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: fast IM code to add rounded corners to image with known

Post 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.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: fast IM code to add rounded corners to image with known

Post 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.
Last edited by NicolasRobidoux on 2012-03-14T17:42:56-07:00, edited 2 times in total.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: fast IM code to add rounded corners to image with known

Post 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. :(
Last edited by NicolasRobidoux on 2012-03-14T11:47:03-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: fast IM code to add rounded corners to image with known

Post 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.
Post Reply