Re: [OT, maybe] C library for doing graphics
- From: "BGB / cr88192" <cr88192@xxxxxxxxxxx>
- Date: Sat, 18 Jul 2009 13:03:15 -0700
"Beej Jorgensen" <beej@xxxxxxx> wrote in message
news:h3qh3q$7pj$2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
superpollo <user@xxxxxxxxxxx> wrote:
i know that graphics is outside the scope of standard C, but i was
somewhat interested in knowing what clc "regulars" use when they want
to "visualize" some data produced by their code, graphically. (perhaps
in various graphics format).
I've never written anything using Cairo, but I would:
http://cairographics.org/FAQ/
It's been ported to Unix/OSX/Windows, is really featureful, and is
LGPL'd.
RH's suggestion for DIY is great, because writing such things can be fun
learning exercises. (I've written so many PPM encoders I can't even
remember.) But if you decided you wanted antialiased vector graphics
and font support, you might have a big coding task ahead of you. :)
antialiasing and fonts are not "too" big of an issue...
the main trick to AA is to just draw everything at a higher resolution and
then downsampling.
the simplest option is to upscale by a power-of-2 factor, and then
downsample by averaging...
the problems is that, though this approach works, it manages to re-introduce
almost as much aliasing as it eliminates. however, if 2 passes are used for
each reduction, namely the first being a filter (essentially, a small scale
blur filter), followed by the average, the results are much better.
sinc is also an option, but IME the problem with sinc is that it is too
computationally expensive to be practical, and using a windowed sinc with a
window small enough to get done in a reasonably short timeframe IME tends to
produce unacceptably poor quality.
the main advantage though would be that it could scale to an arbitrary
resolution (sinc works well for both upscaling and downscaling).
another option is cubic splines, which are much faster than sinc, and offer
similar advantages, but the problem with cubic splines is that implementing
them in the full 2D case unleashes a sort of mathematical demon (note that
one could apply a horizontal and vertical image scale and get a much simpler
algo, however, in this case it will only have a sort of rectangular
symmetry, and is not strictly transpose-symmetric).
however, prefilter+log2 downsample, and LERP for upsample, is fairly simple,
and by combining these approaches one can get an analogue of trilinear or
anistropic filtering...
note that if doing line-based vector graphics and using AA, it is necessary
to add some "thickness" to the lines, otherwise they will generally
disappear when downsampling. simple options are:
A: draw several parallel lines;
B: draw a circular or rectangular glob of pixels for each virtual pixel.
fonts are not TOO bad either, especially if one opts for bitmapped fonts.
one recommendation here is "unifont", which handles the entire UTF-16 BMP
(Basic Multilingual Plane). the people in this project went through the
effort of producing images for all the characters in a reasonably
easy-to-process file format.
granted, these are not vector fonts...
so, for example, drawing TrueType fonts would be a little more work
(especially if antialiased, where then the issue is both making the font
look good and avoiding losing the small details). additional complexity is
added if one tries for "proper" TTF rendering, which may involve curved
lines and the use of Bezier-Splines...
http://en.wikipedia.org/wiki/B%C3%A9zier_curve
however, for my uses, using the prior mentioned image-scaling techniques and
bitmapped fonts has usually produced "good enough" results...
granted though, by the time one is considering all of this stuff, it is
probably really worthwhile to consider using OpenGL or similar for doing a
lot of the rendering, given it is generally much faster (due to the GPU),
and also saves a lot of coding effort.
granted though, one may still need some of the above tricks if trying for
higher-quality than usually provided by GL (such as a cubic scaler to both
get the textures into power-of-2 sizes, and maybe also perform a
higher-quality upsampling than one will get with the built in LERP, as used
bilinear and trilinear filtering...).
or such...
-Beej
.
- References:
- [OT, maybe] C library for doing graphics
- From: superpollo
- Re: [OT, maybe] C library for doing graphics
- From: Beej Jorgensen
- [OT, maybe] C library for doing graphics
- Prev by Date: Re: linking c++ code with c library
- Next by Date: Re: linking c++ code with c library
- Previous by thread: Re: [OT, maybe] C library for doing graphics
- Next by thread: Re: C library for doing graphics
- Index(es):
Relevant Pages
|