Use proper rounding when casting RGB values to int

When doing an RGB->HSV->RGB roundtrip, the original value should be
restored (HSV, being double, has sufficient precision).

For e.g. `RGBValue(1,2,3)`, the equivalent resulting code for blue is
`trunc(int * 255.0 / 255.0)` (cast from double to int truncates).
At least with x87 FP math and its immediate 80bit extended precision
the resulting value is ~trunc(2.9999..), i.e. 2, similar problems may
exist on other architectures with other values.

Using proper rounding avoids the error magnification.

Closes https://github.com/wxWidgets/wxWidgets/pull/2078
This commit is contained in:
Stefan Brüns
2020-10-05 16:56:09 +02:00
committed by paulcor
parent a3f14b2fc2
commit c3873ea313
2 changed files with 59 additions and 3 deletions

View File

@@ -3312,9 +3312,9 @@ wxImage::RGBValue wxImage::HSVtoRGB(const HSVValue& hsv)
}
}
return RGBValue((unsigned char)(red * 255.0),
(unsigned char)(green * 255.0),
(unsigned char)(blue * 255.0));
return RGBValue((unsigned char)wxRound(red * 255.0),
(unsigned char)wxRound(green * 255.0),
(unsigned char)wxRound(blue * 255.0));
}
/*