Fix frame origins not be de-serialized

This commit is contained in:
Kovid Goyal 2025-10-09 19:22:22 +05:30
parent 9212c08638
commit 35093d2105
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 16 additions and 15 deletions

View file

@ -155,9 +155,9 @@ func ImageFrameFromSerialized(s SerializableImageFrame, data []byte) (aa *ImageF
return nil, fmt.Errorf("serialized image data has size: %d != %d", len(data), expected)
}
if s.Is_opaque {
ans.Img, err = NewNRGBWithContiguousRGBPixels(data, s.Width, s.Height)
ans.Img, err = NewNRGBWithContiguousRGBPixels(data, s.Left, s.Top, s.Width, s.Height)
} else {
ans.Img, err = NewNRGBAWithContiguousRGBAPixels(data, s.Width, s.Height)
ans.Img, err = NewNRGBAWithContiguousRGBAPixels(data, s.Left, s.Top, s.Width, s.Height)
}
return &ans, err
}

View file

@ -45,17 +45,18 @@ func nrgbModel(c color.Color) color.Color {
return c
}
r, g, b, a := c.RGBA()
if a == 0xffff {
switch a {
case 0xffff:
return NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
case 0:
return NRGBColor{0, 0, 0}
default:
// Since Color.RGBA returns an alpha-premultiplied color, we should have r <= a && g <= a && b <= a.
r = (r * 0xffff) / a
g = (g * 0xffff) / a
b = (b * 0xffff) / a
return NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
}
if a == 0 {
return NRGBColor{0, 0, 0}
}
// Since Color.RGBA returns an alpha-premultiplied color, we should have r <= a && g <= a && b <= a.
r = (r * 0xffff) / a
g = (g * 0xffff) / a
b = (b * 0xffff) / a
return NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
}
var NRGBModel color.Model = color.ModelFunc(nrgbModel)
@ -429,7 +430,7 @@ func NewNRGB(r image.Rectangle) *NRGB {
}
}
func NewNRGBWithContiguousRGBPixels(p []byte, width, height int) (*NRGB, error) {
func NewNRGBWithContiguousRGBPixels(p []byte, left, top, width, height int) (*NRGB, error) {
const bpp = 3
if expected := bpp * width * height; expected != len(p) {
return nil, fmt.Errorf("the image width and height dont match the size of the specified pixel data: width=%d height=%d sz=%d != %d", width, height, len(p), expected)
@ -437,6 +438,6 @@ func NewNRGBWithContiguousRGBPixels(p []byte, width, height int) (*NRGB, error)
return &NRGB{
Pix: p,
Stride: bpp * width,
Rect: image.Rectangle{image.Point{}, image.Point{width, height}},
Rect: image.Rectangle{image.Point{left, top}, image.Point{left + width, top + height}},
}, nil
}

View file

@ -406,7 +406,7 @@ func FitImage(width, height, pwidth, pheight int) (final_width int, final_height
return width, height
}
func NewNRGBAWithContiguousRGBAPixels(p []byte, width, height int) (*image.NRGBA, error) {
func NewNRGBAWithContiguousRGBAPixels(p []byte, left, top, width, height int) (*image.NRGBA, error) {
const bpp = 4
if expected := bpp * width * height; expected != len(p) {
return nil, fmt.Errorf("the image width and height dont match the size of the specified pixel data: width=%d height=%d sz=%d != %d", width, height, len(p), expected)
@ -414,6 +414,6 @@ func NewNRGBAWithContiguousRGBAPixels(p []byte, width, height int) (*image.NRGBA
return &image.NRGBA{
Pix: p,
Stride: bpp * width,
Rect: image.Rectangle{image.Point{}, image.Point{width, height}},
Rect: image.Rectangle{image.Point{left, top}, image.Point{left + width, left + height}},
}, nil
}