|
Boost : |
From: Lubomir Bourdev (lbourdev_at_[hidden])
Date: 2006-10-17 14:50:53
>
> I my experience promotion traits are absolutely necessary to
> implement genereric image processing algorithms. Consider the
> simple matter of adding two or more byte images. In order to
> avoid overflow, intermediate results should be stored in a
> short or an int. If the result is to be written back to a
> byte image, the destination accessor can take care of
> shifting the result back into the appropriate range or
> clipping at the range limits. Promotion traits control this
> behavior. Hardly any algorithm in VIGRA can do without them.
>
> Regards
> Ulli
Ulli:
We don't believe the type of the result could automatically be inferred
simply from the types of arguments. The optimal type depends on your
intent and the context.
For example, VIGRA's promotion traits choose double as the promotion for
int when dealing with floating point operations. Why not float? On many
architectures float is faster, and precision may not be an issue in the
context in which you use it.
Why should int always be the result type of adding two ints? Depending
on what you are doing int may overflow... Int for summing two shorts
will not overflow, but what if you are building, say, the integral image
and you want to sum the values of all pixels in your image?
As you can see, the optimal type depends on a complex interaction
between speed, capacity and desired precision and hard-coding it makes
the algorithms less generic.
This is why in GIL we allow the type of the result to be specified
explicitly upon invoking the operation. Promotion traits can at best be
useful for a reasonable default, but even then we feel that they belong
in boost::type_traits and not in an image library, as the problem
generalizes beyond images.
Lubomir
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk