-
Notifications
You must be signed in to change notification settings - Fork 18k
image/color: Go 1.5 unexpected change in color.YCbCr.RGBA() #11691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
color.YCbCr.RGBA() uses:
|
Leaving to Nigel to decide whether this needs to be documented or fixed. |
Yes, this was a deliberate change: https://go-review.googlesource.com/8073 As for whether this needs calling out in the Go 1.5 release notes, I'll leave that decision up to @robpike. |
Someone was bitten so an item in the release notes would be good. Words please @nigeltao? |
The conversion of a color.YCbCr to RGBA now has finer detail. The RGBA method has always returned 16-bit color, but prior to Go 1.5, the low 8 bits equalled the high 8 bits. The following code: var c color.YCbCr = etc has always been incorrect, but coincidentally printed the right number prior to Go 1.5. The correct code replaces "uint8(r)" with "uint8(r >> 8)". Even so, the number printed can differ slightly between Go 1.5 and previous versions. |
Also, if you are converting an image.YCbCr full of many YCbCr colors, then consider using the image/draw package (http://blog.golang.org/go-imagedraw-package) instead of explicitly calling the RGBA method on each pixel. Once again, the resultant RGBA image can differ slightly between Go 1.5 and previous versions. |
This code: func main() {
c := color.YCbCr{Y: 0, Cb: 128, Cr: 128}
r, g, b, a := c.RGBA()
fmt.Println(r, g, b, a)
} Prints |
I understand the "finer detail" improvement with 16-bit color. But I don't understand:
It was done correctly in Go 1.4. |
I just saw the release note:
Does |
Yes, it is more accurate. Those numbers (0 vs 128) are integers. Here's a floating point analogy: you can think of red, green, etc. values being between 0 (no red) and 1 (fully saturated red). In the equivalent of Go 1.4, converting a particular YCbCr color to RGBA would yield something like '0.1 red'. In the equivalent of Go 1.5, it would yield something like '0.102 red'. The actual number is different; it is more accurate. |
Separately, you're right that color.YCbCr{Y: 0, Cb: 128, Cr: 128} should be black. Go 1.5 returns 128 instead of 0 because it adds one half (of 256) to round to nearest instead of round down. Perhaps it shouldn't. I'll think about this. |
CL https://golang.org/cl/12220 mentions this issue. |
Thank you! :) |
My code: https://play.golang.org/p/AslT5NMUiU
Go 1.4: 0 0 0 65535
Go 1.5: 128 128 128 65535
The text was updated successfully, but these errors were encountered: