[Bf-committers] [Bf-blender-cvs] SVN commit: /data/svn/bf-blender[11919] trunk/blender/source/blender/imbuf/intern/divers.c: Bugfix #7125

paprmh paprmh at gmail.com
Thu Sep 6 01:13:42 CEST 2007


>
> --- Ton Roosendaal <ton at blender.org> wrote:
>
> > Revision: 11919
> >
> >
>
> http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11919
> > Author:   ton
> > Date:     2007-09-02 18:34:02 +0200 (Sun, 02 Sep
> > 2007)
> >
> > Log Message:
> > -----------
> > Bugfix #7125
> >
> > The conversion from float to char (byte) was not
> > correct. It should include
> > the possibility for colors like 0.999999 to become
> > 255 still. The correct
> > multiplication factor I don't know though... and
> > this should become a system
> > wide definition!
> >



From: Martin Poirier <theeth at yahoo.com>
> You can probably use that too:
>
> #define FTOCHAR(val) val<=0.0f?0: (val>(1.0f - 0.5f /
> 255.0f)?255: (char)(255.0f*val))
>
> Multiplying by 255.99 will give values 1 higher than
> they should be for float values > 0.5. (I dealt with a
> similar issue at work, that's more or less what we
> used).
>
> Martin


...

That was assuming rounding, of course, in this case,
> it's truncating on cast, so you can use:
>
> #define FTOCHAR(val) val<=0.0f?0: (val>(1.0f - 1.0f /
> 255.0f)?255: (char)(255.0f*val))
>
> The 0.5 was to make it round in the correct direction.
>
> Martin


Isn't the cast truncate bug a problem over the whole range of values? Not
only is it important to round up anything 254.5 (1.0f - 0.5f /255.0f) or
higher to 255, but you need to address for instance, a value like
0.49fwhich would work out to
124.95 and get truncated to 124.
I suggest using this:

#define FTOCHAR(val) val<=0.0f? 0 : (val>(1.0f - 0.5f /255.0f)? 255 :
(char)((255.0f*val)+0.5f))

Adding .5 allows val to properly round up when the char cast truncates the
float.  It also has the advantage of working over the entire range of values
and not just for 255.  Here, checking for 254.5 (1.0f - 0.5f /255.0f)
instead of 1.0f is just more efficient as any value higher will round up to
255 anyway...

I found this tip a long time ago on the web when I needed a set of
conversion functions for my seq plugins...and it has worked well there.

rob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.blender.org/pipermail/bf-committers/attachments/20070905/11b62237/attachment.htm 


More information about the Bf-committers mailing list