rendering text performance - very poor

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
rperez

rendering text performance - very poor

Post by rperez »

Hi All.

I did some benchmarks on the library and found that drawing text on an image is very slow in comparison to composing images.
When I say very slow I mean by a factor of 10 approximatley.

I am running with one thread only.

What can be the cause of this?
Does the library reads the font from the disk every drawing ?
Is the time spent looking in the SplayTree for configuration ?
I am using the list<Drawable>, is this a problem ?

Thanks,
Ronen.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

Fonts and glyths are not currently cached in ImageMagick which would be a great speedup. Its on our list of things to do.
rperez

Post by rperez »

do you have an estimation on when this issue will be addressed?
this is very important to our project as it is what cause us to exceed our time requirements.

Thanks,
Ronen.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

Implementing the font caching in ImageMagick will be quite time consuming to do it properly. Instead consider sending us a stripped down code/script of what you are doing and we will run a profile to see if we can identify opportunities for speed-ups. If we reproduce the slow-down you are getting, perhaps we can shoot for a short term improvement.
rperez

Post by rperez »

Code: Select all

try
{
                mImage = new Image();
                mImage->size(Geometry(fWidth,fHeight));
                mImage->backgroundColor(fColor.getColor());
	mImage->erase();

                // render the text.
	list<Drawable> drawList;
	drawList.push_back(DrawableFont(fFontFace,fFontSyle,fFontWeight,NormalStretch));
	drawList.push_back(DrawablePointSize(fFontSize));
	drawList.push_back(DrawableTextDecoration(fTextDecoration));
	drawList.push_back(DrawableGravity(fGravity));
	drawList.push_back(DrawableFillColor(fFontColor));
	drawList.push_back(DrawableText(0,0,fText,"UTF-8"));
	mImage->draw(drawList);
}
catch (Magick::Exception& err)
{
	LOG_MESSAGE
	return false;
}
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

If you have more than one text string to render, you should get a substantial speed up by collecting all the strings into a drawable and only rendering them once rather than individually. Use DrawablePushGraphicContext() and DrawablePopGraphicContext() to separate the text attributes if need be.
rperez

Post by rperez »

This solution is problematic for us because we render text on various Imageg and not only on one. if I understand your suggestion, this may help when all text item are on the same Image object.
do you have any idea on how can we improve our performance and still render text on more then one Image?

Thanks,
Ronen.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

For a high performance needs we recommend drawing directly with the Freetype library and utilizing the font cache API they provide.
rperez

Post by rperez »

Hi there,
I am not familiar with the FreeType usage in ImageMagick.
do you have a sample code to illustrate the way to render text with both libraries? (render the text with FreeType onto an ImageMagick Image object).

Thanks,
Ronen.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

The best path forward would be to download the Freetype library and reads its documentation or we suspect there may be a Freetype book by now that would be helpful. All the ImageMagick Freetype related code can be found in the source distribution in the file magick/annotate.c.
rperez

Post by rperez »

Hi and thanks for the quick response.
in order for us to render text directly with the Freetype library, we need to copy a huge amount of code from annotate.c in order to gain all the functionality we used up until now.
can you point out the place in the code where the fonts and glyphs are read from the file and not cached. I am trying to find the easiest way to use Freetype and ImageMagic to accomplished that.

Thanks,
Ronen.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

Everything Freetype related is found in RenderFreetype() of magick/annotate.c. The FT_Open_Face() Freetype method opens a font file.
rperez

Post by rperez »

Hi,
I used this information of FT_Open_Face that read the font from the file and it was very helpful.
My question is whether there are more actions that are "heavy" in performance.
I have some other suspects I wanted to ask about.
FT_Attach_File (also open file?)
FT_Load_Glyph
and FT_Init_FreeType - is there a problem to init the library only once instead of each time we render text? is it a heavy action?

Thanks in advance,
Ronen.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

You will need to find a Freetype support forum for answers to your questions. This forum is about ImageMagick development.
rperez

Post by rperez »

Hi there,
I asked you some monthes ago, about the performance of text rendering on an image and the reply was that text caching is not yet supported in ImageMagick but is on the "todo list".
are there any news on the subject? is this supported in the new versions of the library?

Thanks in advance,
Ronen.
Post Reply