15

I've been looking for a good cross-platform 2D drawing library that can be called from C++ and can be used to draw some fairly simple geometry; lines, rectangles, circles, and text (horizontal and vertical) for some charts, and save the output to PNG.

I think a commercial package would be preferable over open source because we would prefer not to have to worry about licensing issues (unless there's something with a BSD style license with no credit clause). I've looked at Cairo Graphics which seemed promising, but the text rendering looks like crap out of the box, and upgrading the text back-end brings us into murky license land.

I need it for Windows, Mac and Linux. Preferably something fairly lightweight and simple to integrate. I've thought about Qt but that's way too heavy for our application.

Any ideas on this would be awesome.

Gerald
  • 23,011
  • 10
  • 73
  • 102
  • 2
    Yes, it's a pity `QImage` and `QPainter` are buried in `QtGui` instead of in a separate `QtPainting` lib. When they split the libs in Qt 4.0, they weren't really usable without a `QApplication`, but nowadays you can use them in separate threads, so a `QtPainting` would make a lot of sense, IMO. Let's wait for Qt 5 :) – Marc Mutz - mmutz Jul 29 '09 at 15:32

11 Answers11

12

Try Anti-Grain Geometry. From the description:

Anti-Grain Geometry (AGG) is an Open Source, free of charge graphic library, written in industrially standard C++. The terms and conditions of use AGG are described on The License page. AGG doesn't depend on any graphic API or technology. Basically, you can think of AGG as of a rendering engine that produces pixel images in memory from some vectorial data. But of course, AGG can do much more than that. The ideas and the philosophy of AGG are:

  • Anti-Aliasing.
  • Subpixel Accuracy.
  • The highest possible quality.
  • High performance.
  • Platform independence and compatibility.
  • Flexibility and extensibility.
  • Lightweight design.
  • Reliability and stability (including numerical stability).
Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92
  • Just the other day I noticed my GPS unit was using AGG for map rendering. Neat! – Tomas Andrle Jul 31 '09 at 10:11
  • 5
    As an FYI, I tried repeatedly to contact the developers of AGG in relation to obtaining a commercial license, and never got a response. It has also been 2 years since any news was posted on the site, so I'm assuming it's no longer supported. So I ended up just rolling my own custom solution across the 3 platforms that I needed. It's still probably a good solution if using the older version with the non-GPL license, so I will leave this as the best answer, but I just didn't want to deal with an unsupported library. – Gerald Aug 18 '09 at 00:21
  • 3
    I just read that Maxim Shemanarev, the creator of AGG, passed away in November 2013 according to [this website](http://comments.gmane.org/gmane.comp.graphics.agg/5732). I only knew him through his extraordinary work on AGG. You can still find some independent improved versions of AGG in GitHub. – Jaime Ivan Cervantes Jan 30 '15 at 17:23
  • @JaimeIvanCervantes It would be helpful if you could include a link to recommended versions of AGG. I searched for highest stars for C++ libraries and found this one: https://github.com/tyoma/agge – JamesHoux Apr 30 '19 at 03:11
9

Another one: Skia. Used in Android and Chrome, under active development, HW acceleration.

Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92
5

Have a look at SFML. It's open source but the license is very permissive.

Drawing simple shapes
Displaying text

Bastien Léonard
  • 60,478
  • 20
  • 78
  • 95
2

Antigrain does high quality primitive rendering and seems to be able to render true type fonts and has a commercial license available upon request.

http://www.antigrain.com/

Laserallan
  • 11,072
  • 10
  • 46
  • 67
2

Have you tried FLTK? It is lightweight, cross-platform, has support for 2D/3D graphics and comes with a good widget set (including a charting component). The API is simple and straight forward.

Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
1

Use SDL

Toad
  • 15,593
  • 16
  • 82
  • 128
  • 2
    I've used both SDL and SFML, and I would strongly recommend SFML over SDL. It makes use of C++ constructs (OOP, function overlods, ...), and it's much easier and fun. To me the only possible drawback is that it's still a recent library compared to SDL, so it may not be as stable. – Bastien Léonard Jul 29 '09 at 14:48
  • 2
    also, SDL does not come with functions to draw primitives he listed. You have to find code done by others or code them yourself. – Michaël Larouche Jul 29 '09 at 15:04
1

You might use Allegro 5 (since SDL and SFML are mentioned). This provides all of the platforms you require (and more) and can render shapes and save to PNG. Version 5 has a much improved API and hardware acceleration. With any of these low level cross platform libraries you'd have to find your own charting solution.

I put some notes on my blog about Allegro and using it on the Mac.

Nick
  • 27,566
  • 12
  • 60
  • 72
  • This looks very nice. Do you know if it's possible to use it in an application that has it's own window management instead of using the Allegro window management? – Gerald Nov 21 '12 at 04:27
1

There is also libgd - simple one, but well-written.

Regarding Cairo Graphics, I can't believe it renders text that looks bad. If you are particularly concerned about text rendering, State of the Text Rendering from Jan 2010 gives quite good overview.

mloskot
  • 37,086
  • 11
  • 109
  • 136
  • I should have been a little more explicit; Cairo Graphics renders large text well, but I needed to render small text labels on charts and graphs, and that's were it looks like crap because it uses vector graphics to render it. As long as the text is large enough for at least 2-pixel lines all the way around it's okay, but try rendering some text at font size 8 or so and see if you can read it ;) – Gerald Jan 24 '10 at 21:03
1

I use CImg: cross platform (self contained single header file), simple, concise. PNG is not natively supported but can be handled if ImageMagick is installed (see supported formats).

See also this related question.

Community
  • 1
  • 1
rafak
  • 5,501
  • 2
  • 19
  • 30
  • I am very impressed with CImg. By far the easiest to integrate of several libraries I played around with. – M Katz Apr 08 '11 at 02:26
0

OpenGL?

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
  • Please explain your definition of a library. – graham.reeds Jul 22 '13 at 07:50
  • 1
    It's something that has code. OpenGL is just a standard API used for talking with GPU. You cannot link to it, you cannot buy it, you cannot download it. By the way, calling OpenGL functions directly is actually never cross-platform. Libraries like SDL or SFML are. – cubuspl42 Jul 24 '13 at 00:05
  • OpenGL has code. You can link to it (opengl32.lib iirc), you purchase an implementation of it with your graphics card, you download it when you update your drivers. I can't comment on cross platform issues but I would hazard a guess that SDL and SFML would use OpenGL under the hood to acheive cross platform-ness, even if it is exposed via an abstraction layer. – graham.reeds Jul 24 '13 at 07:56
  • 1
    OpenGL implementations have code, not OpenGL. Would you call POSIX a library, only because it guarantees that some functions will exist on platforms that implement it? A few quotes from opengl.org FAQ: _"(...)No, OpenGL doesn't have any source code. GL is a specification which can be found on this website. (...)"_, _"(...)OpenGL is not a software product. it is a specification.(...)"_ – cubuspl42 Jul 24 '13 at 12:58
  • You really have something against OpenGL don't you? Okay. Maybe it is a specification. But people have implemented as a library which you can use and it is cross platform which solves the OP problem. 4 years ago. – graham.reeds Jul 24 '13 at 13:58
  • Also if you are that rabid about it, take a permanent marker, cross out OpenGL and write MesaGL over the top of it. – graham.reeds Jul 24 '13 at 14:05
  • 2
    Don't call me rabid only because you don't understand what OpenGL is. It won't solve OP problem, because he/she needs a cross platform rendering library. BTW, I love OpenGL. – cubuspl42 Jul 24 '13 at 14:28
  • In what way will it not solve the OP problem? I've done rendering on Windows with OpenGL and I am pretty sure that there are OpenGL implementations on Linux as well. If that isn't cross platform I don't know what is. If those implementations on Linux/Mac etc are compliant then those calls should work there as well exactly the same. – graham.reeds Jul 25 '13 at 07:52
  • 1
    Only OpenGL calls are cross-platform, not actually using pure OpenGL itself. You need to call a lot of platform-specific functions to create context, initialize OpenGL-related stuff, create windows, etc. Unless you use cross-platform **library** like SDL, SFML, etc. That's what OP is looking for. Specification itself won't help. – cubuspl42 Jul 25 '13 at 12:58
0

I would go for AGG or Cairo.