Bug in NewImageFromImage function? #412
-
If you look at NewImageFromImage, it converts color weirdly. func NewImageFromImage(img image.Image) *Image {
size := img.Bounds().Size()
cx := (C.int)(size.X)
cy := (C.int)(size.Y)
ccolor := colorCptr(White)
ret := C.GenImageColor(cx, cy, *ccolor)
for y := 0; y < size.Y; y++ {
for x := 0; x < size.X; x++ {
color := img.At(x, y)
r, g, b, a := color.RGBA()
rcolor := NewColor(uint8(r), uint8(g), uint8(b), uint8(a))
ccolor = colorCptr(rcolor)
cx = (C.int)(x)
cy = (C.int)(y)
C.ImageDrawPixel(&ret, cx, cy, *ccolor)
}
}
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
} when it really should be func NewImageFromImage(img image.Image) *Image {
size := img.Bounds().Size()
cx := (C.int)(size.X)
cy := (C.int)(size.Y)
ccolor := colorCptr(White)
ret := C.GenImageColor(cx, cy, *ccolor)
for y := 0; y < size.Y; y++ {
for x := 0; x < size.X; x++ {
color := img.At(x, y)
r, g, b, a := color.RGBA()
// DIFF HERE ================================
rcolor := NewColor(uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8))
// ==========================================
ccolor = colorCptr(rcolor)
cx = (C.int)(x)
cy = (C.int)(y)
C.ImageDrawPixel(&ret, cx, cy, *ccolor)
}
}
v := newImageFromPointer(unsafe.Pointer(&ret))
return v
} That's how image/color package does it. func rgbaModel(c Color) Color {
if _, ok := c.(RGBA); ok {
return c
}
r, g, b, a := c.RGBA()
return RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)}
} Reason I'm posting here instead of making an issue post is because I'm not sure if it's a bug or a clever optimization. Since the code works well if underlying image color is RGBA or NRGBA, which will be the case for most images. |
Beta Was this translation helpful? Give feedback.
Answered by
JupiterRider
Sep 14, 2024
Replies: 1 comment 1 reply
-
@imprity This definitely is a bug! Thanks for finding it. I cloud reproduce this with some .png Images and always reproduce for .jpeg (color.YCbCr). Feel free to open a pull request. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
imprity
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@imprity This definitely is a bug! Thanks for finding it.
I cloud reproduce this with some .png Images and always reproduce for .jpeg (color.YCbCr).
Feel free to open a pull request.