Re: [AD] Faster hsv_to_rgb() |
[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
> Inspired by your efforts, I rearranged rgb_to_hsv() (conversion the
> other way) a bit. A call to the new version takes 60% of the time that a
> call to previous version took.
The speed improvement is 48.5% on my machine.
> I ran through all possible r,g,b triples, and it gives the same result to a
> factor within 0.0000001, and I also verified that converting back the hsv
> values computed by rgb_to_hsv() always gives the original rgb. The patch is
> attached and I put my test program at http://peg.it.uu.se/~sven/test.c. OK
> to commit?
+ if (r > g) {
+ if (b > r) {
+ /* b>r>g */
+ delta = b-g;
+ *h = 240.0f + ((r-g) * 60) / (float)delta;
+ *s = (float)delta / (float)b;
+ *v = (float)b * (1.0f/255.0f);
+ }
+ else {
+ /* r>g and r>b */
+ delta = r - MIN(g, b);
+ *h = ((g-b) * 60) / (float)delta;
+ if (*h < 0)
Why not 0.0f?
+ *h += 360;
Likewise.
+ *s = (float)delta / (float)r;
+ *v = (float)r * (1/255.0f);
Likewise.
+ if (b > g) {
+ /* b>g>=r */
+ delta = b-r;
+ *h = 240.0 + ((r-g) * 60) / (float)delta;
Where is the 'f'?
+ *s = (float)delta / (float)b;
+ *v = (float)b * (1/255.0f);
Why not 1.0f?
+ }
+ else {
+ /* g>=b and g>=r */
+ delta = g - MIN(r, b);
+ if (delta == 0) {
+ *h = 0.0f;
+ if (g == 0)
+ *s = *v = 0;
Likewise.
+ else {
+ *s = (float)delta / (float)g;
+ *v = (float)g * (1/255.0f);
Likewise.
+ }
+ }
+ else {
+ *h = 120.0 + ((b-r) * 60) / (float)delta;
Where is the 'f'?
+ *s = (float)delta / (float)g;
+ *v = (float)g * (1/255.0f);
Why not 1.0f?
Ok for mainline with the corrections, after re-validating against your test.
--
Eric Botcazou